From e641e9c655bef8434d05f3875e6e49b27ba48cb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BA=B3=E4=BF=A1?= Date: Sat, 18 Jul 2026 22:11:07 +0800 Subject: [PATCH] fix(web-shell): dedupe restored images and harden the sidebar shortcut handler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Post-merge review follow-ups from #7134 and #7135: - restoreImages now runs only alongside an actual text change: it appends to the pasted-image list, so a deduplicated restore of the same prompt (reconnect/retry) previously doubled the attachments while the text correctly stayed single. - The Cmd+B/Ctrl+B handler no longer toggles the sidebar while a non-composer editable element is focused (sidebar search, session rename, settings inputs) — matching the codebase's isEditableTarget convention; the composer keeps the editor-convention behavior and still toggles, verified in a real browser. - All state updates moved out of React updater functions (purity contract, mirroring the hamburger handler); the effect re-binds on the involved state so the listener closure stays fresh. - A drawer forced open on a wide viewport now takes the drawer path: the shortcut closes the overlay instead of collapsing the rail invisibly underneath it. Refs #7102 #7128 Co-Authored-By: Claude Fable 5 --- packages/web-shell/client/App.tsx | 49 ++++++++++++++----- .../client/hooks/useQueuedPrompts.ts | 11 +++-- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/packages/web-shell/client/App.tsx b/packages/web-shell/client/App.tsx index 720c3699b00..c9471eead63 100644 --- a/packages/web-shell/client/App.tsx +++ b/packages/web-shell/client/App.tsx @@ -1153,23 +1153,50 @@ export function App({ if (!sidebarOptions.enabled) return undefined; const onKey = (e: KeyboardEvent) => { if (!isSidebarToggleShortcut(e)) return; + // The composer keeps the editor-convention behavior (VS Code toggles + // the sidebar while the editor is focused), but other editable targets + // — sidebar search, session rename, settings inputs — must not have + // the sidebar yanked around while the user is typing, matching the + // codebase's isEditableTarget convention. + const target = e.target as HTMLElement | null; + if ( + isEditableTarget(target) && + !target?.closest('[data-web-shell-composer-editor]') + ) { + return; + } e.preventDefault(); - if (window.matchMedia('(max-width: 760px)').matches) { - setMobileDrawerOpen((open) => { - if (open) setForceMobileDrawer(false); - return !open; - }); + // All state updates are dispatched sequentially outside the updater + // functions (React purity contract — mirrors the hamburger handler), + // which is why this effect re-binds on state changes: the listener + // closure must stay fresh. + if ( + forceMobileDrawer || + window.matchMedia('(max-width: 760px)').matches + ) { + // A forced drawer on a wide viewport still belongs to the drawer + // path: collapsing the rail underneath the overlay would look like + // a no-op to the user. + if (mobileDrawerOpen) { + setMobileDrawerOpen(false); + setForceMobileDrawer(false); + } else { + setMobileDrawerOpen(true); + } return; } - setSidebarCollapsed((collapsed) => { - const next = !collapsed; - writeSidebarCollapsed(next); - return next; - }); + const next = !sidebarCollapsed; + setSidebarCollapsed(next); + writeSidebarCollapsed(next); }; window.addEventListener('keydown', onKey); return () => window.removeEventListener('keydown', onKey); - }, [sidebarOptions.enabled]); + }, [ + sidebarOptions.enabled, + mobileDrawerOpen, + forceMobileDrawer, + sidebarCollapsed, + ]); const customization = useMemo( () => ({ composerTagIcons, diff --git a/packages/web-shell/client/hooks/useQueuedPrompts.ts b/packages/web-shell/client/hooks/useQueuedPrompts.ts index 0f6c7d23d77..e9f9947d12d 100644 --- a/packages/web-shell/client/hooks/useQueuedPrompts.ts +++ b/packages/web-shell/client/hooks/useQueuedPrompts.ts @@ -304,9 +304,14 @@ export function useQueuedPrompts({ const next = mergeRestoredPromptText(current, text); if (next !== current) { editorRef.current?.setText(next); - } - if (images && images.length > 0) { - editorRef.current?.restoreImages(images); + // Restore images only alongside a text change: restoreImages appends + // to the pasted-image list, so running it on a deduplicated restore + // (same prompt restored twice across reconnects/retries) would double + // the attachments while the text correctly stays single (#7134 + // review follow-up). + if (images && images.length > 0) { + editorRef.current?.restoreImages(images); + } } editorRef.current?.focus(); },