Make a dashboard its widgets: drop the pages and sections nobody drew
This commit is contained in:
@@ -1,12 +1,6 @@
|
||||
import { motion } from "motion/react"
|
||||
import { useEffect, useRef, useState } from "react"
|
||||
import type {
|
||||
DashboardDef_Output,
|
||||
PageDef_Output,
|
||||
Placement,
|
||||
SectionDef_Output,
|
||||
WidgetDef,
|
||||
} from "@/client"
|
||||
import type { DashboardDef_Output, Placement, WidgetDef } from "@/client"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
import "./dashboard.css"
|
||||
@@ -68,9 +62,8 @@ export const RAIL_INSET = 72
|
||||
* 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 widgetsOf = (dashboard: DashboardDef_Output) =>
|
||||
dashboard.widgets ?? []
|
||||
|
||||
export const columnsOf = (dashboard: Dashboard) =>
|
||||
dashboard.columns || DEFAULT_COLUMNS
|
||||
@@ -293,52 +286,6 @@ export const isPlaced = (widgets: WidgetDef[]) =>
|
||||
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.
|
||||
*
|
||||
@@ -360,26 +307,14 @@ export function DashboardView({
|
||||
/** Whether the panel carries a rail, which takes a column of the canvas. */
|
||||
rail?: 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)
|
||||
const all = widgetsOf(dashboard)
|
||||
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.
|
||||
Nothing on this dashboard yet. Edit it to add a widget.
|
||||
</p>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user