-
-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathindex.tsx
More file actions
182 lines (170 loc) Β· 6.55 KB
/
Copy pathindex.tsx
File metadata and controls
182 lines (170 loc) Β· 6.55 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
import React, { useState, useEffect } from "react";
import { Card, CardContent } from "@/components/ui/card";
import { Label } from "@/components/ui/label";
import { ShortcutInput } from "@/components/shortcut-input";
import { Separator } from "@/components/ui/separator";
import { api } from "@/trpc/react";
import { toast } from "sonner";
export function ShortcutsSettingsPage() {
const [pushToTalkShortcut, setPushToTalkShortcut] = useState<
number[] | undefined
>();
const [toggleRecordingShortcut, setToggleRecordingShortcut] = useState<
number[] | undefined
>();
const [pasteLastTranscriptShortcut, setPasteLastTranscriptShortcut] =
useState<number[] | undefined>();
const [recordingShortcut, setRecordingShortcut] = useState<
"pushToTalk" | "toggleRecording" | "pasteLastTranscript" | null
>(null);
// tRPC queries and mutations
const shortcutsQuery = api.settings.getShortcuts.useQuery();
const utils = api.useUtils();
const setShortcutMutation = api.settings.setShortcut.useMutation({
onSuccess: (data, variables) => {
utils.settings.getShortcuts.invalidate();
// Show warning if there is one
if (data.warning) {
toast.warning(data.warning);
} else {
const successMessages = {
pushToTalk: "Push to talk shortcut updated",
toggleRecording: "Hands-free mode shortcut updated",
pasteLastTranscript: "Paste last transcript shortcut updated",
} as const;
toast.success(successMessages[variables.type]);
}
},
onError: (error) => {
toast.error(error.message);
const cached = utils.settings.getShortcuts.getData();
if (cached) {
setPushToTalkShortcut(cached.pushToTalk);
setToggleRecordingShortcut(cached.toggleRecording);
setPasteLastTranscriptShortcut(cached.pasteLastTranscript);
} else {
utils.settings.getShortcuts.invalidate();
}
},
});
// Load shortcuts when query data is available
useEffect(() => {
if (shortcutsQuery.data) {
setPushToTalkShortcut(shortcutsQuery.data.pushToTalk);
setToggleRecordingShortcut(shortcutsQuery.data.toggleRecording);
setPasteLastTranscriptShortcut(shortcutsQuery.data.pasteLastTranscript);
}
}, [shortcutsQuery.data]);
const handlePushToTalkChange = (shortcut: number[]) => {
setPushToTalkShortcut(shortcut);
setShortcutMutation.mutate({
type: "pushToTalk",
shortcut: shortcut,
});
};
const handleToggleRecordingChange = (shortcut: number[]) => {
setToggleRecordingShortcut(shortcut);
setShortcutMutation.mutate({
type: "toggleRecording",
shortcut: shortcut,
});
};
const handlePasteLastTranscriptChange = (shortcut: number[]) => {
setPasteLastTranscriptShortcut(shortcut);
setShortcutMutation.mutate({
type: "pasteLastTranscript",
shortcut: shortcut,
});
};
return (
<div className="container mx-auto p-6 max-w-5xl">
<div className="mb-8">
<h1 className="text-xl font-bold">Shortcuts</h1>
<p className="text-muted-foreground mt-1 text-sm">
Configure keyboard shortcuts for dictation and hands-free modes
</p>
</div>
<div className="space-y-6">
<Card>
<CardContent className="space-y-8">
<div>
<div className="flex flex-col md:flex-row md:justify-between gap-4">
<div>
<Label className="text-base font-semibold text-foreground">
Push to talk
</Label>
<p className="text-xs text-muted-foreground mt-1 max-w-md">
Hold to dictate while key is pressed
</p>
</div>
<div className="flex flex-col gap-2 items-end min-w-[260px]">
<ShortcutInput
value={pushToTalkShortcut}
onChange={handlePushToTalkChange}
isRecordingShortcut={recordingShortcut === "pushToTalk"}
onRecordingShortcutChange={(recording) =>
setRecordingShortcut(recording ? "pushToTalk" : null)
}
/>
</div>
</div>
<Separator className="my-4" />
</div>
<div>
<div className="flex flex-col md:flex-row md:justify-between gap-4">
<div>
<Label className="text-base font-semibold text-foreground">
Hands-free mode
</Label>
<p className="text-xs text-muted-foreground mt-1 max-w-md">
Start/stop dictation by pressing once to start and pressing
again to stop
</p>
</div>
<div className="flex flex-col gap-2 items-end min-w-[260px]">
<ShortcutInput
value={toggleRecordingShortcut}
onChange={handleToggleRecordingChange}
isRecordingShortcut={
recordingShortcut === "toggleRecording"
}
onRecordingShortcutChange={(recording) =>
setRecordingShortcut(recording ? "toggleRecording" : null)
}
/>
</div>
</div>
</div>
<div>
<Separator className="my-4" />
<div className="flex flex-col md:flex-row md:justify-between gap-4">
<div>
<Label className="text-base font-semibold text-foreground">
Paste last transcript
</Label>
<p className="text-xs text-muted-foreground mt-1 max-w-md">
Paste your most recent transcription into the active app
</p>
</div>
<div className="flex flex-col gap-2 items-end min-w-[260px]">
<ShortcutInput
value={pasteLastTranscriptShortcut}
onChange={handlePasteLastTranscriptChange}
isRecordingShortcut={
recordingShortcut === "pasteLastTranscript"
}
onRecordingShortcutChange={(recording) =>
setRecordingShortcut(
recording ? "pasteLastTranscript" : null,
)
}
/>
</div>
</div>
</div>
</CardContent>
</Card>
</div>
</div>
);
}