From db2152dcfae8860c487f40f38befd463a50cac66 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 8 Aug 2026 07:25:27 +0000 Subject: [PATCH] fix(scripts): harden delete handler against stale vault state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Read snippets/hosts from refs in the shared delete listener so rapid confirms cannot resurrect already-removed items, and clear pending bulk delete when the scripts panel hides so the confirm dialog does not resurface after a mid-prompt hide. Co-authored-by: 陈大猫 --- .../app/AppSideEffects.snippetsDelete.test.ts | 10 ++++++++++ application/app/AppSideEffects.tsx | 12 ++++++++++-- components/ScriptsSidePanel.test.ts | 9 +++++++++ components/ScriptsSidePanel.tsx | 4 ++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/application/app/AppSideEffects.snippetsDelete.test.ts b/application/app/AppSideEffects.snippetsDelete.test.ts index d03047a52..6c7df5ec2 100644 --- a/application/app/AppSideEffects.snippetsDelete.test.ts +++ b/application/app/AppSideEffects.snippetsDelete.test.ts @@ -16,3 +16,13 @@ test("snippets delete handler cleans host bindings via deleteSelectedSnippetsFro /updateSnippets\(snippets\.filter\(\(s\) => !ids\.has\(s\.id\)\)\)/, ); }); + +test("snippets delete handler reads vault state from refs to avoid stale closures", () => { + // Rapid/double confirms must see the latest snippets/hosts, not the + // values closed over when the listener was last registered. + assert.match(source, /snippetsRef\.current\s*=\s*snippets/); + assert.match( + source, + /deleteSelectedSnippetsFromVault\(\s*snippetsRef\.current\s*,\s*hostsRef\.current\s*,\s*ids,?\s*\)/, + ); +}); diff --git a/application/app/AppSideEffects.tsx b/application/app/AppSideEffects.tsx index 96e8bb7f5..5208626cd 100755 --- a/application/app/AppSideEffects.tsx +++ b/application/app/AppSideEffects.tsx @@ -202,6 +202,8 @@ export function AppSideEffects() { const hostsRef = useRef(hosts); hostsRef.current = hosts; + const snippetsRef = useRef(snippets); + snippetsRef.current = snippets; const keysRef = useRef(keys); keysRef.current = keys; const knownHostsRef = useRef(knownHosts); @@ -1609,19 +1611,25 @@ export function AppSideEffects() { // here (rather than in QuickAddSnippetDialog) because delete needs no UI. // Must go through deleteSelectedSnippetsFromVault so login/connect script // bindings on hosts are cleared with the snippets (SnippetsManager parity). + // Read snippets/hosts from refs so rapid/double confirms do not apply deletes + // against a stale closed-over vault snapshot. useEffect(() => { const handler = (e: Event) => { const detail = (e as CustomEvent<{ id?: string; ids?: string[] }>).detail; const ids = collectSnippetDeleteIds(detail); if (ids.size === 0) return; - const result = deleteSelectedSnippetsFromVault(snippets, hosts, ids); + const result = deleteSelectedSnippetsFromVault( + snippetsRef.current, + hostsRef.current, + ids, + ); if (result.deletedCount === 0) return; updateSnippets(result.snippets); updateHosts(result.hosts); }; window.addEventListener('netcatty:snippets:delete', handler); return () => window.removeEventListener('netcatty:snippets:delete', handler); - }, [hosts, snippets, updateHosts, updateSnippets]); + }, [updateHosts, updateSnippets]); const handleEndSessionDrag = useCallback(() => { setDraggingSessionId(null); diff --git a/components/ScriptsSidePanel.test.ts b/components/ScriptsSidePanel.test.ts index 5a8e0aa1a..f4ad03f43 100644 --- a/components/ScriptsSidePanel.test.ts +++ b/components/ScriptsSidePanel.test.ts @@ -92,6 +92,15 @@ test("scripts side panel confirms bulk delete and routes it through the shared d ); }); +test("scripts side panel clears pending bulk delete when the panel hides", () => { + // Returning null while isVisible is false unmounts the confirm dialog; drop + // pending deletes so a later re-show does not resurrect a half-dismissed prompt. + assert.match( + source, + /if\s*\(\s*!isVisible\s*\)\s*setPendingDeleteIds\(\s*null\s*\)/, + ); +}); + test("scripts side panel package dialog traps focus and exposes dialog close contract", () => { assert.match(source, /packageDialogRef/); assert.match(source, /data-dialog-close="true"/); diff --git a/components/ScriptsSidePanel.tsx b/components/ScriptsSidePanel.tsx index ec38cff31..ba18e6339 100644 --- a/components/ScriptsSidePanel.tsx +++ b/components/ScriptsSidePanel.tsx @@ -389,6 +389,10 @@ const ScriptsSidePanelInner: React.FC = ({ }); }, [snippets]); + useEffect(() => { + if (!isVisible) setPendingDeleteIds(null); + }, [isVisible]); + const toggleSnippetSelection = useCallback((id: string) => { setSelectedSnippetIds((prev) => { const next = new Set(prev);