feat: remember sidebar state

This commit is contained in:
Henry Li
2026-01-16 23:03:39 +08:00
parent f9853f037c
commit 6464a67230
4 changed files with 59 additions and 34 deletions

View File

@@ -1,34 +1,46 @@
import { useCallback, useState } from "react";
import { useEffect } from "react";
import type { AgentThreadContext } from "../threads";
import {
DEFAULT_LOCAL_SETTINGS,
getLocalSettings,
updateContextOfLocalSettings,
saveLocalSettings,
type LocalSettings,
} from "./local";
export function useLocalSettings() {
export function useLocalSettings(): [
LocalSettings,
(
key: keyof LocalSettings,
value: Partial<LocalSettings[keyof LocalSettings]>,
) => void,
] {
const [mounted, setMounted] = useState(false);
const [threadContextState, setThreadContextState] = useState<
Omit<AgentThreadContext, "thread_id">
>(DEFAULT_LOCAL_SETTINGS.context);
const [state, setState] = useState<LocalSettings>(DEFAULT_LOCAL_SETTINGS);
useEffect(() => {
if (!mounted) {
setThreadContextState(getLocalSettings().context);
setState(getLocalSettings());
}
setMounted(true);
}, [mounted]);
const setThreadContext = useCallback(
(context: Omit<AgentThreadContext, "thread_id">) => {
setThreadContextState(context);
updateContextOfLocalSettings(context);
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 {
threadContext: threadContextState,
setThreadContext,
};
return [state, setter];
}

View File

@@ -5,12 +5,18 @@ export const DEFAULT_LOCAL_SETTINGS: LocalSettings = {
model_name: "deepseek-v3.2",
thinking_enabled: true,
},
layout: {
sidebar_collapsed: false,
},
};
const LOCAL_SETTINGS_KEY = "deerflow.local-settings";
export interface LocalSettings {
context: Omit<AgentThreadContext, "thread_id">;
layout: {
sidebar_collapsed: boolean;
};
}
export function getLocalSettings(): LocalSettings {
@@ -20,7 +26,11 @@ export function getLocalSettings(): LocalSettings {
const json = localStorage.getItem(LOCAL_SETTINGS_KEY);
try {
if (json) {
return JSON.parse(json);
const settings = JSON.parse(json);
return {
...DEFAULT_LOCAL_SETTINGS,
...settings,
};
}
} catch {}
return DEFAULT_LOCAL_SETTINGS;
@@ -29,16 +39,3 @@ export function getLocalSettings(): LocalSettings {
export function saveLocalSettings(settings: LocalSettings) {
localStorage.setItem(LOCAL_SETTINGS_KEY, JSON.stringify(settings));
}
export function updateContextOfLocalSettings(
context: LocalSettings["context"],
) {
const settings = getLocalSettings();
saveLocalSettings({
...settings,
context: {
...settings.context,
...context,
},
});
}