mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-13 18:24:45 +08:00
47 lines
1.0 KiB
TypeScript
47 lines
1.0 KiB
TypeScript
import { useCallback, useState } from "react";
|
|
import { useEffect } from "react";
|
|
|
|
import {
|
|
DEFAULT_LOCAL_SETTINGS,
|
|
getLocalSettings,
|
|
saveLocalSettings,
|
|
type LocalSettings,
|
|
} from "./local";
|
|
|
|
export function useLocalSettings(): [
|
|
LocalSettings,
|
|
(
|
|
key: keyof LocalSettings,
|
|
value: Partial<LocalSettings[keyof LocalSettings]>,
|
|
) => void,
|
|
] {
|
|
const [mounted, setMounted] = useState(false);
|
|
const [state, setState] = useState<LocalSettings>(DEFAULT_LOCAL_SETTINGS);
|
|
useEffect(() => {
|
|
if (!mounted) {
|
|
setState(getLocalSettings());
|
|
}
|
|
setMounted(true);
|
|
}, [mounted]);
|
|
const setter = useCallback(
|
|
(
|
|
key: keyof LocalSettings,
|
|
value: Partial<LocalSettings[keyof LocalSettings]>,
|
|
) => {
|
|
setState((prev) => {
|
|
const newState = {
|
|
...prev,
|
|
[key]: {
|
|
...prev[key],
|
|
...value,
|
|
},
|
|
};
|
|
saveLocalSettings(newState);
|
|
return newState;
|
|
});
|
|
},
|
|
[],
|
|
);
|
|
return [state, setter];
|
|
}
|