import { useCallback, useState } from "react"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { useNavigate } from "react-router"; import { toast } from "sonner"; import { ChevronDown, FileIcon, FolderOpen, RotateCcw } from "lucide-react"; import { Button } from "~/client/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "~/client/components/ui/card"; import { Checkbox } from "~/client/components/ui/checkbox"; import { Input } from "~/client/components/ui/input"; import { Label } from "~/client/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "~/client/components/ui/select"; import { PathSelector } from "~/client/components/path-selector"; import { FileTree } from "~/client/components/file-tree"; import { listSnapshotFilesOptions, restoreSnapshotMutation } from "~/client/api-client/@tanstack/react-query.gen"; import { useFileBrowser } from "~/client/hooks/use-file-browser"; import { OVERWRITE_MODES, type OverwriteMode } from "~/schemas/restic"; import type { Snapshot } from "~/client/lib/types"; type RestoreLocation = "original" | "custom"; interface RestoreFormProps { snapshot: Snapshot; repositoryName: string; snapshotId: string; returnPath: string; } export function RestoreForm({ snapshot, repositoryName, snapshotId, returnPath }: RestoreFormProps) { const navigate = useNavigate(); const queryClient = useQueryClient(); const volumeBasePath = snapshot.paths[0]?.match(/^(.*?_data)(\/|$)/)?.[1] || "/"; const [restoreLocation, setRestoreLocation] = useState("original"); const [customTargetPath, setCustomTargetPath] = useState(""); const [overwriteMode, setOverwriteMode] = useState("always"); const [showAdvanced, setShowAdvanced] = useState(false); const [excludeXattr, setExcludeXattr] = useState(""); const [deleteExtraFiles, setDeleteExtraFiles] = useState(false); const [selectedPaths, setSelectedPaths] = useState>(new Set()); const { data: filesData, isLoading: filesLoading } = useQuery({ ...listSnapshotFilesOptions({ path: { name: repositoryName, snapshotId }, query: { path: volumeBasePath }, }), enabled: !!repositoryName && !!snapshotId, }); const stripBasePath = useCallback( (path: string): string => { if (!volumeBasePath) return path; if (path === volumeBasePath) return "/"; if (path.startsWith(`${volumeBasePath}/`)) { const stripped = path.slice(volumeBasePath.length); return stripped; } return path; }, [volumeBasePath], ); const addBasePath = useCallback( (displayPath: string): string => { const vbp = volumeBasePath === "/" ? "" : volumeBasePath; if (!vbp) return displayPath; if (displayPath === "/") return vbp; return `${vbp}${displayPath}`; }, [volumeBasePath], ); const fileBrowser = useFileBrowser({ initialData: filesData, isLoading: filesLoading, fetchFolder: async (path) => { return await queryClient.ensureQueryData( listSnapshotFilesOptions({ path: { name: repositoryName, snapshotId }, query: { path }, }), ); }, prefetchFolder: (path) => { queryClient.prefetchQuery( listSnapshotFilesOptions({ path: { name: repositoryName, snapshotId }, query: { path }, }), ); }, pathTransform: { strip: stripBasePath, add: addBasePath, }, }); const { mutate: restoreSnapshot, isPending: isRestoring } = useMutation({ ...restoreSnapshotMutation(), onSuccess: (data) => { toast.success("Restore completed", { description: `Successfully restored ${data.filesRestored} file(s). ${data.filesSkipped} file(s) skipped.`, }); navigate(returnPath); }, onError: (error) => { toast.error("Restore failed", { description: error.message || "Failed to restore snapshot" }); }, }); const handleRestore = useCallback(() => { if (!repositoryName || !snapshotId) return; const excludeXattrArray = excludeXattr ?.split(",") .map((s) => s.trim()) .filter(Boolean); const isCustomLocation = restoreLocation === "custom"; const targetPath = isCustomLocation && customTargetPath.trim() ? customTargetPath.trim() : undefined; const pathsArray = Array.from(selectedPaths); const includePaths = pathsArray.map((path) => addBasePath(path)); restoreSnapshot({ path: { name: repositoryName }, body: { snapshotId, include: includePaths.length > 0 ? includePaths : undefined, delete: deleteExtraFiles, excludeXattr: excludeXattrArray && excludeXattrArray.length > 0 ? excludeXattrArray : undefined, targetPath, overwrite: overwriteMode, }, }); }, [ repositoryName, snapshotId, excludeXattr, restoreLocation, customTargetPath, selectedPaths, addBasePath, deleteExtraFiles, overwriteMode, restoreSnapshot, ]); const canRestore = restoreLocation === "original" || customTargetPath.trim(); return (

Restore Snapshot

{repositoryName} / {snapshotId}

Restore Location Choose where to restore the files
{restoreLocation === "custom" && (

Files will be restored directly to this path

)}
Overwrite Mode How to handle existing files

{overwriteMode === OVERWRITE_MODES.always && "Existing files will always be replaced with the snapshot version."} {overwriteMode === OVERWRITE_MODES.ifChanged && "Files are only replaced if their content differs from the snapshot."} {overwriteMode === OVERWRITE_MODES.ifNewer && "Files are only replaced if the snapshot version has a newer modification time."} {overwriteMode === OVERWRITE_MODES.never && "Existing files will never be replaced, only missing files are restored."}

setShowAdvanced(!showAdvanced)}>
Advanced options
{showAdvanced && (
setExcludeXattr(e.target.value)} />

Exclude specific extended attributes during restore (comma-separated)

setDeleteExtraFiles(checked === true)} />
)}
Select Files to Restore {selectedPaths.size > 0 ? `${selectedPaths.size} ${selectedPaths.size === 1 ? "item" : "items"} selected` : "Select specific files or folders, or leave empty to restore everything"} {fileBrowser.isLoading && (

Loading files...

)} {fileBrowser.isEmpty && (

No files in this snapshot

)} {!fileBrowser.isLoading && !fileBrowser.isEmpty && (
)}
); }