* fix(frontend): resolve layout flickering by migrating workspace sidebar state to cookie * fix(frontend): unify local settings runtime state to fix state drift * fix(frontend): only persist thread model on explicit context model updates
60 lines
1.4 KiB
TypeScript
60 lines
1.4 KiB
TypeScript
import { useCallback, useMemo, useSyncExternalStore } from "react";
|
|
|
|
import {
|
|
DEFAULT_LOCAL_SETTINGS,
|
|
applyThreadModelOverride,
|
|
type LocalSettings,
|
|
} from "./local";
|
|
import {
|
|
getBaseSettingsSnapshot,
|
|
getThreadModelSnapshot,
|
|
subscribe,
|
|
updateLocalSettings,
|
|
updateThreadSettings,
|
|
type LocalSettingsSetter,
|
|
} from "./store";
|
|
|
|
export function useLocalSettings(): [LocalSettings, LocalSettingsSetter] {
|
|
const settings = useSyncExternalStore(
|
|
subscribe,
|
|
getBaseSettingsSnapshot,
|
|
() => DEFAULT_LOCAL_SETTINGS,
|
|
);
|
|
|
|
const setSettings = useCallback<LocalSettingsSetter>((key, value) => {
|
|
updateLocalSettings(key, value);
|
|
}, []);
|
|
|
|
return [settings, setSettings];
|
|
}
|
|
|
|
export function useThreadSettings(
|
|
threadId: string,
|
|
): [LocalSettings, LocalSettingsSetter] {
|
|
const baseSettings = useSyncExternalStore(
|
|
subscribe,
|
|
getBaseSettingsSnapshot,
|
|
() => DEFAULT_LOCAL_SETTINGS,
|
|
);
|
|
|
|
const threadModelName = useSyncExternalStore(
|
|
subscribe,
|
|
() => getThreadModelSnapshot(threadId),
|
|
() => undefined,
|
|
);
|
|
|
|
const settings = useMemo(
|
|
() => applyThreadModelOverride(baseSettings, threadModelName),
|
|
[baseSettings, threadModelName],
|
|
);
|
|
|
|
const setSettings = useCallback<LocalSettingsSetter>(
|
|
(key, value) => {
|
|
updateThreadSettings(threadId, key, value);
|
|
},
|
|
[threadId],
|
|
);
|
|
|
|
return [settings, setSettings];
|
|
}
|