@nubitio/dashboard 0.7.4 → 0.8.1
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/dist/index.cjs +800 -106
- package/dist/index.d.cts +237 -20
- package/dist/index.d.mts +237 -20
- package/dist/index.mjs +787 -110
- package/dist/style.css +183 -2
- package/package.json +12 -4
package/dist/index.cjs
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
let _nubitio_ui = require("@nubitio/ui");
|
|
3
|
-
let react = require("react");
|
|
4
|
-
let _nubitio_core = require("@nubitio/core");
|
|
5
3
|
let react_jsx_runtime = require("react/jsx-runtime");
|
|
4
|
+
let react = require("react");
|
|
6
5
|
let react_router_dom = require("react-router-dom");
|
|
6
|
+
let _tanstack_react_query = require("@tanstack/react-query");
|
|
7
|
+
let _nubitio_core = require("@nubitio/core");
|
|
8
|
+
let recharts = require("recharts");
|
|
7
9
|
//#region packages/dashboard/dashboard/defineDashboard.ts
|
|
8
10
|
const dashboardCache = /* @__PURE__ */ new Map();
|
|
9
11
|
function cacheKey(config) {
|
|
@@ -44,67 +46,270 @@ function defineDashboard(config) {
|
|
|
44
46
|
return dashboard;
|
|
45
47
|
}
|
|
46
48
|
//#endregion
|
|
47
|
-
//#region packages/dashboard/dashboard/
|
|
48
|
-
function
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
49
|
+
//#region packages/dashboard/dashboard/DashboardLayoutControls.tsx
|
|
50
|
+
function DashboardLayoutControls({ layout }) {
|
|
51
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
52
|
+
className: "nb-dashboard-layout-controls",
|
|
53
|
+
children: [
|
|
54
|
+
layout.editMode && layout.hiddenWidgets.length > 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_nubitio_ui.Popover, {
|
|
55
|
+
ariaLabel: "Hidden widgets",
|
|
56
|
+
align: "end",
|
|
57
|
+
trigger: ({ toggle }) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_nubitio_ui.IconButton, {
|
|
58
|
+
icon: "ph ph-eye-slash",
|
|
59
|
+
label: `Hidden widgets (${layout.hiddenWidgets.length})`,
|
|
60
|
+
onClick: toggle
|
|
61
|
+
}),
|
|
62
|
+
panel: () => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("ul", {
|
|
63
|
+
className: "nb-dashboard-layout-controls__hidden-list",
|
|
64
|
+
children: layout.hiddenWidgets.map(({ sectionId, widget }) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("li", { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { children: widget.title }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
|
|
65
|
+
type: "button",
|
|
66
|
+
className: "nb-dashboard-layout-controls__show-btn",
|
|
67
|
+
onClick: () => layout.showWidget(sectionId, widget.id),
|
|
68
|
+
children: "Show"
|
|
69
|
+
})] }, `${sectionId}-${widget.id}`))
|
|
70
|
+
})
|
|
71
|
+
}),
|
|
72
|
+
layout.editMode && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_nubitio_ui.IconButton, {
|
|
73
|
+
icon: "ph ph-arrow-counter-clockwise",
|
|
74
|
+
label: "Reset layout",
|
|
75
|
+
onClick: layout.resetLayout
|
|
76
|
+
}),
|
|
77
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_nubitio_ui.IconButton, {
|
|
78
|
+
icon: layout.editMode ? "ph ph-check" : "ph ph-sliders-horizontal",
|
|
79
|
+
label: layout.editMode ? "Done customizing" : "Customize dashboard",
|
|
80
|
+
onClick: () => layout.setEditMode(!layout.editMode)
|
|
81
|
+
})
|
|
82
|
+
]
|
|
61
83
|
});
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
84
|
+
}
|
|
85
|
+
//#endregion
|
|
86
|
+
//#region packages/dashboard/dashboard/periodPresets.ts
|
|
87
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
88
|
+
const toIso = (d) => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
|
|
89
|
+
function daysAgo(today, days) {
|
|
90
|
+
const d = new Date(today);
|
|
91
|
+
d.setDate(d.getDate() - days);
|
|
92
|
+
return d;
|
|
93
|
+
}
|
|
94
|
+
function startOfMonth(date) {
|
|
95
|
+
return new Date(date.getFullYear(), date.getMonth(), 1);
|
|
96
|
+
}
|
|
97
|
+
function startOfYear(date) {
|
|
98
|
+
return new Date(date.getFullYear(), 0, 1);
|
|
99
|
+
}
|
|
100
|
+
const DEFAULT_PERIOD_PRESETS = [
|
|
101
|
+
{
|
|
102
|
+
key: "today",
|
|
103
|
+
label: "Today",
|
|
104
|
+
getRange: (today) => ({
|
|
105
|
+
start: toIso(today),
|
|
106
|
+
end: toIso(today)
|
|
107
|
+
})
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
key: "7d",
|
|
111
|
+
label: "7d",
|
|
112
|
+
getRange: (today) => ({
|
|
113
|
+
start: toIso(daysAgo(today, 6)),
|
|
114
|
+
end: toIso(today)
|
|
115
|
+
})
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
key: "30d",
|
|
119
|
+
label: "30d",
|
|
120
|
+
getRange: (today) => ({
|
|
121
|
+
start: toIso(daysAgo(today, 29)),
|
|
122
|
+
end: toIso(today)
|
|
123
|
+
})
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
key: "month",
|
|
127
|
+
label: "This month",
|
|
128
|
+
getRange: (today) => ({
|
|
129
|
+
start: toIso(startOfMonth(today)),
|
|
130
|
+
end: toIso(today)
|
|
131
|
+
})
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
key: "year",
|
|
135
|
+
label: "This year",
|
|
136
|
+
getRange: (today) => ({
|
|
137
|
+
start: toIso(startOfYear(today)),
|
|
138
|
+
end: toIso(today)
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
];
|
|
142
|
+
function getPeriodPreset(key, presets = DEFAULT_PERIOD_PRESETS) {
|
|
143
|
+
return presets.find((preset) => preset.key === key);
|
|
144
|
+
}
|
|
145
|
+
//#endregion
|
|
146
|
+
//#region packages/dashboard/dashboard/useDashboardPeriod.ts
|
|
147
|
+
const CUSTOM_PERIOD_KEY = "custom";
|
|
148
|
+
function useDashboardPeriod(config) {
|
|
149
|
+
const presets = config?.presets ?? DEFAULT_PERIOD_PRESETS;
|
|
150
|
+
const defaultPreset = config?.defaultPreset ?? presets[0]?.key ?? "7d";
|
|
151
|
+
const allowCustomRange = config?.allowCustomRange ?? true;
|
|
152
|
+
const urlParam = config?.urlParam ?? "period";
|
|
153
|
+
const [searchParams, setSearchParams] = (0, react_router_dom.useSearchParams)();
|
|
154
|
+
const presetKey = searchParams.get(urlParam) ?? defaultPreset;
|
|
155
|
+
const customStart = searchParams.get(`${urlParam}_from`);
|
|
156
|
+
const customEnd = searchParams.get(`${urlParam}_to`);
|
|
157
|
+
const value = (0, react.useMemo)(() => {
|
|
158
|
+
if (presetKey === "custom" && customStart && customEnd) return {
|
|
159
|
+
presetKey,
|
|
160
|
+
start: customStart,
|
|
161
|
+
end: customEnd
|
|
162
|
+
};
|
|
163
|
+
const preset = getPeriodPreset(presetKey, presets) ?? getPeriodPreset(defaultPreset, presets);
|
|
164
|
+
const range = preset?.getRange(/* @__PURE__ */ new Date()) ?? {
|
|
165
|
+
start: "",
|
|
166
|
+
end: ""
|
|
167
|
+
};
|
|
168
|
+
return {
|
|
169
|
+
presetKey: preset?.key ?? defaultPreset,
|
|
170
|
+
...range
|
|
171
|
+
};
|
|
97
172
|
}, [
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
173
|
+
presetKey,
|
|
174
|
+
customStart,
|
|
175
|
+
customEnd,
|
|
176
|
+
presets,
|
|
177
|
+
defaultPreset
|
|
101
178
|
]);
|
|
179
|
+
const setPreset = (0, react.useCallback)((key) => {
|
|
180
|
+
setSearchParams((prev) => {
|
|
181
|
+
const next = new URLSearchParams(prev);
|
|
182
|
+
next.set(urlParam, key);
|
|
183
|
+
next.delete(`${urlParam}_from`);
|
|
184
|
+
next.delete(`${urlParam}_to`);
|
|
185
|
+
return next;
|
|
186
|
+
});
|
|
187
|
+
}, [setSearchParams, urlParam]);
|
|
188
|
+
const setCustomRange = (0, react.useCallback)((start, end) => {
|
|
189
|
+
setSearchParams((prev) => {
|
|
190
|
+
const next = new URLSearchParams(prev);
|
|
191
|
+
next.set(urlParam, CUSTOM_PERIOD_KEY);
|
|
192
|
+
if (start) next.set(`${urlParam}_from`, start);
|
|
193
|
+
else next.delete(`${urlParam}_from`);
|
|
194
|
+
if (end) next.set(`${urlParam}_to`, end);
|
|
195
|
+
else next.delete(`${urlParam}_to`);
|
|
196
|
+
return next;
|
|
197
|
+
});
|
|
198
|
+
}, [setSearchParams, urlParam]);
|
|
199
|
+
if (!config) return void 0;
|
|
200
|
+
return {
|
|
201
|
+
value,
|
|
202
|
+
presets,
|
|
203
|
+
allowCustomRange,
|
|
204
|
+
setPreset,
|
|
205
|
+
setCustomRange
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
//#endregion
|
|
209
|
+
//#region packages/dashboard/dashboard/DashboardPeriodFilter.tsx
|
|
210
|
+
function DashboardPeriodFilter({ period }) {
|
|
211
|
+
const { value, presets = [], allowCustomRange, setPreset, setCustomRange } = period;
|
|
212
|
+
const options = [...presets.map((preset) => ({
|
|
213
|
+
value: preset.key,
|
|
214
|
+
label: preset.label
|
|
215
|
+
})), ...allowCustomRange ? [{
|
|
216
|
+
value: CUSTOM_PERIOD_KEY,
|
|
217
|
+
label: "Custom"
|
|
218
|
+
}] : []];
|
|
219
|
+
const isCustom = value.presetKey === CUSTOM_PERIOD_KEY;
|
|
220
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
221
|
+
className: "nb-dashboard-period-filter",
|
|
222
|
+
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_nubitio_ui.SegmentedControl, {
|
|
223
|
+
ariaLabel: "Period",
|
|
224
|
+
size: "sm",
|
|
225
|
+
options,
|
|
226
|
+
value: value.presetKey,
|
|
227
|
+
onChange: setPreset
|
|
228
|
+
}), isCustom && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_nubitio_ui.DateRangePicker, {
|
|
229
|
+
startValue: value.start,
|
|
230
|
+
endValue: value.end,
|
|
231
|
+
onChange: setCustomRange,
|
|
232
|
+
ariaLabel: "Custom period"
|
|
233
|
+
})]
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
//#endregion
|
|
237
|
+
//#region packages/dashboard/dashboard/defaults.ts
|
|
238
|
+
const DEFAULT_PERIOD_PARAM_NAMES = {
|
|
239
|
+
start: "from",
|
|
240
|
+
end: "to"
|
|
241
|
+
};
|
|
242
|
+
//#endregion
|
|
243
|
+
//#region packages/dashboard/dashboard/useWidgetQuery.ts
|
|
244
|
+
/**
|
|
245
|
+
* Fetches a single widget's data independently of the dashboard's shared
|
|
246
|
+
* payload — its own cache entry, its own loading/error state, no head-of-line
|
|
247
|
+
* blocking behind slower widgets. Returns `undefined` when the widget has no
|
|
248
|
+
* `query` configured, so callers fall back to the dashboard's shared data.
|
|
249
|
+
*/
|
|
250
|
+
function useWidgetQuery(query, period, periodParamNames = DEFAULT_PERIOD_PARAM_NAMES) {
|
|
251
|
+
const http = (0, _nubitio_core.useCoreHttpClient)();
|
|
252
|
+
const periodParams = (query?.usePeriod ?? true) && period ? {
|
|
253
|
+
[periodParamNames.start]: period.start,
|
|
254
|
+
[periodParamNames.end]: period.end
|
|
255
|
+
} : void 0;
|
|
256
|
+
const params = query ? {
|
|
257
|
+
...query.params,
|
|
258
|
+
...periodParams
|
|
259
|
+
} : void 0;
|
|
260
|
+
const result = (0, _tanstack_react_query.useQuery)({
|
|
261
|
+
queryKey: [
|
|
262
|
+
"nb-dashboard-widget",
|
|
263
|
+
query?.url,
|
|
264
|
+
params
|
|
265
|
+
],
|
|
266
|
+
queryFn: async () => {
|
|
267
|
+
return (await http.get(query.url, { params })).data;
|
|
268
|
+
},
|
|
269
|
+
enabled: !!query,
|
|
270
|
+
refetchInterval: query?.refreshInterval,
|
|
271
|
+
retry: false
|
|
272
|
+
});
|
|
273
|
+
if (!query) return void 0;
|
|
102
274
|
return {
|
|
103
|
-
|
|
104
|
-
|
|
275
|
+
data: result.data ?? {},
|
|
276
|
+
loading: result.isLoading,
|
|
277
|
+
error: result.error ? result.error instanceof Error ? result.error.message : "Failed to load widget" : null
|
|
105
278
|
};
|
|
106
279
|
}
|
|
107
280
|
//#endregion
|
|
281
|
+
//#region packages/dashboard/dashboard/widgetRegistry.ts
|
|
282
|
+
const registry = /* @__PURE__ */ new Map();
|
|
283
|
+
/**
|
|
284
|
+
* Registers a component to render for `widget.type`. Built-in types (`stat`,
|
|
285
|
+
* `bar-chart`, `donut-chart`, `line-chart`, `area-chart`, `progress`, `table`)
|
|
286
|
+
* are registered automatically. Call this to add a new widget type — or to
|
|
287
|
+
* replace a built-in one — without forking `@nubitio/dashboard`.
|
|
288
|
+
*
|
|
289
|
+
* ```ts
|
|
290
|
+
* registerWidgetType('funnel', FunnelWidgetView);
|
|
291
|
+
* ```
|
|
292
|
+
*/
|
|
293
|
+
function registerWidgetType(type, component) {
|
|
294
|
+
registry.set(type, component);
|
|
295
|
+
}
|
|
296
|
+
function getWidgetComponent(type) {
|
|
297
|
+
return registry.get(type);
|
|
298
|
+
}
|
|
299
|
+
function isWidgetTypeRegistered(type) {
|
|
300
|
+
return registry.has(type);
|
|
301
|
+
}
|
|
302
|
+
//#endregion
|
|
303
|
+
//#region packages/dashboard/dashboard/chartPalette.ts
|
|
304
|
+
const DEFAULT_CHART_COLORS = [
|
|
305
|
+
"var(--accent-color)",
|
|
306
|
+
"var(--success-color)",
|
|
307
|
+
"var(--warning-color)",
|
|
308
|
+
"var(--info-color)",
|
|
309
|
+
"#6b4fc8",
|
|
310
|
+
"#c2410c"
|
|
311
|
+
];
|
|
312
|
+
//#endregion
|
|
108
313
|
//#region packages/dashboard/dashboard/formatValue.ts
|
|
109
314
|
function formatDashboardValue(value, format = "text") {
|
|
110
315
|
if (value === null || value === void 0) return "—";
|
|
@@ -160,14 +365,145 @@ function resolveArray(source, path) {
|
|
|
160
365
|
return value.filter((item) => item !== null && typeof item === "object");
|
|
161
366
|
}
|
|
162
367
|
//#endregion
|
|
368
|
+
//#region packages/dashboard/dashboard/widgets/ChartTooltip.tsx
|
|
369
|
+
function ChartTooltip({ active, label, payload, labelFormat, series }) {
|
|
370
|
+
if (!active || !payload?.length) return null;
|
|
371
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
372
|
+
className: "nb-dashboard-chart-tooltip",
|
|
373
|
+
children: [label !== void 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
374
|
+
className: "nb-dashboard-chart-tooltip__label",
|
|
375
|
+
children: label
|
|
376
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("ul", {
|
|
377
|
+
className: "nb-dashboard-chart-tooltip__items",
|
|
378
|
+
children: payload.map((item) => {
|
|
379
|
+
const seriesConfig = series?.find((s) => s.key === item.dataKey);
|
|
380
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("li", {
|
|
381
|
+
className: "nb-dashboard-chart-tooltip__item",
|
|
382
|
+
children: [
|
|
383
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
384
|
+
className: "nb-dashboard-chart-tooltip__swatch",
|
|
385
|
+
style: { background: item.color }
|
|
386
|
+
}),
|
|
387
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
388
|
+
className: "nb-dashboard-chart-tooltip__name",
|
|
389
|
+
children: item.name
|
|
390
|
+
}),
|
|
391
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
392
|
+
className: "nb-dashboard-chart-tooltip__value",
|
|
393
|
+
children: formatDashboardValue(item.value, seriesConfig?.valueFormat ?? labelFormat)
|
|
394
|
+
})
|
|
395
|
+
]
|
|
396
|
+
}, String(item.dataKey));
|
|
397
|
+
})
|
|
398
|
+
})]
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
//#endregion
|
|
402
|
+
//#region packages/dashboard/dashboard/widgets/AreaChartWidgetView.tsx
|
|
403
|
+
function AreaChartWidgetView({ widget, data, loading }) {
|
|
404
|
+
const rows = resolveArray(data, widget.dataPath);
|
|
405
|
+
const height = widget.height ?? 240;
|
|
406
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_nubitio_ui.StatCard, {
|
|
407
|
+
title: widget.title,
|
|
408
|
+
headerExtra: widget.subtitle ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
409
|
+
className: "nb-dashboard-widget__subtitle",
|
|
410
|
+
children: widget.subtitle
|
|
411
|
+
}) : void 0,
|
|
412
|
+
menuVisible: widget.menuVisible,
|
|
413
|
+
isLoading: loading,
|
|
414
|
+
className: "nb-dashboard-chart-card",
|
|
415
|
+
children: rows.length === 0 ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
416
|
+
className: "nb-dashboard-chart-empty",
|
|
417
|
+
children: "No data"
|
|
418
|
+
}) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
419
|
+
style: { height },
|
|
420
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(recharts.ResponsiveContainer, {
|
|
421
|
+
width: "100%",
|
|
422
|
+
height: "100%",
|
|
423
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(recharts.AreaChart, {
|
|
424
|
+
data: rows,
|
|
425
|
+
margin: {
|
|
426
|
+
top: 4,
|
|
427
|
+
right: 8,
|
|
428
|
+
bottom: 0,
|
|
429
|
+
left: 0
|
|
430
|
+
},
|
|
431
|
+
children: [
|
|
432
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("defs", { children: widget.series.map((series, index) => {
|
|
433
|
+
const color = series.color ?? DEFAULT_CHART_COLORS[index % DEFAULT_CHART_COLORS.length];
|
|
434
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("linearGradient", {
|
|
435
|
+
id: `nb-area-${widget.id}-${series.key}`,
|
|
436
|
+
x1: "0",
|
|
437
|
+
y1: "0",
|
|
438
|
+
x2: "0",
|
|
439
|
+
y2: "1",
|
|
440
|
+
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("stop", {
|
|
441
|
+
offset: "5%",
|
|
442
|
+
stopColor: color,
|
|
443
|
+
stopOpacity: .35
|
|
444
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("stop", {
|
|
445
|
+
offset: "95%",
|
|
446
|
+
stopColor: color,
|
|
447
|
+
stopOpacity: .02
|
|
448
|
+
})]
|
|
449
|
+
}, series.key);
|
|
450
|
+
}) }),
|
|
451
|
+
widget.showGrid !== false && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(recharts.CartesianGrid, {
|
|
452
|
+
strokeDasharray: "3 3",
|
|
453
|
+
stroke: "var(--border-color)",
|
|
454
|
+
vertical: false
|
|
455
|
+
}),
|
|
456
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(recharts.XAxis, {
|
|
457
|
+
dataKey: widget.xKey,
|
|
458
|
+
tick: {
|
|
459
|
+
fontSize: 11,
|
|
460
|
+
fill: "var(--text-tertiary)"
|
|
461
|
+
},
|
|
462
|
+
axisLine: { stroke: "var(--border-color)" },
|
|
463
|
+
tickLine: false
|
|
464
|
+
}),
|
|
465
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(recharts.YAxis, {
|
|
466
|
+
tick: {
|
|
467
|
+
fontSize: 11,
|
|
468
|
+
fill: "var(--text-tertiary)"
|
|
469
|
+
},
|
|
470
|
+
axisLine: false,
|
|
471
|
+
tickLine: false,
|
|
472
|
+
width: 48,
|
|
473
|
+
tickFormatter: (v) => formatDashboardValue(v, widget.valueFormat)
|
|
474
|
+
}),
|
|
475
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(recharts.Tooltip, { content: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ChartTooltip, {
|
|
476
|
+
labelFormat: widget.valueFormat,
|
|
477
|
+
series: widget.series
|
|
478
|
+
}) }),
|
|
479
|
+
widget.showLegend && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(recharts.Legend, { wrapperStyle: { fontSize: 12 } }),
|
|
480
|
+
widget.series.map((series, index) => {
|
|
481
|
+
const color = series.color ?? DEFAULT_CHART_COLORS[index % DEFAULT_CHART_COLORS.length];
|
|
482
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(recharts.Area, {
|
|
483
|
+
type: "monotone",
|
|
484
|
+
dataKey: series.key,
|
|
485
|
+
name: series.label ?? series.key,
|
|
486
|
+
stroke: color,
|
|
487
|
+
strokeWidth: 2,
|
|
488
|
+
fill: `url(#nb-area-${widget.id}-${series.key})`,
|
|
489
|
+
stackId: widget.stacked ? "stack" : void 0
|
|
490
|
+
}, series.key);
|
|
491
|
+
})
|
|
492
|
+
]
|
|
493
|
+
})
|
|
494
|
+
})
|
|
495
|
+
})
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
//#endregion
|
|
163
499
|
//#region packages/dashboard/dashboard/widgets/BarChartWidgetView.tsx
|
|
164
|
-
function toNumber$
|
|
500
|
+
function toNumber$2(value) {
|
|
165
501
|
const numeric = typeof value === "number" ? value : Number(value);
|
|
166
502
|
return Number.isFinite(numeric) ? numeric : 0;
|
|
167
503
|
}
|
|
168
504
|
function BarChartWidgetView({ widget, data, loading }) {
|
|
169
505
|
const rows = resolveArray(data, widget.dataPath);
|
|
170
|
-
const max = rows.reduce((peak, row) => Math.max(peak, toNumber$
|
|
506
|
+
const max = rows.reduce((peak, row) => Math.max(peak, toNumber$2(row[widget.yKey])), 0) || 1;
|
|
171
507
|
const height = widget.height ?? 200;
|
|
172
508
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_nubitio_ui.StatCard, {
|
|
173
509
|
title: widget.title,
|
|
@@ -188,7 +524,7 @@ function BarChartWidgetView({ widget, data, loading }) {
|
|
|
188
524
|
"aria-label": widget.title,
|
|
189
525
|
children: rows.map((row, index) => {
|
|
190
526
|
const label = String(row[widget.xKey] ?? "");
|
|
191
|
-
const value = toNumber$
|
|
527
|
+
const value = toNumber$2(row[widget.yKey]);
|
|
192
528
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
193
529
|
className: "nb-dashboard-bar-chart__item",
|
|
194
530
|
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
@@ -209,28 +545,20 @@ function BarChartWidgetView({ widget, data, loading }) {
|
|
|
209
545
|
}
|
|
210
546
|
//#endregion
|
|
211
547
|
//#region packages/dashboard/dashboard/widgets/DonutChartWidgetView.tsx
|
|
212
|
-
|
|
213
|
-
"var(--accent-color)",
|
|
214
|
-
"var(--success-color)",
|
|
215
|
-
"var(--warning-color)",
|
|
216
|
-
"var(--info-color)",
|
|
217
|
-
"#6b4fc8",
|
|
218
|
-
"#c2410c"
|
|
219
|
-
];
|
|
220
|
-
function toNumber(value) {
|
|
548
|
+
function toNumber$1(value) {
|
|
221
549
|
const numeric = typeof value === "number" ? value : Number(value);
|
|
222
550
|
return Number.isFinite(numeric) ? numeric : 0;
|
|
223
551
|
}
|
|
224
552
|
function DonutChartWidgetView({ widget, data, loading }) {
|
|
225
553
|
const rows = resolveArray(data, widget.dataPath);
|
|
226
|
-
const colors = widget.colors ??
|
|
227
|
-
const total = rows.reduce((sum, row) => sum + toNumber(row[widget.valueKey]), 0);
|
|
554
|
+
const colors = widget.colors ?? DEFAULT_CHART_COLORS;
|
|
555
|
+
const total = rows.reduce((sum, row) => sum + toNumber$1(row[widget.valueKey]), 0);
|
|
228
556
|
const centerValue = widget.centerValuePath !== void 0 ? resolvePath(data, widget.centerValuePath) : total;
|
|
229
557
|
const radius = 38;
|
|
230
558
|
const stroke = 14;
|
|
231
559
|
const circumference = 2 * Math.PI * radius;
|
|
232
560
|
const arcs = rows.reduce((segments, row, index) => {
|
|
233
|
-
const pct = toNumber(row[widget.valueKey]) / total;
|
|
561
|
+
const pct = toNumber$1(row[widget.valueKey]) / total;
|
|
234
562
|
const offset = segments.reduce((sum, segment) => sum + segment.dash, 0);
|
|
235
563
|
segments.push({
|
|
236
564
|
key: `${String(row[widget.labelKey])}-${index}`,
|
|
@@ -294,7 +622,7 @@ function DonutChartWidgetView({ widget, data, loading }) {
|
|
|
294
622
|
className: "nb-dashboard-donut__legend",
|
|
295
623
|
children: rows.map((row, index) => {
|
|
296
624
|
const label = String(row[widget.labelKey] ?? "");
|
|
297
|
-
const value = toNumber(row[widget.valueKey]);
|
|
625
|
+
const value = toNumber$1(row[widget.valueKey]);
|
|
298
626
|
const pct = total > 0 ? value / total * 100 : 0;
|
|
299
627
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("li", {
|
|
300
628
|
className: "nb-dashboard-donut__legend-item",
|
|
@@ -319,6 +647,123 @@ function DonutChartWidgetView({ widget, data, loading }) {
|
|
|
319
647
|
});
|
|
320
648
|
}
|
|
321
649
|
//#endregion
|
|
650
|
+
//#region packages/dashboard/dashboard/widgets/LineChartWidgetView.tsx
|
|
651
|
+
function LineChartWidgetView({ widget, data, loading }) {
|
|
652
|
+
const rows = resolveArray(data, widget.dataPath);
|
|
653
|
+
const height = widget.height ?? 240;
|
|
654
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_nubitio_ui.StatCard, {
|
|
655
|
+
title: widget.title,
|
|
656
|
+
headerExtra: widget.subtitle ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
657
|
+
className: "nb-dashboard-widget__subtitle",
|
|
658
|
+
children: widget.subtitle
|
|
659
|
+
}) : void 0,
|
|
660
|
+
menuVisible: widget.menuVisible,
|
|
661
|
+
isLoading: loading,
|
|
662
|
+
className: "nb-dashboard-chart-card",
|
|
663
|
+
children: rows.length === 0 ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
664
|
+
className: "nb-dashboard-chart-empty",
|
|
665
|
+
children: "No data"
|
|
666
|
+
}) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
667
|
+
style: { height },
|
|
668
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(recharts.ResponsiveContainer, {
|
|
669
|
+
width: "100%",
|
|
670
|
+
height: "100%",
|
|
671
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(recharts.LineChart, {
|
|
672
|
+
data: rows,
|
|
673
|
+
margin: {
|
|
674
|
+
top: 4,
|
|
675
|
+
right: 8,
|
|
676
|
+
bottom: 0,
|
|
677
|
+
left: 0
|
|
678
|
+
},
|
|
679
|
+
children: [
|
|
680
|
+
widget.showGrid !== false && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(recharts.CartesianGrid, {
|
|
681
|
+
strokeDasharray: "3 3",
|
|
682
|
+
stroke: "var(--border-color)",
|
|
683
|
+
vertical: false
|
|
684
|
+
}),
|
|
685
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(recharts.XAxis, {
|
|
686
|
+
dataKey: widget.xKey,
|
|
687
|
+
tick: {
|
|
688
|
+
fontSize: 11,
|
|
689
|
+
fill: "var(--text-tertiary)"
|
|
690
|
+
},
|
|
691
|
+
axisLine: { stroke: "var(--border-color)" },
|
|
692
|
+
tickLine: false
|
|
693
|
+
}),
|
|
694
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(recharts.YAxis, {
|
|
695
|
+
tick: {
|
|
696
|
+
fontSize: 11,
|
|
697
|
+
fill: "var(--text-tertiary)"
|
|
698
|
+
},
|
|
699
|
+
axisLine: false,
|
|
700
|
+
tickLine: false,
|
|
701
|
+
width: 48,
|
|
702
|
+
tickFormatter: (v) => formatDashboardValue(v, widget.valueFormat)
|
|
703
|
+
}),
|
|
704
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(recharts.Tooltip, { content: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ChartTooltip, {
|
|
705
|
+
labelFormat: widget.valueFormat,
|
|
706
|
+
series: widget.series
|
|
707
|
+
}) }),
|
|
708
|
+
widget.showLegend && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(recharts.Legend, { wrapperStyle: { fontSize: 12 } }),
|
|
709
|
+
widget.series.map((series, index) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(recharts.Line, {
|
|
710
|
+
type: widget.curved === false ? "linear" : "monotone",
|
|
711
|
+
dataKey: series.key,
|
|
712
|
+
name: series.label ?? series.key,
|
|
713
|
+
stroke: series.color ?? DEFAULT_CHART_COLORS[index % DEFAULT_CHART_COLORS.length],
|
|
714
|
+
strokeWidth: 2,
|
|
715
|
+
dot: false,
|
|
716
|
+
activeDot: { r: 4 }
|
|
717
|
+
}, series.key))
|
|
718
|
+
]
|
|
719
|
+
})
|
|
720
|
+
})
|
|
721
|
+
})
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
//#endregion
|
|
725
|
+
//#region packages/dashboard/dashboard/widgets/ProgressWidgetView.tsx
|
|
726
|
+
function toNumber(value, fallback) {
|
|
727
|
+
const numeric = typeof value === "number" ? value : Number(value);
|
|
728
|
+
return Number.isFinite(numeric) ? numeric : fallback;
|
|
729
|
+
}
|
|
730
|
+
function ProgressWidgetView({ widget, data, loading }) {
|
|
731
|
+
const value = toNumber(widget.valuePath !== void 0 ? resolvePath(data, widget.valuePath) : widget.value, 0);
|
|
732
|
+
const max = widget.maxPath !== void 0 ? toNumber(resolvePath(data, widget.maxPath), 100) : widget.max ?? 100;
|
|
733
|
+
const pct = max > 0 ? Math.min(100, Math.max(0, value / max * 100)) : 0;
|
|
734
|
+
const tone = widget.tone ?? "accent";
|
|
735
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_nubitio_ui.StatCard, {
|
|
736
|
+
title: widget.title,
|
|
737
|
+
headerExtra: widget.subtitle ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
738
|
+
className: "nb-dashboard-widget__subtitle",
|
|
739
|
+
children: widget.subtitle
|
|
740
|
+
}) : void 0,
|
|
741
|
+
menuVisible: widget.menuVisible,
|
|
742
|
+
isLoading: loading,
|
|
743
|
+
className: `nb-dashboard-progress nb-dashboard-progress--${tone}`,
|
|
744
|
+
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
745
|
+
className: "nb-dashboard-progress__meter",
|
|
746
|
+
role: "progressbar",
|
|
747
|
+
"aria-valuenow": pct,
|
|
748
|
+
"aria-valuemin": 0,
|
|
749
|
+
"aria-valuemax": 100,
|
|
750
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
751
|
+
className: "nb-dashboard-progress__fill",
|
|
752
|
+
style: { width: `${pct}%` }
|
|
753
|
+
})
|
|
754
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
755
|
+
className: "nb-dashboard-progress__labels",
|
|
756
|
+
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
757
|
+
className: "nb-dashboard-progress__value",
|
|
758
|
+
children: formatDashboardValue(value, widget.format ?? "number")
|
|
759
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("span", {
|
|
760
|
+
className: "nb-dashboard-progress__max",
|
|
761
|
+
children: ["/ ", formatDashboardValue(max, widget.format ?? "number")]
|
|
762
|
+
})]
|
|
763
|
+
})]
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
//#endregion
|
|
322
767
|
//#region packages/dashboard/dashboard/widgets/StatWidgetView.tsx
|
|
323
768
|
function resolveTrend(widget, data) {
|
|
324
769
|
if (!widget.trend) return { value: null };
|
|
@@ -416,73 +861,278 @@ function TableWidgetView({ widget, data, loading }) {
|
|
|
416
861
|
});
|
|
417
862
|
}
|
|
418
863
|
//#endregion
|
|
864
|
+
//#region packages/dashboard/dashboard/widgets/registerBuiltinWidgets.ts
|
|
865
|
+
registerWidgetType("stat", StatWidgetView);
|
|
866
|
+
registerWidgetType("bar-chart", BarChartWidgetView);
|
|
867
|
+
registerWidgetType("donut-chart", DonutChartWidgetView);
|
|
868
|
+
registerWidgetType("line-chart", LineChartWidgetView);
|
|
869
|
+
registerWidgetType("area-chart", AreaChartWidgetView);
|
|
870
|
+
registerWidgetType("progress", ProgressWidgetView);
|
|
871
|
+
registerWidgetType("table", TableWidgetView);
|
|
872
|
+
//#endregion
|
|
419
873
|
//#region packages/dashboard/dashboard/widgets/WidgetRenderer.tsx
|
|
874
|
+
const isDev = typeof process !== "undefined" && process.env?.NODE_ENV !== "production";
|
|
420
875
|
function WidgetRenderer({ widget, data, loading }) {
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
876
|
+
const Component = getWidgetComponent(widget.type);
|
|
877
|
+
if (!Component) {
|
|
878
|
+
if (isDev) console.warn(`[@nubitio/dashboard] No widget registered for type "${widget.type}".`);
|
|
879
|
+
return null;
|
|
880
|
+
}
|
|
881
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Component, {
|
|
882
|
+
widget,
|
|
883
|
+
data,
|
|
884
|
+
loading
|
|
885
|
+
});
|
|
886
|
+
}
|
|
887
|
+
//#endregion
|
|
888
|
+
//#region packages/dashboard/dashboard/DashboardWidgetSlot.tsx
|
|
889
|
+
function DashboardWidgetSlot({ widget, data, loading, editMode, onHide, onMoveUp, onMoveDown, period, periodParamNames = DEFAULT_PERIOD_PARAM_NAMES }) {
|
|
890
|
+
const widgetQuery = useWidgetQuery(widget.query, period, periodParamNames);
|
|
891
|
+
const effectiveData = widgetQuery ? widgetQuery.data : data;
|
|
892
|
+
const effectiveLoading = widgetQuery ? widgetQuery.loading : loading;
|
|
893
|
+
const body = /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [widgetQuery?.error && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
894
|
+
className: "nb-dashboard-widget-slot__error",
|
|
895
|
+
children: widgetQuery.error
|
|
896
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(WidgetRenderer, {
|
|
897
|
+
widget,
|
|
898
|
+
data: effectiveData,
|
|
899
|
+
loading: effectiveLoading
|
|
900
|
+
})] });
|
|
901
|
+
if (!editMode) return body;
|
|
902
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
903
|
+
className: "nb-dashboard-widget-slot",
|
|
904
|
+
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
905
|
+
className: "nb-dashboard-widget-slot__controls",
|
|
906
|
+
children: [
|
|
907
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
|
|
908
|
+
type: "button",
|
|
909
|
+
className: "nb-dashboard-widget-slot__btn",
|
|
910
|
+
"aria-label": `Move ${widget.title} earlier`,
|
|
911
|
+
onClick: onMoveUp,
|
|
912
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("i", {
|
|
913
|
+
className: "ph ph-caret-up",
|
|
914
|
+
"aria-hidden": "true"
|
|
915
|
+
})
|
|
916
|
+
}),
|
|
917
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
|
|
918
|
+
type: "button",
|
|
919
|
+
className: "nb-dashboard-widget-slot__btn",
|
|
920
|
+
"aria-label": `Move ${widget.title} later`,
|
|
921
|
+
onClick: onMoveDown,
|
|
922
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("i", {
|
|
923
|
+
className: "ph ph-caret-down",
|
|
924
|
+
"aria-hidden": "true"
|
|
925
|
+
})
|
|
926
|
+
}),
|
|
927
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
|
|
928
|
+
type: "button",
|
|
929
|
+
className: "nb-dashboard-widget-slot__btn nb-dashboard-widget-slot__btn--hide",
|
|
930
|
+
"aria-label": `Hide ${widget.title}`,
|
|
931
|
+
onClick: onHide,
|
|
932
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("i", {
|
|
933
|
+
className: "ph ph-eye-slash",
|
|
934
|
+
"aria-hidden": "true"
|
|
935
|
+
})
|
|
936
|
+
})
|
|
937
|
+
]
|
|
938
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
939
|
+
className: "nb-dashboard-widget-slot__body",
|
|
940
|
+
children: body
|
|
941
|
+
})]
|
|
942
|
+
});
|
|
943
|
+
}
|
|
944
|
+
//#endregion
|
|
945
|
+
//#region packages/dashboard/dashboard/useDashboardData.ts
|
|
946
|
+
function useDashboardData(dataUrl, refreshInterval, period, periodParamNames = DEFAULT_PERIOD_PARAM_NAMES) {
|
|
947
|
+
const http = (0, _nubitio_core.useCoreHttpClient)();
|
|
948
|
+
const params = period ? {
|
|
949
|
+
[periodParamNames.start]: period.start,
|
|
950
|
+
[periodParamNames.end]: period.end
|
|
951
|
+
} : void 0;
|
|
952
|
+
const query = (0, _tanstack_react_query.useQuery)({
|
|
953
|
+
queryKey: [
|
|
954
|
+
"nb-dashboard",
|
|
955
|
+
dataUrl,
|
|
956
|
+
params
|
|
957
|
+
],
|
|
958
|
+
queryFn: async () => {
|
|
959
|
+
return (await http.get(dataUrl, { params })).data;
|
|
960
|
+
},
|
|
961
|
+
enabled: !!dataUrl,
|
|
962
|
+
refetchInterval: refreshInterval,
|
|
963
|
+
retry: false
|
|
964
|
+
});
|
|
965
|
+
return {
|
|
966
|
+
data: query.data ?? {},
|
|
967
|
+
loading: query.isLoading,
|
|
968
|
+
error: query.error ? query.error instanceof Error ? query.error.message : "Failed to load dashboard" : null,
|
|
969
|
+
refetch: () => {
|
|
970
|
+
query.refetch();
|
|
971
|
+
}
|
|
972
|
+
};
|
|
973
|
+
}
|
|
974
|
+
//#endregion
|
|
975
|
+
//#region packages/dashboard/dashboard/useDashboardLayout.ts
|
|
976
|
+
function storageKey(dashboardId) {
|
|
977
|
+
return `nb-dashboard-layout:${dashboardId}`;
|
|
978
|
+
}
|
|
979
|
+
function readStore(dashboardId) {
|
|
980
|
+
try {
|
|
981
|
+
const raw = window.localStorage.getItem(storageKey(dashboardId));
|
|
982
|
+
return raw ? JSON.parse(raw) : {};
|
|
983
|
+
} catch {
|
|
984
|
+
return {};
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
function writeStore(dashboardId, store) {
|
|
988
|
+
try {
|
|
989
|
+
window.localStorage.setItem(storageKey(dashboardId), JSON.stringify(store));
|
|
990
|
+
} catch {}
|
|
991
|
+
}
|
|
992
|
+
function useDashboardLayout(dashboardId, sections, enabled) {
|
|
993
|
+
const [editMode, setEditMode] = (0, react.useState)(false);
|
|
994
|
+
const id = dashboardId ?? "dashboard";
|
|
995
|
+
const [store, setStore] = (0, react.useState)(() => enabled ? readStore(id) : {});
|
|
996
|
+
const updateSection = (0, react.useCallback)((sectionId, updater) => {
|
|
997
|
+
setStore((prev) => {
|
|
998
|
+
const current = prev[sectionId] ?? {
|
|
999
|
+
order: [],
|
|
1000
|
+
hidden: []
|
|
1001
|
+
};
|
|
1002
|
+
const next = {
|
|
1003
|
+
...prev,
|
|
1004
|
+
[sectionId]: updater(current)
|
|
1005
|
+
};
|
|
1006
|
+
writeStore(id, next);
|
|
1007
|
+
return next;
|
|
431
1008
|
});
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
1009
|
+
}, [id]);
|
|
1010
|
+
const getSectionWidgets = (0, react.useCallback)((section, sectionId) => {
|
|
1011
|
+
if (!enabled) return section.widgets;
|
|
1012
|
+
const layout = store[sectionId];
|
|
1013
|
+
if (!layout) return section.widgets;
|
|
1014
|
+
const byId = new Map(section.widgets.map((widget) => [widget.id, widget]));
|
|
1015
|
+
const ordered = layout.order.map((wid) => byId.get(wid)).filter((w) => !!w);
|
|
1016
|
+
const remaining = section.widgets.filter((widget) => !layout.order.includes(widget.id));
|
|
1017
|
+
return [...ordered, ...remaining].filter((widget) => !layout.hidden.includes(widget.id));
|
|
1018
|
+
}, [enabled, store]);
|
|
1019
|
+
const hideWidget = (0, react.useCallback)((sectionId, widgetId) => {
|
|
1020
|
+
updateSection(sectionId, (current) => ({
|
|
1021
|
+
...current,
|
|
1022
|
+
hidden: current.hidden.includes(widgetId) ? current.hidden : [...current.hidden, widgetId]
|
|
1023
|
+
}));
|
|
1024
|
+
}, [updateSection]);
|
|
1025
|
+
const showWidget = (0, react.useCallback)((sectionId, widgetId) => {
|
|
1026
|
+
updateSection(sectionId, (current) => ({
|
|
1027
|
+
...current,
|
|
1028
|
+
hidden: current.hidden.filter((wid) => wid !== widgetId)
|
|
1029
|
+
}));
|
|
1030
|
+
}, [updateSection]);
|
|
1031
|
+
const moveWidget = (0, react.useCallback)((sectionId, widgetId, direction) => {
|
|
1032
|
+
const section = sections.find((s) => (s.id ?? "") === sectionId);
|
|
1033
|
+
if (!section) return;
|
|
1034
|
+
updateSection(sectionId, (current) => {
|
|
1035
|
+
const base = current.order.length ? current.order : section.widgets.map((w) => w.id);
|
|
1036
|
+
const index = base.indexOf(widgetId);
|
|
1037
|
+
const targetIndex = index + direction;
|
|
1038
|
+
if (index === -1 || targetIndex < 0 || targetIndex >= base.length) return current;
|
|
1039
|
+
const order = [...base];
|
|
1040
|
+
[order[index], order[targetIndex]] = [order[targetIndex], order[index]];
|
|
1041
|
+
return {
|
|
1042
|
+
...current,
|
|
1043
|
+
order
|
|
1044
|
+
};
|
|
436
1045
|
});
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
1046
|
+
}, [sections, updateSection]);
|
|
1047
|
+
const resetLayout = (0, react.useCallback)(() => {
|
|
1048
|
+
setStore({});
|
|
1049
|
+
writeStore(id, {});
|
|
1050
|
+
}, [id]);
|
|
1051
|
+
const hiddenWidgets = (0, react.useMemo)(() => {
|
|
1052
|
+
if (!enabled) return [];
|
|
1053
|
+
return sections.flatMap((section) => {
|
|
1054
|
+
const sectionId = section.id ?? "";
|
|
1055
|
+
const hidden = store[sectionId]?.hidden ?? [];
|
|
1056
|
+
return section.widgets.filter((widget) => hidden.includes(widget.id)).map((widget) => ({
|
|
1057
|
+
sectionId,
|
|
1058
|
+
widget
|
|
1059
|
+
}));
|
|
441
1060
|
});
|
|
442
|
-
|
|
443
|
-
|
|
1061
|
+
}, [
|
|
1062
|
+
enabled,
|
|
1063
|
+
sections,
|
|
1064
|
+
store
|
|
1065
|
+
]);
|
|
1066
|
+
if (!enabled) return void 0;
|
|
1067
|
+
return {
|
|
1068
|
+
editMode,
|
|
1069
|
+
setEditMode,
|
|
1070
|
+
getSectionWidgets,
|
|
1071
|
+
hiddenWidgets,
|
|
1072
|
+
hideWidget,
|
|
1073
|
+
showWidget,
|
|
1074
|
+
moveWidget,
|
|
1075
|
+
resetLayout
|
|
1076
|
+
};
|
|
444
1077
|
}
|
|
445
1078
|
//#endregion
|
|
446
1079
|
//#region packages/dashboard/dashboard/DashboardPage.tsx
|
|
447
1080
|
function sectionClassName(section) {
|
|
448
|
-
|
|
449
|
-
if (layout === "grid") return (typeof section.columns === "number" ? `repeat(${section.columns}, minmax(0, 1fr))` : section.columns) ? "nb-dashboard-section nb-dashboard-section--grid" : "nb-dashboard-section nb-dashboard-section--grid";
|
|
450
|
-
return `nb-dashboard-section nb-dashboard-section--${layout}`;
|
|
1081
|
+
return `nb-dashboard-section nb-dashboard-section--${section.layout ?? "grid"}`;
|
|
451
1082
|
}
|
|
452
1083
|
function sectionStyle(section) {
|
|
453
1084
|
if (section.layout !== "grid" && section.layout !== void 0) return void 0;
|
|
454
1085
|
const columns = typeof section.columns === "number" ? `repeat(${section.columns}, minmax(0, 1fr))` : section.columns;
|
|
455
1086
|
if (!columns) return void 0;
|
|
456
|
-
return {
|
|
1087
|
+
return { "--nb-dashboard-columns": String(columns) };
|
|
457
1088
|
}
|
|
458
|
-
function DashboardSectionView({ section, data, loading }) {
|
|
1089
|
+
function DashboardSectionView({ section, sectionId, data, loading, layout, period, periodParamNames }) {
|
|
1090
|
+
const widgets = layout ? layout.getSectionWidgets(section, sectionId) : section.widgets;
|
|
459
1091
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("section", {
|
|
460
1092
|
className: sectionClassName(section),
|
|
461
1093
|
style: sectionStyle(section),
|
|
462
1094
|
"data-section": section.id,
|
|
463
|
-
children:
|
|
1095
|
+
children: widgets.map((widget) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DashboardWidgetSlot, {
|
|
464
1096
|
widget,
|
|
465
1097
|
data,
|
|
466
|
-
loading
|
|
1098
|
+
loading,
|
|
1099
|
+
editMode: !!layout?.editMode,
|
|
1100
|
+
onHide: () => layout?.hideWidget(sectionId, widget.id),
|
|
1101
|
+
onMoveUp: () => layout?.moveWidget(sectionId, widget.id, -1),
|
|
1102
|
+
onMoveDown: () => layout?.moveWidget(sectionId, widget.id, 1),
|
|
1103
|
+
period,
|
|
1104
|
+
periodParamNames
|
|
467
1105
|
}, widget.id))
|
|
468
1106
|
});
|
|
469
1107
|
}
|
|
470
1108
|
function DashboardPage({ config }) {
|
|
471
|
-
const
|
|
472
|
-
const
|
|
1109
|
+
const period = useDashboardPeriod(config.period);
|
|
1110
|
+
const layout = useDashboardLayout(config.id, config.sections, !!config.customizable);
|
|
1111
|
+
const fetched = useDashboardData(config.dataUrl, config.refreshInterval, period?.value, config.period?.paramNames);
|
|
1112
|
+
const { data, loading, error, refetch } = config.useData?.(period?.value) ?? fetched;
|
|
1113
|
+
const periodParamNames = config.period?.paramNames ?? DEFAULT_PERIOD_PARAM_NAMES;
|
|
1114
|
+
const toolbarExtras = /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [period && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DashboardPeriodFilter, { period }), layout && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DashboardLayoutControls, { layout })] });
|
|
473
1115
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
474
1116
|
className: "view-wrapper-scroll nb-dashboard-page",
|
|
475
1117
|
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_nubitio_ui.AppToolbar, {
|
|
476
1118
|
title: config.title,
|
|
477
1119
|
onRefresh: refetch,
|
|
1120
|
+
additionalToolbarContent: toolbarExtras,
|
|
478
1121
|
children: [error && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
479
1122
|
className: "nb-dashboard-page__error",
|
|
480
1123
|
children: error
|
|
481
|
-
}), config.sections.map((section, index) =>
|
|
482
|
-
section
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
1124
|
+
}), config.sections.map((section, index) => {
|
|
1125
|
+
const sectionId = section.id ?? `section-${index}`;
|
|
1126
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DashboardSectionView, {
|
|
1127
|
+
section,
|
|
1128
|
+
sectionId,
|
|
1129
|
+
data,
|
|
1130
|
+
loading,
|
|
1131
|
+
layout,
|
|
1132
|
+
period: period?.value,
|
|
1133
|
+
periodParamNames
|
|
1134
|
+
}, sectionId);
|
|
1135
|
+
})]
|
|
486
1136
|
}), loading && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
487
1137
|
className: "nb-dashboard-page__loading",
|
|
488
1138
|
"aria-hidden": "true",
|
|
@@ -524,14 +1174,58 @@ function tableWidget(config) {
|
|
|
524
1174
|
...config
|
|
525
1175
|
};
|
|
526
1176
|
}
|
|
1177
|
+
function lineChartWidget(config) {
|
|
1178
|
+
return {
|
|
1179
|
+
type: "line-chart",
|
|
1180
|
+
menuVisible: false,
|
|
1181
|
+
showGrid: true,
|
|
1182
|
+
height: 240,
|
|
1183
|
+
...config
|
|
1184
|
+
};
|
|
1185
|
+
}
|
|
1186
|
+
function areaChartWidget(config) {
|
|
1187
|
+
return {
|
|
1188
|
+
type: "area-chart",
|
|
1189
|
+
menuVisible: false,
|
|
1190
|
+
showGrid: true,
|
|
1191
|
+
height: 240,
|
|
1192
|
+
...config
|
|
1193
|
+
};
|
|
1194
|
+
}
|
|
1195
|
+
function progressWidget(config) {
|
|
1196
|
+
return {
|
|
1197
|
+
type: "progress",
|
|
1198
|
+
menuVisible: false,
|
|
1199
|
+
max: 100,
|
|
1200
|
+
format: "number",
|
|
1201
|
+
...config
|
|
1202
|
+
};
|
|
1203
|
+
}
|
|
527
1204
|
//#endregion
|
|
1205
|
+
exports.CUSTOM_PERIOD_KEY = CUSTOM_PERIOD_KEY;
|
|
1206
|
+
exports.DEFAULT_CHART_COLORS = DEFAULT_CHART_COLORS;
|
|
1207
|
+
exports.DEFAULT_PERIOD_PARAM_NAMES = DEFAULT_PERIOD_PARAM_NAMES;
|
|
1208
|
+
exports.DEFAULT_PERIOD_PRESETS = DEFAULT_PERIOD_PRESETS;
|
|
1209
|
+
exports.DashboardLayoutControls = DashboardLayoutControls;
|
|
528
1210
|
exports.DashboardPage = DashboardPage;
|
|
1211
|
+
exports.DashboardPeriodFilter = DashboardPeriodFilter;
|
|
1212
|
+
exports.DashboardWidgetSlot = DashboardWidgetSlot;
|
|
1213
|
+
exports.areaChartWidget = areaChartWidget;
|
|
529
1214
|
exports.barChartWidget = barChartWidget;
|
|
530
1215
|
exports.defineDashboard = defineDashboard;
|
|
531
1216
|
exports.donutChartWidget = donutChartWidget;
|
|
532
1217
|
exports.formatDashboardValue = formatDashboardValue;
|
|
1218
|
+
exports.getPeriodPreset = getPeriodPreset;
|
|
1219
|
+
exports.getWidgetComponent = getWidgetComponent;
|
|
1220
|
+
exports.isWidgetTypeRegistered = isWidgetTypeRegistered;
|
|
1221
|
+
exports.lineChartWidget = lineChartWidget;
|
|
1222
|
+
exports.progressWidget = progressWidget;
|
|
1223
|
+
exports.registerWidgetType = registerWidgetType;
|
|
533
1224
|
exports.resolveArray = resolveArray;
|
|
534
1225
|
exports.resolvePath = resolvePath;
|
|
535
1226
|
exports.statWidget = statWidget;
|
|
536
1227
|
exports.tableWidget = tableWidget;
|
|
537
1228
|
exports.useDashboardData = useDashboardData;
|
|
1229
|
+
exports.useDashboardLayout = useDashboardLayout;
|
|
1230
|
+
exports.useDashboardPeriod = useDashboardPeriod;
|
|
1231
|
+
exports.useWidgetQuery = useWidgetQuery;
|