import { useState, useEffect, useCallback } from "react"; import { AnimatedModal } from "../ui/AnimatedModal"; import { Button } from "../ui/Button"; import { BackupProgress } from "./BackupProgress"; import { useBackupStore } from "../../stores/backupStore"; import { useNotificationStore } from "../../stores/notificationStore"; import { detectPgTools, pgRestore } from "../../lib/commands"; import type { PgToolStatus } from "../../lib/types"; interface RestoreDialogProps { open: boolean; connectionId: string; onClose: () => void; } const PLATFORM_INSTALL_INSTRUCTIONS: Record = { darwin: "brew install libpq", linux: "sudo apt install postgresql-client # Debian/Ubuntu\nsudo dnf install postgresql # Fedora\nsudo pacman -S postgresql # Arch", win32: "Download PostgreSQL installer from https://www.postgresql.org/download/windows/ and ensure pg_restore is in your PATH.", }; function getPlatformInstructions(): string { const platform = typeof navigator !== "undefined" ? navigator.platform.toLowerCase() : ""; if (platform.includes("mac") || platform.includes("darwin")) return PLATFORM_INSTALL_INSTRUCTIONS.darwin; if (platform.includes("linux")) return PLATFORM_INSTALL_INSTRUCTIONS.linux; if (platform.includes("win")) return PLATFORM_INSTALL_INSTRUCTIONS.win32; return PLATFORM_INSTALL_INSTRUCTIONS.linux; } export function RestoreDialog({ open, connectionId, onClose }: RestoreDialogProps) { const [filePath, setFilePath] = useState(""); const [format, setFormat] = useState("custom"); const [clean, setClean] = useState(true); const [schema, setSchema] = useState(""); const [confirmed, setConfirmed] = useState(false); const [toolStatus, setToolStatus] = useState(null); const [checkingTools, setCheckingTools] = useState(false); const [running, setRunning] = useState(false); const activeJobId = useBackupStore((s) => s.activeJobId); const jobs = useBackupStore((s) => s.jobs); const startJob = useBackupStore((s) => s.startJob); const notify = useNotificationStore((s) => s.notify); const activeJob = jobs.find((j) => j.id === activeJobId); useEffect(() => { if (!open) return; setCheckingTools(true); setConfirmed(false); detectPgTools() .then((status) => setToolStatus(status)) .catch(() => setToolStatus({ pg_dump_found: false, pg_restore_found: false, pg_dump_version: null, pg_restore_version: null })) .finally(() => setCheckingTools(false)); }, [open]); const handlePickFile = useCallback(async () => { try { const { open: openDialog } = await import("@tauri-apps/plugin-dialog"); const picked = await openDialog({ multiple: false, filters: [{ name: "Backup Files", extensions: ["dump", "sql", "tar", "custom", "gz"] }], }); if (picked && typeof picked === "string") setFilePath(picked); } catch { // dialog not available (non-Tauri env), use manual path input } }, []); const handleStartRestore = useCallback(async () => { if (!filePath) { notify("Please select a file path", "error"); return; } setRunning(true); const jobId = `restore-${Date.now()}`; startJob(jobId, "restore"); try { await pgRestore(connectionId, { format, filePath, clean, schema: schema || undefined, }); notify("Restore completed successfully", "success"); onClose(); } catch (e) { const msg = e instanceof Error ? e.message : String(e); notify(`Restore failed: ${parseError(msg)}`, "error"); } finally { setRunning(false); } }, [filePath, format, clean, schema, connectionId, startJob, notify, onClose]); const toolsMissing = toolStatus && !toolStatus.pg_restore_found; const canStart = filePath && confirmed && !running; return (

Restore Database

{checkingTools && (

Checking for pg_restore...

)} {toolsMissing && (

pg_restore not found

The PostgreSQL client tools are required for backup/restore operations. Install them using:

              {getPlatformInstructions()}
            
)} {!checkingTools && !toolsMissing && (
{/* File path */}
setFilePath(e.target.value)} placeholder="/path/to/backup.dump" className="flex-1 rounded-full bg-surface border border-border px-4 py-2 text-sm text-text placeholder-text-muted/60 focus:outline-none focus:border-accent focus:ring-1 focus:ring-accent/50 transition-colors" />
{/* Format */}
{/* Schema filter */}
setSchema(e.target.value)} placeholder="public" className="w-full rounded-full bg-surface border border-border px-4 py-2 text-sm text-text placeholder-text-muted/60 focus:outline-none focus:border-accent focus:ring-1 focus:ring-accent/50 transition-colors" />
{/* Clean toggle */} {/* Destructive confirmation */}
{/* Progress */} {activeJob?.status === "running" && ( )} {/* Actions */}
)}
); } function parseError(msg: string): string { if (msg.includes("pg_restore:")) { const [, ...rest] = msg.split("pg_restore:"); return rest.join(":").trim() || msg; } if (msg.includes("No such file or directory")) { return `File not found. Check the path and try again.`; } if (msg.includes("Permission denied")) { return `Permission denied. Check file permissions.`; } return msg; }