MCPcopy Create free account
hub / github.com/TanStack/hotkeys / App

Function App

examples/react/useHotkeyRecorder/src/index.tsx:67–275  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

65]
66
67function App() {
68 const [shortcuts, setShortcuts] = React.useState<Array<Shortcut>>(
69 () => INITIAL_SHORTCUTS,
70 )
71
72 // Track which shortcut is being edited (recording + name/description editing)
73 const [editingId, setEditingId] = React.useState<string | null>(null)
74 // Draft name/description while editing
75 const [draftName, setDraftName] = React.useState('')
76 const [draftDescription, setDraftDescription] = React.useState('')
77
78 const recorder = useHotkeyRecorder({
79 onRecord: (hotkey: Hotkey) => {
80 if (editingId) {
81 setShortcuts((prev) =>
82 prev.map((s) =>
83 s.id === editingId
84 ? { ...s, hotkey, name: draftName, description: draftDescription }
85 : s,
86 ),
87 )
88 setEditingId(null)
89 }
90 },
91 onCancel: () => {
92 // If this was a brand-new shortcut with no hotkey yet, remove it
93 if (editingId) {
94 setShortcuts((prev) => {
95 const shortcut = prev.find((s) => s.id === editingId)
96 if (shortcut && shortcut.hotkey === '') {
97 return prev.filter((s) => s.id !== editingId)
98 }
99 return prev
100 })
101 }
102 setEditingId(null)
103 },
104 onClear: () => {
105 if (editingId) {
106 setShortcuts((prev) =>
107 prev.map((s) =>
108 s.id === editingId
109 ? {
110 ...s,
111 hotkey: '' as Hotkey | '',
112 name: draftName,
113 description: draftDescription,
114 }
115 : s,
116 ),
117 )
118 setEditingId(null)
119 }
120 },
121 })
122
123 const isRecording = recorder.isRecording
124

Callers

nothing calls this directly

Calls 4

useHotkeyRecorderFunction · 0.90
useHotkeysFunction · 0.90
handleEditFunction · 0.70
handleDeleteFunction · 0.70

Tested by

no test coverage detected