This commit is contained in:
140
apps/web/src/hooks/useKeyboardShortcuts.ts
Normal file
140
apps/web/src/hooks/useKeyboardShortcuts.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
import type { AppId } from '@/lib/dock-tiers';
|
||||
|
||||
interface UseKeyboardShortcutsOptions {
|
||||
onOpenApp: (id: AppId) => void;
|
||||
onToggleGlobalSearch: () => void;
|
||||
onTogglePersonaSwitcher: () => void;
|
||||
onToggleWorkspaceSwitcher: () => void;
|
||||
onToggleKeyboardHelp: () => void;
|
||||
/**
|
||||
* Phase A.3 — originally "new chat window on the active workspace"; since
|
||||
* the P1a AppShell conversion (§4.2) this navigates to the active
|
||||
* workspace's chat tab instead (Ctrl+W / Ctrl+Shift+M window handlers
|
||||
* retired with the window manager, plan §3.1).
|
||||
*/
|
||||
onNewChatWindow?: () => void;
|
||||
}
|
||||
|
||||
const APP_SHORTCUTS: Record<string, AppId> = {
|
||||
'0': 'dashboard',
|
||||
'1': 'chat',
|
||||
'2': 'agents',
|
||||
'3': 'files',
|
||||
'4': 'cockpit',
|
||||
'5': 'memory',
|
||||
'6': 'events',
|
||||
'7': 'settings',
|
||||
'8': 'capabilities',
|
||||
'9': 'waggle-dance',
|
||||
};
|
||||
|
||||
/** Shortcuts that work even when an input/textarea is focused */
|
||||
function isGlobalShortcut(e: KeyboardEvent): boolean {
|
||||
const ctrl = e.ctrlKey || e.metaKey;
|
||||
return (
|
||||
(ctrl && e.key === 'k')
|
||||
|| (ctrl && e.shiftKey && (e.key === 'N' || e.key === 'n'))
|
||||
// Pillar 2.7: "open last workspace" is a from-anywhere power path, so it
|
||||
// survives an input focus (parity with new-chat above).
|
||||
|| (ctrl && e.shiftKey && (e.key === 'O' || e.key === 'o'))
|
||||
|| e.key === 'Escape'
|
||||
);
|
||||
}
|
||||
|
||||
function isInputFocused(): boolean {
|
||||
const active = document.activeElement;
|
||||
if (!active) return false;
|
||||
const tag = active.tagName.toLowerCase();
|
||||
return tag === 'input' || tag === 'textarea' || (active as HTMLElement).isContentEditable;
|
||||
}
|
||||
|
||||
export const useKeyboardShortcuts = (options: UseKeyboardShortcutsOptions) => {
|
||||
// Store callbacks in refs so the event listener never re-registers
|
||||
const optsRef = useRef(options);
|
||||
optsRef.current = options;
|
||||
|
||||
useEffect(() => {
|
||||
const handler = (e: KeyboardEvent) => {
|
||||
const ctrl = e.ctrlKey || e.metaKey;
|
||||
const opts = optsRef.current;
|
||||
|
||||
// Skip non-global shortcuts when typing in an input
|
||||
if (isInputFocused() && !isGlobalShortcut(e)) return;
|
||||
|
||||
// Ctrl+Shift+R: Open the Room (Phase A.3 — sub-agent visibility canvas)
|
||||
if (ctrl && e.shiftKey && (e.key === 'R' || e.key === 'r')) {
|
||||
e.preventDefault();
|
||||
opts.onOpenApp('room');
|
||||
return;
|
||||
}
|
||||
|
||||
// Ctrl+Shift+N: Open chat on the active workspace.
|
||||
// Matches the cornerstone's Phase A.3 acceptance test flow:
|
||||
// "Cmd+Shift+N → pick Researcher → ..."
|
||||
if (ctrl && e.shiftKey && (e.key === 'N' || e.key === 'n')) {
|
||||
e.preventDefault();
|
||||
opts.onNewChatWindow?.();
|
||||
return;
|
||||
}
|
||||
|
||||
// Ctrl+Shift+O: Open the last / active workspace's desktop from anywhere
|
||||
// (Pillar 2.7 ≤2-keystroke power path). onOpenApp('workspace-desktop')
|
||||
// resolves through routeFor, which lands the persisted active workspace
|
||||
// (or Home when nothing is selected — Home IS the workspace selector).
|
||||
if (ctrl && e.shiftKey && (e.key === 'O' || e.key === 'o')) {
|
||||
e.preventDefault();
|
||||
opts.onOpenApp('workspace-desktop');
|
||||
return;
|
||||
}
|
||||
|
||||
// Ctrl+Shift+0-9: Open apps
|
||||
if (ctrl && e.shiftKey && APP_SHORTCUTS[e.key]) {
|
||||
e.preventDefault();
|
||||
opts.onOpenApp(APP_SHORTCUTS[e.key]);
|
||||
return;
|
||||
}
|
||||
|
||||
// Ctrl+K: Global search
|
||||
if (ctrl && e.key === 'k') {
|
||||
e.preventDefault();
|
||||
opts.onToggleGlobalSearch();
|
||||
return;
|
||||
}
|
||||
|
||||
// Ctrl+Shift+P: Persona switcher
|
||||
if (ctrl && e.shiftKey && e.key === 'P') {
|
||||
e.preventDefault();
|
||||
opts.onTogglePersonaSwitcher();
|
||||
return;
|
||||
}
|
||||
|
||||
// Ctrl+Tab: Workspace switcher
|
||||
if (ctrl && e.key === 'Tab') {
|
||||
e.preventDefault();
|
||||
opts.onToggleWorkspaceSwitcher();
|
||||
return;
|
||||
}
|
||||
|
||||
// Ctrl+/ or Ctrl+?: Keyboard help. Browsers/OS layouts disagree on the
|
||||
// reported key for the shifted slash shortcut, so accept both forms.
|
||||
if (ctrl && (e.key === '?' || e.key === '/')) {
|
||||
e.preventDefault();
|
||||
opts.onToggleKeyboardHelp();
|
||||
return;
|
||||
}
|
||||
|
||||
// `?` (no modifier): the conventional "press ? for help" discoverability
|
||||
// path (Pillar 2.7 cheat-sheet). The input-focus early return above keeps
|
||||
// it from ever hijacking a literal "?" typed into a field.
|
||||
if (!ctrl && e.key === '?') {
|
||||
e.preventDefault();
|
||||
opts.onToggleKeyboardHelp();
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener('keydown', handler);
|
||||
return () => window.removeEventListener('keydown', handler);
|
||||
}, []); // Empty deps — handler is stable via ref
|
||||
};
|
||||
Reference in New Issue
Block a user