-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSettingsPage.tsx
More file actions
409 lines (386 loc) · 14.8 KB
/
Copy pathSettingsPage.tsx
File metadata and controls
409 lines (386 loc) · 14.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
import { useState, useEffect } from 'react';
import { invoke } from '@tauri-apps/api/core';
import {
Card,
CardHeader,
CardTitle,
CardDescription,
CardContent,
Button,
Switch,
useToast,
ToastContainer,
} from '@mcpmux/ui';
import {
Sun,
Moon,
Monitor,
FileText,
FolderOpen,
Loader2,
Power,
Minimize2,
XCircle,
Trash2,
BarChart3,
} from 'lucide-react';
import { useAppStore, useTheme, useAnalyticsEnabled } from '@/stores';
import { UpdateChecker } from './UpdateChecker';
interface StartupSettings {
autoLaunch: boolean;
startMinimized: boolean;
closeToTray: boolean;
}
export function SettingsPage() {
const theme = useTheme();
const setTheme = useAppStore((state) => state.setTheme);
const analyticsEnabled = useAnalyticsEnabled();
const setAnalyticsEnabled = useAppStore((state) => state.setAnalyticsEnabled);
const [logsPath, setLogsPath] = useState<string>('');
const [openingLogs, setOpeningLogs] = useState(false);
const { toasts, success, error } = useToast();
// Startup settings state
const [startupSettings, setStartupSettings] = useState<StartupSettings>({
autoLaunch: false,
startMinimized: false,
closeToTray: true,
});
const [loadingSettings, setLoadingSettings] = useState(true);
const [savingSettings, setSavingSettings] = useState(false);
// Log retention state
const [logRetentionDays, setLogRetentionDays] = useState<number>(30);
const [savingRetention, setSavingRetention] = useState(false);
// Load logs path on mount
useEffect(() => {
const loadLogsPath = async () => {
try {
const path = await invoke<string>('get_logs_path');
setLogsPath(path);
} catch (error) {
console.error('Failed to get logs path:', error);
}
};
loadLogsPath();
}, []);
// Load log retention setting on mount
useEffect(() => {
const loadRetention = async () => {
try {
const days = await invoke<number>('get_log_retention_days');
setLogRetentionDays(days);
} catch (err) {
console.error('Failed to load log retention setting:', err);
}
};
loadRetention();
}, []);
// Load startup settings on mount
useEffect(() => {
const loadStartupSettings = async () => {
try {
const settings = await invoke<StartupSettings>('get_startup_settings');
setStartupSettings(settings);
} catch (error) {
console.error('Failed to load startup settings:', error);
} finally {
setLoadingSettings(false);
}
};
loadStartupSettings();
}, []);
// Save startup settings when they change
const updateStartupSetting = async (
key: keyof StartupSettings,
value: boolean
) => {
console.log(`[Settings] Updating ${key} to ${value}`);
// Save old state for rollback
const oldSettings = { ...startupSettings };
const newSettings = { ...startupSettings, [key]: value };
// Update UI immediately for better UX
setStartupSettings(newSettings);
setSavingSettings(true);
try {
console.log('[Settings] Invoking update_startup_settings:', newSettings);
await invoke('update_startup_settings', { settings: newSettings });
console.log('[Settings] Successfully saved:', newSettings);
// Show success toast
success('Settings saved', 'Your preferences have been updated');
} catch (err) {
console.error('[Settings] Failed to save:', err);
// Show error toast
const errorMessage = err instanceof Error ? err.message : 'Unknown error';
error('Failed to save settings', errorMessage);
// Revert on error
setStartupSettings(oldSettings);
} finally {
setSavingSettings(false);
}
};
const handleRetentionChange = async (days: number) => {
const oldDays = logRetentionDays;
setLogRetentionDays(days);
setSavingRetention(true);
try {
await invoke('set_log_retention_days', { days });
success('Settings saved', `Log retention set to ${days === 0 ? 'keep forever' : `${days} days`}`);
} catch (err) {
const errorMessage = err instanceof Error ? err.message : 'Unknown error';
error('Failed to save setting', errorMessage);
setLogRetentionDays(oldDays);
} finally {
setSavingRetention(false);
}
};
const handleOpenLogs = async () => {
setOpeningLogs(true);
try {
await invoke('open_logs_folder');
} catch (error) {
console.error('Failed to open logs folder:', error);
} finally {
setOpeningLogs(false);
}
};
return (
<>
<ToastContainer toasts={toasts} onClose={(id) => toasts.find(t => t.id === id)?.onClose(id)} />
<div className="space-y-6">
<div>
<h1 className="text-2xl font-bold">Settings</h1>
<p className="text-[rgb(var(--muted))]">Configure McpMux preferences.</p>
</div>
{/* Updates Section */}
<UpdateChecker />
{/* Startup & System Tray Section - always show toggles so e2e and slow backends see the section */}
<Card data-testid="settings-startup-section">
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Power className="h-5 w-5" />
Startup & System Tray
</CardTitle>
<CardDescription>
Control how McpMux starts and behaves with the system tray.
</CardDescription>
</CardHeader>
<CardContent>
{loadingSettings ? (
<div className="flex items-center gap-2 text-sm text-[rgb(var(--muted))] mb-4">
<Loader2 className="h-4 w-4 animate-spin" />
Loading…
</div>
) : null}
<div className="space-y-6">
<div className="flex items-center justify-between gap-4">
<div className="flex items-start gap-3 flex-1 min-w-0">
<Power className="h-5 w-5 mt-0.5 text-[rgb(var(--muted))] flex-shrink-0" />
<div>
<label className="text-sm font-medium">Launch at Startup</label>
<p className="text-xs text-[rgb(var(--muted))] mt-1">
Start McpMux automatically when you log in to your system
</p>
</div>
</div>
<Switch
checked={startupSettings.autoLaunch}
onCheckedChange={(checked) => {
console.log('Auto-launch toggled:', checked);
updateStartupSetting('autoLaunch', checked);
}}
disabled={savingSettings}
data-testid="auto-launch-switch"
/>
</div>
<div className="flex items-center justify-between gap-4">
<div className="flex items-start gap-3 flex-1 min-w-0">
<Minimize2 className="h-5 w-5 mt-0.5 text-[rgb(var(--muted))] flex-shrink-0" />
<div>
<label className="text-sm font-medium">Start Minimized</label>
<p className="text-xs text-[rgb(var(--muted))] mt-1">
Launch in background to system tray (requires auto-launch enabled)
</p>
</div>
</div>
<Switch
checked={startupSettings.startMinimized}
onCheckedChange={(checked) => {
console.log('Start minimized toggled:', checked);
updateStartupSetting('startMinimized', checked);
}}
disabled={savingSettings || !startupSettings.autoLaunch}
data-testid="start-minimized-switch"
/>
</div>
<div className="flex items-center justify-between gap-4">
<div className="flex items-start gap-3 flex-1 min-w-0">
<XCircle className="h-5 w-5 mt-0.5 text-[rgb(var(--muted))] flex-shrink-0" />
<div>
<label className="text-sm font-medium">Close to Tray</label>
<p className="text-xs text-[rgb(var(--muted))] mt-1">
Keep running in system tray when window is closed (use "Quit" from tray to exit)
</p>
</div>
</div>
<Switch
checked={startupSettings.closeToTray}
onCheckedChange={(checked) => {
console.log('Close to tray toggled:', checked);
updateStartupSetting('closeToTray', checked);
}}
disabled={savingSettings}
data-testid="close-to-tray-switch"
/>
</div>
{savingSettings && (
<div className="flex items-center gap-2 text-sm text-[rgb(var(--muted))]">
<Loader2 className="h-4 w-4 animate-spin" />
Saving settings...
</div>
)}
</div>
</CardContent>
</Card>
{/* Appearance Section */}
<Card>
<CardHeader>
<CardTitle>Appearance</CardTitle>
<CardDescription>Customize the look and feel of McpMux.</CardDescription>
</CardHeader>
<CardContent>
<div className="space-y-4">
<div>
<label className="text-sm font-medium">Theme</label>
<div className="flex gap-2 mt-2" data-testid="theme-buttons">
<Button
variant={theme === 'light' ? 'primary' : 'secondary'}
size="sm"
onClick={() => setTheme('light')}
data-testid="theme-light-btn"
>
<Sun className="h-4 w-4 mr-2" />
Light
</Button>
<Button
variant={theme === 'dark' ? 'primary' : 'secondary'}
size="sm"
onClick={() => setTheme('dark')}
data-testid="theme-dark-btn"
>
<Moon className="h-4 w-4 mr-2" />
Dark
</Button>
<Button
variant={theme === 'system' ? 'primary' : 'secondary'}
size="sm"
onClick={() => setTheme('system')}
data-testid="theme-system-btn"
>
<Monitor className="h-4 w-4 mr-2" />
System
</Button>
</div>
</div>
</div>
</CardContent>
</Card>
{/* Analytics Section */}
<Card data-testid="settings-analytics-section">
<CardHeader>
<CardTitle className="flex items-center gap-2">
<BarChart3 className="h-5 w-5" />
Analytics
</CardTitle>
<CardDescription>
Help improve McpMux by sharing anonymous usage data. No personal information is collected.
</CardDescription>
</CardHeader>
<CardContent>
<div className="flex items-center justify-between gap-4">
<div className="flex items-start gap-3 flex-1 min-w-0">
<BarChart3 className="h-5 w-5 mt-0.5 text-[rgb(var(--muted))] flex-shrink-0" />
<div>
<label className="text-sm font-medium">Share Usage Data</label>
<p className="text-xs text-[rgb(var(--muted))] mt-1">
Sends anonymous data like app version, OS, and feature usage to help us prioritize improvements.
Location is approximated from IP by PostHog. No credentials or server configurations are shared.
</p>
</div>
</div>
<Switch
checked={analyticsEnabled}
onCheckedChange={setAnalyticsEnabled}
data-testid="analytics-switch"
/>
</div>
</CardContent>
</Card>
{/* Logs Section */}
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<FileText className="h-5 w-5" />
Logs
</CardTitle>
<CardDescription>View application logs for debugging and troubleshooting.</CardDescription>
</CardHeader>
<CardContent>
<div className="space-y-4">
<div>
<label className="text-sm font-medium">Log Files Location</label>
<p className="text-sm text-[rgb(var(--muted))] mt-1 font-mono bg-surface-secondary rounded px-2 py-1" data-testid="logs-path">
{logsPath || 'Loading...'}
</p>
</div>
<div className="flex items-center gap-2">
<Button
variant="secondary"
size="sm"
onClick={handleOpenLogs}
disabled={openingLogs}
data-testid="open-logs-btn"
>
{openingLogs ? (
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
) : (
<FolderOpen className="h-4 w-4 mr-2" />
)}
Open Logs Folder
</Button>
</div>
<div className="border-t border-[rgb(var(--border))] pt-4">
<div className="flex items-center justify-between gap-4">
<div className="flex items-start gap-3 flex-1 min-w-0">
<Trash2 className="h-5 w-5 mt-0.5 text-[rgb(var(--muted))] flex-shrink-0" />
<div>
<label className="text-sm font-medium">Auto-Cleanup</label>
<p className="text-xs text-[rgb(var(--muted))] mt-1">
Automatically delete log files older than the selected period
</p>
</div>
</div>
<select
value={logRetentionDays}
onChange={(e) => handleRetentionChange(Number(e.target.value))}
disabled={savingRetention}
className="px-3 py-1.5 text-sm border border-[rgb(var(--border))] rounded-lg bg-[rgb(var(--surface))] text-[rgb(var(--foreground))]"
data-testid="log-retention-select"
>
<option value={7}>7 days</option>
<option value={14}>14 days</option>
<option value={30}>30 days</option>
<option value={60}>60 days</option>
<option value={90}>90 days</option>
<option value={0}>Keep forever</option>
</select>
</div>
</div>
<p className="text-xs text-[rgb(var(--muted))]">
Logs are rotated daily. Each file contains detailed debug information including thread IDs and source locations.
</p>
</div>
</CardContent>
</Card>
</div>
</>
);
}