mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-28 16:30:52 +08:00
fix: use verge hook
This commit is contained in:
@@ -24,61 +24,6 @@ export const DEFAULT_STATE: HeadState = {
|
||||
testUrl: "",
|
||||
};
|
||||
|
||||
export default function useHeadState(groupName: string) {
|
||||
const current = useRecoilValue(atomCurrentProfile);
|
||||
|
||||
const [state, setState] = useState<HeadState>(DEFAULT_STATE);
|
||||
|
||||
useEffect(() => {
|
||||
if (!current) {
|
||||
setState(DEFAULT_STATE);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const data = JSON.parse(
|
||||
localStorage.getItem(HEAD_STATE_KEY)!
|
||||
) as HeadStateStorage;
|
||||
|
||||
const value = data[current][groupName] || DEFAULT_STATE;
|
||||
|
||||
if (value && typeof value === "object") {
|
||||
setState({ ...DEFAULT_STATE, ...value });
|
||||
} else {
|
||||
setState(DEFAULT_STATE);
|
||||
}
|
||||
} catch {}
|
||||
}, [current, groupName]);
|
||||
|
||||
const setHeadState = useCallback(
|
||||
(obj: Partial<HeadState>) => {
|
||||
setState((old) => {
|
||||
const ret = { ...old, ...obj };
|
||||
|
||||
setTimeout(() => {
|
||||
try {
|
||||
const item = localStorage.getItem(HEAD_STATE_KEY);
|
||||
|
||||
let data = (item ? JSON.parse(item) : {}) as HeadStateStorage;
|
||||
|
||||
if (!data || typeof data !== "object") data = {};
|
||||
if (!data[current]) data[current] = {};
|
||||
|
||||
data[current][groupName] = ret;
|
||||
|
||||
localStorage.setItem(HEAD_STATE_KEY, JSON.stringify(data));
|
||||
} catch {}
|
||||
});
|
||||
|
||||
return ret;
|
||||
});
|
||||
},
|
||||
[current, groupName]
|
||||
);
|
||||
|
||||
return [state, setHeadState] as const;
|
||||
}
|
||||
|
||||
export function useHeadStateNew() {
|
||||
const current = useRecoilValue(atomCurrentProfile);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user