refactor: extract persisted ui state

This commit is contained in:
MythEclipse
2026-05-19 14:11:09 +07:00
parent f48f62893d
commit a1b85d8ac3
5 changed files with 164 additions and 119 deletions
+7 -19
View File
@@ -1,26 +1,14 @@
import type { Router } from "express";
import express from "express";
import { createChildLogger } from "../logger";
import type { SharedUIState, SharedUIStatePatch } from "../state/uiState";
const logger = createChildLogger("ui-state-routes");
export interface SharedUIState {
selectedVoiceGuild: string;
selectedVoiceChannel: string;
selectedTextGuild: string;
selectedTextChannel: string;
activeTab: "voice" | "messages" | "media" | "review" | "recordings";
isListening: boolean;
isStreaming: boolean;
}
export type SharedUIStatePatch = Partial<SharedUIState> & {
selectedGuild?: string;
};
export { SharedUIState, SharedUIStatePatch };
export interface UIStateRouteOptions {
getSharedUIState: () => SharedUIState;
patchSharedUIState: (patch: SharedUIStatePatch) => SharedUIState;
patchSharedUIState: (
patch: SharedUIStatePatch,
) => Promise<SharedUIState> | SharedUIState;
}
export function createUIStateRoutes(options: UIStateRouteOptions): Router {
@@ -38,10 +26,10 @@ export function createUIStateRoutes(options: UIStateRouteOptions): Router {
});
// POST /api/ui-state - Update UI state
router.post("/ui-state", (req, res, next) => {
router.post("/ui-state", async (req, res, next) => {
try {
const patch = req.body as SharedUIStatePatch;
const updated = patchSharedUIState(patch);
const updated = await patchSharedUIState(patch);
res.json(updated);
} catch (error) {
next(error);