-
Notifications
You must be signed in to change notification settings - Fork 613
Expand file tree
/
Copy pathSettingsDialog.tsx
More file actions
492 lines (454 loc) · 17.8 KB
/
SettingsDialog.tsx
File metadata and controls
492 lines (454 loc) · 17.8 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import { useState, useEffect } from "react";
import {
Dialog,
DialogTrigger,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogFooter,
} from "../ui/dialog";
import { Input } from "../ui/input";
import { Button } from "../ui/button";
import { Settings } from "lucide-react";
import { useToast } from "../../contexts/toast";
type APIProvider = "openai" | "gemini";
type AIModel = {
id: string;
name: string;
description: string;
};
type ModelCategory = {
key: 'extractionModel' | 'solutionModel' | 'debuggingModel';
title: string;
description: string;
openaiModels: AIModel[];
geminiModels: AIModel[];
};
// Define available models for each category
const modelCategories: ModelCategory[] = [
{
key: 'extractionModel',
title: 'Problem Extraction',
description: 'Model used to analyze screenshots and extract problem details',
openaiModels: [
{
id: "gpt-4o",
name: "gpt-4o",
description: "Best overall performance for problem extraction"
},
{
id: "gpt-4o-mini",
name: "gpt-4o-mini",
description: "Faster, more cost-effective option"
}
],
geminiModels: [
{
id: "gemini-1.5-pro",
name: "Gemini 1.5 Pro",
description: "Best overall performance for problem extraction"
},
{
id: "gemini-2.0-flash",
name: "Gemini 2.0 Flash",
description: "Faster, more cost-effective option"
}
]
},
{
key: 'solutionModel',
title: 'Solution Generation',
description: 'Model used to generate coding solutions',
openaiModels: [
{
id: "gpt-4o",
name: "gpt-4o",
description: "Strong overall performance for coding tasks"
},
{
id: "gpt-4o-mini",
name: "gpt-4o-mini",
description: "Faster, more cost-effective option"
}
],
geminiModels: [
{
id: "gemini-1.5-pro",
name: "Gemini 1.5 Pro",
description: "Strong overall performance for coding tasks"
},
{
id: "gemini-2.0-flash",
name: "Gemini 2.0 Flash",
description: "Faster, more cost-effective option"
}
]
},
{
key: 'debuggingModel',
title: 'Debugging',
description: 'Model used to debug and improve solutions',
openaiModels: [
{
id: "gpt-4o",
name: "gpt-4o",
description: "Best for analyzing code and error messages"
},
{
id: "gpt-4o-mini",
name: "gpt-4o-mini",
description: "Faster, more cost-effective option"
}
],
geminiModels: [
{
id: "gemini-1.5-pro",
name: "Gemini 1.5 Pro",
description: "Best for analyzing code and error messages"
},
{
id: "gemini-2.0-flash",
name: "Gemini 2.0 Flash",
description: "Faster, more cost-effective option"
}
]
}
];
interface SettingsDialogProps {
open?: boolean;
onOpenChange?: (open: boolean) => void;
}
export function SettingsDialog({ open: externalOpen, onOpenChange }: SettingsDialogProps) {
const [open, setOpen] = useState(externalOpen || false);
const [apiKey, setApiKey] = useState("");
const [apiProvider, setApiProvider] = useState<APIProvider>("openai");
const [extractionModel, setExtractionModel] = useState("gpt-4o");
const [solutionModel, setSolutionModel] = useState("gpt-4o");
const [debuggingModel, setDebuggingModel] = useState("gpt-4o");
const [isLoading, setIsLoading] = useState(false);
const { showToast } = useToast();
// Sync with external open state
useEffect(() => {
if (externalOpen !== undefined) {
setOpen(externalOpen);
}
}, [externalOpen]);
// Handle open state changes
const handleOpenChange = (newOpen: boolean) => {
setOpen(newOpen);
// Only call onOpenChange when there's actually a change
if (onOpenChange && newOpen !== externalOpen) {
onOpenChange(newOpen);
}
};
// Load current config on dialog open
useEffect(() => {
if (open) {
setIsLoading(true);
interface Config {
apiKey?: string;
apiProvider?: APIProvider;
extractionModel?: string;
solutionModel?: string;
debuggingModel?: string;
}
window.electronAPI
.getConfig()
.then((config: Config) => {
setApiKey(config.apiKey || "");
setApiProvider(config.apiProvider || "openai");
setExtractionModel(config.extractionModel || "gpt-4o");
setSolutionModel(config.solutionModel || "gpt-4o");
setDebuggingModel(config.debuggingModel || "gpt-4o");
})
.catch((error: unknown) => {
console.error("Failed to load config:", error);
showToast("Error", "Failed to load settings", "error");
})
.finally(() => {
setIsLoading(false);
});
}
}, [open, showToast]);
// Handle API provider change
const handleProviderChange = (provider: APIProvider) => {
setApiProvider(provider);
// Reset models to defaults when changing provider
if (provider === "openai") {
setExtractionModel("gpt-4o");
setSolutionModel("gpt-4o");
setDebuggingModel("gpt-4o");
} else {
setExtractionModel("gemini-1.5-pro");
setSolutionModel("gemini-1.5-pro");
setDebuggingModel("gemini-1.5-pro");
}
};
const handleSave = async () => {
setIsLoading(true);
try {
const result = await window.electronAPI.updateConfig({
apiKey,
apiProvider,
extractionModel,
solutionModel,
debuggingModel,
});
if (result) {
showToast("Success", "Settings saved successfully", "success");
handleOpenChange(false);
// Force reload the app to apply the API key
setTimeout(() => {
window.location.reload();
}, 1500);
}
} catch (error) {
console.error("Failed to save settings:", error);
showToast("Error", "Failed to save settings", "error");
} finally {
setIsLoading(false);
}
};
// Mask API key for display
const maskApiKey = (key: string) => {
if (!key || key.length < 10) return "";
return `${key.substring(0, 4)}...${key.substring(key.length - 4)}`;
};
// Open external link handler
const openExternalLink = (url: string) => {
window.electronAPI.openLink(url);
};
// Return null if not explicitly opened
if (!open) return null;
return (
<Dialog open={open} onOpenChange={handleOpenChange}>
<DialogContent
className="sm:max-w-md bg-black border border-white/10 text-white settings-dialog"
style={{
position: 'fixed',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
width: 'min(450px, 90vw)',
height: 'auto',
minHeight: '400px',
maxHeight: '90vh',
overflowY: 'auto',
zIndex: 9999,
margin: 0,
padding: '20px',
transition: 'opacity 0.25s ease, transform 0.25s ease',
animation: 'fadeIn 0.25s ease forwards',
opacity: 0.98
}}
>
<DialogHeader>
<DialogTitle>API Settings</DialogTitle>
<DialogDescription className="text-white/70">
Configure your API key and model preferences. You'll need your own API key to use this application.
</DialogDescription>
</DialogHeader>
<div className="space-y-4 py-4">
{/* API Provider Selection */}
<div className="space-y-2">
<label className="text-sm font-medium text-white">API Provider</label>
<div className="flex gap-2">
<div
className={`flex-1 p-2 rounded-lg cursor-pointer transition-colors ${
apiProvider === "openai"
? "bg-white/10 border border-white/20"
: "bg-black/30 border border-white/5 hover:bg-white/5"
}`}
onClick={() => handleProviderChange("openai")}
>
<div className="flex items-center gap-2">
<div
className={`w-3 h-3 rounded-full ${
apiProvider === "openai" ? "bg-white" : "bg-white/20"
}`}
/>
<div className="flex flex-col">
<p className="font-medium text-white text-sm">OpenAI</p>
<p className="text-xs text-white/60">GPT-4o models</p>
</div>
</div>
</div>
<div
className={`flex-1 p-2 rounded-lg cursor-pointer transition-colors ${
apiProvider === "gemini"
? "bg-white/10 border border-white/20"
: "bg-black/30 border border-white/5 hover:bg-white/5"
}`}
onClick={() => handleProviderChange("gemini")}
>
<div className="flex items-center gap-2">
<div
className={`w-3 h-3 rounded-full ${
apiProvider === "gemini" ? "bg-white" : "bg-white/20"
}`}
/>
<div className="flex flex-col">
<p className="font-medium text-white text-sm">Gemini</p>
<p className="text-xs text-white/60">Gemini 1.5 models</p>
</div>
</div>
</div>
</div>
</div>
<div className="space-y-2">
<label className="text-sm font-medium text-white" htmlFor="apiKey">
{apiProvider === "openai" ? "OpenAI API Key" : "Gemini API Key"}
</label>
<Input
id="apiKey"
type="password"
value={apiKey}
onChange={(e) => setApiKey(e.target.value)}
placeholder={apiProvider === "openai" ? "sk-..." : "Enter your Gemini API key"}
className="bg-black/50 border-white/10 text-white"
/>
{apiKey && (
<p className="text-xs text-white/50">
Current: {maskApiKey(apiKey)}
</p>
)}
<p className="text-xs text-white/50">
Your API key is stored locally and never sent to any server except {apiProvider === "openai" ? "OpenAI" : "Google"}
</p>
<div className="mt-2 p-2 rounded-md bg-white/5 border border-white/10">
<p className="text-xs text-white/80 mb-1">Don't have an API key?</p>
{apiProvider === "openai" ? (
<>
<p className="text-xs text-white/60 mb-1">1. Create an account at <button
onClick={() => openExternalLink('https://platform.openai.com/signup')}
className="text-blue-400 hover:underline cursor-pointer">OpenAI</button>
</p>
<p className="text-xs text-white/60 mb-1">2. Go to <button
onClick={() => openExternalLink('https://platform.openai.com/api-keys')}
className="text-blue-400 hover:underline cursor-pointer">API Keys</button> section
</p>
<p className="text-xs text-white/60">3. Create a new secret key and paste it here</p>
</>
) : (
<>
<p className="text-xs text-white/60 mb-1">1. Create an account at <button
onClick={() => openExternalLink('https://aistudio.google.com/')}
className="text-blue-400 hover:underline cursor-pointer">Google AI Studio</button>
</p>
<p className="text-xs text-white/60 mb-1">2. Go to the <button
onClick={() => openExternalLink('https://aistudio.google.com/app/apikey')}
className="text-blue-400 hover:underline cursor-pointer">API Keys</button> section
</p>
<p className="text-xs text-white/60">3. Create a new API key and paste it here</p>
</>
)}
</div>
</div>
<div className="space-y-2 mt-4">
<label className="text-sm font-medium text-white mb-2 block">Keyboard Shortcuts</label>
<div className="bg-black/30 border border-white/10 rounded-lg p-3">
<div className="grid grid-cols-2 gap-y-2 text-xs">
<div className="text-white/70">Toggle Visibility</div>
<div className="text-white/90 font-mono">Ctrl+B / Cmd+B</div>
<div className="text-white/70">Take Screenshot</div>
<div className="text-white/90 font-mono">Ctrl+H / Cmd+H</div>
<div className="text-white/70">Process Screenshots</div>
<div className="text-white/90 font-mono">Ctrl+Enter / Cmd+Enter</div>
<div className="text-white/70">Delete Last Screenshot</div>
<div className="text-white/90 font-mono">Ctrl+L / Cmd+L</div>
<div className="text-white/70">Reset View</div>
<div className="text-white/90 font-mono">Ctrl+R / Cmd+R</div>
<div className="text-white/70">Quit Application</div>
<div className="text-white/90 font-mono">Ctrl+Q / Cmd+Q</div>
<div className="text-white/70">Move Window</div>
<div className="text-white/90 font-mono">Ctrl+Arrow Keys</div>
<div className="text-white/70">Decrease Opacity</div>
<div className="text-white/90 font-mono">Ctrl+[ / Cmd+[</div>
<div className="text-white/70">Increase Opacity</div>
<div className="text-white/90 font-mono">Ctrl+] / Cmd+]</div>
<div className="text-white/70">Zoom Out</div>
<div className="text-white/90 font-mono">Ctrl+- / Cmd+-</div>
<div className="text-white/70">Reset Zoom</div>
<div className="text-white/90 font-mono">Ctrl+0 / Cmd+0</div>
<div className="text-white/70">Zoom In</div>
<div className="text-white/90 font-mono">Ctrl+= / Cmd+=</div>
</div>
</div>
</div>
<div className="space-y-4 mt-4">
<label className="text-sm font-medium text-white">AI Model Selection</label>
<p className="text-xs text-white/60 -mt-3 mb-2">
Select which models to use for each stage of the process
</p>
{modelCategories.map((category) => {
// Get the appropriate model list based on selected provider
const models = apiProvider === "openai" ? category.openaiModels : category.geminiModels;
return (
<div key={category.key} className="mb-4">
<label className="text-sm font-medium text-white mb-1 block">
{category.title}
</label>
<p className="text-xs text-white/60 mb-2">{category.description}</p>
<div className="space-y-2">
{models.map((m) => {
// Determine which state to use based on category key
const currentValue =
category.key === 'extractionModel' ? extractionModel :
category.key === 'solutionModel' ? solutionModel :
debuggingModel;
// Determine which setter function to use
const setValue =
category.key === 'extractionModel' ? setExtractionModel :
category.key === 'solutionModel' ? setSolutionModel :
setDebuggingModel;
return (
<div
key={m.id}
className={`p-2 rounded-lg cursor-pointer transition-colors ${
currentValue === m.id
? "bg-white/10 border border-white/20"
: "bg-black/30 border border-white/5 hover:bg-white/5"
}`}
onClick={() => setValue(m.id)}
>
<div className="flex items-center gap-2">
<div
className={`w-3 h-3 rounded-full ${
currentValue === m.id ? "bg-white" : "bg-white/20"
}`}
/>
<div>
<p className="font-medium text-white text-xs">{m.name}</p>
<p className="text-xs text-white/60">{m.description}</p>
</div>
</div>
</div>
);
})}
</div>
</div>
);
})}
</div>
</div>
<DialogFooter className="flex justify-between sm:justify-between">
<Button
variant="outline"
onClick={() => handleOpenChange(false)}
className="border-white/10 hover:bg-white/5 text-white"
>
Cancel
</Button>
<Button
className="px-4 py-3 bg-white text-black rounded-xl font-medium hover:bg-white/90 transition-colors"
onClick={handleSave}
disabled={isLoading || !apiKey}
>
{isLoading ? "Saving..." : "Save Settings"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}