import { useMutation } from "@tanstack/react-query"; import { RotateCcw } from "lucide-react"; import { useId, useState } from "react"; import { toast } from "sonner"; import { restoreSnapshotMutation } from "~/client/api-client/@tanstack/react-query.gen"; import { parseError } from "~/client/lib/errors"; import { Button } from "~/client/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "~/client/components/ui/dialog"; import { ScrollArea } from "~/client/components/ui/scroll-area"; import { RestoreSnapshotForm, type RestoreSnapshotFormValues } from "./restore-snapshot-form"; type Props = { name: string; snapshotId: string; }; export const RestoreSnapshotDialog = ({ name, snapshotId }: Props) => { const [open, setOpen] = useState(false); const formId = useId(); const restore = useMutation({ ...restoreSnapshotMutation(), onSuccess: (data) => { toast.success("Snapshot restored successfully", { description: `${data.filesRestored} files restored, ${data.filesSkipped} files skipped`, }); setOpen(false); }, onError: (error) => { toast.error("Failed to restore snapshot", { description: parseError(error)?.message, }); }, }); const handleSubmit = (values: RestoreSnapshotFormValues) => { const include = values.include ?.split(",") .map((s) => s.trim()) .filter(Boolean); const exclude = values.exclude ?.split(",") .map((s) => s.trim()) .filter(Boolean); restore.mutate({ path: { name }, body: { snapshotId, include: include && include.length > 0 ? include : undefined, exclude: exclude && exclude.length > 0 ? exclude : undefined, }, }); }; return ( Restore Snapshot Restore snapshot {snapshotId.substring(0, 8)} to a local filesystem path ); };