import { Activity, DollarSign, ShoppingCart, Users } from "lucide-react" import { definePage } from "@squadbase/vantage" import { DashboardCard, DashboardCardAction, DashboardCardContent, DashboardCardHeader, DashboardCardPreset, DashboardCardTitle, AreaChart, PageShell, PageShellContent, PageShellDescription, PageShellHeader, PageShellHeaderEnd, PageShellHeading, PageShellTitle, Placeholder, SegmentedControl, Sparkline, } from "@squadbase/vantage/components" import type { SegmentOption } from "@squadbase/vantage/components" import { Button, ErrorState, Loading } from "@squadbase/vantage/ui" import { useApiQuery } from "@squadbase/vantage/query" import { useSearchParam } from "@squadbase/vantage/router" import { TopItemsTable } from "./components/kpi-chart-simple/top-items-table" import type { KpiId, KpiSummary } from "./lib/kpi-chart-simple/types" export const page = definePage({ title: "KPI + Chart (Simple)", description: "4 KPI cards, a single trend line, and a ranked-items table, served by server/api and filtered through the URL.", }) // ── formatting ─────────────────────────────────────────────────────────────── function formatCurrency(n: number): string { const sign = n < 0 ? "-" : "" const abs = Math.abs(n) if (abs >= 1_000_000) return `${sign}$${(abs / 1_000_000).toFixed(2)}M` if (abs >= 1_000) return `${sign}$${(abs / 1_000).toFixed(1)}K` return `${sign}$${abs.toFixed(0)}` } // ── page ───────────────────────────────────────────────────────────────────── /** Display labels live here, not in the payload the API returns. */ const KPI_META: Record< KpiId, { label: string; icon: typeof DollarSign; format: (value: number) => string } > = { "total-revenue": { label: "Metric 1", icon: DollarSign, format: formatCurrency }, "active-users": { label: "Metric 2", icon: Users, format: (v) => Math.round(v).toLocaleString("en-US"), }, "conversion-rate": { label: "Metric 3", icon: Activity, format: (v) => `${v.toFixed(2)}%` }, aov: { label: "Metric 4", icon: ShoppingCart, format: (v) => `$${v.toFixed(2)}` }, } const RANGE_OPTIONS: SegmentOption[] = [ { label: "7 days", value: "7d" }, { label: "30 days", value: "30d" }, { label: "90 days", value: "90d" }, ] export default function HomePage() { // Filter state belongs in the URL rather than `useState`: it survives a // reload, and a pasted link reproduces exactly what you were looking at. const [range, setRange] = useSearchParam("range", "30d") // `search` goes into the query string *and* the cache key, so changing the // range refetches instead of serving the previous window. const summary = useApiQuery("/api/kpi-summary", { search: { range } }) return ( [Template] Performance overview Key metrics and trends for the selected period. {summary.isPending ? ( ) : summary.isError ? ( // `summary.error` is an `ApiError`: `message` is the text the handler // passed to `HttpError`, so there is no cast and no generic wording. void summary.refetch()}>Retry} /> ) : ( )} ) } function DashboardBody({ data }: { data: KpiSummary }) { return ( <>
{data.kpis.map((kpi) => { const { label, icon: Icon, format } = KPI_META[kpi.id] return ( {label} {format(kpi.value)}
{kpi.change >= 0 ? `+${kpi.change}%` : `${kpi.change}%`}
({ value: v }))} height={32} area className="mt-3" />
) })}
) }