import { useState } from "react"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { Calendar, Clock, Database, FolderTree, HardDrive, 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 { Tooltip, TooltipContent, TooltipTrigger } from "~/client/components/ui/tooltip"; 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"; type Props = { snapshots: Snapshot[]; backups: BackupSchedule[]; repositoryName: string; }; export const SnapshotsTable = ({ snapshots, repositoryName, 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: { name: repositoryName, snapshotId: snapshotToDelete }, }), { loading: "Deleting snapshot...", success: "Snapshot deleted successfully", error: (error) => parseError(error)?.message || "Failed to delete snapshot", }, ); } }; const handleRowClick = (snapshotId: string) => { navigate(`/repositories/${repositoryName}/${snapshotId}`); }; return ( <>
Snapshot ID Schedule Date & Time Size Duration Paths Actions {snapshots.map((snapshot) => { const backupIds = snapshot.tags.map(Number).filter((tag) => !Number.isNaN(tag)); const backup = backups.find((b) => backupIds.includes(b.id)); return ( handleRowClick(snapshot.short_id)} >
{snapshot.short_id}
e.stopPropagation()} className="hover:underline" > {backup ? backup.id : "-"}
{new Date(snapshot.time).toLocaleString()}
{formatDuration(snapshot.duration / 1000)}
{snapshot.paths.length} {snapshot.paths.length === 1 ? "path" : "paths"}
{snapshot.paths.map((path) => (
{path}
))}
); })}
Delete snapshot? This action cannot be undone. This will permanently delete the snapshot and all its data from the repository. Cancel Delete snapshot ); };