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.
This commit is contained in:
ZacharyZhang-NY committed 2026-04-16 22:58:12 -04:00
1 parent 2bed06ca64
commit 8dbd0867b0
2 files changed
+5 -7

No files matched your search

-1
View File
@@ -110,7 +110,6 @@ export function App() {
setCommandPaletteSearch: ui.setCommandPaletteSearch, setCommandPaletteSearch: ui.setCommandPaletteSearch,
setSelectedPaletteIndex: ui.setSelectedPaletteIndex, setSelectedPaletteIndex: ui.setSelectedPaletteIndex,
paletteActions, paletteActions,
selectedPaletteIndex: ui.selectedPaletteIndex,
navigateTo, navigateTo,
commandPaletteSearch: ui.commandPaletteSearch, commandPaletteSearch: ui.commandPaletteSearch,
}); });
@@ -8,7 +8,6 @@ interface CommandPaletteHotkeysOptions {
setCommandPaletteSearch: (value: string) => void; setCommandPaletteSearch: (value: string) => void;
setSelectedPaletteIndex: (index: number) => void; setSelectedPaletteIndex: (index: number) => void;
paletteActions: PaletteAction[]; paletteActions: PaletteAction[];
selectedPaletteIndex: number;
navigateTo: (section: PaletteAction["section"], entityId?: string) => void; navigateTo: (section: PaletteAction["section"], entityId?: string) => void;
commandPaletteSearch: string; commandPaletteSearch: string;
} }
@@ -19,7 +18,6 @@ export function useCommandPaletteHotkeys({
setCommandPaletteSearch, setCommandPaletteSearch,
setSelectedPaletteIndex, setSelectedPaletteIndex,
paletteActions, paletteActions,
selectedPaletteIndex,
navigateTo, navigateTo,
commandPaletteSearch, commandPaletteSearch,
}: CommandPaletteHotkeysOptions) { }: CommandPaletteHotkeysOptions) {
@@ -43,11 +41,12 @@ export function useCommandPaletteHotkeys({
function onKey(event: KeyboardEvent) { function onKey(event: KeyboardEvent) {
if (event.key === "Escape") { setCommandPaletteOpen(false); setCommandPaletteSearch(""); setSelectedPaletteIndex(0); return; } if (event.key === "Escape") { setCommandPaletteOpen(false); setCommandPaletteSearch(""); setSelectedPaletteIndex(0); return; }
if (paletteActions.length === 0) return; if (paletteActions.length === 0) return;
if (event.key === "ArrowDown") { event.preventDefault(); setSelectedPaletteIndex((useAppStore.getState().selectedPaletteIndex + 1) % paletteActions.length); return; } const currentIndex = useAppStore.getState().selectedPaletteIndex;
if (event.key === "ArrowUp") { event.preventDefault(); setSelectedPaletteIndex((useAppStore.getState().selectedPaletteIndex - 1 + paletteActions.length) % paletteActions.length); return; } 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; if (event.key !== "Enter") return;
event.preventDefault(); event.preventDefault();
const selectedAction = paletteActions[Math.min(selectedPaletteIndex, paletteActions.length - 1)]; const selectedAction = paletteActions[Math.min(currentIndex, paletteActions.length - 1)];
if (!selectedAction) return; if (!selectedAction) return;
navigateTo(selectedAction.section, selectedAction.entityId); navigateTo(selectedAction.section, selectedAction.entityId);
setCommandPaletteOpen(false); setCommandPaletteOpen(false);
@@ -56,7 +55,7 @@ export function useCommandPaletteHotkeys({
} }
document.addEventListener("keydown", onKey); document.addEventListener("keydown", onKey);
return () => document.removeEventListener("keydown", onKey); return () => document.removeEventListener("keydown", onKey);
}, [commandPaletteOpen, navigateTo, paletteActions, selectedPaletteIndex, setCommandPaletteOpen, setCommandPaletteSearch, setSelectedPaletteIndex]); }, [commandPaletteOpen, navigateTo, paletteActions, setCommandPaletteOpen, setCommandPaletteSearch, setSelectedPaletteIndex]);
useEffect(() => { useEffect(() => {
if (!commandPaletteOpen) return; if (!commandPaletteOpen) return;