import { useState } from "react"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { Calendar, Clock, Database, HardDrive, Server, Trash2 } from "lucide-react"; import { Link, useNavigate } from "react-router"; import { toast } from "sonner"; import { ByteSize } from "~/client/components/bytes-size"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "~/client/components/ui/table"; import { Button } from "~/client/components/ui/button"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "~/client/components/ui/alert-dialog"; import { formatDuration } from "~/utils/utils"; import { deleteSnapshotMutation } from "~/client/api-client/@tanstack/react-query.gen"; import { parseError } from "~/client/lib/errors"; import type { BackupSchedule, Snapshot } from "../lib/types"; import { cn } from "../lib/utils"; type Props = { snapshots: Snapshot[]; backups: BackupSchedule[]; repositoryId: string; }; export const SnapshotsTable = ({ snapshots, repositoryId, backups }: Props) => { const navigate = useNavigate(); const queryClient = useQueryClient(); const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); const [snapshotToDelete, setSnapshotToDelete] = useState(null); const deleteSnapshot = useMutation({ ...deleteSnapshotMutation(), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["listSnapshots"] }); setShowDeleteConfirm(false); setSnapshotToDelete(null); }, }); const handleDeleteClick = (e: React.MouseEvent, snapshotId: string) => { e.stopPropagation(); setSnapshotToDelete(snapshotId); setShowDeleteConfirm(true); }; const handleConfirmDelete = () => { if (snapshotToDelete) { toast.promise( deleteSnapshot.mutateAsync({ path: { id: repositoryId, snapshotId: snapshotToDelete }, }), { loading: "Deleting snapshot...", success: "Snapshot deleted successfully", error: (error) => parseError(error)?.message || "Failed to delete snapshot", }, ); } }; const handleRowClick = (snapshotId: string) => { navigate(`/repositories/${repositoryId}/${snapshotId}`); }; return ( <>
Snapshot ID Schedule Date & Time Size Duration Volume Actions {snapshots.map((snapshot) => { const backup = backups.find((b) => snapshot.tags.includes(b.shortId)); return ( handleRowClick(snapshot.short_id)} >
{snapshot.short_id}
e.stopPropagation()} className="hover:underline" > {backup ? backup.name : "-"}
{new Date(snapshot.time).toLocaleString()}
{formatDuration(snapshot.duration / 1000)}
e.stopPropagation()} className="hover:underline" > {backup ? backup.volume.name : "-"}
); })}
Delete snapshot? This action cannot be undone. This will permanently delete the snapshot and all its data from the repository. Cancel Delete snapshot ); };