import { useUIStore } from '@/stores/useUIStore'; import { updateDesktopSettings } from '@/lib/persistence'; import type { DesktopSettings } from '@/lib/desktop'; type AppearanceSlice = { showReasoningTraces: boolean; }; let initialized = false; export const startAppearanceAutoSave = (): void => { if (initialized || typeof window === 'undefined') { return; } initialized = true; let previous: AppearanceSlice = { showReasoningTraces: useUIStore.getState().showReasoningTraces, }; let pending: Partial | null = null; let timer: ReturnType | null = null; const flush = () => { const payload = pending; pending = null; timer = null; if (payload && Object.keys(payload).length > 0) { void updateDesktopSettings(payload); } }; const schedule = (changes: Partial) => { pending = { ...(pending ?? {}), ...changes }; if (timer) { return; } timer = setTimeout(flush, 150); }; useUIStore.subscribe((state) => { const current: AppearanceSlice = { showReasoningTraces: state.showReasoningTraces, }; const diff: Partial = {}; if (current.showReasoningTraces !== previous.showReasoningTraces) { diff.showReasoningTraces = current.showReasoningTraces; } previous = current; if (Object.keys(diff).length > 0) { schedule(diff); } }); };