♻️(frontend) handle proper shortcut object

Created a type to properly manipulate any data related to a
shortcut. Make the code more concise.
This commit is contained in:
lebaudantoine
2024-09-27 12:57:53 +02:00
committed by aleb_the_flash
parent 1faae96ccd
commit 15d43b8d5e
4 changed files with 30 additions and 25 deletions

View File

@@ -23,6 +23,7 @@ import {
formatShortcutKey, formatShortcutKey,
appendShortcutLabel, appendShortcutLabel,
} from '@/features/shortcuts/utils' } from '@/features/shortcuts/utils'
import { Shortcut } from '@/features/shortcuts/types'
export type ToggleSource = Exclude< export type ToggleSource = Exclude<
Track.Source, Track.Source,
@@ -35,7 +36,7 @@ type SelectToggleDeviceConfig = {
kind: MediaDeviceKind kind: MediaDeviceKind
iconOn: RemixiconComponentType iconOn: RemixiconComponentType
iconOff: RemixiconComponentType iconOff: RemixiconComponentType
shortcutKey?: string shortcut?: Shortcut
} }
type SelectToggleDeviceConfigMap = { type SelectToggleDeviceConfigMap = {
@@ -47,20 +48,25 @@ const selectToggleDeviceConfig: SelectToggleDeviceConfigMap = {
kind: 'audioinput', kind: 'audioinput',
iconOn: RiMicLine, iconOn: RiMicLine,
iconOff: RiMicOffLine, iconOff: RiMicOffLine,
shortcutKey: 'd', shortcut: {
key: 'd',
ctrlKey: true,
},
}, },
[Track.Source.Camera]: { [Track.Source.Camera]: {
kind: 'videoinput', kind: 'videoinput',
iconOn: RiVideoOnLine, iconOn: RiVideoOnLine,
iconOff: RiVideoOffLine, iconOff: RiVideoOffLine,
shortcutKey: 'e', shortcut: {
key: 'e',
ctrlKey: true,
},
}, },
} }
type SelectToggleDeviceProps<T extends ToggleSource> = type SelectToggleDeviceProps<T extends ToggleSource> =
UseTrackToggleProps<T> & { UseTrackToggleProps<T> & {
onActiveDeviceChange: (deviceId: string) => void onActiveDeviceChange: (deviceId: string) => void
shortcutKey?: string
source: SelectToggleSource source: SelectToggleSource
} }
@@ -85,21 +91,19 @@ export const SelectToggleDevice = <T extends ToggleSource>({
const label = t(enabled ? 'disable' : 'enable', { const label = t(enabled ? 'disable' : 'enable', {
keyPrefix: `join.${kind}`, keyPrefix: `join.${kind}`,
}) })
return config.shortcutKey return config.shortcut ? appendShortcutLabel(label, config.shortcut) : label
? appendShortcutLabel(label, config.shortcutKey, true) }, [enabled, kind, config.shortcut, t])
: label
}, [enabled, kind, config.shortcutKey, t])
const selectLabel = t('choose', { keyPrefix: `join.${kind}` }) const selectLabel = t('choose', { keyPrefix: `join.${kind}` })
const Icon = enabled ? iconOn : iconOff const Icon = enabled ? iconOn : iconOff
useEffect(() => { useEffect(() => {
if (!config.shortcutKey) return if (!config.shortcut) return
keyboardShortcutsStore.shortcuts.set( keyboardShortcutsStore.shortcuts.set(
formatShortcutKey(config.shortcutKey, true), formatShortcutKey(config.shortcut),
() => toggle() () => toggle()
) )
}, [toggle, config.shortcutKey]) }, [toggle, config.shortcut])
return ( return (
<HStack gap={0}> <HStack gap={0}>

View File

@@ -0,0 +1,4 @@
export type Shortcut = {
key: string
ctrlKey?: boolean
}

View File

@@ -12,10 +12,10 @@ export const useKeyboardShortcuts = () => {
// Issues might occur. First draft. // Issues might occur. First draft.
const onKeyDown = (e: KeyboardEvent) => { const onKeyDown = (e: KeyboardEvent) => {
const { key, metaKey, ctrlKey } = e const { key, metaKey, ctrlKey } = e
const shortcutKey = formatShortcutKey( const shortcutKey = formatShortcutKey({
key, key,
ctrlKey || (isMacintosh() && metaKey) ctrlKey: ctrlKey || (isMacintosh() && metaKey),
) })
const shortcut = shortcutsSnap.shortcuts.get(shortcutKey) const shortcut = shortcutsSnap.shortcuts.get(shortcutKey)
if (!shortcut) return if (!shortcut) return
e.preventDefault() e.preventDefault()

View File

@@ -1,20 +1,17 @@
import { isMacintosh } from '@/utils/livekit' import { isMacintosh } from '@/utils/livekit'
import { Shortcut } from '@/features/shortcuts/types'
export const CTRL = 'ctrl' export const CTRL = 'ctrl'
export const formatShortcutKey = (key: string, ctrlKey?: boolean) => { export const formatShortcutKey = (shortcut: Shortcut) => {
if (ctrlKey) return `${CTRL}+${key.toUpperCase()}` if (shortcut.ctrlKey) return `${CTRL}+${shortcut.key.toUpperCase()}`
return key.toUpperCase() return shortcut.key.toUpperCase()
} }
export const appendShortcutLabel = ( export const appendShortcutLabel = (label: string, shortcut: Shortcut) => {
label: string, if (!shortcut.key) return
key: string, let formattedKeyLabel = shortcut.key.toLowerCase()
ctrlKey?: boolean if (shortcut.ctrlKey) {
) => {
if (!key) return
let formattedKeyLabel = key.toLowerCase()
if (ctrlKey) {
formattedKeyLabel = `${isMacintosh() ? '⌘' : 'Ctrl'}+${formattedKeyLabel}` formattedKeyLabel = `${isMacintosh() ? '⌘' : 'Ctrl'}+${formattedKeyLabel}`
} }
return `${label} (${formattedKeyLabel})` return `${label} (${formattedKeyLabel})`