"use client"; import * as React from "react"; import { useQuery } from "@tanstack/react-query"; import type { AnalyticsSummary } from "@/types/analytics"; async function fetchSummary(ownersLimit = 8): Promise { const url = new URL("/api/analytics/summary", window.location.origin); url.searchParams.set("ownersLimit", String(ownersLimit)); const res = await fetch(url.toString(), { cache: "no-store" }); if (!res.ok) { const payload = await res.json().catch(() => ({})); throw new Error(payload?.message || `Failed to load summary (${res.status})`); } return (await res.json()) as AnalyticsSummary; } function formatBytes(bytes?: number) { const b = Number(bytes || 0); if (!b) return "0 B"; const k = 1024; const sizes = ["B", "KB", "MB", "GB", "TB", "PB"]; const i = Math.floor(Math.log(b) / Math.log(k)); return `${parseFloat((b / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`; } export function KPICards() { const q = useQuery({ queryKey: ["analytics", "summary"], queryFn: () => fetchSummary(8), }); const s = q.data; return (
{/* Owners list */}
Top Owners
{q.isLoading &&
Loading…
} {!q.isLoading && (s?.owners?.length ?? 0) === 0 && (
No owner data
)}
    {(s?.owners ?? []).map((o) => (
  • {o.owner || "unknown"} {o.count.toLocaleString()}
  • ))}
Last Uploaded
{q.isLoading ? "…" : (s?.lastUploadedAt ? new Date(s.lastUploadedAt).toLocaleString() : "—")}
); } function KPI({ title, value }: { title: string; value: React.ReactNode }) { return (
{title}
{value}
); }