import { motion } from "motion/react" import { useEffect, useRef, useState } from "react" import type { DashboardDef_Output, PageDef_Output, Placement, SectionDef_Output, WidgetDef, } from "@/client" import { cn } from "@/lib/utils" import "./dashboard.css" import { LockedProvider, PaletteProvider, useDashboardBackground, } from "./settings" import { useUi } from "./ui" import { showTitle } from "./ui/core/config" import { LookProvider, useCanvasRoot, useLook } from "./ui/core/look" import { gridStagger, LOOK } from "./ui/core/motion" import { WidgetBody, widgetIssue } from "./widgets" export type Dashboard = DashboardDef_Output /** How many columns a wall panel is cut into, if the document does not say. */ export const DEFAULT_COLUMNS = 12 /** What a grid size setting may be set to; a panel is matched to one of these. */ export const COLUMN_CHOICES = [6, 8, 12, 16, 24] /** The panel a dashboard is drawn for, if the document does not say. */ export const DEFAULT_CANVAS = { width: 1920, height: 1080 } /** Panels worth a name; anything else is typed in as two numbers. */ export const CANVAS_PRESETS = [ { label: '7" panel', width: 1024, height: 600 }, { label: '10" tablet', width: 1280, height: 800 }, { label: "Full HD", width: 1920, height: 1080 }, { label: '10" tablet, portrait', width: 800, height: 1280 }, { label: "Full HD, portrait", width: 1080, height: 1920 }, ] /** The gap between widgets, in pixels. Matches the `gap-3` view mode uses. */ export const GRID_GAP = 12 /** * The generated client marks every list optional, because the server fills * them in. These three keep that from spreading through the components. */ export const pagesOf = (dashboard: DashboardDef_Output) => dashboard.pages ?? [] export const sectionsOf = (page: PageDef_Output) => page.sections ?? [] export const widgetsOf = (section: SectionDef_Output) => section.widgets ?? [] export const columnsOf = (dashboard: Dashboard) => dashboard.columns || DEFAULT_COLUMNS export const canvasOf = (dashboard: Dashboard) => ({ width: dashboard.canvas_width || DEFAULT_CANVAS.width, height: dashboard.canvas_height || DEFAULT_CANVAS.height, }) /** One grid row, in pixels, on the canvas the grid was first drawn for. */ const DEFAULT_ROW_HEIGHT = 80 /** A column's own width on that canvas, gaps taken off. */ const DEFAULT_COLUMN_WIDTH = (DEFAULT_CANVAS.width + GRID_GAP) / DEFAULT_COLUMNS - GRID_GAP /** * How much wider a cell is than it is tall. * * A column's width already follows the canvas — it is the canvas cut into * `columns` — while the row height was a fixed 80px, so the same arrangement * came out as a different picture on every panel: cells taller than wide on a * 7" one, twice as wide as tall on a 4K one. Holding the proportion instead is * what makes the canvas worth scaling to fit. The number is the one the * default canvas already had rather than a new one, so a panel that says * nothing about its size keeps exactly the 80px rows it had. */ const CELL_ASPECT = DEFAULT_COLUMN_WIDTH / DEFAULT_ROW_HEIGHT const columnWidthOf = (dashboard: Dashboard) => (canvasOf(dashboard).width + GRID_GAP) / columnsOf(dashboard) - GRID_GAP /** One grid row, in pixels — the unit widget heights are multiples of. */ export const rowHeightOf = (dashboard: Dashboard) => Math.round(columnWidthOf(dashboard) / CELL_ASPECT) /** Distance between two row origins — what a widget's `y` is counted in. */ export const rowPitchOf = (dashboard: Dashboard) => rowHeightOf(dashboard) + GRID_GAP /** How many rows of the grid fit in the canvas; the rest is off the panel. */ export const rowsOf = (dashboard: Dashboard) => Math.max( 1, Math.floor((canvasOf(dashboard).height + GRID_GAP) / rowPitchOf(dashboard)), ) /** * The dashboard's own surface: exactly the panel's pixel size, scaled to fit * whatever room is left around it. * * Scaling rather than reflowing is the point. A side panel opening, or a * narrower screen, changes only the scale — the arrangement being designed, * and the dot grid under it, stay the layout the panel will actually show. * * This box is also exactly what a dashboard's own appearance applies to: it is * the panel, so its palette, its look and its light or dark all land here and * leave the shell around it alone. Which is why it states a background at all * — without one it would borrow whatever it was dropped into. */ export function CanvasSurface({ dashboard, dots, children, }: { dashboard: Dashboard /** Draw the placement grid. A wall panel is not being arranged, so: no. */ dots?: boolean children: (scale: number) => React.ReactNode }) { const ref = useRef(null) const [box, setBox] = useState({ width: 0, height: 0 }) useEffect(() => { const element = ref.current if (!element) return const observer = new ResizeObserver(([entry]) => setBox({ width: entry.contentRect.width, height: entry.contentRect.height, }), ) observer.observe(element) return () => observer.disconnect() }, []) const { width, height } = canvasOf(dashboard) const scale = Math.min(box.width / width, box.height / height) const root = useCanvasRoot(dashboard) return (
{/* Measured first: a guessed scale would place the whole panel once and then move it. */} {scale > 0 ? (
{/* Over the ground rather than under it: the dots are what is being arranged against, and a wallpaper must not hide them. */} {dots ? (
) : null} {children(scale)}
) : null}
) } /** * What the widgets sit on. * * The image a dashboard was given, or whatever ground its look brings — glass * needs something behind it to refract, and material is happy with the surface * colour alone. */ export function CanvasGround({ dashboard }: { dashboard: Dashboard }) { const { Backdrop } = useUi() return } export function placement(widget: WidgetDef): Placement { const layout = (widget.layout ?? {}) as Record return layout.lg ?? layout.md ?? layout.sm ?? {} } /** * A widget's box, as plain CSS grid. * * View mode never loads a grid library: a wall panel that only displays should * not pay for the code that lets someone drag things around. */ export function widgetStyle( widget: WidgetDef, columns = DEFAULT_COLUMNS, ): React.CSSProperties { const { x = 0, y = 0, w = 3, h = 2 } = placement(widget) const width = Math.min(columns, Math.max(1, w)) return { "--x": Math.min(columns - width, Math.max(0, x)) + 1, "--y": Math.max(0, y) + 1, "--w": width, "--h": Math.max(1, h), } as React.CSSProperties } /** * Has anyone actually arranged this dashboard? * * Before drag-and-drop every widget was written at 0,0, so honouring the * stored position would pile the whole page onto one cell. */ export const isPlaced = (widgets: WidgetDef[]) => widgets.some((widget) => { const { x = 0, y = 0 } = placement(widget) return x > 0 || y > 0 }) /** * The page's widgets as one arrangement. * * Sections used to be drawn as separate grids stacked down the page, each * starting at its own row zero, so a document written with several of them is * read as one grid by pushing each below the one before it. That is what lets * the editor and the panel show the same thing: the editor arranges one grid, * and it now arranges all of them. * * An unarranged document is left alone — every widget still sits at 0,0 there, * so there is no arrangement to keep apart and the grid auto-flows instead. * * ponytail: a section's own heading stops being drawn with it. No UI ever * wrote one, so only a seeded document has any — restoring them means giving * a group a box on the canvas, which is a widget, not a section. */ export function flatWidgets(page: PageDef_Output): WidgetDef[] { const sections = sectionsOf(page) const all = sections.flatMap(widgetsOf) if (sections.length < 2 || !isPlaced(all)) return all const flat: WidgetDef[] = [] let offset = 0 for (const section of sections) { let bottom = 0 for (const widget of widgetsOf(section)) { const placed = placement(widget) const y = Math.max(0, placed.y ?? 0) bottom = Math.max(bottom, y + Math.max(1, placed.h ?? 2)) flat.push( offset === 0 ? widget : { ...widget, layout: { ...(widget.layout ?? {}), lg: { ...placed, y: y + offset }, }, }, ) } offset += bottom } return flat } /** * One page, drawn as the single grid the panel shows. * * A dashboard is one canvas — several dashboards on a panel is what the rail * is for — so the page's sections are one arrangement rather than a stack of * headed grids. `SectionDef` stays in the schema, and the editor writes what * it arranged back into the first section. */ export function DashboardView({ dashboard, renderWidget, stacked, }: { dashboard: Dashboard renderWidget?: (widget: WidgetDef) => React.ReactNode /** One column at the viewport's width, for a phone. */ stacked?: boolean }) { // ponytail: the first page only. A panel carries several whole dashboards // — that is what the rail is for — so nothing writes a second page. const page = pagesOf(dashboard)[0] if (!page) { return (

This dashboard has no pages yet.

) } const all = flatWidgets(page) if (all.length === 0) { return (

Nothing on this page yet. Edit it to add a widget.

) } // Stacked, the arrangement becomes a reading order, so it follows the rows // the panel shows rather than the order widgets happened to be added in. const widgets = stacked ? [...all].sort((a, b) => { const left = placement(a) const right = placement(b) return (left.y ?? 0) - (right.y ?? 0) || (left.x ?? 0) - (right.x ?? 0) }) : all const columns = columnsOf(dashboard) return ( ) } /** * The arrangement itself. * * Its own component because it draws with the look's components, which are * only knowable below `LookProvider`. Widgets arrive as a page rather than all * at once — a stagger, at whatever stiffness the look moves with. */ function WidgetGrid({ dashboard, widgets, columns, stacked, renderWidget, }: { dashboard: Dashboard widgets: WidgetDef[] columns: number stacked?: boolean renderWidget?: (widget: WidgetDef) => React.ReactNode }) { const { Frame } = useUi() const { look } = useLook() return ( {widgets.map((widget) => ( {renderWidget ? ( renderWidget(widget) ) : ( )} ))} ) }