@loadmap/react 0.39.0 → 0.41.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.
@@ -1,6 +1,6 @@
1
1
  import { Evaluation, Model, Scenario, Values } from "@loadmap/core";
2
2
  import { Direction, ElkEngine, Scene, View, labelsAt } from "@loadmap/layout";
3
- import { ReactNode, Ref } from "react";
3
+ import { CSSProperties, ReactNode, Ref } from "react";
4
4
  import { Room } from "@loadmap/layout/internal";
5
5
  import { Particle, Sim, SimOptions } from "@loadmap/sim";
6
6
  /** A part of the scene the board shows, in scene units. */
@@ -320,10 +320,16 @@ interface LoadmapOptions<P> extends SimOptions {
320
320
  * holds it: 1 never folds. A narrow screen (`narrowBelow`) never folds.
321
321
  */
322
322
  rows?: number | 'auto';
323
- /** What full screen opens with: the panels around the diagram, or mainly the diagram and the story. Default panels. */
323
+ /**
324
+ * How much stands around the diagram in full screen, and in a root that fills its frame: `all`, with the panels'
325
+ * tabs, or `least`, the diagram, the load and the verdict alone. In full screen the reader switches it (the Panels
326
+ * button, F); a root that fills shows what the page picked. Default `all`.
327
+ */
328
+ chrome?: Chrome;
329
+ /** @deprecated `chrome` now: `panels` is `all`, `focus` is `least`. */
324
330
  fullScreenMode?: FullScreenMode;
325
331
  /**
326
- * The root fills its container, laid out as full screen is (in `fullScreenMode`), and stays so: no full screen
332
+ * The root fills its container, laid out as full screen is (with its `chrome`), and stays so: no full screen
327
333
  * button. For a page of its own made to be framed, such as an embed: the container takes the frame's height, and the
328
334
  * layout follows the frame's size as full screen follows the screen's.
329
335
  */
@@ -346,8 +352,10 @@ interface LoadmapOptions<P> extends SimOptions {
346
352
  */
347
353
  transition?: number;
348
354
  }
349
- /** Full screen with every panel it lists in a compact form, or mainly the diagram and the story. */
355
+ /** What stands around the diagram in full screen, as the controller holds it: `panels` is `chrome` `all`, `focus` is `least`. */
350
356
  type FullScreenMode = 'panels' | 'focus';
357
+ /** What stands around the diagram in full screen: everything, or the least. */
358
+ type Chrome = 'all' | 'least';
351
359
  interface LoadmapController<P = any> {
352
360
  model: Model<P>;
353
361
  view: View;
@@ -404,6 +412,32 @@ interface LoadmapController<P = any> {
404
412
  }
405
413
  /** The state behind a loadmap: values, the model's evaluation, the layout and the engine. */
406
414
  declare function useLoadmap<P>(model: Model<P>, whole: View, o?: LoadmapOptions<P>): LoadmapController<P>;
415
+ /** The architecture: SVG for the structure, a canvas on top for the dots. Expands to full screen. */
416
+ interface DiagramProps {
417
+ lm: LoadmapController;
418
+ legend?: boolean;
419
+ /** A click (or Enter) on a box, by its id. Every box is a button then, one without a component too. */
420
+ onNodeClick?: (id: string) => void;
421
+ /** The box drawn as selected, by its id. */
422
+ selected?: string | null;
423
+ /**
424
+ * The board fills its container, which gives it a height, as a canvas the reader pans and zooms (the wheel alone
425
+ * zooms it), with no Full screen button. Insets are the pixels on each side that something of the page floats over:
426
+ * Fit frames the diagram in what they leave, and the legend and the zoom buttons stay clear of them. `reserve` is
427
+ * the part of them that is always there: the diagram is arranged for what `reserve` leaves, so the rest (a panel
428
+ * the reader opens) moves the view and never the layout. Without it the diagram is arranged for what Fit has.
429
+ * `scrolls` says the canvas lies in a page that scrolls: the wheel then zooms only with Ctrl or ⌘ held, and one
430
+ * finger scrolls the page while two pan and pinch the board.
431
+ */
432
+ canvas?: boolean | (Partial<Insets> & {
433
+ reserve?: Partial<Insets>;
434
+ scrolls?: boolean;
435
+ });
436
+ }
437
+ declare function Diagram({ lm, legend, onNodeClick, selected, canvas }: DiagramProps): import("react").JSX.Element;
438
+ /** A frame's panels, in the order of their tabs: one opens at a time. */
439
+ declare const FRAME_PANELS: readonly ['limits', 'controls', 'headroom', 'live', 'assumptions'];
440
+ type FramePanel = (typeof FRAME_PANELS)[number];
407
441
  /** `auto` follows the system's light or dark; `inherit` the page's own switch (`data-theme`, or a `dark` or `light` class, on `<html>`). */
408
442
  type Theme = 'auto' | 'light' | 'dark' | 'inherit';
409
443
  /**
@@ -453,9 +487,11 @@ declare function LoadmapRoot({ lm, theme, children }: {
453
487
  theme?: Theme;
454
488
  children: ReactNode;
455
489
  }): import("react").JSX.Element;
456
- /** The panels full screen shows with the diagram, compact: the verdict and the assumptions in a line under it. */
457
- declare const LOADMAP_PANELS: readonly ['verdict', 'live', 'limits', 'controls', 'headroom', 'compare', 'assumptions'];
458
- type LoadmapPanel = (typeof LOADMAP_PANELS)[number];
490
+ /** A loadmap's panels, in the order of their tabs: one opens at a time, beside the diagram. */
491
+ declare const LOADMAP_PANELS: readonly ["limits", "controls", "headroom", "live", "assumptions"];
492
+ type LoadmapPanel = FramePanel;
493
+ /** The tabs a list of names stands for, old names or new, in the tabs' own order. */
494
+ declare function panelsOf(names: readonly string[]): LoadmapPanel[];
459
495
  interface LoadmapProps<P> extends LoadmapOptions<P> {
460
496
  model: Model<P>;
461
497
  view: View;
@@ -464,25 +500,42 @@ interface LoadmapProps<P> extends LoadmapOptions<P> {
464
500
  title?: ReactNode | false;
465
501
  /** Lines added before the generated assumptions. */
466
502
  assumptions?: readonly ReactNode[];
467
- /** The headroom chart. Default true. */
503
+ /** The headroom chart, in the Headroom tab. Default true. Without it and the comparison the tab is left out. */
468
504
  headroom?: boolean;
469
- /** The scenarios side by side, when the model declares two or more. Default true. */
505
+ /** The scenarios side by side under the headroom chart, when the model declares two or more. Default true. */
470
506
  compare?: boolean;
507
+ /** The panels offered, in the order of their tabs. Default every one; Design only when the model has one. */
508
+ panels?: readonly LoadmapPanel[];
509
+ /** The panel that is open (one of the page's own by its id); null for none. Leave it out and the loadmap keeps it itself. */
510
+ open?: LoadmapPanel | (string & {}) | null;
511
+ /** The panel open at first, when the loadmap keeps it itself. */
512
+ defaultOpen?: LoadmapPanel | (string & {}) | null;
513
+ /** A panel opened or closed (null). */
514
+ onOpen?: (panel: LoadmapPanel | (string & {}) | null) => void;
471
515
  /**
472
- * The panels full screen shows, compact, below the diagram. Default every one the page shows. Focus mode shows the
473
- * verdict of these. The scenarios and the load are in the bar over the diagram, in every layout.
516
+ * The page around it scrolls: the plain wheel and one finger are the page's. Default: true in a page, false with
517
+ * `fill` (say so for a root that fills a frame of a page that scrolls). Never in full screen.
474
518
  */
475
- fullScreen?: readonly LoadmapPanel[];
519
+ scrolls?: boolean;
520
+ /** @deprecated `panels` now, for the page and full screen alike. Its old names still stand for their tabs. */
521
+ fullScreen?: readonly string[];
476
522
  /** A click (or Enter) on a box of the diagram, by its id: every box is a button then. */
477
523
  onNodeClick?: (id: string) => void;
478
524
  /** The box drawn as selected, by its id. */
479
525
  selected?: string | null;
526
+ /**
527
+ * The page's own, in the canvas: each `Canvas.Panel` a panel whose tab comes before the loadmap's, anything else
528
+ * drawn over the canvas.
529
+ */
530
+ children?: ReactNode;
480
531
  ref?: Ref<LoadmapHandle>;
481
532
  }
482
533
  /**
483
- * The whole page piece: the scenarios and the load in a bar over the diagram, the diagram, verdict, headroom, live
484
- * meters, limits, the design controls and assumptions.
534
+ * The whole piece in one box the page sizes (by default `clamp(420px, 70dvh, 760px)` high): the title, the scenarios
535
+ * and the story over the diagram, the load and the verdict under it, and the limits, the design, the headroom, the
536
+ * live meters and the assumptions in panels that open from their tabs. Full screen is the same box at the screen's
537
+ * size.
485
538
  */
486
- declare function Loadmap<P>({ model, view, theme, title, assumptions, headroom, compare, fullScreen, onNodeClick, selected, ref, ...o }: LoadmapProps<P>): import("react").JSX.Element;
539
+ declare function Loadmap<P>({ model, view, theme, title, assumptions, headroom, compare, panels, open, defaultOpen, onOpen, scrolls, fullScreen, onNodeClick, selected, children, ref, ...o }: LoadmapProps<P>): import("react").JSX.Element;
487
540
  declare function handleOf(lm: LoadmapController): LoadmapHandle;
488
- export { Insets as _, LoadmapProps as a, handleOf as c, LoadmapController as d, LoadmapOptions as f, Engine as g, Dot as h, LoadmapPanel as i, usePageTheme as l, Cue as m, Loadmap as n, LoadmapRoot as o, useLoadmap as p, LoadmapHandle as r, Theme as s, LOADMAP_PANELS as t, FullScreenMode as u };
541
+ export { useLoadmap as _, LoadmapProps as a, Engine as b, handleOf as c, Diagram as d, DiagramProps as f, LoadmapOptions as g, LoadmapController as h, LoadmapPanel as i, panelsOf as l, FullScreenMode as m, Loadmap as n, LoadmapRoot as o, Chrome as p, LoadmapHandle as r, Theme as s, LOADMAP_PANELS as t, usePageTheme as u, Cue as v, Dot as y };
@@ -0,0 +1,209 @@
1
+ import { capital, fmt, multiple, pct } from "@loadmap/core/format";
2
+ // src/ranks.ts
3
+ /** Badges after the limit: the two next in line. */
4
+ const BADGES = 2;
5
+ const ORDINAL = [
6
+ "",
7
+ "limit",
8
+ "2nd",
9
+ "3rd"
10
+ ];
11
+ /** Where a component reaches the target, on the pessimistic edge the ranking uses. */
12
+ const edge = (c) => c.targetRange?.low ?? c.target;
13
+ /**
14
+ * The limit and the next two in line, by component: the multiple at which each reaches the target, the number the
15
+ * headline gives. The next ones only when they reach it within the load's range, so a box far away wears nothing.
16
+ */
17
+ function rankLabels(ev, model) {
18
+ const out = /* @__PURE__ */ new Map();
19
+ const first = ev.firstLimit;
20
+ if (!first) return out;
21
+ const at = (c) => multiple(edge(c), model.searchMax);
22
+ out.set(first.id, {
23
+ rank: 1,
24
+ text: `LIMIT · ${at(first)}`,
25
+ word: "limit"
26
+ });
27
+ ev.ranked.filter((c) => c !== first && c.util != null).slice(0, BADGES).forEach((c, i) => {
28
+ const x = edge(c);
29
+ if (x == null || !Number.isFinite(x) || x > model.load.max) return;
30
+ const rank = i + 2;
31
+ out.set(c.id, {
32
+ rank,
33
+ text: `${ORDINAL[rank]} · ${at(c)}`,
34
+ word: ORDINAL[rank]
35
+ });
36
+ });
37
+ return out;
38
+ }
39
+ /** The limit moves from one component to another between these values. */
40
+ const handsOver = (a, b) => !!a.firstLimit && !!b.firstLimit && a.firstLimit.id !== b.firstLimit.id;
41
+ /**
42
+ * What the story says when a step hands the limit to another component: "API gateway fixed: the worker pool is next,
43
+ * at 10×" when the old limit got further away, else "The push broker overtakes the position consumer: the limit is
44
+ * 18× now". Null when the limit stays.
45
+ */
46
+ function handoverText(a, b, model) {
47
+ const was = a.firstLimit;
48
+ const now = b.firstLimit;
49
+ if (!was || !now || !handsOver(a, b)) return null;
50
+ const at = multiple(edge(now), model.searchMax);
51
+ const before = edge(was);
52
+ const after = b.components[was.id] ? edge(b.components[was.id]) : null;
53
+ return before != null && (after == null || after > before) ? `${was.name} fixed: ${now.short} ${now.plural ? "are" : "is"} next, at ${at}` : `${capital(now.short)} ${now.plural ? "overtake" : "overtakes"} ${was.short}: the limit is ${at} now`;
54
+ }
55
+ // src/camera.ts
56
+ /**
57
+ * The shots of a story played, in seconds: the whole diagram first, then for each step a flight in to what it changes,
58
+ * a moment close, the change held close, a flight out, and the whole diagram for the step's own seconds. A step that
59
+ * hands the limit to another component adds `hand`: a flight to the new limit (`HAND_FLY`), whose ring then comes in.
60
+ * `back` is the flight out when the reader pauses.
61
+ */
62
+ const SHOTS = {
63
+ establish: 1.2,
64
+ in: 1,
65
+ close: .5,
66
+ hand: 1.8,
67
+ out: 1,
68
+ back: .4
69
+ };
70
+ /** The change is held close until the structure has moved and settled. */
71
+ const changeSeconds = (transition) => Math.max(1.6, transition + .7);
72
+ /** A step played, from the flight in to the end of its hold on the whole diagram; `hands` when it moves the limit. */
73
+ const stepSeconds = (st, transition, hands = false) => SHOTS.in + SHOTS.close + changeSeconds(transition) + (hands ? SHOTS.hand : 0) + SHOTS.out + st.seconds;
74
+ /** The whole story played from `start`, the values shown when it starts, from the first shot of the whole diagram. */
75
+ function storySeconds(model, start, transition) {
76
+ let ev = start;
77
+ let t = SHOTS.establish;
78
+ for (const st of model.story) {
79
+ const after = model.evaluate(st.values);
80
+ t += stepSeconds(st, transition, handsOver(ev, after));
81
+ ev = after;
82
+ }
83
+ return t;
84
+ }
85
+ const whole = (s) => ({
86
+ x: 0,
87
+ y: 0,
88
+ w: s.width,
89
+ h: s.height
90
+ });
91
+ /** The component a box stands for: its stages' component, or its own. */
92
+ function boxComponent(ev, n) {
93
+ return n.stages.map((s) => ev.stages[s]?.component).find(Boolean) ?? n.component ?? void 0;
94
+ }
95
+ /** Whether a box stands for a component: its own, and for a closed subsystem's box every component inside. */
96
+ const showsComponent = (ev, n, c) => n.subsystem != null ? n.stages.some((s) => ev.stages[s]?.component === c) : boxComponent(ev, n) === c;
97
+ /**
98
+ * The boxes a step's change is about, from the values before (`a`) to the step's (`b`): the box it points at and any box
99
+ * the change reshapes; else the boxes it reshapes or an edge that comes or goes ends at; else those whose component
100
+ * changes state; else the first limit after it.
101
+ */
102
+ function targets(scene, a, b, shapes, focus) {
103
+ const ids = new Set(scene.nodes.map((n) => n.id));
104
+ const reshaped = /* @__PURE__ */ new Set();
105
+ for (const id of ids) if (shapes.a.nodes[id] !== shapes.b.nodes[id]) reshaped.add(id);
106
+ const edgesA = new Set(shapes.a.edges.map((e) => e.id));
107
+ const edgesB = new Set(shapes.b.edges.map((e) => e.id));
108
+ for (const e of [...shapes.a.edges, ...shapes.b.edges]) if (edgesA.has(e.id) !== edgesB.has(e.id)) {
109
+ for (const end of [e.from, e.to]) if (ids.has(end)) reshaped.add(end);
110
+ }
111
+ const of = (c) => scene.nodes.filter((n) => c && showsComponent(b, n, c)).map((n) => n.id);
112
+ const order = (s) => scene.nodes.filter((n) => s.has(n.id)).map((n) => n.id);
113
+ if (focus) return order(/* @__PURE__ */ new Set([...of(focus), ...reshaped]));
114
+ if (reshaped.size) return order(reshaped);
115
+ const turned = scene.nodes.filter((n) => {
116
+ const c = boxComponent(b, n);
117
+ return c && a.components[c]?.state !== b.components[c]?.state;
118
+ });
119
+ if (turned.length) return turned.map((n) => n.id);
120
+ return of(b.firstLimit?.id);
121
+ }
122
+ /** The closest the camera comes: the boxes framed never fill more than this share of the board's width. */
123
+ const MAX_ZOOM = 2.5;
124
+ /** Boxes that take more than this share of the board's width are shown on the whole diagram. */
125
+ const WIDE = .8;
126
+ /**
127
+ * The part of the scene that shows these boxes with room around them, at the scene's own shape (the board's), at most
128
+ * 2.5× closer, inside the scene. The whole scene when there is nothing to frame or it would hardly be closer; with
129
+ * `hold`, as wide as it takes instead (a framing that follows boxes as they grow never jumps out).
130
+ */
131
+ function framing(scene, ids, o = {}) {
132
+ const all = whole(scene);
133
+ const ns = scene.nodes.filter((n) => ids.includes(n.id));
134
+ if (!ns.length || !(scene.width > 0 && scene.height > 0)) return all;
135
+ const x0 = Math.min(...ns.map((n) => n.x));
136
+ const y0 = Math.min(...ns.map((n) => n.y));
137
+ const x1 = Math.max(...ns.map((n) => n.x + n.w));
138
+ const y1 = Math.max(...ns.map((n) => n.y + n.h));
139
+ const aspect = scene.width / scene.height;
140
+ let w = Math.max(x1 - x0 + 60, (y1 - y0 + 60) * aspect, scene.width / MAX_ZOOM);
141
+ if (w > WIDE * scene.width && !o.hold) return all;
142
+ w = Math.min(w, scene.width);
143
+ const h = w / aspect;
144
+ const clamp = (v, hi) => Math.max(0, Math.min(hi, v));
145
+ return {
146
+ x: clamp((x0 + x1 - w) / 2, scene.width - w),
147
+ y: clamp((y0 + y1 - h) / 2, scene.height - h),
148
+ w,
149
+ h
150
+ };
151
+ }
152
+ /** Cubic in and out. */
153
+ const ease = (t) => t < .5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2;
154
+ /**
155
+ * The camera `t` of the way from `a` to `b` (eased): the centre moves evenly, the size changes by the same factor
156
+ * each moment, so a zoom feels even whatever its depth.
157
+ */
158
+ function between(a, b, t) {
159
+ const k = ease(Math.max(0, Math.min(1, t)));
160
+ const w = a.w * (b.w / a.w) ** k;
161
+ const h = a.h * (b.h / a.h) ** k;
162
+ const cx = a.x + a.w / 2 + (b.x + b.w / 2 - a.x - a.w / 2) * k;
163
+ const cy = a.y + a.h / 2 + (b.y + b.h / 2 - a.y - a.h / 2) * k;
164
+ return {
165
+ x: cx - w / 2,
166
+ y: cy - h / 2,
167
+ w,
168
+ h
169
+ };
170
+ }
171
+ /** A label's first words: "Users, × today" says "Users". */
172
+ const short = (label) => label.split(",")[0].trim();
173
+ /** At most this many changed values in a title; the rest are counted. */
174
+ const TITLE_VALUES = 2;
175
+ /**
176
+ * The few words the diagram shows for a step: its own title, or what it changes ("Proposed: Gateway instances 1 → 2,
177
+ * Worker pods 3 → 6"), numbered.
178
+ */
179
+ function stepTitle(model, from, st, i) {
180
+ if (st.title) return `${i + 1} · ${st.title}`;
181
+ const say = (k, v) => {
182
+ if (k === "load") return `${fmt(v)}×`;
183
+ const def = model.params.find((x) => x.id === k)?.def;
184
+ if (typeof v === "boolean") return v ? "on" : "off";
185
+ if (def && def.kind !== "load" && "format" in def && def.format) return def.format(v);
186
+ return typeof v === "number" ? fmt(v) : String(v);
187
+ };
188
+ const changed = Object.keys(st.values).filter((k) => from[k] !== st.values[k]);
189
+ const parts = changed.slice(0, TITLE_VALUES).map((k) => {
190
+ const label = k === "load" ? model.load.label : model.params.find((x) => x.id === k)?.def.label ?? k;
191
+ const to = st.values[k];
192
+ const was = from[k];
193
+ return typeof to === "boolean" ? `${short(label)} ${say(k, to)}` : `${short(label)} ${say(k, was)} → ${say(k, to)}`;
194
+ });
195
+ if (changed.length > TITLE_VALUES) parts.push(`${changed.length - TITLE_VALUES} more`);
196
+ const what = parts.join(", ") || "Nothing changes";
197
+ return `${i + 1} · ${st.scenario && changed.length ? `${st.scenario}: ${what}` : what}`;
198
+ }
199
+ /** What the framed components show: their utilization now, or before and after the change. */
200
+ function stepDetail(a, b, components) {
201
+ return components.slice(0, 2).map((c) => {
202
+ const was = a.components[c];
203
+ const now = b?.components[c];
204
+ if (!was) return "";
205
+ const u = (x) => x == null ? "off" : pct(x);
206
+ return now ? `${was.name} ${u(was.util)} → ${u(now.util)}` : `${was.name} ${u(was.util)}`;
207
+ }).filter(Boolean).join(" · ");
208
+ }
209
+ export { ease as a, stepDetail as c, storySeconds as d, targets as f, rankLabels as g, handoverText as h, changeSeconds as i, stepSeconds as l, edge as m, between as n, framing as o, whole as p, boxComponent as r, showsComponent as s, SHOTS as t, stepTitle as u };
@@ -0,0 +1,24 @@
1
+ import { w as Legend } from "./Loadmap-CM8HshXp.js";
2
+ import { a as Verdict, i as Meters, n as Controls, r as Limits, t as Assumptions } from "./panels-CemD3oVS.js";
3
+ import { n as HeadroomChart, t as Compare } from "./scenarios-D7DDcwS5.js";
4
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
5
+ // src/framePanels.tsx
6
+ /**
7
+ * What a frame's panel holds, whole: put together with the first panel a reader opens.
8
+ */
9
+ function FramePanelBody({ lm, id, assumptions, headroom = true, compare = true }) {
10
+ switch (id) {
11
+ case "limits": return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(Verdict, { lm }), /* @__PURE__ */ jsx(Limits, { lm })] });
12
+ case "controls": return /* @__PURE__ */ jsx(Controls, {
13
+ lm,
14
+ load: false
15
+ });
16
+ case "headroom": return /* @__PURE__ */ jsxs(Fragment, { children: [headroom && /* @__PURE__ */ jsx(HeadroomChart, { lm }), compare && lm.model.scenarios.length > 1 && /* @__PURE__ */ jsx(Compare, { lm })] });
17
+ case "live": return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(Meters, { lm }), /* @__PURE__ */ jsx(Legend, { lm })] });
18
+ case "assumptions": return /* @__PURE__ */ jsx(Assumptions, {
19
+ lm,
20
+ extra: assumptions ? [...assumptions] : []
21
+ });
22
+ }
23
+ }
24
+ export { FramePanelBody };