From 0737c1abaa3e1d06916e114b5c5aa2323c358834 Mon Sep 17 00:00:00 2001 From: Wind Li Date: Sun, 2 Aug 2026 22:21:19 +0800 Subject: [PATCH] fix: rename input pre-fills displayed title and selects all text --- components/SessionSidebar.tsx | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/components/SessionSidebar.tsx b/components/SessionSidebar.tsx index 0514b55e5..cd6bca3e6 100644 --- a/components/SessionSidebar.tsx +++ b/components/SessionSidebar.tsx @@ -1830,19 +1830,29 @@ function SessionItem({ const [deleting, setDeleting] = useState(false); const inputRef = useRef(null); + // Select the whole name once the rename input is mounted (startRename's + // immediate setTimeout can fire before the input exists). + useEffect(() => { + if (renaming) { + const id = requestAnimationFrame(() => inputRef.current?.select()); + return () => cancelAnimationFrame(id); + } + }, [renaming]); + const title = session.name || session.firstMessage.slice(0, 50) || session.id.slice(0, 12); const startRename = useCallback((e: React.MouseEvent) => { e.stopPropagation(); - setRenameValue(session.name ?? ""); + setRenameValue(session.name || session.firstMessage.slice(0, 50) || session.id.slice(0, 12)); setRenaming(true); - setTimeout(() => inputRef.current?.select(), 0); - }, [session.name]); + }, [session.name, session.firstMessage, session.id]); const commitRename = useCallback(async () => { const name = renameValue.trim(); setRenaming(false); - if (name === (session.name ?? "")) return; + // No-op when unchanged: the fallback title (first message / id) isn't a + // real stored name, so don't persist it as one. + if (name === (session.name ?? "") || name === title) return; try { await fetch(`/api/sessions/${encodeURIComponent(session.id)}`, { method: "PATCH", @@ -1853,7 +1863,7 @@ function SessionItem({ } catch { // ignore } - }, [renameValue, session.id, session.name, onRenamed]); + }, [renameValue, session.id, session.name, onRenamed, title]); const performDelete = useCallback(async () => { setConfirmDelete(false);