Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | 9x 57x 57x 57x 57x 57x 3x 3x 57x 1x 1x 1x 1x 57x 2x 2x 57x 2x 2x 57x 57x 57x 121x 121x 2x 119x 2x | /**
* Keyboard shortcut context for global shortcut state management
*
* @module contexts/KeyboardShortcutContext
*/
import React, { createContext, useContext, useState, useCallback, useMemo, ReactNode } from 'react';
import {
KeyboardShortcutContextValue,
ShortcutMap,
KeyboardShortcut,
} from '../types/keyboard';
import { detectPlatform } from '../utils/keyboardUtils';
import logger from '../utils/logger';
/**
* Keyboard shortcut context
*/
const KeyboardShortcutContext = createContext<KeyboardShortcutContextValue | undefined>(undefined);
/**
* Props for KeyboardShortcutProvider
*/
export interface KeyboardShortcutProviderProps {
/** Child components */
children: ReactNode;
/** Initial shortcuts to register */
initialShortcuts?: ShortcutMap;
/** Whether shortcuts are enabled by default */
defaultEnabled?: boolean;
}
/**
* Provider component for keyboard shortcut context
*
* @example
* ```tsx
* <KeyboardShortcutProvider>
* <App />
* </KeyboardShortcutProvider>
* ```
*/
export function KeyboardShortcutProvider({
children,
initialShortcuts = {},
defaultEnabled = true,
}: KeyboardShortcutProviderProps): React.ReactElement {
const [shortcuts, setShortcuts] = useState<ShortcutMap>(initialShortcuts);
const [isEnabled, setIsEnabled] = useState<boolean>(defaultEnabled);
const [showHelp, setShowHelp] = useState<boolean>(false);
// Platform detection is cached at module level, so we can simply call it
// directly without memoization (it returns the cached value instantly)
const platform = detectPlatform();
/**
* Register a new keyboard shortcut
*/
const registerShortcut = useCallback((shortcut: KeyboardShortcut): void => {
logger.debug('Registering keyboard shortcut', {
id: shortcut.id,
keys: shortcut.keys,
description: shortcut.description,
});
setShortcuts(prev => ({
...prev,
[shortcut.id]: shortcut,
}));
}, []);
/**
* Unregister a keyboard shortcut by id
*/
const unregisterShortcut = useCallback((id: string): void => {
logger.debug('Unregistering keyboard shortcut', { id });
setShortcuts(prev => {
const { [id]: removed, ...rest } = prev;
return rest;
});
}, []);
/**
* Enable or disable all shortcuts
*/
const setEnabledCallback = useCallback((enabled: boolean): void => {
logger.debug('Setting keyboard shortcuts enabled', { enabled });
setIsEnabled(enabled);
}, []);
/**
* Toggle help modal visibility
*/
const setShowHelpCallback = useCallback((show: boolean): void => {
logger.debug('Setting keyboard shortcut help visibility', { show });
setShowHelp(show);
}, []);
// Memoize context value to avoid unnecessary re-renders
const contextValue = useMemo<KeyboardShortcutContextValue>(
() => ({
shortcuts,
registerShortcut,
unregisterShortcut,
isEnabled,
setEnabled: setEnabledCallback,
platform,
showHelp,
setShowHelp: setShowHelpCallback,
}),
[
shortcuts,
registerShortcut,
unregisterShortcut,
isEnabled,
setEnabledCallback,
platform,
showHelp,
setShowHelpCallback,
]
);
return (
<KeyboardShortcutContext.Provider value={contextValue}>
{children}
</KeyboardShortcutContext.Provider>
);
}
/**
* Hook to access keyboard shortcut context
*
* @throws Error if used outside of KeyboardShortcutProvider
* @returns Keyboard shortcut context value
*
* @example
* ```tsx
* const { shortcuts, registerShortcut, showHelp, setShowHelp } = useKeyboardShortcutContext();
* ```
*/
export function useKeyboardShortcutContext(): KeyboardShortcutContextValue {
const context = useContext(KeyboardShortcutContext);
if (context === undefined) {
throw new Error(
'useKeyboardShortcutContext must be used within a KeyboardShortcutProvider'
);
}
return context;
}
/**
* Optional hook to access keyboard shortcut context (returns undefined if not in provider)
*
* @returns Keyboard shortcut context value or undefined
*
* @example
* ```tsx
* const context = useKeyboardShortcutContextOptional();
* if (context) {
* // Use context
* }
* ```
*/
export function useKeyboardShortcutContextOptional(): KeyboardShortcutContextValue | undefined {
return useContext(KeyboardShortcutContext);
}
|