feat(web): add ? cheatsheet and / focus-search hotkeys

- New ShortcutsHelp modal enumerates global, nav G-chord and palette
  bindings; openable via ? (Shift+/) or the command palette.
- / dispatches a global decnet:focus-search event; Attackers, Bounty
  and LiveLogs listen and focus their in-page search inputs (pages
  without a local search are skipped per plan).
- Respects the existing editable-element guard and Alt+K palette
  toggle; no rebinds to prior shortcuts.
This commit is contained in:
2026-04-22 17:25:32 -04:00
parent ecb813ad38
commit 4d1e6c0838
9 changed files with 196 additions and 7 deletions

View File

@@ -0,0 +1,18 @@
import { useEffect, RefObject } from 'react';
/**
* Focus the given input when the global `decnet:focus-search` event fires
* (dispatched by the `/` hotkey in useGlobalHotkeys).
*/
export function useFocusSearch(ref: RefObject<HTMLInputElement | null>): void {
useEffect(() => {
const handler = () => {
const el = ref.current;
if (!el) return;
el.focus();
try { el.select(); } catch { /* ignore */ }
};
window.addEventListener('decnet:focus-search', handler);
return () => window.removeEventListener('decnet:focus-search', handler);
}, [ref]);
}

View File

@@ -4,6 +4,8 @@ import { useNavigate } from 'react-router-dom';
interface Options {
cmdOpen: boolean;
setCmdOpen: (v: boolean | ((prev: boolean) => boolean)) => void;
helpOpen: boolean;
setHelpOpen: (v: boolean | ((prev: boolean) => boolean)) => void;
}
const G_NAV: Record<string, string> = {
@@ -27,7 +29,7 @@ function isEditable(el: EventTarget | null): boolean {
return tag === 'INPUT' || tag === 'TEXTAREA' || el.isContentEditable;
}
export function useGlobalHotkeys({ cmdOpen, setCmdOpen }: Options): void {
export function useGlobalHotkeys({ cmdOpen, setCmdOpen, helpOpen, setHelpOpen }: Options): void {
const navigate = useNavigate();
const pendingG = useRef(false);
const gTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
@@ -52,10 +54,26 @@ export function useGlobalHotkeys({ cmdOpen, setCmdOpen }: Options): void {
return;
}
if (cmdOpen) return;
if (cmdOpen || helpOpen) return;
if (isEditable(e.target)) return;
if (e.metaKey || e.ctrlKey || e.altKey) return;
// `?` (Shift+/) — open shortcuts cheatsheet
if (e.key === '?') {
e.preventDefault();
setHelpOpen(true);
clearG();
return;
}
// `/` — focus page search (page listens for the event)
if (e.key === '/') {
e.preventDefault();
window.dispatchEvent(new CustomEvent('decnet:focus-search'));
clearG();
return;
}
const k = e.key.toLowerCase();
if (pendingG.current && G_NAV[k]) {
@@ -77,5 +95,5 @@ export function useGlobalHotkeys({ cmdOpen, setCmdOpen }: Options): void {
window.removeEventListener('keydown', onKey);
if (gTimer.current) clearTimeout(gTimer.current);
};
}, [cmdOpen, setCmdOpen, navigate]);
}, [cmdOpen, setCmdOpen, helpOpen, setHelpOpen, navigate]);
}