refactor: create shortcut hook that with default config

This commit is contained in:
Sonny
2024-05-16 22:03:00 +02:00
committed by Sonny
parent 57ed2c5e94
commit 18b2eb2c5a
6 changed files with 82 additions and 62 deletions

View File

@@ -1,21 +1,14 @@
import KEYS from '#constants/keys';
import { router } from '@inertiajs/react';
import { route } from '@izzyjs/route/client';
import { ReactNode } from 'react';
import { useHotkeys } from 'react-hotkeys-hook';
import useGlobalHotkeys from '~/hooks/use_global_hotkeys';
import useSearchParam from '~/hooks/use_search_param';
import useShortcut from '~/hooks/use_shortcut';
import { appendCollectionId } from '~/lib/navigation';
export default function BackToDashboard({ children }: { children: ReactNode }) {
const collectionId = useSearchParam('collectionId');
const { globalHotkeysEnabled } = useGlobalHotkeys();
useHotkeys(
KEYS.ESCAPE_KEY,
() => {
router.visit(appendCollectionId(route('dashboard').url, collectionId));
},
{ enabled: globalHotkeysEnabled, enableOnFormTags: ['INPUT'] }
useShortcut('ESCAPE_KEY', () =>
router.visit(appendCollectionId(route('dashboard').url, collectionId))
);
return <>{children}</>;
}