Raw
1 import { default as debounceFn } from "lodash.debounce"
2 import { useCallback, useContext, useEffect, useState } from "react"
3 import { useTranslation } from "react-i18next"
4 import { useNavigate } from "react-router-dom"
5
6 import { useLogout } from "../../hooks/api/auth"
7 import { queryClient } from "../../lib/query-client"
8 import {
9 getSearchEntityShortcuts,
10 resolveSearchEntityShortcutLabel,
11 } from "../../lib/search/search-entities"
12 import { KeybindContext } from "./keybind-context"
13 import { Shortcut } from "./types"
14 import { findShortcut } from "./utils"
15
16 export const useKeybind = () => {
17 const context = useContext(KeybindContext)
18
19 if (!context) {
20 throw new Error("useKeybind must be used within a KeybindProvider")
21 }
22
23 return context
24 }
25
26 export const useRegisterShortcut = () => {}
27
28 export const useShortcuts = ({
29 shortcuts = [],
30 debounce,
31 }: {
32 shortcuts?: Shortcut[]
33 debounce: number
34 }) => {
35 const [keys, setKeys] = useState<string[]>([])
36 const navigate = useNavigate()
37
38 // eslint-disable-next-line react-hooks/exhaustive-deps
39 const removeKeys = useCallback(
40 debounceFn(() => setKeys([]), debounce),
41 []
42 )
43
44 // eslint-disable-next-line react-hooks/exhaustive-deps
45 const invokeShortcut = useCallback(
46 debounceFn((shortcut: Shortcut | null) => {
47 if (shortcut && shortcut.callback) {
48 shortcut.callback()
49 setKeys([])
50
51 return
52 }
53
54 if (shortcut && shortcut.to) {
55 navigate(shortcut.to)
56 setKeys([])
57
58 return
59 }
60 }, debounce / 2),
61 []
62 )
63
64 useEffect(() => {
65 if (keys.length > 0 && shortcuts.length > 0) {
66 const shortcut = findShortcut(shortcuts, keys)
67 invokeShortcut(shortcut)
68 }
69
70 return () => invokeShortcut.cancel()
71 }, [keys, shortcuts, invokeShortcut])
72
73 useEffect(() => {
74 const listener = (event: KeyboardEvent) => {
75 const target = event.target as HTMLElement
76
77 /**
78 * Ignore key events from input, textarea and contenteditable elements
79 */
80 if (
81 target.tagName === "INPUT" ||
82 target.tagName === "TEXTAREA" ||
83 target.contentEditable === "true"
84 ) {
85 removeKeys()
86 return
87 }
88
89 setKeys((oldKeys) => [...oldKeys, event.key])
90 removeKeys()
91 }
92
93 window.addEventListener("keydown", listener)
94
95 return () => {
96 window.removeEventListener("keydown", listener)
97 }
98 }, [removeKeys])
99 }
100
101 export const useGlobalShortcuts = () => {
102 const { t } = useTranslation()
103 const navigate = useNavigate()
104
105 const { mutateAsync } = useLogout()
106
107 const handleLogout = async () => {
108 await mutateAsync(undefined, {
109 onSuccess: () => {
110 queryClient.clear()
111 navigate("/login")
112 },
113 })
114 }
115
116 const globalShortcuts: Shortcut[] = [
117 // "Jump to" entries come from the search entity registry, so apps and
118 // plugins customize them through their `search-entities.tsx` definitions.
119 ...getSearchEntityShortcuts().map(
120 ({ entity, shortcut }): Shortcut => ({
121 keys: shortcut.keys,
122 label: resolveSearchEntityShortcutLabel(entity, t),
123 type: shortcut.type ?? "pageShortcut",
124 to: shortcut.to,
125 })
126 ),
127 // The admin shell's own destinations, which belong to no entity.
128 {
129 keys: {
130 Mac: ["G", ","],
131 },
132 label: t("app.keyboardShortcuts.settings.goToSettings"),
133 type: "settingShortcut",
134 to: "/settings",
135 },
136 {
137 keys: {
138 Mac: ["G", ",", "S"],
139 },
140 label: t("app.keyboardShortcuts.settings.goToStore"),
141 type: "settingShortcut",
142 to: "/settings/store",
143 },
144 {
145 keys: {
146 Mac: ["G", ",", "W"],
147 },
148 label: t("app.keyboardShortcuts.settings.goToWorkflows"),
149 type: "settingShortcut",
150 to: "/settings/workflows",
151 },
152 {
153 keys: {
154 Mac: ["G", ",", "M"],
155 },
156 label: t("app.keyboardShortcuts.settings.goToProfile"),
157 type: "settingShortcut",
158 to: "/settings/profile",
159 },
160 // Commands that need app hooks stay built in.
161 {
162 keys: {
163 Mac: ["B", "Y", "E"],
164 },
165 label: t("actions.logout"),
166 type: "commandShortcut",
167 callback: () => handleLogout(),
168 },
169 ]
170
171 return globalShortcuts
172 }