-
Notifications
You must be signed in to change notification settings - Fork 428
fix(renderer): improve shortcut hint rendering #1949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,30 +1,47 @@ | ||||||||||||||||||||||||||||||||||
| import { formatForDisplay } from '@tanstack/react-hotkeys'; | ||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||
| detectPlatform, | ||||||||||||||||||||||||||||||||||
| KEY_DISPLAY_SYMBOLS, | ||||||||||||||||||||||||||||||||||
| MAC_MODIFIER_SYMBOLS, | ||||||||||||||||||||||||||||||||||
| parseHotkey, | ||||||||||||||||||||||||||||||||||
| STANDARD_MODIFIER_LABELS, | ||||||||||||||||||||||||||||||||||
| } from '@tanstack/react-hotkeys'; | ||||||||||||||||||||||||||||||||||
| import React from 'react'; | ||||||||||||||||||||||||||||||||||
| import { useAppSettingsKey } from '@renderer/features/settings/use-app-settings-key'; | ||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||
| getEffectiveHotkey, | ||||||||||||||||||||||||||||||||||
| type ShortcutSettingsKey, | ||||||||||||||||||||||||||||||||||
| } from '@renderer/lib/hooks/useKeyboardShortcuts'; | ||||||||||||||||||||||||||||||||||
| import { cn } from '@renderer/utils/utils'; | ||||||||||||||||||||||||||||||||||
| import { Kbd, KbdGroup } from './kbd'; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| interface ShortcutHintProps { | ||||||||||||||||||||||||||||||||||
| settingsKey: ShortcutSettingsKey; | ||||||||||||||||||||||||||||||||||
| className?: string; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const PLATFORM = detectPlatform(); | ||||||||||||||||||||||||||||||||||
| const IS_MAC = PLATFORM === 'mac'; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| export const ShortcutHint: React.FC<ShortcutHintProps> = ({ settingsKey, className }) => { | ||||||||||||||||||||||||||||||||||
| const { value: keyboard } = useAppSettingsKey('keyboard'); | ||||||||||||||||||||||||||||||||||
| const hotkey = getEffectiveHotkey(settingsKey, keyboard); | ||||||||||||||||||||||||||||||||||
| const display = hotkey ? formatForDisplay(hotkey) : ''; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if (!display) return null; | ||||||||||||||||||||||||||||||||||
| if (!hotkey) return null; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const parsed = parseHotkey(hotkey, PLATFORM); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||
| <span className={`flex items-center gap-1 text-xs text-muted-foreground ${className ?? ''}`}> | ||||||||||||||||||||||||||||||||||
| <span className={cn('flex items-center gap-1 text-xs text-muted-foreground', className)}> | ||||||||||||||||||||||||||||||||||
| <KbdGroup> | ||||||||||||||||||||||||||||||||||
| {display.split('+').map((key, _) => ( | ||||||||||||||||||||||||||||||||||
| <Kbd key={key}>{key.trim()}</Kbd> | ||||||||||||||||||||||||||||||||||
| ))} | ||||||||||||||||||||||||||||||||||
| {parsed.modifiers.map((modifier, idx) => { | ||||||||||||||||||||||||||||||||||
| const glyph = IS_MAC | ||||||||||||||||||||||||||||||||||
| ? MAC_MODIFIER_SYMBOLS[modifier] | ||||||||||||||||||||||||||||||||||
| : STANDARD_MODIFIER_LABELS[modifier]; | ||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||
| <Kbd key={idx}>{IS_MAC ? <span className="translate-y-px">{glyph}</span> : glyph}</Kbd> | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| })} | ||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: src/renderer/lib/ui/shortcut-hint.tsx
Line: 36-43
Comment:
Using the array index as the React `key` for modifier elements is fragile — if modifiers ever reorder, React will diff them incorrectly and potentially keep stale DOM. Each modifier string is already unique within the array (a hotkey can't have duplicate modifiers), so `modifier` itself is a stable, meaningful key.
```suggestion
{parsed.modifiers.map((modifier) => {
const glyph = IS_MAC
? MAC_MODIFIER_SYMBOLS[modifier]
: STANDARD_MODIFIER_LABELS[modifier];
return (
<Kbd key={modifier}>{IS_MAC ? <span className="translate-y-px">{glyph}</span> : glyph}</Kbd>
);
})}
```
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||||||||||||||||||||||||||
| <Kbd>{KEY_DISPLAY_SYMBOLS[parsed.key] ?? parsed.key}</Kbd> | ||||||||||||||||||||||||||||||||||
| </KbdGroup> | ||||||||||||||||||||||||||||||||||
| </span> | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
glyphhas no fallback when a modifier is absent fromMAC_MODIFIER_SYMBOLSorSTANDARD_MODIFIER_LABELS. IfparseHotkeyever surfaces an unexpected modifier name (e.g.'Mod'before it is resolved, or a future library addition),glyphisundefinedand an empty<Kbd>renders in the group. Adding?? modifieras a last-resort fallback — mirroring whatKEY_DISPLAY_SYMBOLS[parsed.key] ?? parsed.keyalready does for the main key — keeps the output legible instead of blank.Prompt To Fix With AI