refactor(backup-details): load snapshots after page load

This commit is contained in:
Nicolas Meienberger
2025-11-08 09:24:30 +01:00
parent c32eb0831f
commit 195aea052e
2 changed files with 39 additions and 29 deletions

View File

@@ -2,23 +2,41 @@ import type { ListSnapshotsResponse } from "~/api-client/types.gen";
import { cn } from "~/lib/utils"; import { cn } from "~/lib/utils";
import { Card } from "~/components/ui/card"; import { Card } from "~/components/ui/card";
import { ByteSize } from "~/components/bytes-size"; import { ByteSize } from "~/components/bytes-size";
import { useEffect } from "react";
interface Props { interface Props {
snapshots: ListSnapshotsResponse; snapshots: ListSnapshotsResponse;
snapshotId: string; snapshotId?: string;
loading?: boolean;
onSnapshotSelect: (snapshotId: string) => void; onSnapshotSelect: (snapshotId: string) => void;
} }
export const SnapshotTimeline = (props: Props) => { export const SnapshotTimeline = (props: Props) => {
const { snapshots, snapshotId, onSnapshotSelect } = props; const { snapshots, snapshotId, loading, onSnapshotSelect } = props;
useEffect(() => {
if (!snapshotId && snapshots.length > 0) {
onSnapshotSelect(snapshots[snapshots.length - 1].short_id);
}
}, [snapshotId, snapshots, onSnapshotSelect]);
if (loading) {
return (
<Card>
<div className="flex items-center justify-center h-24">
<p className="text-muted-foreground">Loading snapshots...</p>
</div>
</Card>
);
}
if (snapshots.length === 0) { if (snapshots.length === 0) {
return ( return (
<div className="w-full bg-card border-t border-border py-4 px-4"> <Card>
<div className="flex items-center justify-center h-24"> <div className="flex items-center justify-center h-24">
<p className="text-muted-foreground">No snapshots available</p> <p className="text-muted-foreground">No snapshots available</p>
</div> </div>
</div> </Card>
); );
} }

View File

@@ -14,7 +14,7 @@ import { parseError } from "~/lib/errors";
import { getCronExpression } from "~/utils/utils"; import { getCronExpression } from "~/utils/utils";
import { CreateScheduleForm, type BackupScheduleFormValues } from "../components/create-schedule-form"; import { CreateScheduleForm, type BackupScheduleFormValues } from "../components/create-schedule-form";
import { ScheduleSummary } from "../components/schedule-summary"; import { ScheduleSummary } from "../components/schedule-summary";
import { getBackupSchedule, listSnapshots } from "~/api-client"; import { getBackupSchedule } from "~/api-client";
import type { Route } from "./+types/backup-details"; import type { Route } from "./+types/backup-details";
import { SnapshotFileBrowser } from "../components/snapshot-file-browser"; import { SnapshotFileBrowser } from "../components/snapshot-file-browser";
import { SnapshotTimeline } from "../components/snapshot-timeline"; import { SnapshotTimeline } from "../components/snapshot-timeline";
@@ -24,34 +24,27 @@ export const clientLoader = async ({ params }: Route.LoaderArgs) => {
if (!data) return redirect("/backups"); if (!data) return redirect("/backups");
const snapshots = await listSnapshots({ return data;
path: { name: data.repository.name },
query: { backupId: params.id },
});
if (snapshots.data) return { snapshots: snapshots.data, schedule: data };
return { snapshots: [], schedule: data };
}; };
export default function ScheduleDetailsPage({ params, loaderData }: Route.ComponentProps) { export default function ScheduleDetailsPage({ params, loaderData }: Route.ComponentProps) {
const navigate = useNavigate(); const navigate = useNavigate();
const [isEditMode, setIsEditMode] = useState(false); const [isEditMode, setIsEditMode] = useState(false);
const formId = useId(); const formId = useId();
const [selectedSnapshotId, setSelectedSnapshotId] = useState<string>(loaderData.snapshots.at(-1)?.short_id ?? ""); const [selectedSnapshotId, setSelectedSnapshotId] = useState<string>();
const { data: schedule } = useQuery({ const { data: schedule } = useQuery({
...getBackupScheduleOptions({ ...getBackupScheduleOptions({
path: { scheduleId: params.id }, path: { scheduleId: params.id },
}), }),
initialData: loaderData.schedule, initialData: loaderData,
}); });
const { data: snapshots } = useQuery({ const { data: snapshots, isLoading } = useQuery({
...listSnapshotsOptions({ ...listSnapshotsOptions({
path: { name: schedule.repository.name }, path: { name: schedule.repository.name },
query: { backupId: schedule.id.toString() }, query: { backupId: schedule.id.toString() },
}), }),
initialData: loaderData.snapshots,
}); });
const upsertSchedule = useMutation({ const upsertSchedule = useMutation({
@@ -166,7 +159,7 @@ export default function ScheduleDetailsPage({ params, loaderData }: Route.Compon
); );
} }
const selectedSnapshot = snapshots.find((s) => s.short_id === selectedSnapshotId); const selectedSnapshot = snapshots?.find((s) => s.short_id === selectedSnapshotId);
return ( return (
<div className="flex flex-col gap-6"> <div className="flex flex-col gap-6">
@@ -177,19 +170,18 @@ export default function ScheduleDetailsPage({ params, loaderData }: Route.Compon
setIsEditMode={setIsEditMode} setIsEditMode={setIsEditMode}
schedule={schedule} schedule={schedule}
/> />
{selectedSnapshot && (
<>
<SnapshotTimeline <SnapshotTimeline
snapshots={snapshots} loading={isLoading}
snapshotId={selectedSnapshot.short_id} snapshots={snapshots ?? []}
snapshotId={selectedSnapshot?.short_id}
onSnapshotSelect={setSelectedSnapshotId} onSnapshotSelect={setSelectedSnapshotId}
/> />
{selectedSnapshot && (
<SnapshotFileBrowser <SnapshotFileBrowser
key={selectedSnapshot.short_id} key={selectedSnapshot?.short_id}
snapshot={selectedSnapshot} snapshot={selectedSnapshot}
repositoryName={schedule.repository.name} repositoryName={schedule.repository.name}
/> />
</>
)} )}
</div> </div>
); );