返回 DeepSeek-Reasonix
useSessionTitleOperation.ts
根目录 / desktop / frontend / src / lib / useSessionTitleOperation.ts
1 import { useRef, useState } from "react";
2 import { app } from "./bridge";
3 import { useT } from "./i18n";
4 import { useToast } from "./toast";
5 import { sessionTitleErrorKey, sessionTitleSelector } from "./sessionTitleOperation";
6
7 export function useSessionTitleOperation(refresh: () => Promise<void>, onTopicsChanged?: () => Promise<void> | void) {
8 const [renaming, setRenaming] = useState<Set<string>>(() => new Set());
9 const operations = useRef(new Map<string, string>());
10 const requestSequence = useRef(0);
11 const t = useT();
12 const { showToast } = useToast();
13 const rename = async (target: string) => {
14 if (operations.current.has(target)) return;
15 const requestId = `${target}:${++requestSequence.current}`;
16 operations.current.set(target, requestId);
17 setRenaming(current => new Set(current).add(target));
18 try {
19 const result = await app.AIRenameSessionTarget(sessionTitleSelector(target));
20 await refresh();
21 await onTopicsChanged?.();
22 if (result.title) showToast(t("projectTree.aiRenameDone", { title: result.title }));
23 } catch (error) {
24 showToast(t(sessionTitleErrorKey(error)), "error");
25 } finally {
26 if (operations.current.get(target) === requestId) {
27 operations.current.delete(target);
28 setRenaming(current => {
29 const next = new Set(current);
30 next.delete(target);
31 return next;
32 });
33 }
34 }
35 };
36 return { renaming, rename };
37 }
38
38 lines TYPESCRIPT