import { Check, Cloud, Download, Github, Keyboard, Loader2, Moon, Palette, RotateCcw, Sun, TerminalSquare, Upload, X, } from "lucide-react"; import React, { useCallback, useEffect, useRef, useState } from "react"; import { useSyncState } from "../application/state/useSyncState"; import { Host, SSHKey, Snippet, TerminalSettings, CursorShape, RightClickBehavior, LinkModifier, DEFAULT_TERMINAL_SETTINGS, HotkeyScheme, KeyBinding, keyEventToString } from "../domain/models"; import { TERMINAL_THEMES } from "../infrastructure/config/terminalThemes"; import { TERMINAL_FONTS, MIN_FONT_SIZE, MAX_FONT_SIZE } from "../infrastructure/config/fonts"; import { logger } from "../lib/logger"; import { cn } from "../lib/utils"; import { SyncConfig } from "../types"; import { Button } from "./ui/button"; import { toast } from "./ui/toast"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "./ui/dialog"; import { Input } from "./ui/input"; import { Label } from "./ui/label"; import { ScrollArea } from "./ui/scroll-area"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "./ui/tabs"; import { Textarea } from "./ui/textarea"; interface SettingsDialogProps { isOpen: boolean; onClose: () => void; onImport: (data: string) => void; exportData: () => unknown; theme: "dark" | "light"; onThemeChange: (theme: "dark" | "light") => void; primaryColor: string; onPrimaryColorChange: (color: string) => void; syncConfig: SyncConfig | null; onSyncConfigChange: (config: SyncConfig | null) => void; terminalThemeId: string; onTerminalThemeChange: (id: string) => void; terminalFontFamilyId?: string; onTerminalFontFamilyChange?: (id: string) => void; terminalFontSize?: number; onTerminalFontSizeChange?: (size: number) => void; terminalSettings?: TerminalSettings; onTerminalSettingsChange?: (key: K, value: TerminalSettings[K]) => void; hotkeyScheme?: HotkeyScheme; onHotkeySchemeChange?: (scheme: HotkeyScheme) => void; keyBindings?: KeyBinding[]; onUpdateKeyBinding?: (bindingId: string, scheme: 'mac' | 'pc', newKey: string) => void; onResetKeyBinding?: (bindingId: string, scheme?: 'mac' | 'pc') => void; onResetAllKeyBindings?: () => void; customCSS?: string; onCustomCSSChange?: (css: string) => void; } // More comprehensive color palette const COLORS = [ // Blues { name: "Sky Blue", value: "199 89% 48%" }, { name: "Blue", value: "221.2 83.2% 53.3%" }, { name: "Indigo", value: "234 89% 62%" }, // Purples { name: "Violet", value: "262.1 83.3% 57.8%" }, { name: "Purple", value: "271 81% 56%" }, { name: "Fuchsia", value: "292 84% 61%" }, // Pinks & Reds { name: "Pink", value: "330 81% 60%" }, { name: "Rose", value: "346.8 77.2% 49.8%" }, { name: "Red", value: "0 72% 51%" }, // Oranges & Yellows { name: "Orange", value: "24.6 95% 53.1%" }, { name: "Amber", value: "38 92% 50%" }, { name: "Yellow", value: "48 96% 53%" }, // Greens { name: "Lime", value: "84 81% 44%" }, { name: "Green", value: "142.1 76.2% 36.3%" }, { name: "Emerald", value: "160 84% 39%" }, { name: "Teal", value: "173 80% 40%" }, { name: "Cyan", value: "186 94% 42%" }, // Neutrals { name: "Slate", value: "215 20% 55%" }, ]; const FONT_WEIGHTS = [ { value: 100, label: "100 - Thin" }, { value: 200, label: "200 - Extra Light" }, { value: 300, label: "300 - Light" }, { value: 400, label: "400 - Normal" }, { value: 500, label: "500 - Medium" }, { value: 600, label: "600 - Semi Bold" }, { value: 700, label: "700 - Bold" }, { value: 800, label: "800 - Extra Bold" }, { value: 900, label: "900 - Black" }, ]; // Setting row component interface SettingRowProps { label: string; description?: string; children: React.ReactNode; } const SettingRow: React.FC = ({ label, description, children }) => (
{label}
{description && (
{description}
)}
{children}
); // Toggle switch component interface ToggleProps { checked: boolean; onChange: (checked: boolean) => void; } const Toggle: React.FC = ({ checked, onChange }) => ( ); // Select dropdown component interface SelectOption { value: T; label: string; } interface SelectProps { value: T; options: SelectOption[]; onChange: (value: T) => void; className?: string; } function Select({ value, options, onChange, className }: SelectProps) { return ( ); } // Section header component const SectionHeader: React.FC<{ title: string }> = ({ title }) => (

{title}

); // Divider component const Divider: React.FC = () =>
; // Tab title mapping const TAB_TITLES: Record = { appearance: "Appearance", terminal: "Terminal", shortcuts: "Shortcuts", sync: "Sync & Cloud", data: "Data", }; // Settings Tab Content wrapper with fixed header interface SettingsTabContentProps { value: string; children: React.ReactNode; className?: string; } const SettingsTabContent: React.FC = ({ value, children, className, }) => ( {/* Fixed Header */}

{TAB_TITLES[value] || value}

{/* Scrollable Content */}
{children}
); const SettingsDialog: React.FC = ({ isOpen, onClose, onImport, exportData, theme, onThemeChange, primaryColor, onPrimaryColorChange, syncConfig, onSyncConfigChange, terminalThemeId, onTerminalThemeChange, terminalFontFamilyId = "menlo", onTerminalFontFamilyChange, terminalFontSize = 14, onTerminalFontSizeChange, terminalSettings = DEFAULT_TERMINAL_SETTINGS, onTerminalSettingsChange, hotkeyScheme = "mac", onHotkeySchemeChange, keyBindings = [], onUpdateKeyBinding, onResetKeyBinding, onResetAllKeyBindings, customCSS = "", onCustomCSSChange, }) => { const [importText, setImportText] = useState(""); const [editingBindingId, setEditingBindingId] = useState(null); const [recordedKey, setRecordedKey] = useState(""); const keyRecordRef = useRef(null); // Sync State const [githubToken, setGithubToken] = useState(syncConfig?.githubToken || ""); const [gistId, setGistId] = useState(syncConfig?.gistId || ""); const { isSyncing, syncStatus, resetSyncStatus, verify, upload, download } = useSyncState(); const isMac = hotkeyScheme === 'mac'; useEffect(() => { if (isOpen) resetSyncStatus(); }, [isOpen, resetSyncStatus]); // Handle key recording for shortcut editing const handleKeyDown = useCallback((e: KeyboardEvent) => { if (!editingBindingId) return; e.preventDefault(); e.stopPropagation(); const keyStr = keyEventToString(e, isMac); // If it's just modifier keys, keep waiting if (['Meta', 'Control', 'Alt', 'Shift'].includes(e.key)) { setRecordedKey(keyStr); return; } // Complete key combination recorded setRecordedKey(keyStr); }, [editingBindingId, isMac]); const handleKeyUp = useCallback((e: KeyboardEvent) => { if (!editingBindingId || !recordedKey) return; // If we have a complete key combo (not just modifiers), save it if (!['Meta', 'Control', 'Alt', 'Shift'].includes(e.key)) { // Recorded key is complete, will be saved on blur or confirm } }, [editingBindingId, recordedKey]); // Start editing a binding const startEditing = useCallback((bindingId: string) => { setEditingBindingId(bindingId); setRecordedKey(''); // Focus will trigger key listening setTimeout(() => { keyRecordRef.current?.focus(); }, 0); }, []); // Save the recorded key const saveRecordedKey = useCallback(() => { if (editingBindingId && recordedKey && onUpdateKeyBinding) { const scheme = hotkeyScheme === 'mac' ? 'mac' : 'pc'; onUpdateKeyBinding(editingBindingId, scheme, recordedKey); } setEditingBindingId(null); setRecordedKey(''); }, [editingBindingId, recordedKey, hotkeyScheme, onUpdateKeyBinding]); // Cancel editing const cancelEditing = useCallback(() => { setEditingBindingId(null); setRecordedKey(''); }, []); // Disable this binding const disableBinding = useCallback((bindingId?: string) => { const targetId = bindingId || editingBindingId; if (targetId && onUpdateKeyBinding) { const scheme = hotkeyScheme === 'mac' ? 'mac' : 'pc'; onUpdateKeyBinding(targetId, scheme, 'Disabled'); } setEditingBindingId(null); setRecordedKey(''); }, [editingBindingId, hotkeyScheme, onUpdateKeyBinding]); // Listen for keydown when editing useEffect(() => { if (editingBindingId) { window.addEventListener('keydown', handleKeyDown, true); window.addEventListener('keyup', handleKeyUp, true); return () => { window.removeEventListener('keydown', handleKeyDown, true); window.removeEventListener('keyup', handleKeyUp, true); }; } }, [editingBindingId, handleKeyDown, handleKeyUp]); const handleManualExport = () => { const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportData(), null, 2)); const downloadAnchorNode = document.createElement("a"); downloadAnchorNode.setAttribute("href", dataStr); downloadAnchorNode.setAttribute("download", "netcatty_backup.json"); document.body.appendChild(downloadAnchorNode); downloadAnchorNode.click(); downloadAnchorNode.remove(); }; const handleManualImport = () => { try { JSON.parse(importText); onImport(importText); toast.success("Configuration imported successfully!"); setImportText(""); } catch { toast.error("Invalid JSON format."); } }; const handleSaveSyncConfig = async () => { if (!githubToken) return; try { await verify(githubToken, gistId || undefined); onSyncConfigChange({ githubToken, gistId }); } catch (e) { logger.error(e); toast.error("Failed to verify Gist or Token."); } }; const performSyncUpload = async () => { if (!githubToken) return; try { const data = exportData() as { keys: SSHKey[]; hosts: Host[]; snippets: Snippet[]; customGroups: string[]; }; const newGistId = await upload(githubToken, gistId || undefined, data); if (!gistId) { setGistId(newGistId); onSyncConfigChange({ githubToken, gistId: newGistId, lastSync: Date.now(), }); } else { onSyncConfigChange({ ...syncConfig!, lastSync: Date.now() }); } toast.success("Backup uploaded to Gist successfully!"); } catch (e) { toast.error(String(e), "Upload failed"); } }; const performSyncDownload = async () => { if (!githubToken || !gistId) return; try { const data = await download(githubToken, gistId); onImport(JSON.stringify(data)); onSyncConfigChange({ ...syncConfig!, lastSync: Date.now() }); toast.success("Configuration restored from Gist!"); } catch (e) { toast.error(String(e), "Download failed"); } }; const getHslStyle = (hsl: string) => ({ backgroundColor: `hsl(${hsl})` }); return ( Settings Configure appearance, terminal theme, sync and data options. {/* Sidebar using TabsList */}

Settings

Appearance Terminal Shortcuts Sync & Cloud Data
{/* Content Area */}
{/* Appearance Tab */}
onThemeChange(v ? "dark" : "light")} />
{COLORS.map((c) => ( ))} {/* Custom color picker */}

Add custom CSS to personalize the app appearance. Changes apply immediately.