HotkeyToggle.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { Fragment } from 'react'
  2. import { CardContent, KeyboardShortcut, Switch } from 'ui'
  3. import { hotkeyToKeys } from '@/state/shortcuts/formatShortcut'
  4. import type { ShortcutId } from '@/state/shortcuts/registry'
  5. import { useShortcutPreferences } from '@/state/shortcuts/state'
  6. import type { ShortcutDefinition } from '@/state/shortcuts/types'
  7. import { useIsShortcutEnabled } from '@/state/shortcuts/useIsShortcutEnabled'
  8. interface HotkeyToggleProps {
  9. definition: ShortcutDefinition
  10. isLast?: boolean
  11. }
  12. export function HotkeyToggle({ definition, isLast }: HotkeyToggleProps) {
  13. const enabled = useIsShortcutEnabled(definition.id as ShortcutId)
  14. const { setShortcutEnabled } = useShortcutPreferences()
  15. return (
  16. <CardContent className={isLast ? undefined : 'border-b'}>
  17. <div className="flex items-center justify-between gap-x-3">
  18. <label className="text-sm text-foreground">{definition.label}</label>
  19. <div className="flex items-center gap-x-3">
  20. <div className="flex items-center gap-1">
  21. {definition.sequence.map((step, i) => (
  22. <Fragment key={i}>
  23. {i > 0 && <span className="text-foreground-lighter text-[11px]">then</span>}
  24. <KeyboardShortcut keys={hotkeyToKeys(step)} />
  25. </Fragment>
  26. ))}
  27. </div>
  28. <Switch
  29. checked={enabled}
  30. onCheckedChange={(checked) => setShortcutEnabled(definition.id as ShortcutId, checked)}
  31. />
  32. </div>
  33. </div>
  34. </CardContent>
  35. )
  36. }