-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
HotKeys.tsx
60 lines (54 loc) · 2.33 KB
/
HotKeys.tsx
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
import { useHotkeys } from 'react-hotkeys-hook';
import Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
import DialogContent from '@mui/material/DialogContent';
import DialogTitle from '@mui/material/DialogTitle';
import Typography from '@mui/material/Typography';
import { FlexBox } from '@/components/styled';
import useHotKeysDialog from '@/store/hotkeys';
import useSidebar from '@/store/sidebar';
import useTheme from '@/store/theme';
function HotKeys() {
const [, themeActions] = useTheme();
const [, sidebarActions] = useSidebar();
const [isHotKeysDialogOpen, hotKeysDialogActions] = useHotKeysDialog();
// I would love to define all hotkeys in the config and loop it here and avoid this repetitive code.
// But the `react-hotkeys-hook` library, which we use to handle hotkeys provides only hook (`useHotkeys`).
// And as you know we can't use hooks inside loops (read "Rules of Hooks" - https://reactjs.org/docs/hooks-rules.html).
// There is always a workaround, but sometimes it's better to avoid premature and unnecessary optimizations :)
useHotkeys('alt+s', sidebarActions.toggle);
useHotkeys('alt+t', themeActions.toggle);
useHotkeys('alt+k', hotKeysDialogActions.toggle);
return (
<Dialog
fullWidth
maxWidth="xs"
onClose={hotKeysDialogActions.close}
open={isHotKeysDialogOpen}
data-pw="hotkeys-dialog"
>
<DialogTitle>Hot Keys</DialogTitle>
<DialogContent>
<FlexBox alignItems="center" height={50} justifyContent="space-between">
<Typography>Toggle Theme</Typography>
<Button color="warning" variant="outlined" onClick={themeActions.toggle}>
alt + t
</Button>
</FlexBox>
<FlexBox alignItems="center" height={50} justifyContent="space-between">
<Typography>Toggle Sidebar</Typography>
<Button color="warning" variant="outlined" onClick={sidebarActions.toggle}>
alt + s
</Button>
</FlexBox>
<FlexBox alignItems="center" height={50} justifyContent="space-between">
<Typography>Toggle Hot Keys' Dialog</Typography>
<Button color="warning" variant="outlined" onClick={hotKeysDialogActions.toggle}>
alt + k
</Button>
</FlexBox>
</DialogContent>
</Dialog>
);
}
export default HotKeys;