import { Link } from "@tanstack/react-router" import { LayoutDashboard } from "lucide-react" import type { DashboardSummary, WidgetFootprint } from "@/client" import { Skeleton } from "@/components/ui/skeleton" import { cn } from "@/lib/utils" /** Columns a dashboard is cut into when its document does not say. */ const DEFAULT_COLUMNS = 12 /** Widgets that draw a shape, and widgets that are controls. The rest read out. */ const GRAPHIC = new Set(["chart", "forecast", "bar", "gauge"]) const INPUT = new Set([ "button", "switch", "slider", "input", "dropdown", "color", "counter", ]) const shade = (type: string) => INPUT.has(type) ? "bg-muted-foreground/30" : GRAPHIC.has(type) ? "bg-primary/45" : "bg-primary/20" type Block = { type: string x: number y: number w: number h: number } /** The dashboard's widget shapes as one grid. */ function blocksOf(footprint: WidgetFootprint[]): { blocks: Block[] rows: number } { const blocks: Block[] = [] let rows = 0 for (const shape of footprint) { const { x = 0, y = 0, w = 3, h = 2 } = shape blocks.push({ type: shape.type, x: Math.max(0, x), y: Math.max(0, y), w: Math.max(1, w), h: Math.max(1, h), }) rows = Math.max(rows, Math.max(0, y) + Math.max(1, h)) } return { blocks, rows: Math.max(1, rows) } } /** * What a dashboard looks like from across the room: its widgets as blocks, * shaded by what kind of thing each one is. * * A footprint rather than a live render. Nothing here subscribes to a message * or reads a value — recognising "the one with the big chart on the left" is * the whole job, and it has to cost nothing on a screen that is not the * dashboard. The summary carries the shapes, so it costs no request either. */ function Footprint({ dashboard }: { dashboard: DashboardSummary }) { if (dashboard.shape === "round") { return } const { blocks, rows } = blocksOf(dashboard.footprint ?? []) const columns = dashboard.columns || DEFAULT_COLUMNS // Before the editor could place things, every widget was written at 0,0; // honouring that would pile the whole page onto one cell. const placed = blocks.some((block) => block.x > 0 || block.y > 0) const area = blocks.reduce((sum, block) => sum + block.w * block.h, 0) if (blocks.length === 0) { return (
) } return (
{blocks.map((block, index) => { const width = Math.min(columns, block.w) return ( ) })}
) } /** * A round dashboard shows one face at a time, so its grid places mean * nothing: it is drawn as the circle, with a dot per face where a panel puts * its page dots. */ function RoundFootprint({ faces }: { faces: WidgetFootprint[] }) { return (
{faces.map((face, index) => ( ))}
) } /** One dashboard in the mosaic. */ function Tile({ dashboard, className, }: { dashboard: DashboardSummary /** What the layout needs of it — a width, in the scrolling strip. */ className?: string }) { return ( {dashboard.title || dashboard.name} {dashboard.has_draft ? ( Unpublished changes ) : null} ) } /** When the working copy was last written, or the epoch for a summary without one. */ const modifiedAt = (summary: { updated_at?: string | null }) => summary.updated_at ? Date.parse(summary.updated_at) : 0 /** * Which of two documents was worked on more recently. * * Both summaries carry the modified time of the working copy, so this is the * order it says it is. The name only settles a tie, which is two documents * written in the same instant — what a seed does. */ export function byRecency< T extends { name: string; updated_at?: string | null }, >(a: T, b: T): number { return modifiedAt(b) - modifiedAt(a) || a.name.localeCompare(b.name) } /** * The dashboards, as the shapes they are. * * Two layouts, because it is read two ways: a column of pairs where it sits * beside something else, and one wide strip where it has the page to itself. * A tile is `content-start` around an `aspect-video` footprint and so has no * width of its own — the strip has to give it one. */ export function DashboardMosaic({ dashboards, isPending, row = false, }: { dashboards: DashboardSummary[] isPending: boolean /** One scrolling strip instead of a two-column grid. */ row?: boolean }) { const container = row ? "flex snap-x snap-mandatory gap-3 overflow-x-auto p-3" : "grid gap-3 p-3 sm:grid-cols-2" const tile = row ? "w-56 shrink-0 snap-start" : "" if (isPending) { return (
{Array.from({ length: 2 }).map((_, index) => ( ))}
) } if (dashboards.length === 0) { return (

Dashboards you build show up here, as the shapes they are.

Go to dashboards
) } return (
{dashboards.map((dashboard) => ( ))}
) }