import { Check, Cloud, Download, Github, Loader2, Moon, Palette, Sun, TerminalSquare, Upload, } from "lucide-react"; import React, { useState } from "react"; import { Host, SSHKey, Snippet } from "../domain/models"; import { TERMINAL_THEMES } from "../infrastructure/config/terminalThemes"; import { loadFromGist, syncToGist, } from "../infrastructure/services/syncService"; import { cn } from "../lib/utils"; import { SyncConfig } from "../types"; import { Button } from "./ui/button"; 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; } const COLORS = [ { name: "Blue", value: "221.2 83.2% 53.3%" }, { name: "Violet", value: "262.1 83.3% 57.8%" }, { name: "Rose", value: "346.8 77.2% 49.8%" }, { name: "Orange", value: "24.6 95% 53.1%" }, { name: "Green", value: "142.1 76.2% 36.3%" }, ]; const SettingsDialog: React.FC = ({ isOpen, onClose, onImport, exportData, theme, onThemeChange, primaryColor, onPrimaryColorChange, syncConfig, onSyncConfigChange, terminalThemeId, onTerminalThemeChange, }) => { const [importText, setImportText] = useState(""); // Sync State const [githubToken, setGithubToken] = useState(syncConfig?.githubToken || ""); const [gistId, setGistId] = useState(syncConfig?.gistId || ""); const [isSyncing, setIsSyncing] = useState(false); const [syncStatus, setSyncStatus] = useState<"idle" | "success" | "error">( "idle", ); 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); alert("Configuration imported successfully!"); setImportText(""); } catch { alert("Invalid JSON format."); } }; const handleSaveSyncConfig = async () => { if (!githubToken) return; setIsSyncing(true); setSyncStatus("idle"); try { if (gistId) { await loadFromGist(githubToken, gistId); } onSyncConfigChange({ githubToken, gistId }); setSyncStatus("success"); } catch (e) { console.error(e); setSyncStatus("error"); alert("Failed to verify Gist or Token."); } finally { setIsSyncing(false); } }; const performSyncUpload = async () => { if (!githubToken) return; setIsSyncing(true); try { const data = exportData() as { keys: SSHKey[]; hosts: Host[]; snippets: Snippet[]; customGroups: string[]; }; const newGistId = await syncToGist( githubToken, gistId || undefined, data, ); if (!gistId) { setGistId(newGistId); onSyncConfigChange({ githubToken, gistId: newGistId, lastSync: Date.now(), }); } else { onSyncConfigChange({ ...syncConfig!, lastSync: Date.now() }); } alert("Backup uploaded to Gist successfully!"); } catch (e) { alert("Upload failed: " + e); } finally { setIsSyncing(false); } }; const performSyncDownload = async () => { if (!githubToken || !gistId) return; setIsSyncing(true); try { const data = await loadFromGist(githubToken, gistId); onImport(JSON.stringify(data)); onSyncConfigChange({ ...syncConfig!, lastSync: Date.now() }); alert("Configuration restored from Gist!"); } catch (e) { alert("Download failed: " + e); } finally { setIsSyncing(false); } }; const getHslStyle = (hsl: string) => ({ backgroundColor: `hsl(${hsl})` }); return ( Settings Configure appearance, terminal theme, sync and data options. {/* Sidebar using TabsList */}

Settings

Appearance Terminal Sync & Cloud Data Management
{/* Content Area */}

UI Theme

onThemeChange("light")} icon={} label="Light" /> onThemeChange("dark")} icon={} label="Dark" />

Accent Color

{COLORS.map((c) => ( ))}

Terminal Themes

{TERMINAL_THEMES.map((t) => (
onTerminalThemeChange(t.id)} className={cn( "cursor-pointer border-2 rounded-lg p-3 flex items-center gap-4 transition-all hover:bg-muted/10", terminalThemeId === t.id ? "border-primary bg-primary/5" : "border-border hover:border-primary/50", )} > {/* Mini Terminal Preview */}
{t.name}
{[ t.colors.black, t.colors.red, t.colors.green, t.colors.blue, ].map((c, i) => (
))}
))}

GitHub Gist Sync

Backup and sync your hosts, keys, and snippets across devices securely using a private GitHub Gist.

setGithubToken(e.target.value)} className="font-mono" />

Token needs gist scope.

setGistId(e.target.value)} className="font-mono" />
{syncConfig?.githubToken && ( <>
{syncConfig.lastSync && (

Last Sync:{" "} {new Date(syncConfig.lastSync).toLocaleString()}

)} )}

Export Data

Download a JSON file containing all your hosts, keys, and snippets.

Import Data

Restore your configuration from a previously exported JSON file.