♻️(frontend) register shortcut using a hook
Encapsulate the logic for shortcuts registering in a proper hook. It makes the code reusable and easier to read.
This commit is contained in:
committed by
aleb_the_flash
parent
15d43b8d5e
commit
ebb8c14eeb
@@ -16,14 +16,11 @@ import {
|
|||||||
} from '@remixicon/react'
|
} from '@remixicon/react'
|
||||||
import { Track } from 'livekit-client'
|
import { Track } from 'livekit-client'
|
||||||
|
|
||||||
import { useEffect, useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
|
|
||||||
import { keyboardShortcutsStore } from '@/stores/keyboardShortcuts'
|
import { appendShortcutLabel } from '@/features/shortcuts/utils'
|
||||||
import {
|
|
||||||
formatShortcutKey,
|
|
||||||
appendShortcutLabel,
|
|
||||||
} from '@/features/shortcuts/utils'
|
|
||||||
import { Shortcut } from '@/features/shortcuts/types'
|
import { Shortcut } from '@/features/shortcuts/types'
|
||||||
|
import { useRegisterKeyboardShortcut } from '@/features/shortcuts/useRegisterKeyboardShortcut'
|
||||||
|
|
||||||
export type ToggleSource = Exclude<
|
export type ToggleSource = Exclude<
|
||||||
Track.Source,
|
Track.Source,
|
||||||
@@ -87,6 +84,8 @@ export const SelectToggleDevice = <T extends ToggleSource>({
|
|||||||
const { devices, activeDeviceId, setActiveMediaDevice } =
|
const { devices, activeDeviceId, setActiveMediaDevice } =
|
||||||
useMediaDeviceSelect({ kind })
|
useMediaDeviceSelect({ kind })
|
||||||
|
|
||||||
|
useRegisterKeyboardShortcut({ shortcut: config.shortcut, handler: toggle })
|
||||||
|
|
||||||
const toggleLabel = useMemo(() => {
|
const toggleLabel = useMemo(() => {
|
||||||
const label = t(enabled ? 'disable' : 'enable', {
|
const label = t(enabled ? 'disable' : 'enable', {
|
||||||
keyPrefix: `join.${kind}`,
|
keyPrefix: `join.${kind}`,
|
||||||
@@ -97,14 +96,6 @@ export const SelectToggleDevice = <T extends ToggleSource>({
|
|||||||
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(() => {
|
|
||||||
if (!config.shortcut) return
|
|
||||||
keyboardShortcutsStore.shortcuts.set(
|
|
||||||
formatShortcutKey(config.shortcut),
|
|
||||||
() => toggle()
|
|
||||||
)
|
|
||||||
}, [toggle, config.shortcut])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<HStack gap={0}>
|
<HStack gap={0}>
|
||||||
<ToggleButton
|
<ToggleButton
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import { useEffect } from 'react'
|
||||||
|
import { keyboardShortcutsStore } from '@/stores/keyboardShortcuts'
|
||||||
|
import { formatShortcutKey } from '@/features/shortcuts/utils'
|
||||||
|
import { Shortcut } from '@/features/shortcuts/types'
|
||||||
|
|
||||||
|
export type useRegisterKeyboardShortcutProps = {
|
||||||
|
shortcut?: Shortcut
|
||||||
|
handler: () => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useRegisterKeyboardShortcut = ({
|
||||||
|
shortcut,
|
||||||
|
handler,
|
||||||
|
}: useRegisterKeyboardShortcutProps) => {
|
||||||
|
useEffect(() => {
|
||||||
|
if (!shortcut) return
|
||||||
|
keyboardShortcutsStore.shortcuts.set(formatShortcutKey(shortcut), handler)
|
||||||
|
}, [handler, shortcut])
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user