Files
twenty_crm/front/src/modules/hotkeys/hooks/useDirectHotkeys.ts
Félix Malfait 827d6390e4 Refactoring shortcuts and commandbar (#412)
* Begin refactoring shortcuts and commandbar

* Continue refacto hotkeys

* Remove debug logs

* Add new story

* Simplify hotkeys

* Simplify hotkeys

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
2023-06-25 22:25:31 -07:00

31 lines
760 B
TypeScript

import { useHotkeys } from 'react-hotkeys-hook';
import {
Hotkey,
HotkeyCallback,
OptionsOrDependencyArray,
} from 'react-hotkeys-hook/dist/types';
import { useRecoilState } from 'recoil';
import { pendingHotkeyState } from '../states/pendingHotkeysState';
export function useDirectHotkeys(
keys: string,
callback: HotkeyCallback,
dependencies?: OptionsOrDependencyArray,
) {
const [pendingHotkey, setPendingHotkey] = useRecoilState(pendingHotkeyState);
const callbackIfDirectKey = function (
keyboardEvent: KeyboardEvent,
hotkeysEvent: Hotkey,
) {
if (!pendingHotkey) {
callback(keyboardEvent, hotkeysEvent);
return;
}
setPendingHotkey(null);
};
useHotkeys(keys, callbackIfDirectKey, dependencies);
}