Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(hotkeys): ignore keyless key events and scope duplicate-token warning (#8205)
teo1ccf8961a61a
diff --git a/apps/web/src/lib/core/hotkey/hotkeys.test.ts b/apps/web/src/lib/core/hotkey/hotkeys.test.tsindex e3b37902a9..ca29dc2b89 100644--- a/apps/web/src/lib/core/hotkey/hotkeys.test.ts+++ b/apps/web/src/lib/core/hotkey/hotkeys.test.ts@@ -1,6 +1,24 @@-import { describe, expect, test, vi } from 'vitest';-import { createHotkeyGroup } from './hotkeys';+import { Telemetry } from '@macro-inc/observability';+import { createRoot } from 'solid-js';+import { afterEach, describe, expect, test, vi } from 'vitest';+import { createHotkeyGroup, registerHotkey, useHotKeyRoot } from './hotkeys';+import { hotkeyTokenMap, setActiveScope, setPressedKeys } from './state';+import type { HotkeyToken } from './tokens';import type { RegisterHotkeyReturn } from './types';+import { registerScope, removeScope } from './utils';++vi.mock('@core/mobile/isTouchDevice', () => ({ isTouchDevice: () => false }));++const disposers: (() => void)[] = [];+const scopes: string[] = [];++afterEach(() => {+ for (const dispose of disposers.splice(0)) dispose();+ for (const scopeId of scopes.splice(0)) removeScope(scopeId);+ setActiveScope('global');+ setPressedKeys(new Set<string>());+ vi.restoreAllMocks();+});const makeRegistration = (): RegisterHotkeyReturn => {const reg: RegisterHotkeyReturn = {@@ -92,3 +110,101 @@ describe('createHotkeyGroup', () => {expect(reg.dispose).toHaveBeenCalledTimes(1);});});++// Chrome autofill dispatches trusted keydown/keyup events that carry no `key`.+describe('key events without a key', () => {+ test('are ignored instead of throwing or re-running a held hotkey', () => {+ const errors: unknown[] = [];+ const onError = (event: ErrorEvent) => {+ errors.push(event.error);+ event.preventDefault();+ };+ window.addEventListener('error', onError);+ const keyDownHandler = vi.fn(() => false);+ createRoot((dispose) => {+ disposers.push(dispose);+ useHotKeyRoot();+ registerHotkey({+ scopeId: 'global',+ hotkey: 'j',+ description: 'Down',+ keyDownHandler,+ });+ });++ document.dispatchEvent(new KeyboardEvent('keydown', { key: 'j' }));+ document.dispatchEvent(new Event('keydown'));+ document.dispatchEvent(new Event('keyup'));+ window.removeEventListener('error', onError);++ expect(errors).toEqual([]);+ expect(keyDownHandler).toHaveBeenCalledTimes(1);+ });+});++describe('registering a token that is already registered', () => {+ const token = 'hotkeys-test:token' as HotkeyToken;++ function makeScope(scopeId: string) {+ registerScope({ parentScopeId: 'global', scopeId, type: 'dom' });+ scopes.push(scopeId);+ }++ function mount(scopeId: string, hotkey: 'j' | ['arrowdown', 'j']) {+ return createRoot((dispose) => {+ registerHotkey({+ scopeId,+ hotkeyToken: token,+ hotkey,+ description: 'Down',+ keyDownHandler: () => true,+ });+ return dispose;+ });+ }++ test('a remount in the same scope keeps one registration and does not warn', () => {+ const warn = vi.spyOn(Telemetry, 'warn');+ makeScope('hotkeys-test-remount');++ mount('hotkeys-test-remount', 'j')();+ disposers.push(mount('hotkeys-test-remount', 'j'));++ expect(hotkeyTokenMap().get(token)).toHaveLength(1);+ expect(warn).not.toHaveBeenCalled();+ });++ test('a remount that registers before the old instance cleans up replaces it', () => {+ const warn = vi.spyOn(Telemetry, 'warn');+ makeScope('hotkeys-test-overlap');++ const disposeOld = mount('hotkeys-test-overlap', 'j');+ disposers.push(mount('hotkeys-test-overlap', 'j'));+ disposeOld();++ expect(hotkeyTokenMap().get(token)).toHaveLength(1);+ expect(warn).not.toHaveBeenCalled();+ });++ test('the same token with other keys in another scope does not warn', () => {+ const warn = vi.spyOn(Telemetry, 'warn');+ makeScope('hotkeys-test-list');+ makeScope('hotkeys-test-detail');++ disposers.push(mount('hotkeys-test-list', ['arrowdown', 'j']));+ disposers.push(mount('hotkeys-test-detail', 'j'));++ expect(hotkeyTokenMap().get(token)).toHaveLength(2);+ expect(warn).not.toHaveBeenCalled();+ });++ test('the same token with other keys in the same scope warns', () => {+ const warn = vi.spyOn(Telemetry, 'warn');+ makeScope('hotkeys-test-conflict');++ disposers.push(mount('hotkeys-test-conflict', ['arrowdown', 'j']));+ disposers.push(mount('hotkeys-test-conflict', 'j'));++ expect(warn).toHaveBeenCalledOnce();+ });+});diff --git a/apps/web/src/lib/core/hotkey/hotkeys.ts b/apps/web/src/lib/core/hotkey/hotkeys.tsindex 0e69f03458..efffe11dce 100644--- a/apps/web/src/lib/core/hotkey/hotkeys.ts+++ b/apps/web/src/lib/core/hotkey/hotkeys.ts@@ -4,7 +4,7 @@ import { isTouchDevice } from '@core/mobile/isTouchDevice';import { isEditableInput } from '@core/util/isEditableInput';import { Telemetry } from '@macro-inc/observability';-import { getOwner, onCleanup, onMount, untrack } from 'solid-js';+import { getOwner, onCleanup, onMount } from 'solid-js';import {EVENT_MODIFIER_KEYS,EVENT_MODIFIER_NAME_MAP,@@ -18,7 +18,6 @@ import {findLastLiveActiveDOMScope,hotkeyScopeTree,hotkeysAwaitingKeyUp,- hotkeyTokenMap,pressedKeys,setActiveScope,setHotkeyTokenMap,@@ -173,9 +172,11 @@ export function registerHotkey(// Convert single hotkey to array for consistent handlingconst hotkeys = hotkey && !Array.isArray(hotkey) ? [hotkey] : hotkey;- // Check for existing duplicate hotkeyToken for non-identical command+ // Tokens are shared across scopes on purpose (a list and an open entity+ // both bind entity.step.end), and getActiveCommandByToken resolves them by+ // the active branch. Only a same-token command in this scope is a conflict.const existingCommand = hotkeyToken- ? untrack(() => hotkeyTokenMap().get(hotkeyToken)?.at(0))+ ? scopeNode.commands.find((c) => c.hotkeyToken === hotkeyToken): undefined;if (existingCommand && hotkeys && existingCommand.hotkeys) {// Yes, you should be able to register multiple hotkeys with the same token. But if you do this, they should be "the same" hotkey.@@ -604,9 +605,12 @@ export function useHotKeyRoot() {// keydown without its keyup leaves a phantom key stuck in pressedKeys that// makes every subsequent plain-key hotkey resolve to e.g. 'arrowdown+j'// until a modifier keyup clears the set. Tests simulate keypresses with- // dispatched (untrusted) events, so they stay exempt.+ // dispatched (untrusted) events, so they stay exempt. Chrome autofill+ // dispatches trusted keydown/keyup events with no `key`; they are not key+ // presses either.const shouldIgnoreKeyEvent = (e: KeyboardEvent) =>- !e.isTrusted && import.meta.env.MODE !== 'test';+ typeof e.key !== 'string' ||+ (!e.isTrusted && import.meta.env.MODE !== 'test');const handleKeyDown = (e: KeyboardEvent) => {if (shouldIgnoreKeyEvent(e)) return;