A dashboard is a wall panel somebody hangs in their own hallway, so it now wears what they choose: a look, and a palette of their own colours. Two complete component sets live under `Dashboard/ui/` — `glass` (translucent panes over a slowly moving ground) and `material` (Material 3 tonal cards) — behind one prop contract. Every control's state, keyboard and `aria-` live in `ui/core` and are shared, so the two sets are the same dashboard drawn twice rather than two products: a set only decides what a control looks like while doing it. Four settings join the channel, each drivable by a flow like any other: `look`, `palette`, `background` and `touch`. A palette is an ordered list of hex colours — background, surface, primary, accent, text, then more chart colours — pasted from a coolors.co link or typed, written onto the canvas as the token variables everything already reads. Trailing roles are derived, so three colours are a whole dashboard, and derived text is held to AA rather than trusted (`theme.check.ts` measures it). A palette also decides light or dark, since its first colour is the ground. Widgets are measured against their own tile with container queries rather than against the viewport, animate through `motion`, and can be drawn without their title. The three reworks: - a bar draws a row per reading, up to eight, each in the dashboard's own data colours and each able to carry its own scale — replacing readings nested in one fill, which could only ever share one colour and stop at three. Documents written the old way are read as rows. - a chart's range picker moved to a column down its right-hand edge, which gives the plot back a whole row of a short tile. - the colour wheel became a disc: hue is the angle and saturation the distance from the middle, so a colour is one gesture rather than three, with brightness on a slider beside it. `index.css` and `lib/motion.ts` are untouched — the dashboard overrides token *values* on its canvas, never the blocks the two repos share.
415 lines
13 KiB
TypeScript
415 lines
13 KiB
TypeScript
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<HTMLDivElement>(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 (
|
|
<div ref={ref} className="relative size-full overflow-hidden">
|
|
{/* Measured first: a guessed scale would place the whole panel once and
|
|
then move it. */}
|
|
{scale > 0 ? (
|
|
<LookProvider dashboard={dashboard}>
|
|
<div
|
|
data-look={root["data-look"]}
|
|
data-touch={root["data-touch"]}
|
|
data-palette={root["data-palette"]}
|
|
className={cn(
|
|
"absolute overflow-hidden bg-background text-foreground",
|
|
root.className,
|
|
)}
|
|
data-testid="canvas-surface"
|
|
style={
|
|
{
|
|
...root.style,
|
|
width,
|
|
height,
|
|
left: (box.width - width * scale) / 2,
|
|
top: (box.height - height * scale) / 2,
|
|
transform: `scale(${scale})`,
|
|
transformOrigin: "top left",
|
|
// One dot per cell corner. The column pitch the grid snaps to
|
|
// is (width + gap) / columns; a row is one row plus the gap.
|
|
"--dot-x": `${(width + GRID_GAP) / columnsOf(dashboard)}px`,
|
|
"--dot-y": `${rowPitchOf(dashboard)}px`,
|
|
} as React.CSSProperties
|
|
}
|
|
>
|
|
<CanvasGround dashboard={dashboard} />
|
|
{/* Over the ground rather than under it: the dots are what is
|
|
being arranged against, and a wallpaper must not hide them. */}
|
|
{dots ? (
|
|
<div className="dot-canvas pointer-events-none absolute inset-0" />
|
|
) : null}
|
|
{children(scale)}
|
|
</div>
|
|
</LookProvider>
|
|
) : null}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
/**
|
|
* 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 <Backdrop image={useDashboardBackground(dashboard)} />
|
|
}
|
|
|
|
export function placement(widget: WidgetDef): Placement {
|
|
const layout = (widget.layout ?? {}) as Record<string, Placement>
|
|
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 (
|
|
<p className="text-sm text-muted-foreground">
|
|
This dashboard has no pages yet.
|
|
</p>
|
|
)
|
|
}
|
|
|
|
const all = flatWidgets(page)
|
|
if (all.length === 0) {
|
|
return (
|
|
<p
|
|
className="text-sm text-muted-foreground"
|
|
data-testid="dashboard-empty"
|
|
>
|
|
Nothing on this page yet. Edit it to add a widget.
|
|
</p>
|
|
)
|
|
}
|
|
|
|
// 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 (
|
|
<LookProvider dashboard={dashboard}>
|
|
<LockedProvider dashboard={dashboard}>
|
|
<PaletteProvider dashboard={dashboard}>
|
|
<WidgetGrid
|
|
dashboard={dashboard}
|
|
widgets={widgets}
|
|
columns={columns}
|
|
stacked={stacked}
|
|
renderWidget={renderWidget}
|
|
/>
|
|
</PaletteProvider>
|
|
</LockedProvider>
|
|
</LookProvider>
|
|
)
|
|
}
|
|
|
|
/**
|
|
* 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 (
|
|
<motion.div
|
|
className={cn("widget-grid", stacked && "widget-stacked")}
|
|
data-placed={isPlaced(widgets) || undefined}
|
|
variants={gridStagger}
|
|
initial="hidden"
|
|
animate="visible"
|
|
style={
|
|
{
|
|
"--widget-cols": columns,
|
|
// The row height follows the canvas, so the CSS grid and the
|
|
// editor's grid library cannot drift apart.
|
|
"--row-height": `${rowHeightOf(dashboard)}px`,
|
|
} as React.CSSProperties
|
|
}
|
|
>
|
|
{widgets.map((widget) => (
|
|
<motion.div
|
|
key={widget.id}
|
|
style={widgetStyle(widget, columns)}
|
|
className="widget-cell"
|
|
variants={LOOK[look].enter}
|
|
transition={LOOK[look].spring}
|
|
>
|
|
{renderWidget ? (
|
|
renderWidget(widget)
|
|
) : (
|
|
<Frame
|
|
title={showTitle(widget) ? widget.title : undefined}
|
|
issue={widgetIssue(widget)}
|
|
>
|
|
<WidgetBody widget={widget} dashboard={dashboard.name} />
|
|
</Frame>
|
|
)}
|
|
</motion.div>
|
|
))}
|
|
</motion.div>
|
|
)
|
|
}
|