@makoai/app-sdk 2.5.0 → 2.7.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/README.md +89 -5
- package/index.d.ts +6 -0
- package/index.js +111 -9
- package/package.json +14 -4
- package/ui-core.js +48 -0
- package/ui.css +280 -0
- package/ui.d.ts +141 -0
- package/ui.js +685 -0
- package/vite.d.ts +8 -0
- package/vite.js +116 -27
package/ui.js
ADDED
|
@@ -0,0 +1,685 @@
|
|
|
1
|
+
// @makoai/app-sdk/ui — Mako's house dashboard kit. Plain ESM, no build step,
|
|
2
|
+
// no dependencies beyond React: styles come from `@makoai/app-sdk/ui.css`,
|
|
3
|
+
// colours from the theme tokens the SDK injects (index.js).
|
|
4
|
+
//
|
|
5
|
+
// What lives here is what the workspace kept re-forking app to app: the
|
|
6
|
+
// refresh-every-binding button and its progress modal, the multi-select
|
|
7
|
+
// filter, the freshness badge's popover, and the few page blocks (header,
|
|
8
|
+
// card, KPI tile) a new app starts from. Anything app-specific — which
|
|
9
|
+
// bindings are heavy, how freshness is computed — stays in the app and comes
|
|
10
|
+
// in through props.
|
|
11
|
+
import * as React from "react";
|
|
12
|
+
import { createPortal } from "react-dom";
|
|
13
|
+
import { refreshBinding } from "./index.js";
|
|
14
|
+
import { formatSeconds as fmtSecs, normalizeTone, refreshOrder } from "./ui-core.js";
|
|
15
|
+
|
|
16
|
+
export { normalizeTone, refreshOrder };
|
|
17
|
+
|
|
18
|
+
const h = React.createElement;
|
|
19
|
+
|
|
20
|
+
/** Join class names, skipping falsy ones. */
|
|
21
|
+
export function cx(...parts) {
|
|
22
|
+
return parts.filter(Boolean).join(" ");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
// Icons (inline, so the kit needs no icon library)
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
function svg(size, strokeWidth, children, className) {
|
|
29
|
+
return h(
|
|
30
|
+
"svg",
|
|
31
|
+
{
|
|
32
|
+
className,
|
|
33
|
+
width: size,
|
|
34
|
+
height: size,
|
|
35
|
+
viewBox: "0 0 24 24",
|
|
36
|
+
fill: "none",
|
|
37
|
+
stroke: "currentColor",
|
|
38
|
+
strokeWidth,
|
|
39
|
+
strokeLinecap: "round",
|
|
40
|
+
strokeLinejoin: "round",
|
|
41
|
+
"aria-hidden": "true",
|
|
42
|
+
},
|
|
43
|
+
...children,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
const IconCheck = ({ size = 11 }) =>
|
|
47
|
+
svg(size, 3.2, [h("polyline", { key: "p", points: "20 6 9 17 4 12" })]);
|
|
48
|
+
const IconChevron = ({ className }) =>
|
|
49
|
+
svg(12, 2.2, [h("polyline", { key: "p", points: "6 9 12 15 18 9" })], className);
|
|
50
|
+
const IconX = ({ size = 16 }) =>
|
|
51
|
+
svg(size, 2, [h("path", { key: "p", d: "M18 6 6 18M6 6l12 12" })]);
|
|
52
|
+
const IconRefresh = () =>
|
|
53
|
+
svg(14, 2.2, [
|
|
54
|
+
h("path", { key: "a", d: "M3 12a9 9 0 0 1 9-9 9.75 9.75 0 0 1 6.74 2.74L21 8" }),
|
|
55
|
+
h("path", { key: "b", d: "M21 3v5h-5" }),
|
|
56
|
+
h("path", { key: "c", d: "M21 12a9 9 0 0 1-9 9 9.75 9.75 0 0 1-6.74-2.74L3 16" }),
|
|
57
|
+
h("path", { key: "d", d: "M3 21v-5h5" }),
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
// Popover plumbing shared by MultiSelect and FreshnessBadge
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
const PHONE = "(max-width: 640px)";
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Close on outside click and Escape, and keep the `.mk-pop` inside `wrapRef`
|
|
67
|
+
* within the viewport: it opens under its trigger aligned to `align`, flips
|
|
68
|
+
* to the other edge when that would overflow, and as a last resort is pinned
|
|
69
|
+
* to the viewport margin. On phones the popover is a bottom sheet (CSS) and
|
|
70
|
+
* is left alone.
|
|
71
|
+
*/
|
|
72
|
+
function usePopover(open, setOpen, wrapRef, align) {
|
|
73
|
+
React.useEffect(() => {
|
|
74
|
+
if (!open) return undefined;
|
|
75
|
+
const onDoc = e => {
|
|
76
|
+
if (wrapRef.current && !wrapRef.current.contains(e.target)) setOpen(false);
|
|
77
|
+
};
|
|
78
|
+
const onKey = e => {
|
|
79
|
+
if (e.key === "Escape") setOpen(false);
|
|
80
|
+
};
|
|
81
|
+
document.addEventListener("mousedown", onDoc);
|
|
82
|
+
document.addEventListener("keydown", onKey);
|
|
83
|
+
return () => {
|
|
84
|
+
document.removeEventListener("mousedown", onDoc);
|
|
85
|
+
document.removeEventListener("keydown", onKey);
|
|
86
|
+
};
|
|
87
|
+
}, [open, setOpen, wrapRef]);
|
|
88
|
+
|
|
89
|
+
React.useLayoutEffect(() => {
|
|
90
|
+
if (!open) return undefined;
|
|
91
|
+
const place = () => {
|
|
92
|
+
const pop = wrapRef.current?.querySelector(".mk-pop");
|
|
93
|
+
if (!pop) return;
|
|
94
|
+
pop.style.left = "";
|
|
95
|
+
pop.style.right = "";
|
|
96
|
+
if (window.matchMedia(PHONE).matches) return;
|
|
97
|
+
const margin = 8;
|
|
98
|
+
const start = align === "end" ? "right" : "left";
|
|
99
|
+
const other = start === "left" ? "right" : "left";
|
|
100
|
+
pop.style[start] = "0";
|
|
101
|
+
pop.style[other] = "auto";
|
|
102
|
+
let r = pop.getBoundingClientRect();
|
|
103
|
+
if (r.right > window.innerWidth - margin || r.left < margin) {
|
|
104
|
+
pop.style[start] = "auto";
|
|
105
|
+
pop.style[other] = "0";
|
|
106
|
+
r = pop.getBoundingClientRect();
|
|
107
|
+
}
|
|
108
|
+
if (r.left < margin) {
|
|
109
|
+
const wrap = wrapRef.current.getBoundingClientRect();
|
|
110
|
+
pop.style.right = "auto";
|
|
111
|
+
pop.style.left = `${margin - wrap.left}px`;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
place();
|
|
115
|
+
window.addEventListener("resize", place);
|
|
116
|
+
return () => window.removeEventListener("resize", place);
|
|
117
|
+
}, [open, wrapRef, align]);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// ---------------------------------------------------------------------------
|
|
121
|
+
// Page blocks
|
|
122
|
+
// ---------------------------------------------------------------------------
|
|
123
|
+
|
|
124
|
+
/** Outline button; `variant="primary"` is the filled one, `"ghost"` borderless. */
|
|
125
|
+
export const Button = React.forwardRef(function Button(
|
|
126
|
+
{ variant = "outline", size = "md", className, type = "button", ...rest },
|
|
127
|
+
ref,
|
|
128
|
+
) {
|
|
129
|
+
return h("button", {
|
|
130
|
+
ref,
|
|
131
|
+
type,
|
|
132
|
+
className: cx("mk-btn", `mk-btn--${variant}`, size === "sm" && "mk-btn--sm", className),
|
|
133
|
+
...rest,
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
/** A status dot: `ok` | `warn` | `bad` | `neutral` | `loading`. */
|
|
138
|
+
export function StatusDot({ tone = "neutral", className }) {
|
|
139
|
+
return h("span", { className: cx("mk-dot", `mk-tone-${normalizeTone(tone)}`, className) });
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** The page's title row: title + subtitle on the left, actions on the right. */
|
|
143
|
+
export function PageHeader({ title, subtitle, actions, className, children }) {
|
|
144
|
+
return h(
|
|
145
|
+
"header",
|
|
146
|
+
{ className: cx("mk-header", className) },
|
|
147
|
+
h(
|
|
148
|
+
"div",
|
|
149
|
+
{ className: "mk-header-text" },
|
|
150
|
+
h("h1", { className: "mk-header-title" }, title),
|
|
151
|
+
subtitle != null && h("p", { className: "mk-header-subtitle" }, subtitle),
|
|
152
|
+
),
|
|
153
|
+
actions != null && h("div", { className: "mk-header-actions" }, actions),
|
|
154
|
+
children,
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** A 12px panel with an optional title row. */
|
|
159
|
+
export function Card({ title, description, actions, className, children, ...rest }) {
|
|
160
|
+
const head =
|
|
161
|
+
title != null || actions != null
|
|
162
|
+
? h(
|
|
163
|
+
"div",
|
|
164
|
+
{ className: "mk-card-head" },
|
|
165
|
+
h(
|
|
166
|
+
"div",
|
|
167
|
+
null,
|
|
168
|
+
title != null && h("h2", { className: "mk-card-title" }, title),
|
|
169
|
+
description != null && h("p", { className: "mk-card-desc" }, description),
|
|
170
|
+
),
|
|
171
|
+
actions != null && h("div", { className: "mk-card-actions" }, actions),
|
|
172
|
+
)
|
|
173
|
+
: null;
|
|
174
|
+
return h("section", { className: cx("mk-card", className), ...rest }, head, children);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** One KPI: label, big value, an optional delta (tone colours it) and hint. */
|
|
178
|
+
export function KpiTile({ label, value, delta, tone, hint, className }) {
|
|
179
|
+
return h(
|
|
180
|
+
"div",
|
|
181
|
+
{ className: cx("mk-kpi", className) },
|
|
182
|
+
h("div", { className: "mk-kpi-label" }, label),
|
|
183
|
+
h("div", { className: "mk-kpi-value" }, value),
|
|
184
|
+
(delta != null || hint != null) &&
|
|
185
|
+
h(
|
|
186
|
+
"div",
|
|
187
|
+
{ className: "mk-kpi-foot" },
|
|
188
|
+
delta != null &&
|
|
189
|
+
h("span", { className: cx("mk-kpi-delta", tone && `mk-tone-${normalizeTone(tone)}`) }, delta),
|
|
190
|
+
hint != null && h("span", { className: "mk-kpi-hint" }, hint),
|
|
191
|
+
),
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** A row of KPI tiles that wraps. */
|
|
196
|
+
export function KpiRow({ className, children }) {
|
|
197
|
+
return h("div", { className: cx("mk-kpi-row", className) }, children);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// ---------------------------------------------------------------------------
|
|
201
|
+
// MultiSelect
|
|
202
|
+
// ---------------------------------------------------------------------------
|
|
203
|
+
function toOption(o) {
|
|
204
|
+
return typeof o === "string" ? { value: o, label: o } : o;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Multi-select filter (shadcn combobox look). Controlled: `value` is the
|
|
209
|
+
* selected values, `onChange` gets the next array.
|
|
210
|
+
*
|
|
211
|
+
* `emptyMeansAll` (default true): an empty selection is "all" — the common
|
|
212
|
+
* filter shape. Set it to false when "all" is every value selected; the
|
|
213
|
+
* "All" row then selects every option instead of clearing.
|
|
214
|
+
*/
|
|
215
|
+
export function MultiSelect({
|
|
216
|
+
options,
|
|
217
|
+
value,
|
|
218
|
+
onChange,
|
|
219
|
+
allLabel = "All",
|
|
220
|
+
emptyMeansAll = true,
|
|
221
|
+
showOnly = false,
|
|
222
|
+
maxChips = 2,
|
|
223
|
+
align = "start",
|
|
224
|
+
className,
|
|
225
|
+
"aria-label": ariaLabel,
|
|
226
|
+
}) {
|
|
227
|
+
const [open, setOpen] = React.useState(false);
|
|
228
|
+
const ref = React.useRef(null);
|
|
229
|
+
usePopover(open, setOpen, ref, align);
|
|
230
|
+
const opts = options.map(toOption);
|
|
231
|
+
const allOn = emptyMeansAll ? value.length === 0 : value.length === opts.length;
|
|
232
|
+
const chosen = opts.filter(o => value.includes(o.value));
|
|
233
|
+
|
|
234
|
+
const toggle = v =>
|
|
235
|
+
onChange(value.includes(v) ? value.filter(x => x !== v) : [...value, v]);
|
|
236
|
+
const all = () => onChange(emptyMeansAll ? [] : opts.map(o => o.value));
|
|
237
|
+
|
|
238
|
+
const box = on => h("span", { className: cx("mk-ms-box", on && "on") }, on && h(IconCheck));
|
|
239
|
+
const dot = o =>
|
|
240
|
+
o.color ? h("span", { className: "mk-ms-swatch", style: { background: o.color } }) : null;
|
|
241
|
+
|
|
242
|
+
return h(
|
|
243
|
+
"div",
|
|
244
|
+
{ className: cx("mk-ms", className), ref },
|
|
245
|
+
h(
|
|
246
|
+
"button",
|
|
247
|
+
{
|
|
248
|
+
type: "button",
|
|
249
|
+
className: cx("mk-ms-trigger", open && "open"),
|
|
250
|
+
"aria-haspopup": "listbox",
|
|
251
|
+
"aria-expanded": open,
|
|
252
|
+
"aria-label": ariaLabel,
|
|
253
|
+
onClick: () => setOpen(o => !o),
|
|
254
|
+
},
|
|
255
|
+
h(
|
|
256
|
+
"span",
|
|
257
|
+
{ className: "mk-ms-value" },
|
|
258
|
+
allOn || chosen.length === 0
|
|
259
|
+
? h("span", null, allLabel)
|
|
260
|
+
: h(
|
|
261
|
+
"span",
|
|
262
|
+
{ className: "mk-ms-chips" },
|
|
263
|
+
...chosen.slice(0, maxChips).map(o =>
|
|
264
|
+
h("span", { key: o.value, className: "mk-ms-chip" }, dot(o), o.label),
|
|
265
|
+
),
|
|
266
|
+
chosen.length > maxChips &&
|
|
267
|
+
h("span", { key: "+", className: "mk-ms-chip mk-ms-more" }, `+${chosen.length - maxChips}`),
|
|
268
|
+
),
|
|
269
|
+
),
|
|
270
|
+
h(IconChevron, { className: "mk-ms-caret" }),
|
|
271
|
+
),
|
|
272
|
+
open &&
|
|
273
|
+
h(
|
|
274
|
+
"div",
|
|
275
|
+
{ className: "mk-pop mk-ms-menu", role: "listbox", "aria-multiselectable": true },
|
|
276
|
+
h(
|
|
277
|
+
"button",
|
|
278
|
+
{ type: "button", className: cx("mk-ms-opt", allOn && "active"), onClick: all },
|
|
279
|
+
box(allOn),
|
|
280
|
+
h("span", { className: "mk-ms-label" }, allLabel),
|
|
281
|
+
),
|
|
282
|
+
h("div", { className: "mk-ms-sep" }),
|
|
283
|
+
...opts.map(o => {
|
|
284
|
+
const on = value.includes(o.value);
|
|
285
|
+
return h(
|
|
286
|
+
"div",
|
|
287
|
+
{
|
|
288
|
+
key: o.value,
|
|
289
|
+
role: "option",
|
|
290
|
+
"aria-selected": on,
|
|
291
|
+
tabIndex: 0,
|
|
292
|
+
className: cx("mk-ms-opt", on && "active"),
|
|
293
|
+
onClick: () => toggle(o.value),
|
|
294
|
+
onKeyDown: e => {
|
|
295
|
+
if (e.key === "Enter" || e.key === " ") {
|
|
296
|
+
e.preventDefault();
|
|
297
|
+
toggle(o.value);
|
|
298
|
+
}
|
|
299
|
+
},
|
|
300
|
+
},
|
|
301
|
+
box(on),
|
|
302
|
+
dot(o),
|
|
303
|
+
h("span", { className: "mk-ms-label" }, o.label),
|
|
304
|
+
showOnly &&
|
|
305
|
+
h(
|
|
306
|
+
"button",
|
|
307
|
+
{
|
|
308
|
+
type: "button",
|
|
309
|
+
className: "mk-ms-only",
|
|
310
|
+
onClick: e => {
|
|
311
|
+
e.stopPropagation();
|
|
312
|
+
onChange([o.value]);
|
|
313
|
+
},
|
|
314
|
+
},
|
|
315
|
+
"Only",
|
|
316
|
+
),
|
|
317
|
+
);
|
|
318
|
+
}),
|
|
319
|
+
),
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// ---------------------------------------------------------------------------
|
|
324
|
+
// FreshnessBadge
|
|
325
|
+
// ---------------------------------------------------------------------------
|
|
326
|
+
/**
|
|
327
|
+
* How old the data is, as a dot + headline that opens a panel listing each
|
|
328
|
+
* source. Presentational: the app decides what "fresh" means and passes the
|
|
329
|
+
* verdict in. `tone`/source tones accept ok|warn|bad (or green|amber|red).
|
|
330
|
+
*
|
|
331
|
+
* sources: [{ key, label, note?, value?, detail?, tone? }]
|
|
332
|
+
* action: a node shown as a callout above the sources (e.g. "Load latest").
|
|
333
|
+
*/
|
|
334
|
+
export function FreshnessBadge({
|
|
335
|
+
tone = "neutral",
|
|
336
|
+
headline,
|
|
337
|
+
title,
|
|
338
|
+
meta,
|
|
339
|
+
schedule,
|
|
340
|
+
sources = [],
|
|
341
|
+
action,
|
|
342
|
+
footer,
|
|
343
|
+
align = "end",
|
|
344
|
+
className,
|
|
345
|
+
}) {
|
|
346
|
+
const [open, setOpen] = React.useState(false);
|
|
347
|
+
const ref = React.useRef(null);
|
|
348
|
+
usePopover(open, setOpen, ref, align);
|
|
349
|
+
const t = normalizeTone(tone);
|
|
350
|
+
const hasPanel = title != null || schedule != null || sources.length > 0 || action || footer;
|
|
351
|
+
|
|
352
|
+
return h(
|
|
353
|
+
"div",
|
|
354
|
+
{ className: cx("mk-fresh", className), ref },
|
|
355
|
+
h(
|
|
356
|
+
"button",
|
|
357
|
+
{
|
|
358
|
+
type: "button",
|
|
359
|
+
className: cx("mk-fresh-trigger", `mk-tone-${t}`, open && "open"),
|
|
360
|
+
"aria-haspopup": hasPanel ? "dialog" : undefined,
|
|
361
|
+
"aria-expanded": hasPanel ? open : undefined,
|
|
362
|
+
"aria-live": "polite",
|
|
363
|
+
disabled: !hasPanel,
|
|
364
|
+
onClick: () => setOpen(o => !o),
|
|
365
|
+
},
|
|
366
|
+
h("span", { className: cx("mk-dot", `mk-tone-${t}`) }),
|
|
367
|
+
h("span", { className: "mk-fresh-headline" }, headline),
|
|
368
|
+
),
|
|
369
|
+
open &&
|
|
370
|
+
hasPanel &&
|
|
371
|
+
h(
|
|
372
|
+
"div",
|
|
373
|
+
{ className: "mk-pop mk-fresh-pop", role: "dialog", "aria-label": "Data freshness" },
|
|
374
|
+
(title != null || meta != null) &&
|
|
375
|
+
h(
|
|
376
|
+
"div",
|
|
377
|
+
{ className: "mk-fresh-head" },
|
|
378
|
+
h("strong", null, title),
|
|
379
|
+
meta != null && h("span", null, meta),
|
|
380
|
+
),
|
|
381
|
+
schedule != null && h("p", { className: "mk-fresh-schedule" }, schedule),
|
|
382
|
+
action && h("div", { className: "mk-fresh-action" }, action),
|
|
383
|
+
sources.length > 0 &&
|
|
384
|
+
h(
|
|
385
|
+
"ul",
|
|
386
|
+
{ className: "mk-fresh-sources", "aria-label": "Sources" },
|
|
387
|
+
...sources.map(s =>
|
|
388
|
+
h(
|
|
389
|
+
"li",
|
|
390
|
+
{ key: s.key ?? s.label },
|
|
391
|
+
h("span", { className: cx("mk-dot", `mk-tone-${normalizeTone(s.tone)}`) }),
|
|
392
|
+
h(
|
|
393
|
+
"span",
|
|
394
|
+
{ className: "mk-fresh-source" },
|
|
395
|
+
h("span", null, s.label),
|
|
396
|
+
s.note != null && h("small", null, s.note),
|
|
397
|
+
s.detail != null && h("small", { className: "mk-fresh-detail" }, s.detail),
|
|
398
|
+
),
|
|
399
|
+
h("span", { className: "mk-fresh-value" }, s.value ?? "—"),
|
|
400
|
+
),
|
|
401
|
+
),
|
|
402
|
+
),
|
|
403
|
+
footer != null && h("p", { className: "mk-fresh-foot" }, footer),
|
|
404
|
+
),
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// ---------------------------------------------------------------------------
|
|
409
|
+
// RefreshAllButton
|
|
410
|
+
// ---------------------------------------------------------------------------
|
|
411
|
+
/**
|
|
412
|
+
* Re-runs every binding's warehouse query, `concurrency` at a time, and
|
|
413
|
+
* updates the app as each one lands (refreshBinding). The header shows one
|
|
414
|
+
* fixed-size button; progress lives in a modal (confirm → live checklist
|
|
415
|
+
* with per-source timings → summary). Closing the modal does not stop the
|
|
416
|
+
* refresh. A failed binding is reported and keeps serving its old data.
|
|
417
|
+
*
|
|
418
|
+
* `bindings` is the app's binding names — usually a build-time define over
|
|
419
|
+
* `bindings/*.sql` (see the README). Keep `concurrency` low: stacked heavy
|
|
420
|
+
* warehouse builds fail transiently and can wedge a slot pool.
|
|
421
|
+
*/
|
|
422
|
+
export function RefreshAllButton({
|
|
423
|
+
bindings,
|
|
424
|
+
labels = {},
|
|
425
|
+
first,
|
|
426
|
+
last,
|
|
427
|
+
skip,
|
|
428
|
+
concurrency = 3,
|
|
429
|
+
schedule,
|
|
430
|
+
refresh = refreshBinding,
|
|
431
|
+
className,
|
|
432
|
+
}) {
|
|
433
|
+
const sources = React.useMemo(
|
|
434
|
+
() => refreshOrder(bindings, { first, last, skip }),
|
|
435
|
+
// Patterns are usually literals; key them by content, not identity.
|
|
436
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
437
|
+
[bindings.join("\0"), (first ?? []).join("\0"), (last ?? []).join("\0"), (skip ?? []).join("\0")],
|
|
438
|
+
);
|
|
439
|
+
const busy = React.useRef(false);
|
|
440
|
+
const [state, setState] = React.useState({ status: "idle" });
|
|
441
|
+
const [items, setItems] = React.useState({});
|
|
442
|
+
const [errors, setErrors] = React.useState([]);
|
|
443
|
+
const [took, setTook] = React.useState({});
|
|
444
|
+
const [open, setOpen] = React.useState(false);
|
|
445
|
+
const [now, setNow] = React.useState(() => Date.now());
|
|
446
|
+
const closeRef = React.useRef(null);
|
|
447
|
+
|
|
448
|
+
const running = state.status === "running";
|
|
449
|
+
const total = sources.length;
|
|
450
|
+
const labelOf = name => labels[name] ?? name;
|
|
451
|
+
|
|
452
|
+
React.useEffect(() => {
|
|
453
|
+
if (!running) return undefined;
|
|
454
|
+
const t = setInterval(() => setNow(Date.now()), 1000);
|
|
455
|
+
return () => clearInterval(t);
|
|
456
|
+
}, [running]);
|
|
457
|
+
|
|
458
|
+
React.useEffect(() => {
|
|
459
|
+
if (!open) return undefined;
|
|
460
|
+
const onKey = e => e.key === "Escape" && setOpen(false);
|
|
461
|
+
window.addEventListener("keydown", onKey);
|
|
462
|
+
closeRef.current?.focus();
|
|
463
|
+
const prev = document.body.style.overflow;
|
|
464
|
+
document.body.style.overflow = "hidden";
|
|
465
|
+
return () => {
|
|
466
|
+
window.removeEventListener("keydown", onKey);
|
|
467
|
+
document.body.style.overflow = prev;
|
|
468
|
+
};
|
|
469
|
+
}, [open]);
|
|
470
|
+
|
|
471
|
+
async function refreshAll() {
|
|
472
|
+
if (busy.current) return;
|
|
473
|
+
busy.current = true;
|
|
474
|
+
const startedAt = Date.now();
|
|
475
|
+
const failures = [];
|
|
476
|
+
let done = 0;
|
|
477
|
+
let next = 0;
|
|
478
|
+
setNow(startedAt);
|
|
479
|
+
setErrors([]);
|
|
480
|
+
setTook({});
|
|
481
|
+
setItems(Object.fromEntries(sources.map(n => [n, "queued"])));
|
|
482
|
+
setState({ status: "running", done, startedAt });
|
|
483
|
+
const mark = (name, st) => setItems(m => ({ ...m, [name]: st }));
|
|
484
|
+
async function worker() {
|
|
485
|
+
while (next < sources.length) {
|
|
486
|
+
const name = sources[next++];
|
|
487
|
+
mark(name, "running");
|
|
488
|
+
const t0 = Date.now();
|
|
489
|
+
try {
|
|
490
|
+
await refresh(name);
|
|
491
|
+
mark(name, "done");
|
|
492
|
+
} catch (error) {
|
|
493
|
+
failures.push({ name, message: error instanceof Error ? error.message : String(error) });
|
|
494
|
+
setErrors([...failures]);
|
|
495
|
+
mark(name, "failed");
|
|
496
|
+
}
|
|
497
|
+
const secs = Math.round((Date.now() - t0) / 1000);
|
|
498
|
+
setTook(m => ({ ...m, [name]: secs }));
|
|
499
|
+
done += 1;
|
|
500
|
+
setState({ status: "running", done, startedAt });
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
try {
|
|
504
|
+
await Promise.all(Array.from({ length: Math.min(concurrency, sources.length) }, worker));
|
|
505
|
+
setState({
|
|
506
|
+
status: "done",
|
|
507
|
+
at: new Date().toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }),
|
|
508
|
+
failures,
|
|
509
|
+
seconds: Math.round((Date.now() - startedAt) / 1000),
|
|
510
|
+
});
|
|
511
|
+
} finally {
|
|
512
|
+
busy.current = false;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
const failedCount = state.status === "done" ? state.failures.length : 0;
|
|
517
|
+
const label = running
|
|
518
|
+
? `Refreshing ${state.done}/${total}…`
|
|
519
|
+
: state.status === "done"
|
|
520
|
+
? failedCount
|
|
521
|
+
? `${total - failedCount}/${total} refreshed`
|
|
522
|
+
: `Refreshed ${state.at}`
|
|
523
|
+
: "Refresh data";
|
|
524
|
+
const elapsed = running
|
|
525
|
+
? Math.max(0, Math.round((now - state.startedAt) / 1000))
|
|
526
|
+
: state.status === "done"
|
|
527
|
+
? state.seconds
|
|
528
|
+
: 0;
|
|
529
|
+
const doneCount = running ? state.done : state.status === "done" ? total : 0;
|
|
530
|
+
const pct = total ? Math.round((doneCount / total) * 100) : 0;
|
|
531
|
+
|
|
532
|
+
const title =
|
|
533
|
+
state.status === "idle"
|
|
534
|
+
? "Refresh data"
|
|
535
|
+
: running
|
|
536
|
+
? "Refreshing data…"
|
|
537
|
+
: failedCount
|
|
538
|
+
? "Refresh finished with errors"
|
|
539
|
+
: "Data refreshed";
|
|
540
|
+
const desc =
|
|
541
|
+
state.status === "idle"
|
|
542
|
+
? `Re-runs ${total} warehouse ${total === 1 ? "query" : "queries"} (${concurrency} at a time) and updates the app as each one lands.${schedule ? ` ${schedule}` : ""}`
|
|
543
|
+
: running
|
|
544
|
+
? "You can close this — the refresh keeps running in the background."
|
|
545
|
+
: failedCount
|
|
546
|
+
? "Failed sources keep serving their previous data."
|
|
547
|
+
: `All ${total} sources rebuilt in ${fmtSecs(elapsed)}.`;
|
|
548
|
+
|
|
549
|
+
const itemIcon = st =>
|
|
550
|
+
st === "done"
|
|
551
|
+
? svg(14, 2.5, [h("path", { key: "p", d: "M20 6 9 17l-5-5" })])
|
|
552
|
+
: st === "failed"
|
|
553
|
+
? h(IconX, { size: 14 })
|
|
554
|
+
: st === "running"
|
|
555
|
+
? h("span", { className: "mk-rf-spin" })
|
|
556
|
+
: h("span", { className: "mk-rf-dot" });
|
|
557
|
+
const itemState = (st, name) =>
|
|
558
|
+
st === "queued"
|
|
559
|
+
? state.status === "idle"
|
|
560
|
+
? ""
|
|
561
|
+
: "Queued"
|
|
562
|
+
: st === "running"
|
|
563
|
+
? "Running"
|
|
564
|
+
: st === "done"
|
|
565
|
+
? took[name] != null
|
|
566
|
+
? fmtSecs(took[name])
|
|
567
|
+
: "Done"
|
|
568
|
+
: "Failed";
|
|
569
|
+
|
|
570
|
+
const modal =
|
|
571
|
+
open &&
|
|
572
|
+
createPortal(
|
|
573
|
+
h(
|
|
574
|
+
"div",
|
|
575
|
+
{
|
|
576
|
+
className: "mk-rf-backdrop",
|
|
577
|
+
onMouseDown: e => e.target === e.currentTarget && setOpen(false),
|
|
578
|
+
},
|
|
579
|
+
h(
|
|
580
|
+
"div",
|
|
581
|
+
{ className: "mk-rf-modal", role: "dialog", "aria-modal": "true", "aria-labelledby": "mk-rf-title" },
|
|
582
|
+
h(
|
|
583
|
+
"div",
|
|
584
|
+
{ className: "mk-rf-head" },
|
|
585
|
+
h(
|
|
586
|
+
"div",
|
|
587
|
+
null,
|
|
588
|
+
h("h2", { id: "mk-rf-title", className: "mk-rf-title" }, title),
|
|
589
|
+
h("p", { className: "mk-rf-desc" }, desc),
|
|
590
|
+
),
|
|
591
|
+
h(
|
|
592
|
+
"button",
|
|
593
|
+
{ ref: closeRef, type: "button", className: "mk-rf-x", "aria-label": "Close", onClick: () => setOpen(false) },
|
|
594
|
+
h(IconX),
|
|
595
|
+
),
|
|
596
|
+
),
|
|
597
|
+
state.status !== "idle" &&
|
|
598
|
+
h(
|
|
599
|
+
"div",
|
|
600
|
+
{ className: "mk-rf-progress" },
|
|
601
|
+
h(
|
|
602
|
+
"div",
|
|
603
|
+
{
|
|
604
|
+
className: "mk-rf-bar",
|
|
605
|
+
role: "progressbar",
|
|
606
|
+
"aria-valuemin": 0,
|
|
607
|
+
"aria-valuemax": total,
|
|
608
|
+
"aria-valuenow": doneCount,
|
|
609
|
+
},
|
|
610
|
+
h("div", {
|
|
611
|
+
className: cx("mk-rf-fill", (failedCount || errors.length) && "has-err"),
|
|
612
|
+
style: { width: `${pct}%` },
|
|
613
|
+
}),
|
|
614
|
+
),
|
|
615
|
+
h(
|
|
616
|
+
"div",
|
|
617
|
+
{ className: "mk-rf-meta" },
|
|
618
|
+
h("span", null, `${doneCount} of ${total} rebuilt`),
|
|
619
|
+
h("span", null, fmtSecs(elapsed)),
|
|
620
|
+
),
|
|
621
|
+
),
|
|
622
|
+
h(
|
|
623
|
+
"ul",
|
|
624
|
+
{ className: "mk-rf-list", "aria-live": "polite" },
|
|
625
|
+
...sources.map(name => {
|
|
626
|
+
const st = state.status === "idle" ? "queued" : (items[name] ?? "queued");
|
|
627
|
+
const err = errors.find(e => e.name === name);
|
|
628
|
+
return h(
|
|
629
|
+
"li",
|
|
630
|
+
{ key: name, className: cx("mk-rf-item", st), title: name },
|
|
631
|
+
h("span", { className: "mk-rf-ico" }, itemIcon(st)),
|
|
632
|
+
h(
|
|
633
|
+
"span",
|
|
634
|
+
{ className: "mk-rf-name" },
|
|
635
|
+
labelOf(name),
|
|
636
|
+
err && h("span", { className: "mk-rf-err" }, err.message),
|
|
637
|
+
),
|
|
638
|
+
h("span", { className: "mk-rf-state" }, itemState(st, name)),
|
|
639
|
+
);
|
|
640
|
+
}),
|
|
641
|
+
),
|
|
642
|
+
h(
|
|
643
|
+
"div",
|
|
644
|
+
{ className: "mk-rf-foot" },
|
|
645
|
+
state.status === "idle" && [
|
|
646
|
+
h(Button, { key: "c", onClick: () => setOpen(false) }, "Cancel"),
|
|
647
|
+
h(Button, { key: "s", variant: "primary", onClick: refreshAll }, "Start refresh"),
|
|
648
|
+
],
|
|
649
|
+
running && h(Button, { onClick: () => setOpen(false) }, "Hide"),
|
|
650
|
+
state.status === "done" && [
|
|
651
|
+
h(Button, { key: "a", onClick: refreshAll }, "Refresh again"),
|
|
652
|
+
h(Button, { key: "d", variant: "primary", onClick: () => setOpen(false) }, "Done"),
|
|
653
|
+
],
|
|
654
|
+
),
|
|
655
|
+
),
|
|
656
|
+
),
|
|
657
|
+
document.body,
|
|
658
|
+
);
|
|
659
|
+
|
|
660
|
+
return h(
|
|
661
|
+
React.Fragment,
|
|
662
|
+
null,
|
|
663
|
+
h(
|
|
664
|
+
"button",
|
|
665
|
+
{
|
|
666
|
+
type: "button",
|
|
667
|
+
className: cx(
|
|
668
|
+
"mk-btn mk-btn--outline mk-refresh",
|
|
669
|
+
running && "refreshing",
|
|
670
|
+
state.status === "done" && (failedCount ? "failed" : "ok"),
|
|
671
|
+
className,
|
|
672
|
+
),
|
|
673
|
+
title: running
|
|
674
|
+
? "Refresh in progress — click to see details."
|
|
675
|
+
: `Re-runs every binding's warehouse query.${schedule ? ` ${schedule}` : ""}`,
|
|
676
|
+
disabled: !total,
|
|
677
|
+
onClick: () => setOpen(true),
|
|
678
|
+
"aria-haspopup": "dialog",
|
|
679
|
+
},
|
|
680
|
+
h(IconRefresh),
|
|
681
|
+
label,
|
|
682
|
+
),
|
|
683
|
+
modal,
|
|
684
|
+
);
|
|
685
|
+
}
|
package/vite.d.ts
CHANGED
|
@@ -30,6 +30,14 @@ export interface MakoDataOptions {
|
|
|
30
30
|
* MAKO_DBT_ENV, else production.
|
|
31
31
|
*/
|
|
32
32
|
dbtEnvironment?: string;
|
|
33
|
+
/**
|
|
34
|
+
* A build longer than the API answers synchronously comes back as a job;
|
|
35
|
+
* the plugin polls it starting at this interval (ms, backing off to 5×).
|
|
36
|
+
* Default: 1000.
|
|
37
|
+
*/
|
|
38
|
+
pollIntervalMs?: number;
|
|
39
|
+
/** Give up waiting for one build after this long (ms). Default: 30 minutes. */
|
|
40
|
+
buildTimeoutMs?: number;
|
|
33
41
|
/**
|
|
34
42
|
* Preview the app as this member (an email): `__data/viewer.json` answers
|
|
35
43
|
* as Mako would for them. Default: MAKO_VIEWER_AS, else yourself.
|