nexus/ui/src/hooks/useKeyboardShortcuts.ts
Nexus Dev 0406362e04 feat(21-06): add conversation search input and Cmd+K shortcut
- Add Search/X icons and Input to ChatConversationList with 300ms debounce
- Listen for nexus:focus-chat-search event to focus search input
- Add onSearch handler to useKeyboardShortcuts (fires before input guard)
- Wire Layout Cmd+K to open chat panel and dispatch focus event
2026-04-02 15:08:50 +00:00

48 lines
1.4 KiB
TypeScript

import { useEffect } from "react";
interface ShortcutHandlers {
onNewIssue?: () => void;
onToggleSidebar?: () => void;
onTogglePanel?: () => void;
onSearch?: () => void;
}
export function useKeyboardShortcuts({ onNewIssue, onToggleSidebar, onTogglePanel, onSearch }: ShortcutHandlers) {
useEffect(() => {
function handleKeyDown(e: KeyboardEvent) {
// Cmd+K / Ctrl+K → Search (global, works even from inputs)
if (e.key === "k" && (e.metaKey || e.ctrlKey)) {
e.preventDefault();
onSearch?.();
return;
}
// Don't fire shortcuts when typing in inputs
const target = e.target as HTMLElement;
if (target.tagName === "INPUT" || target.tagName === "TEXTAREA" || target.isContentEditable) {
return;
}
// C → New Issue
if (e.key === "c" && !e.metaKey && !e.ctrlKey && !e.altKey) {
e.preventDefault();
onNewIssue?.();
}
// [ → Toggle Sidebar
if (e.key === "[" && !e.metaKey && !e.ctrlKey) {
e.preventDefault();
onToggleSidebar?.();
}
// ] → Toggle Panel
if (e.key === "]" && !e.metaKey && !e.ctrlKey) {
e.preventDefault();
onTogglePanel?.();
}
}
document.addEventListener("keydown", handleKeyDown);
return () => document.removeEventListener("keydown", handleKeyDown);
}, [onNewIssue, onToggleSidebar, onTogglePanel, onSearch]);
}