From 8dbd0867b06c4aa57e430e7d3a392803a7d2f9eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E7=94=B5=E8=8A=BD=E8=A1=A3?= Date: Thu, 16 Apr 2026 22:58:12 -0400 Subject: [PATCH] refactor(useCommandPaletteHotkeys): read selectedPaletteIndex from store, drop prop (P2-44) Remove dual-source: option no longer takes selectedPaletteIndex; all reads go through useAppStore.getState().selectedPaletteIndex inside the keydown handler. --- src/renderer/App.tsx | 1 - src/renderer/hooks/useCommandPaletteHotkeys.ts | 11 +++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/renderer/App.tsx b/src/renderer/App.tsx index f1db9d0a..7889a383 100644 --- a/src/renderer/App.tsx +++ b/src/renderer/App.tsx @@ -110,7 +110,6 @@ export function App() { setCommandPaletteSearch: ui.setCommandPaletteSearch, setSelectedPaletteIndex: ui.setSelectedPaletteIndex, paletteActions, - selectedPaletteIndex: ui.selectedPaletteIndex, navigateTo, commandPaletteSearch: ui.commandPaletteSearch, }); diff --git a/src/renderer/hooks/useCommandPaletteHotkeys.ts b/src/renderer/hooks/useCommandPaletteHotkeys.ts index 429fd263..97bc5ced 100644 --- a/src/renderer/hooks/useCommandPaletteHotkeys.ts +++ b/src/renderer/hooks/useCommandPaletteHotkeys.ts @@ -8,7 +8,6 @@ interface CommandPaletteHotkeysOptions { setCommandPaletteSearch: (value: string) => void; setSelectedPaletteIndex: (index: number) => void; paletteActions: PaletteAction[]; - selectedPaletteIndex: number; navigateTo: (section: PaletteAction["section"], entityId?: string) => void; commandPaletteSearch: string; } @@ -19,7 +18,6 @@ export function useCommandPaletteHotkeys({ setCommandPaletteSearch, setSelectedPaletteIndex, paletteActions, - selectedPaletteIndex, navigateTo, commandPaletteSearch, }: CommandPaletteHotkeysOptions) { @@ -43,11 +41,12 @@ export function useCommandPaletteHotkeys({ function onKey(event: KeyboardEvent) { if (event.key === "Escape") { setCommandPaletteOpen(false); setCommandPaletteSearch(""); setSelectedPaletteIndex(0); return; } if (paletteActions.length === 0) return; - if (event.key === "ArrowDown") { event.preventDefault(); setSelectedPaletteIndex((useAppStore.getState().selectedPaletteIndex + 1) % paletteActions.length); return; } - if (event.key === "ArrowUp") { event.preventDefault(); setSelectedPaletteIndex((useAppStore.getState().selectedPaletteIndex - 1 + paletteActions.length) % paletteActions.length); return; } + const currentIndex = useAppStore.getState().selectedPaletteIndex; + if (event.key === "ArrowDown") { event.preventDefault(); setSelectedPaletteIndex((currentIndex + 1) % paletteActions.length); return; } + if (event.key === "ArrowUp") { event.preventDefault(); setSelectedPaletteIndex((currentIndex - 1 + paletteActions.length) % paletteActions.length); return; } if (event.key !== "Enter") return; event.preventDefault(); - const selectedAction = paletteActions[Math.min(selectedPaletteIndex, paletteActions.length - 1)]; + const selectedAction = paletteActions[Math.min(currentIndex, paletteActions.length - 1)]; if (!selectedAction) return; navigateTo(selectedAction.section, selectedAction.entityId); setCommandPaletteOpen(false); @@ -56,7 +55,7 @@ export function useCommandPaletteHotkeys({ } document.addEventListener("keydown", onKey); return () => document.removeEventListener("keydown", onKey); - }, [commandPaletteOpen, navigateTo, paletteActions, selectedPaletteIndex, setCommandPaletteOpen, setCommandPaletteSearch, setSelectedPaletteIndex]); + }, [commandPaletteOpen, navigateTo, paletteActions, setCommandPaletteOpen, setCommandPaletteSearch, setSelectedPaletteIndex]); useEffect(() => { if (!commandPaletteOpen) return;