dify/web/app/components/workflow/shortcuts/shortcut-kbd.tsx
Stephen Zhou a84c2d36a3
style: format with vp fmt (#38803)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-07-12 15:57:46 +00:00

61 lines
1.7 KiB
TypeScript

import type { KbdColor } from '@langgenius/dify-ui/kbd'
import type { FormatDisplayOptions, RegisterableHotkey } from '@tanstack/react-hotkeys'
import type { WorkflowCanvasShortcutId } from './definitions'
import { cn } from '@langgenius/dify-ui/cn'
import { Kbd, KbdGroup } from '@langgenius/dify-ui/kbd'
import { formatForDisplay } from '@tanstack/react-hotkeys'
import { getWorkflowCanvasShortcutDisplayHotkey } from './definitions'
type ShortcutKbdProps = {
shortcut?: WorkflowCanvasShortcutId
hotkey?: RegisterableHotkey | (string & {})
className?: string
textColor?: 'default' | 'secondary'
bgColor?: KbdColor
platform?: FormatDisplayOptions['platform']
}
const getDisplayKeys = (
hotkey: RegisterableHotkey | (string & {}),
platform?: FormatDisplayOptions['platform'],
) => {
const displayOptions = platform ? { platform } : undefined
if (typeof hotkey !== 'string') return [formatForDisplay(hotkey, displayOptions)]
return hotkey
.split('+')
.filter(Boolean)
.map((key) => formatForDisplay(key, displayOptions))
}
export const ShortcutKbd = ({
shortcut,
hotkey,
className,
textColor = 'default',
bgColor = 'gray',
platform,
}: ShortcutKbdProps) => {
const displayHotkey =
hotkey ?? (shortcut ? getWorkflowCanvasShortcutDisplayHotkey(shortcut) : undefined)
if (!displayHotkey) return null
const displayKeys = getDisplayKeys(displayHotkey, platform)
return (
<KbdGroup className={cn(className)}>
{displayKeys.map((key) => (
<Kbd
key={key}
color={bgColor}
className={cn(textColor === 'secondary' && 'text-text-tertiary')}
>
{key}
</Kbd>
))}
</KbdGroup>
)
}