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);
+8 -4
View File
@@ -10,7 +10,9 @@ const logger = createChildLogger("voice-routes");
export interface VoiceRouteOptions {
voiceController: VoiceController;
patchSharedUIState: (patch: Partial<SharedUIState>) => SharedUIState;
patchSharedUIState: (
patch: Partial<SharedUIState>,
) => Promise<SharedUIState> | SharedUIState;
broadcaster: ModerationBroadcaster;
adminPassword?: string;
}
@@ -23,7 +25,9 @@ export function createVoiceRoutes(
// Support both old signature (VoiceController) and new signature (options object)
let voiceController: VoiceController;
let patchSharedUIState:
| ((patch: Partial<SharedUIState>) => SharedUIState)
| ((
patch: Partial<SharedUIState>,
) => Promise<SharedUIState> | SharedUIState)
| undefined;
let broadcaster: ModerationBroadcaster | undefined;
let adminPassword: string | undefined;
@@ -137,7 +141,7 @@ export function createVoiceRoutes(
// Update UI state and broadcast to connected clients
if (patchSharedUIState && broadcaster) {
const updatedState = patchSharedUIState({
const updatedState = await patchSharedUIState({
selectedVoiceGuild: guildId,
selectedVoiceChannel: channelId,
});
@@ -163,7 +167,7 @@ export function createVoiceRoutes(
// Update UI state and broadcast to connected clients
if (patchSharedUIState && broadcaster) {
const updatedState = patchSharedUIState({
const updatedState = await patchSharedUIState({
selectedVoiceGuild: "",
selectedVoiceChannel: "",
});