mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-05-22 16:06:50 +00:00
fix(frontend): persist model selection per thread (#1553)
* fix(frontend): persist model selection per thread * fix(frontend): apply thread model override on fallback * refactor(frontend): split thread settings hook * fix frontend local storage guards
This commit is contained in:
@@ -3,44 +3,62 @@ import { useCallback, useLayoutEffect, useState } from "react";
|
||||
import {
|
||||
DEFAULT_LOCAL_SETTINGS,
|
||||
getLocalSettings,
|
||||
getThreadLocalSettings,
|
||||
saveLocalSettings,
|
||||
saveThreadLocalSettings,
|
||||
type LocalSettings,
|
||||
} from "./local";
|
||||
|
||||
export function useLocalSettings(): [
|
||||
LocalSettings,
|
||||
(
|
||||
key: keyof LocalSettings,
|
||||
value: Partial<LocalSettings[keyof LocalSettings]>,
|
||||
) => void,
|
||||
] {
|
||||
const [mounted, setMounted] = useState(false);
|
||||
type LocalSettingsSetter = (
|
||||
key: keyof LocalSettings,
|
||||
value: Partial<LocalSettings[keyof LocalSettings]>,
|
||||
) => void;
|
||||
|
||||
function useSettingsState(
|
||||
getSettings: () => LocalSettings,
|
||||
saveSettings: (settings: LocalSettings) => void,
|
||||
): [LocalSettings, LocalSettingsSetter] {
|
||||
const [state, setState] = useState<LocalSettings>(DEFAULT_LOCAL_SETTINGS);
|
||||
|
||||
const [mounted, setMounted] = useState(false);
|
||||
useLayoutEffect(() => {
|
||||
if (!mounted) {
|
||||
setState(getLocalSettings());
|
||||
}
|
||||
setState(getSettings());
|
||||
setMounted(true);
|
||||
}, [mounted]);
|
||||
const setter = useCallback(
|
||||
(
|
||||
key: keyof LocalSettings,
|
||||
value: Partial<LocalSettings[keyof LocalSettings]>,
|
||||
) => {
|
||||
}, [getSettings]);
|
||||
|
||||
const setter = useCallback<LocalSettingsSetter>(
|
||||
(key, value) => {
|
||||
if (!mounted) return;
|
||||
setState((prev) => {
|
||||
const newState = {
|
||||
const newState: LocalSettings = {
|
||||
...prev,
|
||||
[key]: {
|
||||
...prev[key],
|
||||
...value,
|
||||
},
|
||||
};
|
||||
saveLocalSettings(newState);
|
||||
saveSettings(newState);
|
||||
return newState;
|
||||
});
|
||||
},
|
||||
[mounted],
|
||||
[mounted, saveSettings],
|
||||
);
|
||||
|
||||
return [state, setter];
|
||||
}
|
||||
|
||||
export function useLocalSettings(): [LocalSettings, LocalSettingsSetter] {
|
||||
return useSettingsState(getLocalSettings, saveLocalSettings);
|
||||
}
|
||||
|
||||
export function useThreadSettings(
|
||||
threadId: string,
|
||||
): [LocalSettings, LocalSettingsSetter] {
|
||||
return useSettingsState(
|
||||
useCallback(() => getThreadLocalSettings(threadId), [threadId]),
|
||||
useCallback(
|
||||
(settings: LocalSettings) => saveThreadLocalSettings(threadId, settings),
|
||||
[threadId],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user