Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import { useState } from "react";
import { IconWarning, ToastType, useToast } from "@humansignal/ui";
 
// Shadcn UI components
import { Button } from "@humansignal/ui";
import { Card, CardContent, CardHeader } from "@humansignal/shad/components/ui/card";
import { Skeleton } from "@humansignal/shad/components/ui/skeleton";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@humansignal/shad/components/ui/dialog";
 
import { HotkeySection } from "./Hotkeys/Section";
import { ImportDialog } from "./Hotkeys/Import";
import { KeyboardKey } from "./Hotkeys/Key";
import type { Hotkey, Section, DirtyState, DuplicateConfirmDialog, ImportData } from "./Hotkeys/utils";
// @ts-ignore
import { HOTKEY_SECTIONS } from "./Hotkeys/defaults";
import styles from "../AccountSettings.module.scss";
import { useHotkeys } from "../hooks/useHotkeys";
 
// Type the imported defaults
const typedHotkeySections = HOTKEY_SECTIONS as Section[];
 
export const HotkeysHeaderButtons = () => {
  const [importDialogOpen, setImportDialogOpen] = useState<boolean>(false);
  const { handleResetToDefaults, handleExportHotkeys, handleImportHotkeys } = useHotkeys();
 
  return (
    <>
      <div className={`${styles.flexRow} justify-end gap-tight`}>
        <Button variant="neutral" look="outlined" onClick={() => setImportDialogOpen(true)}>
          Import
        </Button>
        <Button variant="neutral" look="outlined" onClick={handleExportHotkeys}>
          Export
        </Button>
        <Button variant="negative" look="outlined" onClick={handleResetToDefaults}>
          Reset to Defaults
        </Button>
      </div>
 
      {/* Import Dialog */}
      <ImportDialog open={importDialogOpen} onOpenChange={setImportDialogOpen} onImport={handleImportHotkeys} />
    </>
  );
};
 
export const HotkeysManager = () => {
  const toast = useToast();
  const [editingHotkeyId, setEditingHotkeyId] = useState<string | null>(null);
  const [dirtyState, setDirtyState] = useState<DirtyState>({});
  const [duplicateConfirmDialog, setDuplicateConfirmDialog] = useState<DuplicateConfirmDialog>({
    open: false,
    hotkeyId: null,
    newKey: null,
    conflictingHotkeys: [],
  });
 
  // Use the shared hook for common functionality
  const { hotkeys, setHotkeys, isLoading, setIsLoading, saveHotkeysToAPI } = useHotkeys();
 
  // Check if a hotkey conflicts with others globally
  const getGlobalDuplicates = (hotkeyId: string, newKey: string): Hotkey[] => {
    return hotkeys.filter((h: Hotkey) => h.id !== hotkeyId && h.key === newKey);
  };
 
  // Handle toggling a single hotkey
  const handleToggleHotkey = (hotkeyId: string) => {
    // Update the hotkey
    const updatedHotkeys = hotkeys.map((hotkey: Hotkey) => {
      if (hotkey.id === hotkeyId) {
        return { ...hotkey, active: !hotkey.active };
      }
      return hotkey;
    });
 
    setHotkeys(updatedHotkeys);
 
    // Mark the section as having changes
    const hotkey = hotkeys.find((h: Hotkey) => h.id === hotkeyId);
    if (hotkey) {
      setDirtyState({
        ...dirtyState,
        [hotkey.section]: true,
      });
    }
  };
 
  // Helper function to get section title by ID
  const getSectionTitle = (sectionId: string): string => {
    const section = typedHotkeySections.find((s: Section) => s.id === sectionId);
    return section ? section.title : sectionId;
  };
 
  // Handle saving an edited hotkey
  const handleSaveHotkey = (hotkeyId: string, newKey: string) => {
    // Find the hotkey to update
    const hotkey = hotkeys.find((h: Hotkey) => h.id === hotkeyId);
    if (!hotkey) return;
 
    // Check for global duplicates
    const conflictingHotkeys = getGlobalDuplicates(hotkeyId, newKey);
 
    if (conflictingHotkeys.length > 0) {
      // Show confirmation dialog for duplicates
      setDuplicateConfirmDialog({
        open: true,
        hotkeyId,
        newKey,
        conflictingHotkeys,
      });
      return;
    }
 
    // No conflicts, proceed with the update
    updateHotkeyKey(hotkeyId, newKey);
  };
 
  // Function to actually update the hotkey key
  const updateHotkeyKey = (hotkeyId: string, newKey: string) => {
    // Find the hotkey to update
    const hotkey = hotkeys.find((h: Hotkey) => h.id === hotkeyId);
    if (!hotkey) return;
 
    // Update the hotkey
    const updatedHotkeys = hotkeys.map((h: Hotkey) => {
      if (h.id === hotkeyId) {
        return { ...h, key: newKey, mac: newKey };
      }
      return h;
    });
 
    setHotkeys(updatedHotkeys);
 
    // Mark the section as having changes
    setDirtyState({
      ...dirtyState,
      [hotkey.section]: true,
    });
 
    // Exit edit mode
    setEditingHotkeyId(null);
  };
 
  // Handle confirming duplicate hotkey
  const handleConfirmDuplicate = () => {
    const { hotkeyId, newKey } = duplicateConfirmDialog;
 
    // Close the dialog
    setDuplicateConfirmDialog({
      open: false,
      hotkeyId: null,
      newKey: null,
      conflictingHotkeys: [],
    });
 
    // Proceed with the update
    if (hotkeyId && newKey) {
      updateHotkeyKey(hotkeyId, newKey);
    }
  };
 
  // Handle canceling duplicate confirmation
  const handleCancelDuplicate = () => {
    setDuplicateConfirmDialog({
      open: false,
      hotkeyId: null,
      newKey: null,
      conflictingHotkeys: [],
    });
  };
 
  // Handle canceling edit mode
  const handleCancelEdit = () => {
    setEditingHotkeyId(null);
  };
 
  // Handle saving a section's hotkeys
  const handleSaveSection = async (sectionId: string) => {
    setIsLoading(true);
 
    try {
      // Save ALL modified hotkeys and settings, not just this section
      const result = await saveHotkeysToAPI(hotkeys, {});
 
      if (result.ok) {
        // Clear the dirty state for this section
        const newDirtyState = { ...dirtyState };
        delete newDirtyState[sectionId];
        setDirtyState(newDirtyState);
 
        const sectionName =
          sectionId === "settings" ? "Settings" : typedHotkeySections.find((s: Section) => s.id === sectionId)?.title;
 
        if (toast) {
          toast.show({
            message: `${sectionName} hotkeys saved successfully`,
            type: ToastType.info,
          });
        }
      } else {
        if (toast) {
          toast.show({
            message: `Failed to save: ${result.error || "Unknown error"}`,
            type: ToastType.error,
          });
        }
      }
    } catch (error: unknown) {
      if (toast) {
        const errorMessage = error instanceof Error ? error.message : "Unknown error";
        toast.show({
          message: `Error saving: ${errorMessage}`,
          type: ToastType.error,
        });
      }
    } finally {
      setIsLoading(false);
    }
  };
 
  // Enhanced import handler that manages dirty state
  const handleImportHotkeys = async (importedData: ImportData | Hotkey[]) => {
    try {
      setIsLoading(true);
 
      // Handle both old format (just hotkeys array) and new format (with settings)
      const importedHotkeys = Array.isArray(importedData) ? importedData : importedData.hotkeys || [];
      const importedSettings = Array.isArray(importedData) ? {} : importedData.settings || {};
 
      // Update local state
      setHotkeys(importedHotkeys);
 
      // Save all imported data to API (including settings)
      const result = await saveHotkeysToAPI(importedHotkeys, importedSettings);
 
      if (!result.ok) {
        throw new Error(result.error || "Failed to save imported hotkeys");
      }
 
      // Reset dirty state
      setDirtyState({});
 
      if (toast) {
        toast.show({ message: "Hotkeys imported successfully", type: ToastType.info });
      }
    } catch (error: unknown) {
      if (toast) {
        const errorMessage = error instanceof Error ? error.message : "Unknown error";
        toast.show({ message: `Error importing hotkeys: ${errorMessage}`, type: ToastType.error });
      }
    } finally {
      setIsLoading(false);
    }
  };
 
  // Group hotkeys by section
  const getHotkeysBySection = (sectionId: string): Hotkey[] => {
    return hotkeys.filter((hotkey: Hotkey) => hotkey.section === sectionId);
  };
 
  return (
    <div id="hotkeys-manager">
      <div className={styles.sectionContent}>
        {isLoading && hotkeys.length === 0 ? (
          <div className="flex flex-col gap-wide">
            {/* Platform settings skeleton */}
            <Card>
              <CardHeader className="pb-tight">
                <Skeleton className="h-wide w-[16rem]" />
                <Skeleton className="h-base w-[18rem]" />
              </CardHeader>
              <CardContent>
                <Skeleton className="h-5 w-44 mb-tight" />
                <Skeleton className="h-base w-[16rem]" />
              </CardContent>
            </Card>
 
            {/* Hotkey sections skeleton */}
            {typedHotkeySections.map((section: Section) => (
              <Card key={section.id}>
                <CardHeader className="pb-tight">
                  <Skeleton className="h-wide w-[16rem]" />
                  <Skeleton className="h-base w-[18rem]" />
                </CardHeader>
                <CardContent>
                  {[1, 2, 3].map((i) => (
                    <div key={i} className={`py-wide ${i < 3 ? "border-b border-border" : ""}`}>
                      <Skeleton className="h-5 w-44 mb-tight" />
                      <Skeleton className="h-base w-[16rem]" />
                    </div>
                  ))}
                </CardContent>
              </Card>
            ))}
          </div>
        ) : (
          <div className="flex flex-col gap-wide">
            {/* Hotkey Sections */}
            {typedHotkeySections.map((section: Section) => (
              <HotkeySection
                key={section.id}
                section={section}
                hotkeys={getHotkeysBySection(section.id)}
                editingHotkeyId={editingHotkeyId}
                onSaveHotkey={handleSaveHotkey}
                onCancelEdit={handleCancelEdit}
                onToggleHotkey={handleToggleHotkey}
                onSaveSection={handleSaveSection}
                hasChanges={dirtyState[section.id] || false}
                onEditHotkey={setEditingHotkeyId}
              />
            ))}
          </div>
        )}
      </div>
 
      {/* Duplicate Confirmation Dialog */}
      <Dialog open={duplicateConfirmDialog.open} onOpenChange={handleCancelDuplicate}>
        <DialogContent className="bg-neutral-surface">
          <DialogHeader>
            <DialogTitle>Warning: Duplicate Hotkey Detected</DialogTitle>
            <DialogDescription>
              The hotkey combination "<strong>{duplicateConfirmDialog.newKey}</strong>" is already being used by:
            </DialogDescription>
          </DialogHeader>
 
          <div className="max-h-60 overflow-y-auto">
            <div className="flex flex-col gap-base">
              {duplicateConfirmDialog.conflictingHotkeys.map((conflictHotkey: Hotkey) => (
                <div
                  key={conflictHotkey.id}
                  className="flex items-center justify-between p-base bg-neutral-surface rounded-small border border-warning-border-subtle"
                >
                  <div className="flex-1 min-w-0">
                    <div className="font-medium overflow-hidden text-ellipsis whitespace-nowrap">
                      {conflictHotkey.label}
                    </div>
                    <div className="text-small text-neutral-content-subtler">
                      {getSectionTitle(conflictHotkey.section)}
                    </div>
                  </div>
                  <div className="ml-tight flex-shrink-0">
                    <KeyboardKey>{conflictHotkey.key}</KeyboardKey>
                  </div>
                </div>
              ))}
            </div>
          </div>
 
          <DialogDescription className="text-warning-text bg-warning-background p-base rounded-small border border-warning-border-subtle flex items-start gap-tight">
            <div>
              <IconWarning className="text-warning-icon" />
            </div>
            <div>
              Having duplicate hotkeys may cause conflicts and unexpected behavior. Are you sure you want to proceed?
            </div>
          </DialogDescription>
 
          <DialogFooter>
            <Button variant="neutral" onClick={handleCancelDuplicate}>
              Cancel
            </Button>
            <Button onClick={handleConfirmDuplicate}>Allow Duplicate</Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>
    </div>
  );
};