Files
openchamber/packages/ui/src/lib/appearanceAutoSave.ts
T

144 lines
5.2 KiB
TypeScript
Raw Normal View History

2025-12-07 19:32:53 +02:00
import { useUIStore } from '@/stores/useUIStore';
import { updateDesktopSettings } from '@/lib/persistence';
import type { DesktopSettings } from '@/lib/desktop';
type AppearanceSlice = {
showReasoningTraces: boolean;
showTextJustificationActivity: boolean;
nativeNotificationsEnabled: boolean;
notificationMode: 'always' | 'hidden-only';
notifyOnSubtasks: boolean;
autoDeleteEnabled: boolean;
autoDeleteAfterDays: number;
toolCallExpansion: 'collapsed' | 'activity' | 'detailed';
fontSize: number;
terminalFontSize: number;
padding: number;
cornerRadius: number;
inputBarOffset: number;
diffLayoutPreference: 'dynamic' | 'inline' | 'side-by-side';
diffViewMode: 'single' | 'stacked';
2025-12-07 19:32:53 +02:00
};
let initialized = false;
export const startAppearanceAutoSave = (): void => {
if (initialized || typeof window === 'undefined') {
return;
}
initialized = true;
let previous: AppearanceSlice = {
showReasoningTraces: useUIStore.getState().showReasoningTraces,
showTextJustificationActivity: useUIStore.getState().showTextJustificationActivity,
nativeNotificationsEnabled: useUIStore.getState().nativeNotificationsEnabled,
notificationMode: useUIStore.getState().notificationMode,
notifyOnSubtasks: useUIStore.getState().notifyOnSubtasks,
autoDeleteEnabled: useUIStore.getState().autoDeleteEnabled,
autoDeleteAfterDays: useUIStore.getState().autoDeleteAfterDays,
toolCallExpansion: useUIStore.getState().toolCallExpansion,
fontSize: useUIStore.getState().fontSize,
terminalFontSize: useUIStore.getState().terminalFontSize,
padding: useUIStore.getState().padding,
cornerRadius: useUIStore.getState().cornerRadius,
inputBarOffset: useUIStore.getState().inputBarOffset,
diffLayoutPreference: useUIStore.getState().diffLayoutPreference,
diffViewMode: useUIStore.getState().diffViewMode,
2025-12-07 19:32:53 +02:00
};
let pending: Partial<DesktopSettings> | null = null;
let timer: ReturnType<typeof setTimeout> | 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<DesktopSettings>) => {
pending = { ...(pending ?? {}), ...changes };
if (timer) {
return;
}
timer = setTimeout(flush, 150);
};
useUIStore.subscribe((state) => {
const current: AppearanceSlice = {
showReasoningTraces: state.showReasoningTraces,
showTextJustificationActivity: state.showTextJustificationActivity,
nativeNotificationsEnabled: state.nativeNotificationsEnabled,
notificationMode: state.notificationMode,
notifyOnSubtasks: state.notifyOnSubtasks,
autoDeleteEnabled: state.autoDeleteEnabled,
autoDeleteAfterDays: state.autoDeleteAfterDays,
toolCallExpansion: state.toolCallExpansion,
fontSize: state.fontSize,
terminalFontSize: state.terminalFontSize,
padding: state.padding,
cornerRadius: state.cornerRadius,
inputBarOffset: state.inputBarOffset,
diffLayoutPreference: state.diffLayoutPreference,
diffViewMode: state.diffViewMode,
2025-12-07 19:32:53 +02:00
};
const diff: Partial<DesktopSettings> = {};
if (current.showReasoningTraces !== previous.showReasoningTraces) {
diff.showReasoningTraces = current.showReasoningTraces;
}
if (current.showTextJustificationActivity !== previous.showTextJustificationActivity) {
diff.showTextJustificationActivity = current.showTextJustificationActivity;
}
if (current.nativeNotificationsEnabled !== previous.nativeNotificationsEnabled) {
diff.nativeNotificationsEnabled = current.nativeNotificationsEnabled;
}
if (current.notificationMode !== previous.notificationMode) {
diff.notificationMode = current.notificationMode;
}
if (current.notifyOnSubtasks !== previous.notifyOnSubtasks) {
diff.notifyOnSubtasks = current.notifyOnSubtasks;
}
if (current.autoDeleteEnabled !== previous.autoDeleteEnabled) {
diff.autoDeleteEnabled = current.autoDeleteEnabled;
}
if (current.autoDeleteAfterDays !== previous.autoDeleteAfterDays) {
diff.autoDeleteAfterDays = current.autoDeleteAfterDays;
}
if (current.toolCallExpansion !== previous.toolCallExpansion) {
diff.toolCallExpansion = current.toolCallExpansion;
}
if (current.fontSize !== previous.fontSize) {
diff.fontSize = current.fontSize;
}
if (current.terminalFontSize !== previous.terminalFontSize) {
diff.terminalFontSize = current.terminalFontSize;
}
if (current.padding !== previous.padding) {
diff.padding = current.padding;
}
if (current.cornerRadius !== previous.cornerRadius) {
diff.cornerRadius = current.cornerRadius;
}
if (current.inputBarOffset !== previous.inputBarOffset) {
diff.inputBarOffset = current.inputBarOffset;
}
if (current.diffLayoutPreference !== previous.diffLayoutPreference) {
diff.diffLayoutPreference = current.diffLayoutPreference;
}
if (current.diffViewMode !== previous.diffViewMode) {
diff.diffViewMode = current.diffViewMode;
}
2025-12-07 19:32:53 +02:00
previous = current;
if (Object.keys(diff).length > 0) {
schedule(diff);
}
});
};