import { useQuery } from "@tanstack/react-query"; import { ByteSize } from "~/client/components/bytes-size"; import { useFormatBytes } from "~/client/hooks/use-format-bytes"; import { useRootLoaderData } from "~/client/hooks/use-root-loader-data"; import { Card } from "~/client/components/ui/card"; import { Progress } from "~/client/components/ui/progress"; import { getBackupProgressOptions } from "~/client/api-client/@tanstack/react-query.gen"; import type { GetBackupProgressResponse } from "~/client/api-client/types.gen"; import { formatDuration } from "~/utils/utils"; type Props = { scheduleShortId: string; initialProgress: GetBackupProgressResponse; }; export const BackupProgressCard = ({ scheduleShortId, initialProgress }: Props) => { const formatBytes = useFormatBytes(); const { locale } = useRootLoaderData(); const { data: progress } = useQuery({ ...getBackupProgressOptions({ path: { shortId: scheduleShortId } }), initialData: initialProgress, refetchInterval: 1000, }); const { percent_done = 0, bytes_done = 0, total_bytes = 0, seconds_elapsed = 0, files_done = 0, total_files = 0, } = progress ?? {}; const percentDone = progress ? Math.round(percent_done * 100) : 0; const currentFile = progress?.current_files?.[0] || ""; const fileName = currentFile.split("/").pop() || currentFile; const speed = progress ? formatBytes(bytes_done / seconds_elapsed) : null; const eta = progress?.seconds_remaining ? formatDuration(progress.seconds_remaining) : null; return (
Backup in progress
{progress ? `${percentDone}%` : "—"}

Files

{progress ? ( <> {files_done.toLocaleString(locale)} / {total_files.toLocaleString(locale)} ) : ( "—" )}

Data

{progress ? ( <>  /  ) : ( "—" )}

Elapsed

{progress ? formatDuration(seconds_elapsed) : "—"}

Speed

{progress ? (seconds_elapsed > 0 ? `${speed?.text} ${speed?.unit}/s` : "Calculating...") : "—"}

ETA

{progress ? (eta ?? "Calculating...") : "—"}

Current file

{fileName || "—"}

); };