import { useState } from "react"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { Calendar, Clock, Database, FolderTree, HardDrive, Trash2, X } from "lucide-react"; import { 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 type { ListSnapshotsResponse } from "../api-client"; import { deleteSnapshotMutation } from "~/client/api-client/@tanstack/react-query.gen"; import { parseError } from "~/client/lib/errors"; type Snapshot = ListSnapshotsResponse[number]; type Props = { snapshots: Snapshot[]; repositoryName: string; }; export const SnapshotsTable = ({ snapshots, repositoryName }: 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 Date & Time Size Duration Paths Actions {snapshots.map((snapshot) => ( handleRowClick(snapshot.short_id)} >
{snapshot.short_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 ); };