Files
TREK/client/src/components/Assistant/useAssistantHotkey.ts
T
Maurice 3c3a228737 fix(assistant): lint, types and tests for the CI round
- agent loop and decisions without await in loops: steps as recursion,
  calls and applied changes in order through one promise chain, the
  independent reads and claims in parallel; part mapping in its own helpers
- place card, header and weather strip without refs or components created
  in render; long hooks and components split; state reset during render
- trip notes only in a turn's tool session, never an undo's; map view
  reported only while the assistant can run
- place strip scrolls through the strip's own smooth scroll, no physical side
- tests follow the raised limits and the new card fields; toasts read through
  a mocked useToast instead of a window global; baselines lowered where the
  counts fell
2026-10-21 22:29:58 +02:00

35 lines
1.3 KiB
TypeScript

import { useEffect } from 'react';
import { useTranslation } from '../../i18n';
import { isApplePlatform } from '../../utils/applePlatform';
/** Whether a key press is the assistant's shortcut: ⌘K on a Mac, Ctrl+K elsewhere, either works on both. */
export function isAssistantHotkey(e: KeyboardEvent): boolean {
return (e.metaKey || e.ctrlKey) && !e.altKey && !e.shiftKey && e.key.toLowerCase() === 'k';
}
/**
* ⌘K / Ctrl+K opens or closes the assistant. A key press something else
* already took is left alone, and so is one while a dialog is open: the
* shortcut must not pull a window up behind a question being asked.
*/
export function useAssistantHotkey(enabled: boolean, onToggle: () => void): void {
useEffect(() => {
if (!enabled) return;
const onKey = (e: KeyboardEvent) => {
if (e.defaultPrevented || !isAssistantHotkey(e)) return;
if (document.querySelector('.trek-modal-backdrop')) return;
e.preventDefault();
onToggle();
};
window.addEventListener('keydown', onKey);
return () => window.removeEventListener('keydown', onKey);
}, [enabled, onToggle]);
}
/** The shortcut as the keys a tooltip shows. */
export function useAssistantShortcutKeys(): string[] {
const { t } = useTranslation();
return isApplePlatform() ? ['⌘', 'K'] : [t('assistant.launcher.ctrlKey'), 'K'];
}