basalt-ui 2.18.0 → 2.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +90 -0
- package/MIGRATING.md +238 -12
- package/agent/agents/basalt-design-reviewer.md +2 -2
- package/agent/rules/basalt-charts.md +1 -1
- package/agent/rules/basalt-mantine.md +1 -1
- package/agent/skills/basalt-app/SKILL.md +1 -1
- package/agent/skills/basalt-design/SKILL.md +1 -1
- package/agent/templates/DESIGN.md.tpl +1 -1
- package/configs/oxlint-plugin.js +33 -19
- package/configs/oxlint-plugin.test.ts +32 -2
- package/dist/charts/ChartCard.d.ts +28 -3
- package/dist/charts/ChartCard.d.ts.map +1 -1
- package/dist/charts/ChartCard.js +33 -11
- package/dist/charts/Donut.d.ts.map +1 -1
- package/dist/charts/Donut.js +13 -1
- package/dist/charts/Heatmap.js +1 -1
- package/dist/charts/Sparkline.d.ts +7 -1
- package/dist/charts/Sparkline.d.ts.map +1 -1
- package/dist/charts/Sparkline.js +137 -18
- package/dist/charts/box.d.ts +2 -0
- package/dist/charts/box.d.ts.map +1 -1
- package/dist/charts/box.js +2 -0
- package/dist/charts/chart-box.module.css +30 -0
- package/dist/charts/chart-box.module.css.d.ts +1 -0
- package/dist/charts/chart-card.module.css +47 -0
- package/dist/charts/chart-card.module.css.d.ts +7 -0
- package/dist/charts/compile.d.ts.map +1 -1
- package/dist/charts/compile.js +35 -8
- package/dist/charts/host.d.ts.map +1 -1
- package/dist/charts/host.js +2 -1
- package/dist/charts/index.d.ts +1 -1
- package/dist/charts/index.d.ts.map +1 -1
- package/dist/charts/sparkline.module.css +59 -0
- package/dist/charts/sparkline.module.css.d.ts +13 -0
- package/dist/charts/state.d.ts.map +1 -1
- package/dist/charts/state.js +32 -4
- package/dist/charts/types.d.ts +6 -0
- package/dist/charts/types.d.ts.map +1 -1
- package/dist/commands/notifications/center.d.ts.map +1 -1
- package/dist/commands/notifications/center.js +18 -26
- package/dist/content/callout.d.ts +1 -1
- package/dist/content/callout.d.ts.map +1 -1
- package/dist/content/callout.js +10 -1
- package/dist/content/callout.module.css +15 -3
- package/dist/content/callout.module.css.d.ts +1 -0
- package/dist/controls/sync-button.d.ts +3 -0
- package/dist/controls/sync-button.d.ts.map +1 -1
- package/dist/dashboard/bar-list.d.ts +15 -4
- package/dist/dashboard/bar-list.d.ts.map +1 -1
- package/dist/dashboard/bar-list.js +56 -19
- package/dist/dashboard/bar-list.module.css +110 -17
- package/dist/dashboard/bar-list.module.css.d.ts +2 -0
- package/dist/dashboard/delta-badge.d.ts +8 -3
- package/dist/dashboard/delta-badge.d.ts.map +1 -1
- package/dist/dashboard/delta-badge.js +9 -2
- package/dist/dashboard/index.d.ts +2 -0
- package/dist/dashboard/index.d.ts.map +1 -1
- package/dist/dashboard/meter-model.d.ts +68 -1
- package/dist/dashboard/meter-model.d.ts.map +1 -1
- package/dist/dashboard/meter-model.js +65 -4
- package/dist/dashboard/meter-parts.d.ts +5 -1
- package/dist/dashboard/meter-parts.d.ts.map +1 -1
- package/dist/dashboard/meter-parts.js +234 -60
- package/dist/dashboard/meter.d.ts +38 -6
- package/dist/dashboard/meter.d.ts.map +1 -1
- package/dist/dashboard/meter.js +4 -1
- package/dist/dashboard/meter.module.css +209 -13
- package/dist/dashboard/meter.module.css.d.ts +16 -0
- package/dist/dashboard/query-state.d.ts.map +1 -1
- package/dist/dashboard/query-state.js +14 -9
- package/dist/dashboard/settings-section.d.ts.map +1 -1
- package/dist/dashboard/settings-section.js +14 -25
- package/dist/dashboard/stat-card-parts.d.ts +3 -1
- package/dist/dashboard/stat-card-parts.d.ts.map +1 -1
- package/dist/dashboard/stat-card-parts.js +18 -3
- package/dist/dashboard/stat-card.d.ts +7 -6
- package/dist/dashboard/stat-card.d.ts.map +1 -1
- package/dist/dashboard/stat-card.js +51 -83
- package/dist/dashboard/stat-card.module.css +42 -14
- package/dist/dashboard/stat-group.d.ts +11 -2
- package/dist/dashboard/stat-group.d.ts.map +1 -1
- package/dist/dashboard/stat-group.js +9 -7
- package/dist/dashboard/stat-strip.d.ts +50 -0
- package/dist/dashboard/stat-strip.d.ts.map +1 -0
- package/dist/dashboard/stat-strip.js +85 -0
- package/dist/dashboard/stat-strip.module.css +208 -0
- package/dist/dashboard/stat-strip.module.css.d.ts +28 -0
- package/dist/dashboard/widget-header.d.ts +3 -3
- package/dist/dashboard/widget-header.d.ts.map +1 -1
- package/dist/dashboard/widget-header.js +2 -1
- package/dist/dashboard/widget-header.module.css +38 -7
- package/dist/format/date.d.ts.map +1 -1
- package/dist/format/date.js +4 -3
- package/dist/format/shared.d.ts +6 -0
- package/dist/format/shared.d.ts.map +1 -1
- package/dist/format/shared.js +6 -2
- package/dist/guard/index.d.ts +1 -1
- package/dist/guard/index.js +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/styles.css +6 -0
- package/dist/theme/card-inset.module.css +76 -0
- package/dist/theme/card-inset.module.css.d.ts +9 -0
- package/dist/theme/chrome-vars.d.ts +29 -17
- package/dist/theme/chrome-vars.d.ts.map +1 -1
- package/dist/theme/chrome-vars.js +18 -8
- package/dist/theme/controls.module.css +3 -0
- package/dist/theme/index.d.ts.map +1 -1
- package/dist/theme/index.js +32 -5
- package/dist/theme/nav-link.module.css +8 -10
- package/dist/theme/status-dot.d.ts +7 -0
- package/dist/theme/status-dot.d.ts.map +1 -0
- package/dist/theme/status-dot.js +18 -0
- package/dist/theme/status-dot.module.css +9 -0
- package/dist/theme/status-dot.module.css.d.ts +8 -0
- package/dist/theme/status-verdict.d.ts +10 -0
- package/dist/theme/status-verdict.d.ts.map +1 -0
- package/dist/theme/status-verdict.js +14 -0
- package/dist/theme/status-verdict.module.css +25 -0
- package/dist/theme/status-verdict.module.css.d.ts +9 -0
- package/llms.txt +30 -6
- package/package.json +1 -1
- package/src/charts/ChartCard.tsx +70 -4
- package/src/charts/Donut.tsx +9 -1
- package/src/charts/Heatmap.tsx +1 -1
- package/src/charts/Sparkline.tsx +180 -18
- package/src/charts/box.tsx +2 -0
- package/src/charts/chart-box.module.css +30 -0
- package/src/charts/chart-box.module.css.d.ts +1 -0
- package/src/charts/chart-card.module.css +47 -0
- package/src/charts/chart-card.module.css.d.ts +7 -0
- package/src/charts/compile.ts +42 -9
- package/src/charts/host.tsx +6 -3
- package/src/charts/index.ts +1 -1
- package/src/charts/sparkline.module.css +59 -0
- package/src/charts/sparkline.module.css.d.ts +13 -0
- package/src/charts/state.tsx +44 -4
- package/src/charts/types.ts +6 -0
- package/src/commands/notifications/center.tsx +29 -32
- package/src/content/callout.module.css +15 -3
- package/src/content/callout.module.css.d.ts +1 -0
- package/src/content/callout.tsx +15 -2
- package/src/controls/sync-button.tsx +3 -0
- package/src/dashboard/bar-list.module.css +110 -17
- package/src/dashboard/bar-list.module.css.d.ts +2 -0
- package/src/dashboard/bar-list.tsx +102 -21
- package/src/dashboard/delta-badge.tsx +21 -5
- package/src/dashboard/index.ts +2 -0
- package/src/dashboard/meter-model.ts +132 -4
- package/src/dashboard/meter-parts.tsx +305 -25
- package/src/dashboard/meter.module.css +209 -13
- package/src/dashboard/meter.module.css.d.ts +16 -0
- package/src/dashboard/meter.tsx +42 -7
- package/src/dashboard/query-state.tsx +28 -19
- package/src/dashboard/settings-section.tsx +1 -8
- package/src/dashboard/stat-card-parts.tsx +22 -2
- package/src/dashboard/stat-card.module.css +42 -14
- package/src/dashboard/stat-card.tsx +23 -60
- package/src/dashboard/stat-group.tsx +19 -8
- package/src/dashboard/stat-strip.module.css +208 -0
- package/src/dashboard/stat-strip.module.css.d.ts +28 -0
- package/src/dashboard/stat-strip.tsx +183 -0
- package/src/dashboard/widget-header.module.css +38 -7
- package/src/dashboard/widget-header.tsx +7 -4
- package/src/format/date.ts +6 -3
- package/src/format/shared.ts +11 -2
- package/src/guard/index.ts +1 -1
- package/src/index.ts +7 -0
- package/src/styles.css +6 -0
- package/src/theme/__snapshots__/plain-theme.test.ts.snap +242 -26
- package/src/theme/card-inset.module.css +76 -0
- package/src/theme/card-inset.module.css.d.ts +9 -0
- package/src/theme/chrome-vars.ts +48 -23
- package/src/theme/controls.module.css +3 -0
- package/src/theme/index.ts +40 -6
- package/src/theme/nav-link.module.css +8 -10
- package/src/theme/status-dot.module.css +9 -0
- package/src/theme/status-dot.module.css.d.ts +8 -0
- package/src/theme/status-dot.tsx +31 -0
- package/src/theme/status-verdict.module.css +25 -0
- package/src/theme/status-verdict.module.css.d.ts +9 -0
- package/src/theme/status-verdict.tsx +32 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* DeltaBadge — the KPI trend readout (docs/DESIGN-CORE.md: "up/down deltas are text colour only,
|
|
3
|
-
* never a fill"): a glyph and a signed figure in the trend colour,
|
|
4
|
-
* or padding in either scheme. The optional ▲/▼ glyph sits on the `micro` rung and `period` is
|
|
3
|
+
* never a fill"): a glyph and a signed figure in the trend colour, proportional-sans `sm` with
|
|
4
|
+
* lining figures, with NO fill, border or padding in either scheme. The optional ▲/▼ glyph sits on the `micro` rung and `period` is
|
|
5
5
|
* `micro` muted. `polarity` decides the TONE the sign maps to — `'up-good'` (default) reads a rise
|
|
6
6
|
* as success and a fall as danger; `'up-bad'` inverts that for a metric where a rise is the worse
|
|
7
7
|
* reading (a cycle-top index, latency, error rate); `'neutral'` renders every sign in a faint,
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
* <DeltaBadge value={0} /> // 0.0%, faint, no glyph
|
|
24
24
|
* <DeltaBadge value={182} format={(v) => `${Math.abs(v)}ms`} withGlyph={false} />
|
|
25
25
|
* <DeltaBadge value={35.9} polarity="up-bad" /> // ▲ 35.9%, danger — a rise is bad here
|
|
26
|
+
* <DeltaBadge value={4.2} period="last week" /> // ▲ 4.2% vs last week
|
|
26
27
|
*/
|
|
27
28
|
import { VX } from '../tokens'
|
|
28
29
|
import classes from './widget-header.module.css'
|
|
@@ -41,13 +42,24 @@ export type DeltaBadgeProps = {
|
|
|
41
42
|
format?: (value: number) => string
|
|
42
43
|
/** Render the ▲/▼ direction glyph. Defaults to `true`; a zero value never shows a glyph. */
|
|
43
44
|
withGlyph?: boolean
|
|
44
|
-
/** Optional comparison
|
|
45
|
-
* `
|
|
45
|
+
/** Optional comparison period rendered after the value, muted, as `<periodPrefix> <period>`
|
|
46
|
+
* (`period="last week"` → "vs last week"). One rule: the prefix is prepended unless the period
|
|
47
|
+
* already starts with it (case-insensitive, optional trailing dot: "vs", "VS", "vs."), so a caller
|
|
48
|
+
* still writing "vs last week" renders it once. Other words ("versus") are not detected. */
|
|
46
49
|
period?: string | undefined
|
|
50
|
+
/** The word composed before `period`. Defaults to `'vs'`; pass your locale's word, or `''` to
|
|
51
|
+
* render `period` raw. */
|
|
52
|
+
periodPrefix?: string
|
|
47
53
|
}
|
|
48
54
|
|
|
49
55
|
const defaultFormat = (value: number): string => `${Math.abs(value).toFixed(1)}%`
|
|
50
56
|
|
|
57
|
+
function composePeriod(period: string, prefix: string): string {
|
|
58
|
+
if (prefix === '') return period
|
|
59
|
+
const escaped = prefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
60
|
+
return new RegExp(`^${escaped}\\.?(\\s|$)`, 'i').test(period) ? period : `${prefix} ${period}`
|
|
61
|
+
}
|
|
62
|
+
|
|
51
63
|
function resolveTone(value: number, polarity: DeltaPolarity): string {
|
|
52
64
|
if (value === 0 || polarity === 'neutral') return VX.faint
|
|
53
65
|
const rose = value > 0
|
|
@@ -68,6 +80,7 @@ export function DeltaBadge({
|
|
|
68
80
|
format = defaultFormat,
|
|
69
81
|
withGlyph = true,
|
|
70
82
|
period,
|
|
83
|
+
periodPrefix = 'vs',
|
|
71
84
|
}: DeltaBadgeProps) {
|
|
72
85
|
const tone = resolveTone(value, polarity)
|
|
73
86
|
const glyph = directionGlyph(value)
|
|
@@ -77,7 +90,10 @@ export function DeltaBadge({
|
|
|
77
90
|
<span className={classes.deltaBadge} style={{ color: tone }}>
|
|
78
91
|
{showGlyph && <span className={classes.deltaGlyph}>{glyph}</span>}
|
|
79
92
|
{format(value)}
|
|
80
|
-
{period !== undefined &&
|
|
93
|
+
{period !== undefined && ' '}
|
|
94
|
+
{period !== undefined && (
|
|
95
|
+
<span className={classes.deltaPeriod}>{composePeriod(period, periodPrefix)}</span>
|
|
96
|
+
)}
|
|
81
97
|
</span>
|
|
82
98
|
)
|
|
83
99
|
}
|
package/src/dashboard/index.ts
CHANGED
|
@@ -3,7 +3,9 @@ export { DeltaBadge } from './delta-badge'
|
|
|
3
3
|
export { Section } from './section'
|
|
4
4
|
export { StatCard, type StatCardBreakdownRow, type StatCardSparklineSize } from './stat-card'
|
|
5
5
|
export { StatGroup } from './stat-group'
|
|
6
|
+
export type { StatStripCell, StatStripRow } from './stat-strip'
|
|
6
7
|
export { EmptyState } from './empty-state'
|
|
7
8
|
export { SettingsSection, SettingsRow, DangerZone } from './settings-section'
|
|
8
9
|
export { Meter, type MeterProps } from './meter'
|
|
10
|
+
export type { MeterSegment, MeterTick, MeterZone } from './meter-model'
|
|
9
11
|
export { BarList, type BarListProps } from './bar-list'
|
|
@@ -1,22 +1,68 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Meter's pure model: everything the component shows, computed from numbers (no DOM, no React).
|
|
2
|
+
* Meter's pure model: everything the component shows, computed from numbers (no DOM, no React runtime).
|
|
3
3
|
* Internal — `meter.tsx` and `meter-parts.tsx` render what this returns.
|
|
4
4
|
*/
|
|
5
|
+
import type { ReactNode } from 'react'
|
|
5
6
|
import { clamp01, toPct } from './scale'
|
|
6
7
|
|
|
7
8
|
export type MeterSegment = { value: number; label: string; color?: string; forecast?: boolean }
|
|
8
9
|
export type MeterZone = { from: number; to: number; label: string; color?: string }
|
|
10
|
+
/** A mark on the track at `at` (in the reading's units). A label is captioned beside it. */
|
|
11
|
+
export type MeterTick = { at: number; label?: string }
|
|
9
12
|
|
|
10
|
-
/**
|
|
13
|
+
/** One tick on the track: its percent position and what it says (caption `label`, aria `text`). */
|
|
14
|
+
export type TickMark = { key: string; pct: number; label: string | undefined; text: string }
|
|
15
|
+
|
|
16
|
+
/** A fill on the track, as fractions (0–1) of the span, with the segment it was laid out from. */
|
|
11
17
|
export type Part = {
|
|
12
18
|
readonly start: number
|
|
13
19
|
readonly size: number
|
|
14
20
|
readonly color: string | undefined
|
|
15
21
|
readonly forecast: boolean
|
|
22
|
+
readonly label: string
|
|
23
|
+
/** The segment's own (normalised) amount, in the reading's units — what its caption states. */
|
|
24
|
+
readonly amount: number
|
|
16
25
|
}
|
|
17
26
|
|
|
27
|
+
/** One categorical part's caption: its position key, name, formatted amount and fill colour (`undefined` = neutral). */
|
|
28
|
+
export type PartCaption = { key: string; label: string; text: string; color: string | undefined }
|
|
29
|
+
|
|
30
|
+
/** One zone's name on the band: where its band starts (percent) and whether it holds the reading. */
|
|
31
|
+
export type ZoneLabel = { key: string; label: string; startPct: number; holding: boolean }
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* What the caption row says. `reading`: the formatted reading; `parts`: categorical segments name
|
|
35
|
+
* every part instead; `forecast`: committed/forecast keeps the reading plus the forecast caption.
|
|
36
|
+
*/
|
|
37
|
+
export type CaptionMode = 'reading' | 'parts' | 'forecast'
|
|
38
|
+
|
|
18
39
|
type Format = (value: number) => string
|
|
19
40
|
|
|
41
|
+
/**
|
|
42
|
+
* The ticks that sit on the track, in the order given. A tick outside `[min, max]` (or not a number)
|
|
43
|
+
* is DROPPED, never clamped: a mark pinned to the end would state a position it is not at.
|
|
44
|
+
*/
|
|
45
|
+
export function tickMarks(
|
|
46
|
+
ticks: readonly MeterTick[] | undefined,
|
|
47
|
+
min: number,
|
|
48
|
+
max: number,
|
|
49
|
+
format: Format,
|
|
50
|
+
): TickMark[] {
|
|
51
|
+
const { span } = spanOf(min, max)
|
|
52
|
+
return (ticks ?? []).flatMap((t, i) =>
|
|
53
|
+
Number.isFinite(t.at) && t.at >= min && t.at <= max
|
|
54
|
+
? [
|
|
55
|
+
{
|
|
56
|
+
key: String(i),
|
|
57
|
+
pct: toPct((t.at - min) / span),
|
|
58
|
+
label: t.label,
|
|
59
|
+
text: `${t.label ?? 'Tick'} ${format(t.at)}`,
|
|
60
|
+
},
|
|
61
|
+
]
|
|
62
|
+
: [],
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
|
|
20
66
|
export const isReading = (n: number | null | undefined): n is number =>
|
|
21
67
|
typeof n === 'number' && Number.isFinite(n)
|
|
22
68
|
|
|
@@ -50,7 +96,14 @@ export function layoutSegments(segments: readonly MeterSegment[], span: number):
|
|
|
50
96
|
const size = Math.max(0, Math.min(clamp01(s.value / span), 1 - cursor))
|
|
51
97
|
const forecast = s.forecast === true
|
|
52
98
|
// A forecast stays the lighter neutral: a colour would turn the committed/forecast pair into hue.
|
|
53
|
-
const part = {
|
|
99
|
+
const part = {
|
|
100
|
+
start: cursor,
|
|
101
|
+
size,
|
|
102
|
+
color: forecast ? undefined : s.color,
|
|
103
|
+
forecast,
|
|
104
|
+
label: s.label,
|
|
105
|
+
amount: s.value,
|
|
106
|
+
}
|
|
54
107
|
cursor += size
|
|
55
108
|
return part
|
|
56
109
|
})
|
|
@@ -62,6 +115,27 @@ export function findHoldingZone(zones: readonly MeterZone[], value: number): Met
|
|
|
62
115
|
return zones.find((z) => value >= z.from && (value < z.to || (value === z.to && z.to === end)))
|
|
63
116
|
}
|
|
64
117
|
|
|
118
|
+
/** Sub-pixel rounding in measured boxes: two labels touching within this do not collide. */
|
|
119
|
+
export const LABEL_SUBPIXEL = 0.5
|
|
120
|
+
|
|
121
|
+
/** A measured label box on one axis (any shared origin), in CSS pixels. */
|
|
122
|
+
export type LabelBox = { left: number; right: number }
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Whether every zone name fits on the band: inside `[0, rowWidth]` and clear of every other name
|
|
126
|
+
* (pairwise, so a long name cannot hide behind a short one between it and a distant neighbour).
|
|
127
|
+
* Boxes are relative to the row's own start edge.
|
|
128
|
+
*/
|
|
129
|
+
export function zoneLabelsFit(boxes: readonly LabelBox[], rowWidth: number): boolean {
|
|
130
|
+
const sorted = boxes.toSorted((a, b) => a.left - b.left)
|
|
131
|
+
return sorted.every(
|
|
132
|
+
(a, i) =>
|
|
133
|
+
a.left >= -LABEL_SUBPIXEL &&
|
|
134
|
+
a.right <= rowWidth + LABEL_SUBPIXEL &&
|
|
135
|
+
sorted.slice(i + 1).every((b) => a.right <= b.left + LABEL_SUBPIXEL),
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
|
|
65
139
|
/** A zone band as left edge + width in percent, clipped to the track. */
|
|
66
140
|
export function zoneBand(zone: MeterZone, min: number, span: number) {
|
|
67
141
|
const lo = clamp01((zone.from - min) / span)
|
|
@@ -80,6 +154,7 @@ export type BarInput = {
|
|
|
80
154
|
segments?: readonly MeterSegment[] | undefined
|
|
81
155
|
zones?: readonly MeterZone[] | undefined
|
|
82
156
|
target?: number | undefined
|
|
157
|
+
ticks?: readonly MeterTick[] | undefined
|
|
83
158
|
color?: string | undefined
|
|
84
159
|
forecastCaption?: string | undefined
|
|
85
160
|
}
|
|
@@ -91,8 +166,15 @@ export type BarModel = {
|
|
|
91
166
|
/** Percent where the first forecast part starts; absent when the forecast opens the track. */
|
|
92
167
|
boundaryPct: number | undefined
|
|
93
168
|
forecastText: string | null
|
|
169
|
+
captionMode: CaptionMode
|
|
170
|
+
/** Only in `parts` mode: one caption per part, in order. Empty otherwise. */
|
|
171
|
+
partCaptions: readonly PartCaption[]
|
|
172
|
+
/** Only on a zoned track: every zone's name, in zone order. Empty otherwise. */
|
|
173
|
+
zoneLabels: readonly ZoneLabel[]
|
|
174
|
+
ticks: readonly TickMark[]
|
|
94
175
|
markerPct: number | undefined
|
|
95
176
|
targetPct: number | undefined
|
|
177
|
+
/** The default start caption (the zone is named on the band, so it is the bare reading); `startCaption` decides whether it shows. */
|
|
96
178
|
startText: string
|
|
97
179
|
endText: string | null
|
|
98
180
|
describedByText: string | null
|
|
@@ -115,8 +197,39 @@ function fillParts(input: BarInput, span: number, zoned: boolean): Part[] {
|
|
|
115
197
|
export const segmentReading = (segments: readonly MeterSegment[], min: number): number =>
|
|
116
198
|
segments.reduce((sum, s) => sum + s.value, min)
|
|
117
199
|
|
|
200
|
+
/** Keyed by index: segments are an ordered list, and labels are the consumer's, so they may repeat. */
|
|
201
|
+
function partCaptionsOf(parts: readonly Part[], format: Format): PartCaption[] {
|
|
202
|
+
return parts.map((p, i) => ({
|
|
203
|
+
key: String(i),
|
|
204
|
+
label: p.label,
|
|
205
|
+
text: format(p.amount),
|
|
206
|
+
color: p.color,
|
|
207
|
+
}))
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Zones win over segments (the union forbids both, a JS caller can still pass them): a zoned track
|
|
212
|
+
* has no fills to caption. Empty segments are the empty state, never categorical.
|
|
213
|
+
*/
|
|
214
|
+
function captionModeOf(
|
|
215
|
+
segments: readonly MeterSegment[] | undefined,
|
|
216
|
+
zoned: boolean,
|
|
217
|
+
firstForecast: number,
|
|
218
|
+
): CaptionMode {
|
|
219
|
+
if (zoned) return 'reading'
|
|
220
|
+
if (segments === undefined || segments.length === 0) return 'reading'
|
|
221
|
+
return firstForecast < 0 ? 'parts' : 'forecast'
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** The start caption: the consumer's override, else the model's default — absent in `parts` mode. */
|
|
225
|
+
export function startCaptionText(model: BarModel, override: ReactNode | undefined): ReactNode {
|
|
226
|
+
if (override !== undefined) return override
|
|
227
|
+
return model.captionMode === 'parts' ? null : model.startText
|
|
228
|
+
}
|
|
229
|
+
|
|
118
230
|
export function modelBar(input: BarInput): BarModel {
|
|
119
231
|
const { reading, min, max, format, segments, zones, target } = input
|
|
232
|
+
const ticks = tickMarks(input.ticks, min, max, format)
|
|
120
233
|
const { span } = spanOf(min, max)
|
|
121
234
|
const zoned = zones !== undefined && zones.length > 0
|
|
122
235
|
const holding = zoned ? findHoldingZone(zones, reading) : undefined
|
|
@@ -126,6 +239,9 @@ export function modelBar(input: BarInput): BarModel {
|
|
|
126
239
|
const forecastLabels =
|
|
127
240
|
segments?.filter((s) => s.forecast === true).map((s) => `${s.label} ${format(s.value)}`) ?? []
|
|
128
241
|
|
|
242
|
+
const captionMode = captionModeOf(segments, zoned, firstForecast)
|
|
243
|
+
const partCaptions = captionMode === 'parts' ? partCaptionsOf(parts, format) : []
|
|
244
|
+
|
|
129
245
|
const zoneText = !zoned
|
|
130
246
|
? []
|
|
131
247
|
: [
|
|
@@ -138,6 +254,7 @@ export function modelBar(input: BarInput): BarModel {
|
|
|
138
254
|
...(segments?.map((s) => segmentText(s, format)) ?? []),
|
|
139
255
|
...zoneText,
|
|
140
256
|
...(target === undefined ? [] : [`Target ${format(target)}`]),
|
|
257
|
+
...ticks.map((t) => t.text),
|
|
141
258
|
]
|
|
142
259
|
|
|
143
260
|
return {
|
|
@@ -148,9 +265,20 @@ export function modelBar(input: BarInput): BarModel {
|
|
|
148
265
|
forecastLabels.length === 0
|
|
149
266
|
? null
|
|
150
267
|
: (input.forecastCaption ?? `Forecast: ${forecastLabels.join(', ')}`),
|
|
268
|
+
captionMode,
|
|
269
|
+
partCaptions,
|
|
270
|
+
zoneLabels: !zoned
|
|
271
|
+
? []
|
|
272
|
+
: zones.map((z, i) => ({
|
|
273
|
+
key: String(i),
|
|
274
|
+
label: z.label,
|
|
275
|
+
startPct: zoneBand(z, min, span).start,
|
|
276
|
+
holding: z === holding,
|
|
277
|
+
})),
|
|
278
|
+
ticks,
|
|
151
279
|
markerPct: zoned ? toPct((reading - min) / span) : undefined,
|
|
152
280
|
targetPct: target === undefined ? undefined : toPct((target - min) / span),
|
|
153
|
-
startText:
|
|
281
|
+
startText: format(reading),
|
|
154
282
|
endText: endCaptionText(reading, target, format),
|
|
155
283
|
describedByText: describedBy.length > 0 ? describedBy.join('; ') : null,
|
|
156
284
|
}
|