Skip to content

Commit 2bdaff0

Browse files
committed
fix(modes): resolve react-hooks/exhaustive-deps in window message listener
Use a stable ref to hold the latest switchMode and avoid capturing it in the window "message" listener closure: Add a switchModeRef updated via useEffect; call switchModeRef.current in the importModeResult fallback instead of switchMode Mirrors existing handleModeSwitchRef/customModesRef pattern to keep handler stable while accessing fresh values Prevents re-registration churn and removes the ESLint warning with no runtime behavior change File touched: webview-ui/src/components/modes/ModesView.tsx Why: The effect that registers the window event listener intentionally has an empty dependency array; directly referencing switchMode violates react-hooks/exhaustive-deps. Using a ref decouples the effect from function identity changes while preserving up-to-date behavior.
1 parent a2792d1 commit 2bdaff0

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

webview-ui/src/components/modes/ModesView.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ const ModesView = ({ onDone }: ModesViewProps) => {
197197
customModesRef.current = customModes
198198
}, [customModes])
199199

200+
// Keep latest switchMode available inside window message handler
201+
const switchModeRef = useRef(switchMode)
202+
useEffect(() => {
203+
switchModeRef.current = switchMode
204+
}, [switchMode])
205+
200206
// Handler for popover open state change
201207
const onOpenChange = useCallback((open: boolean) => {
202208
setOpen(open)
@@ -482,7 +488,7 @@ const ModesView = ({ onDone }: ModesViewProps) => {
482488
} else {
483489
// Fallback: switch by slug to keep backend in sync and update visual selection
484490
setVisualMode(slug)
485-
switchMode(slug)
491+
switchModeRef.current?.(slug)
486492
}
487493
}
488494
} else {

0 commit comments

Comments
 (0)