Public: anyone can read this; the crew can change it.
A board panel whose source has no rows says so in one line instead of drawing an empty chart #16154
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/16137-a-board-panel-whose-source-has-no-rows-s → main 0 commits fork
3 files
changed +46
−3
src/boards/chart.ts +17 −2
| @@ -324,8 +324,23 @@ export function tableChart(cells: readonly Cell[], title: string): string { | ||
| 324 | 324 | ].join(""); |
| 325 | 325 | } |
| 326 | 326 | |
| 327 | /** One chart by name: the one door a page draws a panel through. */ | |
| 328 | export function drawChart(chart: Chart, cells: readonly Cell[], title: string): string { | |
| 327 | /** | |
| 328 | * What a panel says when its source has no rows: one line naming the source, in the box a chart | |
| 329 | * would fill. | |
| 330 | */ | |
| 331 | export function emptySource(kind: Chart, title: string, source: string): string { | |
| 332 | const line = `Nothing recorded yet in ${clean(source)}.`; | |
| 333 | return kind === "table" | |
| 334 | ? `<table class="table chart-table" data-dense><caption class="sr-only">${t(title)}</caption><tbody><tr><td class="muted">${escapeXml(line)}</td></tr></tbody></table>` | |
| 335 | : frame(kind, title, `${clean(title)}: ${line}`, `<p class="chart-none">${escapeXml(line)}</p>`); | |
| 336 | } | |
| 337 | ||
| 338 | /** | |
| 339 | * One chart by name: the one door a page draws a panel through. With a `source` name and no | |
| 340 | * cells, the panel says its source has nothing recorded instead of drawing an empty chart. | |
| 341 | */ | |
| 342 | export function drawChart(chart: Chart, cells: readonly Cell[], title: string, source?: string): string { | |
| 343 | if (cells.length === 0 && source !== undefined && clean(source) !== "") return emptySource(chart, title, source); | |
| 329 | 344 | switch (chart) { |
| 330 | 345 | case "line": return lineChart(cells, title); |
| 331 | 346 | case "bar": return barChart(cells, title); |
src/boards/panel_data.ts +1 −1
| @@ -142,7 +142,7 @@ export function drawnPanel(panel: Panel, answer: PanelData): PanelDrawn { | ||
| 142 | 142 | const newest = panel.chart === "line" || panel.chart === "bar" ? answer.rows[answer.rows.length - 1] : undefined; |
| 143 | 143 | // One series drawn over time says its latest value beside its name; a split, a table or groups of anything else have no one latest. |
| 144 | 144 | const latest = newest !== undefined && newest.series === undefined && /^\d{4}-\d{2}-\d{2}/.test(String(newest.group)) ? (newest.value ?? 0) : null; |
| 145 | return { ok: true, chart: drawChart(panel.chart, cellsOf(answer.rows), panel.title), latest }; | |
| 145 | return { ok: true, chart: drawChart(panel.chart, cellsOf(answer.rows), panel.title, typeof panel.query.source === "string" ? panel.query.source : undefined), latest }; | |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | /** A text cell a spreadsheet will not read as a formula, quoted when it holds a comma, quote or newline. */ |
test/board-empty.test.ts added +28
| @@ -0,0 +1,28 @@ | ||
| 1 | /** A panel whose source has no rows says so in one line, naming the source (#16137). */ | |
| 2 | import { describe, expect, test } from "bun:test"; | |
| 3 | import { CHARTS, drawChart } from "../src/boards/chart.ts"; | |
| 4 | import { drawnPanel } from "../src/boards/panel_data.ts"; | |
| 5 | ||
| 6 | const panel = (chart: (typeof CHARTS)[number]) => ({ title: "Runs", chart, query: { source: "runs" }, w: 1, h: 1 }); | |
| 7 | ||
| 8 | describe("empty source", () => { | |
| 9 | test("every chart says nothing recorded yet, with the source name", () => { | |
| 10 | for (const c of CHARTS) { | |
| 11 | const out = drawChart(c, [], "Runs", "runs"); | |
| 12 | expect(out).toContain("Nothing recorded yet in runs."); | |
| 13 | expect(out).not.toContain("<path"); | |
| 14 | } | |
| 15 | }); | |
| 16 | test("the source name is escaped", () => { | |
| 17 | expect(drawChart("line", [], "t", "<i>")).not.toContain("<i>"); | |
| 18 | }); | |
| 19 | test("a panel drawn from no rows names its source", () => { | |
| 20 | const d = drawnPanel(panel("bar"), { ok: true, rows: [], from: null, to: null, step: null }); | |
| 21 | expect(d.ok && d.chart).toContain("Nothing recorded yet in runs."); | |
| 22 | }); | |
| 23 | test("a panel with rows is unchanged", () => { | |
| 24 | const cells = [{ label: "a", value: 1 }]; | |
| 25 | expect(drawChart("line", cells, "Runs", "runs")).toBe(drawChart("line", cells, "Runs")); | |
| 26 | expect(drawChart("line", [], "Runs")).toContain("No points in this range."); | |
| 27 | }); | |
| 28 | }); |
new above
offline