mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-25 23:14:46 +08:00
feat: remember sidebar state
This commit is contained in:
@@ -26,7 +26,7 @@ export default function ChatPage() {
|
|||||||
[threadIdFromPath],
|
[threadIdFromPath],
|
||||||
);
|
);
|
||||||
const [threadId, setThreadId] = useState<string | null>(null);
|
const [threadId, setThreadId] = useState<string | null>(null);
|
||||||
const { threadContext, setThreadContext } = useLocalSettings();
|
const [settings, setSettings] = useLocalSettings();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (threadIdFromPath !== "new") {
|
if (threadIdFromPath !== "new") {
|
||||||
@@ -43,7 +43,7 @@ export default function ChatPage() {
|
|||||||
isNewThread,
|
isNewThread,
|
||||||
threadId,
|
threadId,
|
||||||
thread,
|
thread,
|
||||||
threadContext,
|
threadContext: settings.context,
|
||||||
afterSubmit() {
|
afterSubmit() {
|
||||||
router.push(pathOfThread(threadId!));
|
router.push(pathOfThread(threadId!));
|
||||||
},
|
},
|
||||||
@@ -71,8 +71,8 @@ export default function ChatPage() {
|
|||||||
className="w-full max-w-(--container-width-md)"
|
className="w-full max-w-(--container-width-md)"
|
||||||
autoFocus={isNewThread}
|
autoFocus={isNewThread}
|
||||||
status={thread.isLoading ? "streaming" : "ready"}
|
status={thread.isLoading ? "streaming" : "ready"}
|
||||||
context={threadContext}
|
context={settings.context}
|
||||||
onContextChange={setThreadContext}
|
onContextChange={(context) => setSettings("context", context)}
|
||||||
onSubmit={handleSubmit}
|
onSubmit={handleSubmit}
|
||||||
onStop={handleStop}
|
onStop={handleStop}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -1,16 +1,30 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||||
|
import { useCallback, useEffect, useState } from "react";
|
||||||
|
|
||||||
import { SidebarInset, SidebarProvider } from "@/components/ui/sidebar";
|
import { SidebarInset, SidebarProvider } from "@/components/ui/sidebar";
|
||||||
import { Overscroll } from "@/components/workspace/overscroll";
|
import { Overscroll } from "@/components/workspace/overscroll";
|
||||||
import { WorkspaceSidebar } from "@/components/workspace/workspace-sidebar";
|
import { WorkspaceSidebar } from "@/components/workspace/workspace-sidebar";
|
||||||
|
import { useLocalSettings } from "@/core/settings";
|
||||||
|
|
||||||
const queryClient = new QueryClient();
|
const queryClient = new QueryClient();
|
||||||
|
|
||||||
export default function WorkspaceLayout({
|
export default function WorkspaceLayout({
|
||||||
children,
|
children,
|
||||||
}: Readonly<{ children: React.ReactNode }>) {
|
}: Readonly<{ children: React.ReactNode }>) {
|
||||||
|
const [settings, setSettings] = useLocalSettings();
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
useEffect(() => {
|
||||||
|
setOpen(!settings.layout.sidebar_collapsed);
|
||||||
|
}, [settings.layout.sidebar_collapsed]);
|
||||||
|
const handleOpenChange = useCallback(
|
||||||
|
(open: boolean) => {
|
||||||
|
setOpen(open);
|
||||||
|
setSettings("layout", { sidebar_collapsed: !open });
|
||||||
|
},
|
||||||
|
[setSettings],
|
||||||
|
);
|
||||||
return (
|
return (
|
||||||
<QueryClientProvider client={queryClient}>
|
<QueryClientProvider client={queryClient}>
|
||||||
<SidebarProvider
|
<SidebarProvider
|
||||||
@@ -19,6 +33,8 @@ export default function WorkspaceLayout({
|
|||||||
"--sidebar-width": "calc(var(--spacing) * 72)",
|
"--sidebar-width": "calc(var(--spacing) * 72)",
|
||||||
} as React.CSSProperties
|
} as React.CSSProperties
|
||||||
}
|
}
|
||||||
|
open={open}
|
||||||
|
onOpenChange={handleOpenChange}
|
||||||
>
|
>
|
||||||
<Overscroll behavior="none" overflow="hidden" />
|
<Overscroll behavior="none" overflow="hidden" />
|
||||||
<WorkspaceSidebar />
|
<WorkspaceSidebar />
|
||||||
|
|||||||
@@ -1,34 +1,46 @@
|
|||||||
import { useCallback, useState } from "react";
|
import { useCallback, useState } from "react";
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
|
|
||||||
import type { AgentThreadContext } from "../threads";
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
DEFAULT_LOCAL_SETTINGS,
|
DEFAULT_LOCAL_SETTINGS,
|
||||||
getLocalSettings,
|
getLocalSettings,
|
||||||
updateContextOfLocalSettings,
|
saveLocalSettings,
|
||||||
|
type LocalSettings,
|
||||||
} from "./local";
|
} from "./local";
|
||||||
|
|
||||||
export function useLocalSettings() {
|
export function useLocalSettings(): [
|
||||||
|
LocalSettings,
|
||||||
|
(
|
||||||
|
key: keyof LocalSettings,
|
||||||
|
value: Partial<LocalSettings[keyof LocalSettings]>,
|
||||||
|
) => void,
|
||||||
|
] {
|
||||||
const [mounted, setMounted] = useState(false);
|
const [mounted, setMounted] = useState(false);
|
||||||
const [threadContextState, setThreadContextState] = useState<
|
const [state, setState] = useState<LocalSettings>(DEFAULT_LOCAL_SETTINGS);
|
||||||
Omit<AgentThreadContext, "thread_id">
|
|
||||||
>(DEFAULT_LOCAL_SETTINGS.context);
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!mounted) {
|
if (!mounted) {
|
||||||
setThreadContextState(getLocalSettings().context);
|
setState(getLocalSettings());
|
||||||
}
|
}
|
||||||
setMounted(true);
|
setMounted(true);
|
||||||
}, [mounted]);
|
}, [mounted]);
|
||||||
const setThreadContext = useCallback(
|
const setter = useCallback(
|
||||||
(context: Omit<AgentThreadContext, "thread_id">) => {
|
(
|
||||||
setThreadContextState(context);
|
key: keyof LocalSettings,
|
||||||
updateContextOfLocalSettings(context);
|
value: Partial<LocalSettings[keyof LocalSettings]>,
|
||||||
|
) => {
|
||||||
|
setState((prev) => {
|
||||||
|
const newState = {
|
||||||
|
...prev,
|
||||||
|
[key]: {
|
||||||
|
...prev[key],
|
||||||
|
...value,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
saveLocalSettings(newState);
|
||||||
|
return newState;
|
||||||
|
});
|
||||||
},
|
},
|
||||||
[],
|
[],
|
||||||
);
|
);
|
||||||
return {
|
return [state, setter];
|
||||||
threadContext: threadContextState,
|
|
||||||
setThreadContext,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,12 +5,18 @@ export const DEFAULT_LOCAL_SETTINGS: LocalSettings = {
|
|||||||
model_name: "deepseek-v3.2",
|
model_name: "deepseek-v3.2",
|
||||||
thinking_enabled: true,
|
thinking_enabled: true,
|
||||||
},
|
},
|
||||||
|
layout: {
|
||||||
|
sidebar_collapsed: false,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const LOCAL_SETTINGS_KEY = "deerflow.local-settings";
|
const LOCAL_SETTINGS_KEY = "deerflow.local-settings";
|
||||||
|
|
||||||
export interface LocalSettings {
|
export interface LocalSettings {
|
||||||
context: Omit<AgentThreadContext, "thread_id">;
|
context: Omit<AgentThreadContext, "thread_id">;
|
||||||
|
layout: {
|
||||||
|
sidebar_collapsed: boolean;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getLocalSettings(): LocalSettings {
|
export function getLocalSettings(): LocalSettings {
|
||||||
@@ -20,7 +26,11 @@ export function getLocalSettings(): LocalSettings {
|
|||||||
const json = localStorage.getItem(LOCAL_SETTINGS_KEY);
|
const json = localStorage.getItem(LOCAL_SETTINGS_KEY);
|
||||||
try {
|
try {
|
||||||
if (json) {
|
if (json) {
|
||||||
return JSON.parse(json);
|
const settings = JSON.parse(json);
|
||||||
|
return {
|
||||||
|
...DEFAULT_LOCAL_SETTINGS,
|
||||||
|
...settings,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
} catch {}
|
} catch {}
|
||||||
return DEFAULT_LOCAL_SETTINGS;
|
return DEFAULT_LOCAL_SETTINGS;
|
||||||
@@ -29,16 +39,3 @@ export function getLocalSettings(): LocalSettings {
|
|||||||
export function saveLocalSettings(settings: LocalSettings) {
|
export function saveLocalSettings(settings: LocalSettings) {
|
||||||
localStorage.setItem(LOCAL_SETTINGS_KEY, JSON.stringify(settings));
|
localStorage.setItem(LOCAL_SETTINGS_KEY, JSON.stringify(settings));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateContextOfLocalSettings(
|
|
||||||
context: LocalSettings["context"],
|
|
||||||
) {
|
|
||||||
const settings = getLocalSettings();
|
|
||||||
saveLocalSettings({
|
|
||||||
...settings,
|
|
||||||
context: {
|
|
||||||
...settings.context,
|
|
||||||
...context,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user