Skip to content

Commit 30584c7

Browse files
authored
Merge pull request #357 from devtron-labs/fix/use-register-shortcut
fix: timeout not registered a second time unless keyup event is fired
2 parents b875768 + 9b1210e commit 30584c7

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

src/Common/Hooks/UseRegisterShortcut/UseRegisterShortcutProvider.tsx

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const UseRegisterShortcutProvider = ({
3131
}: UseRegisterShortcutProviderType) => {
3232
const disableShortcutsRef = useRef<boolean>(false)
3333
const shortcutsRef = useRef<Record<string, ShortcutType>>({})
34-
const keysDownRef = useRef<Set<string>>(new Set())
34+
const keysDownRef = useRef<Set<Uppercase<string>>>(new Set())
3535
const keyDownTimeoutRef = useRef<ReturnType<typeof setTimeout>>(-1)
3636
const ignoredTags = ignoreTags ?? IGNORE_TAGS_FALLBACK
3737

@@ -116,38 +116,37 @@ const UseRegisterShortcutProvider = ({
116116
}, [])
117117

118118
const handleKeydownEvent = useCallback((event: KeyboardEvent) => {
119-
if (keyDownTimeoutRef.current === -1) {
120-
keyDownTimeoutRef.current = setTimeout(() => {
121-
handleKeyupEvent()
122-
}, shortcutTimeout ?? DEFAULT_TIMEOUT)
123-
}
124-
125119
if (preventDefault) {
126120
event.preventDefault()
127121
}
128122

129123
if (
130124
ignoredTags.map((tag) => tag.toUpperCase()).indexOf((event.target as HTMLElement).tagName.toUpperCase()) >
131-
-1
125+
-1 ||
126+
disableShortcutsRef.current
132127
) {
133128
return
134129
}
135130

136-
if (!disableShortcutsRef.current) {
137-
keysDownRef.current.add(event.key.toUpperCase())
131+
keysDownRef.current.add(event.key.toUpperCase() as Uppercase<string>)
138132

139-
if (event.ctrlKey) {
140-
keysDownRef.current.add('CONTROL')
141-
}
142-
if (event.metaKey) {
143-
keysDownRef.current.add('META')
144-
}
145-
if (event.altKey) {
146-
keysDownRef.current.add('ALT')
147-
}
148-
if (event.shiftKey) {
149-
keysDownRef.current.add('SHIFT')
150-
}
133+
if (event.ctrlKey) {
134+
keysDownRef.current.add('CONTROL')
135+
}
136+
if (event.metaKey) {
137+
keysDownRef.current.add('META')
138+
}
139+
if (event.altKey) {
140+
keysDownRef.current.add('ALT')
141+
}
142+
if (event.shiftKey) {
143+
keysDownRef.current.add('SHIFT')
144+
}
145+
146+
if (keyDownTimeoutRef.current === -1) {
147+
keyDownTimeoutRef.current = setTimeout(() => {
148+
handleKeyupEvent()
149+
}, shortcutTimeout ?? DEFAULT_TIMEOUT)
151150
}
152151
}, [])
153152

0 commit comments

Comments
 (0)