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
import clsx from "clsx";
import { Button } from "@humansignal/ui";
import {
  Card,
  CardContent,
  CardHeader,
  CardTitle,
  CardDescription,
  CardFooter,
} from "@humansignal/shad/components/ui/card";
import { HotkeyItem } from "./Item";
 
// Type definitions
interface Hotkey {
  id: string;
  section: string;
  element: string;
  label: string;
  key: string;
  mac?: string;
  active: boolean;
  subgroup?: string;
  description?: string;
}
 
interface Section {
  id: string;
  title: string;
  description?: string;
}
 
interface GroupedHotkeys {
  [subgroup: string]: Hotkey[];
}
 
interface HotkeySectionProps {
  section: Section;
  hotkeys: Hotkey[];
  editingHotkeyId: string | null;
  onEditHotkey: (id: string) => void;
  onSaveHotkey: (id: string, newKey: string) => void;
  onCancelEdit: () => void;
  onSaveSection: (sectionId: string) => void;
  onToggleHotkey: (id: string) => void;
  hasChanges: boolean;
}
 
/**
 * HotkeySection Component
 *
 * Displays a section of hotkeys grouped by subgroups within a card layout.
 * Provides functionality to edit, toggle, and save hotkeys within the section.
 *
 * @param {HotkeySectionProps} props - Component props
 * @returns {JSX.Element} Rendered HotkeySection component
 *
 * @example
 * <HotkeySection
 *   section={{ id: "editor", title: "Editor", description: "Text editing shortcuts" }}
 *   hotkeys={[
 *     { id: "1", subgroup: "navigation", ... },
 *     { id: "2", subgroup: "editing", ... }
 *   ]}
 *   editingHotkeyId={null}
 *   onEditHotkey={(id) => setEditingId(id)}
 *   onSaveHotkey={(id, newKey) => saveHotkey(id, newKey)}
 *   onCancelEdit={() => setEditingId(null)}
 *   onSaveSection={(sectionId) => saveSection(sectionId)}
 *   onToggleHotkey={(id) => toggleHotkey(id)}
 *   hasChanges={true}
 * />
 */
export const HotkeySection = ({
  section,
  hotkeys,
  editingHotkeyId,
  onEditHotkey,
  onSaveHotkey,
  onCancelEdit,
  onSaveSection,
  onToggleHotkey,
  hasChanges,
}: HotkeySectionProps) => {
  /**
   * Groups hotkeys by their subgroup property
   * Hotkeys without a subgroup are placed in the 'default' group
   *
   * @returns {GroupedHotkeys} Object with subgroup names as keys and arrays of hotkeys as values
   */
  const groupedHotkeys: GroupedHotkeys = hotkeys.reduce((groups: GroupedHotkeys, hotkey: Hotkey) => {
    const subgroup = hotkey.subgroup || "default";
    if (!groups[subgroup]) {
      groups[subgroup] = [];
    }
    groups[subgroup].push(hotkey);
    return groups;
  }, {});
 
  /**
   * Gets sorted subgroup names with 'default' always appearing first
   * Other subgroups are sorted alphabetically
   *
   * @returns {string[]} Sorted array of subgroup names
   */
  const subgroups: string[] = Object.keys(groupedHotkeys).sort((a: string, b: string) => {
    if (a === "default") return -1;
    if (b === "default") return 1;
    return a.localeCompare(b);
  });
 
  /**
   * Handles the save section button click
   */
  const handleSaveSection = (): void => {
    onSaveSection(section.id);
  };
 
  return (
    <Card className="mb-6">
      <CardHeader className="pb-2">
        <CardTitle>{section.title}</CardTitle>
        <CardDescription>{section.description}</CardDescription>
      </CardHeader>
 
      <CardContent>
        <div>
          {subgroups.map((subgroup: string) => (
            <div
              key={subgroup}
              className={clsx(subgroup !== "default" && "mt-4 pt-2 border rounded-md border-border p-3")}
            >
              {groupedHotkeys[subgroup].map((hotkey: Hotkey) => (
                <HotkeyItem
                  key={hotkey.id}
                  hotkey={hotkey}
                  onEdit={onEditHotkey}
                  onToggle={onToggleHotkey}
                  isEditing={editingHotkeyId === hotkey.id}
                  onSave={onSaveHotkey}
                  onCancel={onCancelEdit}
                />
              ))}
            </div>
          ))}
 
          {hotkeys.length === 0 && (
            <div className="py-8 text-center text-muted-foreground italic">No hotkeys in this section</div>
          )}
        </div>
      </CardContent>
 
      <CardFooter className="flex justify-end">
        <Button variant="primary" onClick={handleSaveSection} disabled={!hasChanges}>
          Save
        </Button>
      </CardFooter>
    </Card>
  );
};