feat(webui): rename sidebar topics inline

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-18 01:48:42 +08:00
1 parent 7edd377352
commit d9537bcc94
23 files changed
+736 -68

No files matched your search

+65 -9
View File
@@ -130,6 +130,24 @@ function sameState(a: SidebarStatePayload, b: SidebarStatePayload): boolean {
return JSON.stringify(a) === JSON.stringify(b);
}
function withTitleOverride(
state: SidebarStatePayload,
key: string,
title: string | undefined,
): SidebarStatePayload {
const titles = { ...state.title_overrides };
if (title === undefined) delete titles[key];
else titles[key] = title;
return { ...state, title_overrides: titles };
}
type PendingRename = {
key: string;
title: string;
resolve: () => void;
reject: (error: unknown) => void;
};
export function useSidebarState(
sessions: ChatSummary[],
sessionsLoaded: boolean,
@@ -139,6 +157,7 @@ export function useSidebarState(
update: (
updater: (state: SidebarStatePayload) => SidebarStatePayload,
) => Promise<void>;
rename: (key: string, title: string) => Promise<void>;
} {
const { client, token } = useClient();
const tokenRef = useRef(token);
@@ -146,6 +165,7 @@ export function useSidebarState(
const loadedRef = useRef(false);
const connectionOpenRef = useRef(client.status === "open");
const pendingPersistenceRef = useRef<SidebarStatePayload | null>(null);
const pendingRenamesRef = useRef<PendingRename[]>([]);
const persistenceInFlightRef = useRef(false);
const flushPersistenceRef = useRef<() => void>(() => {});
const [state, setState] = useState<SidebarStatePayload>(DEFAULT_SIDEBAR_STATE);
@@ -180,34 +200,58 @@ export function useSidebarState(
};
}, []);
const rejectPendingRenames = useCallback((error: unknown) => {
for (const request of pendingRenamesRef.current.splice(0)) request.reject(error);
}, []);
const flushPersistence = useCallback(() => {
if (
!loadedRef.current
|| persistenceInFlightRef.current
|| !connectionOpenRef.current
|| pendingPersistenceRef.current === null
) return;
const next = pendingPersistenceRef.current;
const pending = pendingPersistenceRef.current;
// Optimistic snapshots must drain before a rename takes its own snapshot.
const rename = pending === null ? pendingRenamesRef.current.shift() : undefined;
const next = rename
? normalizeSidebarState(withTitleOverride(stateRef.current, rename.key, rename.title))
: pending;
if (next === null) return;
pendingPersistenceRef.current = null;
persistenceInFlightRef.current = true;
void client.setSidebarState(next)
.then((saved) => {
persistenceInFlightRef.current = false;
if (pendingPersistenceRef.current === null) {
if (rename) {
const title = normalizeSidebarState(saved).title_overrides[rename.key];
if (pendingPersistenceRef.current !== null) {
pendingPersistenceRef.current = withTitleOverride(
pendingPersistenceRef.current, rename.key, title,
);
}
const renamed = withTitleOverride(stateRef.current, rename.key, title);
stateRef.current = renamed;
setState(renamed);
rename.resolve();
} else if (pendingPersistenceRef.current === null) {
const canonical = normalizeSidebarState(saved);
stateRef.current = canonical;
setState(canonical);
}
flushPersistenceRef.current();
})
.catch(() => {
.catch((error: unknown) => {
persistenceInFlightRef.current = false;
if (pendingPersistenceRef.current === null) {
pendingPersistenceRef.current = next;
if (rename) {
rename.reject(error);
flushPersistenceRef.current();
} else {
if (pendingPersistenceRef.current === null) pendingPersistenceRef.current = next;
rejectPendingRenames(error);
}
});
}, [client]);
}, [client, rejectPendingRenames]);
flushPersistenceRef.current = flushPersistence;
const persist = useCallback((next: SidebarStatePayload) => {
@@ -218,7 +262,8 @@ export function useSidebarState(
useEffect(() => client.onStatus((status) => {
connectionOpenRef.current = status === "open";
if (status === "open") flushPersistence();
}), [client, flushPersistence]);
else rejectPendingRenames(new Error("WebUI connection is not open"));
}), [client, flushPersistence, rejectPendingRenames]);
useEffect(() => client.onSidebarStateUpdate((incoming) => {
if (
@@ -244,6 +289,17 @@ export function useSidebarState(
[persist],
);
const rename = useCallback((key: string, title: string): Promise<void> => {
if (!loadedRef.current) return Promise.reject(new Error("Sidebar state is not loaded"));
if (!connectionOpenRef.current) {
return Promise.reject(new Error("WebUI connection is not open"));
}
return new Promise<void>((resolve, reject) => {
pendingRenamesRef.current.push({ key: key.trim(), title, resolve, reject });
flushPersistence();
});
}, [flushPersistence]);
const pruned = useMemo(() => {
if (!sessionsLoaded || loading) return state;
return pruneMissingSessions(state, sessions);
@@ -254,5 +310,5 @@ export function useSidebarState(
void update(() => pruned);
}, [loading, pruned, sessionsLoaded, state, update]);
return { state, loading, update };
return { state, loading, update, rename };
}