Skip to content

Commit f26bf25

Browse files
committed
Fix Ctrl/Cmd+Enter shortcut: use refs to avoid stale closure
The useEffect captured a stale handleRunAll on mount because useCallback dependencies changed on every render. Now uses refs to always access the latest function and state without re-registering the event listener.
1 parent 4a03d35 commit f26bf25

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

frontend/src/App.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,18 +602,25 @@ function App() {
602602
const runnableCount = samples.filter((s) => s.status === "pending" || s.status === "error").length;
603603

604604
// Ctrl+Enter / Cmd+Enter shortcut to run analysis
605+
// Use a ref to always capture the latest handleRunAll without re-registering the listener
606+
const handleRunAllRef = useRef(handleRunAll);
607+
handleRunAllRef.current = handleRunAll;
608+
const runStateRef = useRef({ running, filesReady, runnableCount });
609+
runStateRef.current = { running, filesReady, runnableCount };
610+
605611
useEffect(() => {
606612
const handler = (e: KeyboardEvent) => {
607613
if ((e.ctrlKey || e.metaKey) && e.key === "Enter") {
608614
e.preventDefault();
609-
if (!running && filesReady && runnableCount > 0) {
610-
handleRunAll();
615+
const { running: r, filesReady: f, runnableCount: rc } = runStateRef.current;
616+
if (!r && f && rc > 0) {
617+
handleRunAllRef.current();
611618
}
612619
}
613620
};
614621
document.addEventListener("keydown", handler);
615622
return () => document.removeEventListener("keydown", handler);
616-
}, [running, filesReady, runnableCount, handleRunAll]);
623+
}, []);
617624

618625
return (
619626
<div className="app">

0 commit comments

Comments
 (0)