🐛(frontend) disable device shortcuts when permissions not granted
Fix bug where device toggling shortcuts remained active despite lacking permissions, by disabling device-related shortcuts until permissions are granted. Prevents confusing user experience where shortcuts appear to work but have no effect due to missing media permissions.
This commit is contained in:
committed by
aleb_the_flash
parent
5f32a9e6a3
commit
e88aeedbf1
@@ -80,11 +80,16 @@ export const ToggleDevice = <T extends ToggleSource>({
|
|||||||
const cannotUseDevice = useCannotUseDevice(kind)
|
const cannotUseDevice = useCannotUseDevice(kind)
|
||||||
const deviceShortcut = useDeviceShortcut(kind)
|
const deviceShortcut = useDeviceShortcut(kind)
|
||||||
|
|
||||||
useRegisterKeyboardShortcut({ shortcut: deviceShortcut, handler: toggle })
|
useRegisterKeyboardShortcut({
|
||||||
|
shortcut: deviceShortcut,
|
||||||
|
handler: toggle,
|
||||||
|
isDisabled: cannotUseDevice,
|
||||||
|
})
|
||||||
useLongPress({
|
useLongPress({
|
||||||
keyCode: kind === 'audioinput' ? 'Space' : undefined,
|
keyCode: kind === 'audioinput' ? 'Space' : undefined,
|
||||||
onKeyDown,
|
onKeyDown,
|
||||||
onKeyUp,
|
onKeyUp,
|
||||||
|
isDisabled: cannotUseDevice,
|
||||||
})
|
})
|
||||||
|
|
||||||
const toggleLabel = useMemo(() => {
|
const toggleLabel = useMemo(() => {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ export type useLongPressProps = {
|
|||||||
onKeyDown: () => void
|
onKeyDown: () => void
|
||||||
onKeyUp: () => void
|
onKeyUp: () => void
|
||||||
longPressThreshold?: number
|
longPressThreshold?: number
|
||||||
|
isDisabled?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useLongPress = ({
|
export const useLongPress = ({
|
||||||
@@ -12,10 +13,18 @@ export const useLongPress = ({
|
|||||||
onKeyDown,
|
onKeyDown,
|
||||||
onKeyUp,
|
onKeyUp,
|
||||||
longPressThreshold = 300,
|
longPressThreshold = 300,
|
||||||
|
isDisabled = false,
|
||||||
}: useLongPressProps) => {
|
}: useLongPressProps) => {
|
||||||
const timeoutIdRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
const timeoutIdRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (isDisabled) {
|
||||||
|
if (timeoutIdRef.current) {
|
||||||
|
clearTimeout(timeoutIdRef.current)
|
||||||
|
timeoutIdRef.current = null
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
const handleKeyDown = (event: KeyboardEvent) => {
|
const handleKeyDown = (event: KeyboardEvent) => {
|
||||||
if (event.code != keyCode || timeoutIdRef.current) return
|
if (event.code != keyCode || timeoutIdRef.current) return
|
||||||
timeoutIdRef.current = setTimeout(() => {
|
timeoutIdRef.current = setTimeout(() => {
|
||||||
@@ -38,8 +47,12 @@ export const useLongPress = ({
|
|||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener('keydown', handleKeyDown)
|
window.removeEventListener('keydown', handleKeyDown)
|
||||||
window.removeEventListener('keyup', handleKeyUp)
|
window.removeEventListener('keyup', handleKeyUp)
|
||||||
|
if (timeoutIdRef.current) {
|
||||||
|
clearTimeout(timeoutIdRef.current)
|
||||||
|
timeoutIdRef.current = null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, [keyCode, onKeyDown, onKeyUp, longPressThreshold])
|
}, [keyCode, onKeyDown, onKeyUp, longPressThreshold, isDisabled])
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,14 +6,21 @@ import { Shortcut } from '@/features/shortcuts/types'
|
|||||||
export type useRegisterKeyboardShortcutProps = {
|
export type useRegisterKeyboardShortcutProps = {
|
||||||
shortcut?: Shortcut
|
shortcut?: Shortcut
|
||||||
handler: () => void
|
handler: () => void
|
||||||
|
isDisabled?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useRegisterKeyboardShortcut = ({
|
export const useRegisterKeyboardShortcut = ({
|
||||||
shortcut,
|
shortcut,
|
||||||
handler,
|
handler,
|
||||||
|
isDisabled = false,
|
||||||
}: useRegisterKeyboardShortcutProps) => {
|
}: useRegisterKeyboardShortcutProps) => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!shortcut) return
|
if (!shortcut) return
|
||||||
keyboardShortcutsStore.shortcuts.set(formatShortcutKey(shortcut), handler)
|
const formattedKey = formatShortcutKey(shortcut)
|
||||||
}, [handler, shortcut])
|
if (isDisabled) {
|
||||||
|
keyboardShortcutsStore.shortcuts.delete(formattedKey)
|
||||||
|
} else {
|
||||||
|
keyboardShortcutsStore.shortcuts.set(formattedKey, handler)
|
||||||
|
}
|
||||||
|
}, [handler, shortcut, isDisabled])
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user