forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseTaskKeyboardShortcuts.ts
More file actions
189 lines (167 loc) · 4.76 KB
/
Copy pathuseTaskKeyboardShortcuts.ts
File metadata and controls
189 lines (167 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
'use client';
import { useEffect, useState, useCallback } from 'react';
interface UseTaskKeyboardShortcutsProps {
enabled: boolean;
selectedIds: Set<string>;
focusedIndex: number;
totalItems: number;
// Actions
onSetDueToday: (ids: string[]) => void;
onSetDueTomorrow: (ids: string[]) => void;
onDelete: (ids: string[]) => void;
onToggleComplete: (ids: string[]) => void;
onStartEdit: (id: string) => void;
onSelectAll: () => void;
onDeselectAll: () => void;
onNavigate: (direction: 'up' | 'down') => void;
onToggleSelectFocused: () => void;
}
interface UseTaskKeyboardShortcutsReturn {
isMac: boolean;
}
export function useTaskKeyboardShortcuts({
enabled,
selectedIds,
focusedIndex,
totalItems,
onSetDueToday,
onSetDueTomorrow,
onDelete,
onToggleComplete,
onStartEdit,
onSelectAll,
onDeselectAll,
onNavigate,
onToggleSelectFocused,
}: UseTaskKeyboardShortcutsProps): UseTaskKeyboardShortcutsReturn {
const [isMac, setIsMac] = useState(true);
// Detect OS on mount
useEffect(() => {
setIsMac(
typeof navigator !== 'undefined' &&
navigator.platform.toLowerCase().includes('mac')
);
}, []);
const handleKeyDown = useCallback(
(e: KeyboardEvent) => {
if (!enabled) return;
// Don't trigger if user is typing in an input
const target = e.target as HTMLElement;
if (
target.tagName === 'INPUT' ||
target.tagName === 'TEXTAREA' ||
target.isContentEditable
) {
// Allow Escape to work even in inputs
if (e.key !== 'Escape') {
return;
}
}
const modKey = isMac ? e.metaKey : e.ctrlKey;
const ids = Array.from(selectedIds);
switch (e.key.toLowerCase()) {
case 't':
// Set due to today
if (!modKey && ids.length > 0) {
e.preventDefault();
onSetDueToday(ids);
}
break;
case 'n':
// Set due to tomorrow
if (!modKey && ids.length > 0) {
e.preventDefault();
onSetDueTomorrow(ids);
}
break;
case 'd':
// Delete selected
if (!modKey && ids.length > 0) {
e.preventDefault();
onDelete(ids);
}
break;
case 'e':
// Edit description (single selection only)
if (!modKey && ids.length === 1) {
e.preventDefault();
onStartEdit(ids[0]);
}
break;
case 'a':
// Select all
if (modKey) {
e.preventDefault();
onSelectAll();
}
break;
case 'enter':
// Toggle complete
if (!modKey && ids.length > 0) {
e.preventDefault();
onToggleComplete(ids);
}
break;
case 'escape':
// Deselect all
e.preventDefault();
onDeselectAll();
break;
case 'arrowup':
// Navigate up
if (!modKey && totalItems > 0) {
e.preventDefault();
onNavigate('up');
}
break;
case 'arrowdown':
// Navigate down
if (!modKey && totalItems > 0) {
e.preventDefault();
onNavigate('down');
}
break;
case ' ':
// Toggle select focused item
if (!modKey && focusedIndex >= 0) {
e.preventDefault();
onToggleSelectFocused();
}
break;
}
},
[
enabled,
isMac,
selectedIds,
focusedIndex,
totalItems,
onSetDueToday,
onSetDueTomorrow,
onDelete,
onToggleComplete,
onStartEdit,
onSelectAll,
onDeselectAll,
onNavigate,
onToggleSelectFocused,
]
);
useEffect(() => {
document.addEventListener('keydown', handleKeyDown);
return () => document.removeEventListener('keydown', handleKeyDown);
}, [handleKeyDown]);
return { isMac };
}
// Keyboard shortcut definitions for help display
export const KEYBOARD_SHORTCUTS = [
{ key: 't', description: 'Set due today', requiresSelection: true },
{ key: 'n', description: 'Set due tomorrow', requiresSelection: true },
{ key: 'd', description: 'Delete selected', requiresSelection: true },
{ key: 'e', description: 'Edit description', requiresSelection: true, singleOnly: true },
{ key: '⌘/Ctrl + A', description: 'Select all', requiresSelection: false },
{ key: 'Enter', description: 'Toggle complete', requiresSelection: true },
{ key: 'Escape', description: 'Deselect all', requiresSelection: false },
{ key: '↑ / ↓', description: 'Navigate', requiresSelection: false },
{ key: 'Space', description: 'Toggle select', requiresSelection: false },
] as const;