@nubitio/dashboard 0.8.0 → 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 CHANGED
@@ -5,6 +5,7 @@ let react = require("react");
5
5
  let react_router_dom = require("react-router-dom");
6
6
  let _tanstack_react_query = require("@tanstack/react-query");
7
7
  let _nubitio_core = require("@nubitio/core");
8
+ let recharts = require("recharts");
8
9
  //#region packages/dashboard/dashboard/defineDashboard.ts
9
10
  const dashboardCache = /* @__PURE__ */ new Map();
10
11
  function cacheKey(config) {
@@ -364,6 +365,511 @@ function resolveArray(source, path) {
364
365
  return value.filter((item) => item !== null && typeof item === "object");
365
366
  }
366
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
499
+ //#region packages/dashboard/dashboard/widgets/BarChartWidgetView.tsx
500
+ function toNumber$2(value) {
501
+ const numeric = typeof value === "number" ? value : Number(value);
502
+ return Number.isFinite(numeric) ? numeric : 0;
503
+ }
504
+ function BarChartWidgetView({ widget, data, loading }) {
505
+ const rows = resolveArray(data, widget.dataPath);
506
+ const max = rows.reduce((peak, row) => Math.max(peak, toNumber$2(row[widget.yKey])), 0) || 1;
507
+ const height = widget.height ?? 200;
508
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_nubitio_ui.StatCard, {
509
+ title: widget.title,
510
+ headerExtra: widget.subtitle ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
511
+ className: "nb-dashboard-widget__subtitle",
512
+ children: widget.subtitle
513
+ }) : void 0,
514
+ menuVisible: widget.menuVisible,
515
+ isLoading: loading,
516
+ className: "nb-dashboard-chart-card",
517
+ children: rows.length === 0 ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
518
+ className: "nb-dashboard-chart-empty",
519
+ children: "No data"
520
+ }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
521
+ className: "nb-dashboard-bar-chart",
522
+ style: { height },
523
+ role: "img",
524
+ "aria-label": widget.title,
525
+ children: rows.map((row, index) => {
526
+ const label = String(row[widget.xKey] ?? "");
527
+ const value = toNumber$2(row[widget.yKey]);
528
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
529
+ className: "nb-dashboard-bar-chart__item",
530
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
531
+ className: "nb-dashboard-bar-chart__bar-wrap",
532
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
533
+ className: "nb-dashboard-bar-chart__bar",
534
+ style: { height: `${Math.max(4, value / max * 100)}%` },
535
+ title: `${label}: ${formatDashboardValue(value, widget.valueFormat)}`
536
+ })
537
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
538
+ className: "nb-dashboard-bar-chart__label",
539
+ children: label
540
+ })]
541
+ }, `${label}-${index}`);
542
+ })
543
+ })
544
+ });
545
+ }
546
+ //#endregion
547
+ //#region packages/dashboard/dashboard/widgets/DonutChartWidgetView.tsx
548
+ function toNumber$1(value) {
549
+ const numeric = typeof value === "number" ? value : Number(value);
550
+ return Number.isFinite(numeric) ? numeric : 0;
551
+ }
552
+ function DonutChartWidgetView({ widget, data, loading }) {
553
+ const rows = resolveArray(data, widget.dataPath);
554
+ const colors = widget.colors ?? DEFAULT_CHART_COLORS;
555
+ const total = rows.reduce((sum, row) => sum + toNumber$1(row[widget.valueKey]), 0);
556
+ const centerValue = widget.centerValuePath !== void 0 ? resolvePath(data, widget.centerValuePath) : total;
557
+ const radius = 38;
558
+ const stroke = 14;
559
+ const circumference = 2 * Math.PI * radius;
560
+ const arcs = rows.reduce((segments, row, index) => {
561
+ const pct = toNumber$1(row[widget.valueKey]) / total;
562
+ const offset = segments.reduce((sum, segment) => sum + segment.dash, 0);
563
+ segments.push({
564
+ key: `${String(row[widget.labelKey])}-${index}`,
565
+ dash: pct * circumference,
566
+ offset,
567
+ color: colors[index % colors.length]
568
+ });
569
+ return segments;
570
+ }, []);
571
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_nubitio_ui.StatCard, {
572
+ title: widget.title,
573
+ headerExtra: widget.subtitle ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
574
+ className: "nb-dashboard-widget__subtitle",
575
+ children: widget.subtitle
576
+ }) : void 0,
577
+ menuVisible: widget.menuVisible,
578
+ isLoading: loading,
579
+ className: "nb-dashboard-chart-card",
580
+ children: rows.length === 0 || total <= 0 ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
581
+ className: "nb-dashboard-chart-empty",
582
+ children: "No data"
583
+ }) : /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
584
+ className: "nb-dashboard-donut",
585
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
586
+ className: "nb-dashboard-donut__chart",
587
+ role: "img",
588
+ "aria-label": widget.title,
589
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("svg", {
590
+ viewBox: "0 0 100 100",
591
+ className: "nb-dashboard-donut__svg",
592
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("circle", {
593
+ cx: "50",
594
+ cy: "50",
595
+ r: radius,
596
+ fill: "none",
597
+ stroke: "var(--surface-3)",
598
+ strokeWidth: stroke
599
+ }), arcs.map((arc) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("circle", {
600
+ cx: "50",
601
+ cy: "50",
602
+ r: radius,
603
+ fill: "none",
604
+ stroke: arc.color,
605
+ strokeWidth: stroke,
606
+ strokeDasharray: `${arc.dash} ${circumference - arc.dash}`,
607
+ strokeDashoffset: -arc.offset,
608
+ transform: "rotate(-90 50 50)",
609
+ strokeLinecap: "butt"
610
+ }, arc.key))]
611
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
612
+ className: "nb-dashboard-donut__center",
613
+ children: [widget.centerLabel && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
614
+ className: "nb-dashboard-donut__center-label",
615
+ children: widget.centerLabel
616
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
617
+ className: "nb-dashboard-donut__center-value",
618
+ children: formatDashboardValue(centerValue, widget.valueFormat)
619
+ })]
620
+ })]
621
+ }), widget.showLegend !== false && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("ul", {
622
+ className: "nb-dashboard-donut__legend",
623
+ children: rows.map((row, index) => {
624
+ const label = String(row[widget.labelKey] ?? "");
625
+ const value = toNumber$1(row[widget.valueKey]);
626
+ const pct = total > 0 ? value / total * 100 : 0;
627
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("li", {
628
+ className: "nb-dashboard-donut__legend-item",
629
+ children: [
630
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
631
+ className: "nb-dashboard-donut__legend-swatch",
632
+ style: { background: colors[index % colors.length] }
633
+ }),
634
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
635
+ className: "nb-dashboard-donut__legend-label",
636
+ children: label
637
+ }),
638
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("span", {
639
+ className: "nb-dashboard-donut__legend-value",
640
+ children: [pct.toFixed(0), "%"]
641
+ })
642
+ ]
643
+ }, `${label}-${index}`);
644
+ })
645
+ })]
646
+ })
647
+ });
648
+ }
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
767
+ //#region packages/dashboard/dashboard/widgets/StatWidgetView.tsx
768
+ function resolveTrend(widget, data) {
769
+ if (!widget.trend) return { value: null };
770
+ const raw = widget.trend.valuePath !== void 0 ? resolvePath(data, widget.trend.valuePath) : widget.trend.value;
771
+ const numeric = typeof raw === "number" ? raw : Number(raw);
772
+ return {
773
+ value: Number.isFinite(numeric) ? numeric : null,
774
+ label: widget.trend.label,
775
+ invert: widget.trend.invertColors
776
+ };
777
+ }
778
+ function StatWidgetView({ widget, data, loading }) {
779
+ const valueText = formatDashboardValue(widget.valuePath !== void 0 ? resolvePath(data, widget.valuePath) : widget.value, widget.format ?? "text");
780
+ const trend = resolveTrend(widget, data);
781
+ const trendDirection = trend.value === null || trend.value === 0 ? "flat" : trend.value > 0 ? "up" : "down";
782
+ const trendClass = trendDirection === "flat" ? "nb-stat-card__delta--flat" : trendDirection === "up" ? trend.invert ? "nb-stat-card__delta--down" : "nb-stat-card__delta--up" : trend.invert ? "nb-stat-card__delta--up" : "nb-stat-card__delta--down";
783
+ const trendArrow = trendDirection === "up" ? "▲" : trendDirection === "down" ? "▼" : "—";
784
+ const trendText = trend.value === null ? null : `${trendArrow} ${formatDashboardValue(Math.abs(trend.value), "percent")}${trend.label ? ` ${trend.label}` : ""}`;
785
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_nubitio_ui.StatCard, {
786
+ title: widget.title,
787
+ menuVisible: widget.menuVisible,
788
+ isLoading: loading,
789
+ className: `nb-dashboard-stat nb-dashboard-stat--${widget.iconTone ?? "accent"}`,
790
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
791
+ className: "nb-dashboard-stat__content",
792
+ children: [widget.icon && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
793
+ className: `nb-dashboard-stat__icon nb-dashboard-stat__icon--${widget.iconTone ?? "accent"}`,
794
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("i", {
795
+ className: `ph ${widget.icon}`,
796
+ "aria-hidden": "true"
797
+ })
798
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
799
+ className: "nb-dashboard-stat__metrics",
800
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
801
+ className: "nb-stat-card__value nb-stat-card__value--2xl",
802
+ children: valueText
803
+ }), trendText && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
804
+ className: `nb-stat-card__delta ${trendClass}`,
805
+ children: trendText
806
+ })]
807
+ })]
808
+ })
809
+ });
810
+ }
811
+ //#endregion
812
+ //#region packages/dashboard/dashboard/widgets/TableWidgetView.tsx
813
+ function renderCell(row, column) {
814
+ const raw = row[column.key];
815
+ if (column.badge) {
816
+ const key = raw == null ? "" : String(raw);
817
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_nubitio_ui.Badge, {
818
+ variant: column.badge[key] ?? "secondary",
819
+ size: "sm",
820
+ children: column.badgeLabels?.[key] ?? key.replace(/_/g, " ")
821
+ });
822
+ }
823
+ return formatDashboardValue(raw, column.format ?? "text");
824
+ }
825
+ function TableWidgetView({ widget, data, loading }) {
826
+ const rows = resolveArray(data, widget.dataPath);
827
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_nubitio_ui.StatCard, {
828
+ title: widget.title,
829
+ headerExtra: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
830
+ className: "nb-dashboard-table__header-extra",
831
+ children: [widget.subtitle && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
832
+ className: "nb-dashboard-widget__subtitle",
833
+ children: widget.subtitle
834
+ }), widget.viewAll && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_router_dom.Link, {
835
+ to: widget.viewAll.to,
836
+ className: "nb-dashboard-table__view-all",
837
+ children: widget.viewAll.label ?? "View all"
838
+ })]
839
+ }),
840
+ menuVisible: widget.menuVisible,
841
+ isLoading: loading,
842
+ className: "nb-dashboard-table-card",
843
+ children: rows.length === 0 ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_nubitio_ui.EmptyState, {
844
+ size: "sm",
845
+ icon: "ph ph-table",
846
+ title: widget.emptyTitle ?? "No records yet",
847
+ description: widget.emptyDescription
848
+ }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
849
+ className: "nb-dashboard-table-wrap",
850
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("table", {
851
+ className: "nb-dashboard-table",
852
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("thead", { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("tr", { children: widget.columns.map((column) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("th", {
853
+ className: column.align ? `nb-dashboard-table__cell--${column.align}` : void 0,
854
+ children: column.label
855
+ }, column.key)) }) }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("tbody", { children: rows.map((row, rowIndex) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("tr", { children: widget.columns.map((column) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("td", {
856
+ className: column.align ? `nb-dashboard-table__cell--${column.align}` : void 0,
857
+ children: renderCell(row, column)
858
+ }, column.key)) }, rowIndex)) })]
859
+ })
860
+ })
861
+ });
862
+ }
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
367
873
  //#region packages/dashboard/dashboard/widgets/WidgetRenderer.tsx
368
874
  const isDev = typeof process !== "undefined" && process.env?.NODE_ENV !== "production";
369
875
  function WidgetRenderer({ widget, data, loading }) {
package/dist/index.mjs CHANGED
@@ -1,9 +1,10 @@
1
- import { AppToolbar, DateRangePicker, IconButton, Popover, SegmentedControl } from "@nubitio/ui";
1
+ import { AppToolbar, Badge, DateRangePicker, EmptyState, IconButton, Popover, SegmentedControl, StatCard } from "@nubitio/ui";
2
2
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
3
3
  import { useCallback, useMemo, useState } from "react";
4
- import { useSearchParams } from "react-router-dom";
4
+ import { Link, useSearchParams } from "react-router-dom";
5
5
  import { useQuery } from "@tanstack/react-query";
6
6
  import { getCoreCurrency, getCoreLocale, useCoreHttpClient } from "@nubitio/core";
7
+ import { Area, AreaChart, CartesianGrid, Legend, Line, LineChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from "recharts";
7
8
  //#region packages/dashboard/dashboard/defineDashboard.ts
8
9
  const dashboardCache = /* @__PURE__ */ new Map();
9
10
  function cacheKey(config) {
@@ -363,6 +364,511 @@ function resolveArray(source, path) {
363
364
  return value.filter((item) => item !== null && typeof item === "object");
364
365
  }
365
366
  //#endregion
367
+ //#region packages/dashboard/dashboard/widgets/ChartTooltip.tsx
368
+ function ChartTooltip({ active, label, payload, labelFormat, series }) {
369
+ if (!active || !payload?.length) return null;
370
+ return /* @__PURE__ */ jsxs("div", {
371
+ className: "nb-dashboard-chart-tooltip",
372
+ children: [label !== void 0 && /* @__PURE__ */ jsx("div", {
373
+ className: "nb-dashboard-chart-tooltip__label",
374
+ children: label
375
+ }), /* @__PURE__ */ jsx("ul", {
376
+ className: "nb-dashboard-chart-tooltip__items",
377
+ children: payload.map((item) => {
378
+ const seriesConfig = series?.find((s) => s.key === item.dataKey);
379
+ return /* @__PURE__ */ jsxs("li", {
380
+ className: "nb-dashboard-chart-tooltip__item",
381
+ children: [
382
+ /* @__PURE__ */ jsx("span", {
383
+ className: "nb-dashboard-chart-tooltip__swatch",
384
+ style: { background: item.color }
385
+ }),
386
+ /* @__PURE__ */ jsx("span", {
387
+ className: "nb-dashboard-chart-tooltip__name",
388
+ children: item.name
389
+ }),
390
+ /* @__PURE__ */ jsx("span", {
391
+ className: "nb-dashboard-chart-tooltip__value",
392
+ children: formatDashboardValue(item.value, seriesConfig?.valueFormat ?? labelFormat)
393
+ })
394
+ ]
395
+ }, String(item.dataKey));
396
+ })
397
+ })]
398
+ });
399
+ }
400
+ //#endregion
401
+ //#region packages/dashboard/dashboard/widgets/AreaChartWidgetView.tsx
402
+ function AreaChartWidgetView({ widget, data, loading }) {
403
+ const rows = resolveArray(data, widget.dataPath);
404
+ const height = widget.height ?? 240;
405
+ return /* @__PURE__ */ jsx(StatCard, {
406
+ title: widget.title,
407
+ headerExtra: widget.subtitle ? /* @__PURE__ */ jsx("span", {
408
+ className: "nb-dashboard-widget__subtitle",
409
+ children: widget.subtitle
410
+ }) : void 0,
411
+ menuVisible: widget.menuVisible,
412
+ isLoading: loading,
413
+ className: "nb-dashboard-chart-card",
414
+ children: rows.length === 0 ? /* @__PURE__ */ jsx("div", {
415
+ className: "nb-dashboard-chart-empty",
416
+ children: "No data"
417
+ }) : /* @__PURE__ */ jsx("div", {
418
+ style: { height },
419
+ children: /* @__PURE__ */ jsx(ResponsiveContainer, {
420
+ width: "100%",
421
+ height: "100%",
422
+ children: /* @__PURE__ */ jsxs(AreaChart, {
423
+ data: rows,
424
+ margin: {
425
+ top: 4,
426
+ right: 8,
427
+ bottom: 0,
428
+ left: 0
429
+ },
430
+ children: [
431
+ /* @__PURE__ */ jsx("defs", { children: widget.series.map((series, index) => {
432
+ const color = series.color ?? DEFAULT_CHART_COLORS[index % DEFAULT_CHART_COLORS.length];
433
+ return /* @__PURE__ */ jsxs("linearGradient", {
434
+ id: `nb-area-${widget.id}-${series.key}`,
435
+ x1: "0",
436
+ y1: "0",
437
+ x2: "0",
438
+ y2: "1",
439
+ children: [/* @__PURE__ */ jsx("stop", {
440
+ offset: "5%",
441
+ stopColor: color,
442
+ stopOpacity: .35
443
+ }), /* @__PURE__ */ jsx("stop", {
444
+ offset: "95%",
445
+ stopColor: color,
446
+ stopOpacity: .02
447
+ })]
448
+ }, series.key);
449
+ }) }),
450
+ widget.showGrid !== false && /* @__PURE__ */ jsx(CartesianGrid, {
451
+ strokeDasharray: "3 3",
452
+ stroke: "var(--border-color)",
453
+ vertical: false
454
+ }),
455
+ /* @__PURE__ */ jsx(XAxis, {
456
+ dataKey: widget.xKey,
457
+ tick: {
458
+ fontSize: 11,
459
+ fill: "var(--text-tertiary)"
460
+ },
461
+ axisLine: { stroke: "var(--border-color)" },
462
+ tickLine: false
463
+ }),
464
+ /* @__PURE__ */ jsx(YAxis, {
465
+ tick: {
466
+ fontSize: 11,
467
+ fill: "var(--text-tertiary)"
468
+ },
469
+ axisLine: false,
470
+ tickLine: false,
471
+ width: 48,
472
+ tickFormatter: (v) => formatDashboardValue(v, widget.valueFormat)
473
+ }),
474
+ /* @__PURE__ */ jsx(Tooltip, { content: /* @__PURE__ */ jsx(ChartTooltip, {
475
+ labelFormat: widget.valueFormat,
476
+ series: widget.series
477
+ }) }),
478
+ widget.showLegend && /* @__PURE__ */ jsx(Legend, { wrapperStyle: { fontSize: 12 } }),
479
+ widget.series.map((series, index) => {
480
+ const color = series.color ?? DEFAULT_CHART_COLORS[index % DEFAULT_CHART_COLORS.length];
481
+ return /* @__PURE__ */ jsx(Area, {
482
+ type: "monotone",
483
+ dataKey: series.key,
484
+ name: series.label ?? series.key,
485
+ stroke: color,
486
+ strokeWidth: 2,
487
+ fill: `url(#nb-area-${widget.id}-${series.key})`,
488
+ stackId: widget.stacked ? "stack" : void 0
489
+ }, series.key);
490
+ })
491
+ ]
492
+ })
493
+ })
494
+ })
495
+ });
496
+ }
497
+ //#endregion
498
+ //#region packages/dashboard/dashboard/widgets/BarChartWidgetView.tsx
499
+ function toNumber$2(value) {
500
+ const numeric = typeof value === "number" ? value : Number(value);
501
+ return Number.isFinite(numeric) ? numeric : 0;
502
+ }
503
+ function BarChartWidgetView({ widget, data, loading }) {
504
+ const rows = resolveArray(data, widget.dataPath);
505
+ const max = rows.reduce((peak, row) => Math.max(peak, toNumber$2(row[widget.yKey])), 0) || 1;
506
+ const height = widget.height ?? 200;
507
+ return /* @__PURE__ */ jsx(StatCard, {
508
+ title: widget.title,
509
+ headerExtra: widget.subtitle ? /* @__PURE__ */ jsx("span", {
510
+ className: "nb-dashboard-widget__subtitle",
511
+ children: widget.subtitle
512
+ }) : void 0,
513
+ menuVisible: widget.menuVisible,
514
+ isLoading: loading,
515
+ className: "nb-dashboard-chart-card",
516
+ children: rows.length === 0 ? /* @__PURE__ */ jsx("div", {
517
+ className: "nb-dashboard-chart-empty",
518
+ children: "No data"
519
+ }) : /* @__PURE__ */ jsx("div", {
520
+ className: "nb-dashboard-bar-chart",
521
+ style: { height },
522
+ role: "img",
523
+ "aria-label": widget.title,
524
+ children: rows.map((row, index) => {
525
+ const label = String(row[widget.xKey] ?? "");
526
+ const value = toNumber$2(row[widget.yKey]);
527
+ return /* @__PURE__ */ jsxs("div", {
528
+ className: "nb-dashboard-bar-chart__item",
529
+ children: [/* @__PURE__ */ jsx("div", {
530
+ className: "nb-dashboard-bar-chart__bar-wrap",
531
+ children: /* @__PURE__ */ jsx("div", {
532
+ className: "nb-dashboard-bar-chart__bar",
533
+ style: { height: `${Math.max(4, value / max * 100)}%` },
534
+ title: `${label}: ${formatDashboardValue(value, widget.valueFormat)}`
535
+ })
536
+ }), /* @__PURE__ */ jsx("span", {
537
+ className: "nb-dashboard-bar-chart__label",
538
+ children: label
539
+ })]
540
+ }, `${label}-${index}`);
541
+ })
542
+ })
543
+ });
544
+ }
545
+ //#endregion
546
+ //#region packages/dashboard/dashboard/widgets/DonutChartWidgetView.tsx
547
+ function toNumber$1(value) {
548
+ const numeric = typeof value === "number" ? value : Number(value);
549
+ return Number.isFinite(numeric) ? numeric : 0;
550
+ }
551
+ function DonutChartWidgetView({ widget, data, loading }) {
552
+ const rows = resolveArray(data, widget.dataPath);
553
+ const colors = widget.colors ?? DEFAULT_CHART_COLORS;
554
+ const total = rows.reduce((sum, row) => sum + toNumber$1(row[widget.valueKey]), 0);
555
+ const centerValue = widget.centerValuePath !== void 0 ? resolvePath(data, widget.centerValuePath) : total;
556
+ const radius = 38;
557
+ const stroke = 14;
558
+ const circumference = 2 * Math.PI * radius;
559
+ const arcs = rows.reduce((segments, row, index) => {
560
+ const pct = toNumber$1(row[widget.valueKey]) / total;
561
+ const offset = segments.reduce((sum, segment) => sum + segment.dash, 0);
562
+ segments.push({
563
+ key: `${String(row[widget.labelKey])}-${index}`,
564
+ dash: pct * circumference,
565
+ offset,
566
+ color: colors[index % colors.length]
567
+ });
568
+ return segments;
569
+ }, []);
570
+ return /* @__PURE__ */ jsx(StatCard, {
571
+ title: widget.title,
572
+ headerExtra: widget.subtitle ? /* @__PURE__ */ jsx("span", {
573
+ className: "nb-dashboard-widget__subtitle",
574
+ children: widget.subtitle
575
+ }) : void 0,
576
+ menuVisible: widget.menuVisible,
577
+ isLoading: loading,
578
+ className: "nb-dashboard-chart-card",
579
+ children: rows.length === 0 || total <= 0 ? /* @__PURE__ */ jsx("div", {
580
+ className: "nb-dashboard-chart-empty",
581
+ children: "No data"
582
+ }) : /* @__PURE__ */ jsxs("div", {
583
+ className: "nb-dashboard-donut",
584
+ children: [/* @__PURE__ */ jsxs("div", {
585
+ className: "nb-dashboard-donut__chart",
586
+ role: "img",
587
+ "aria-label": widget.title,
588
+ children: [/* @__PURE__ */ jsxs("svg", {
589
+ viewBox: "0 0 100 100",
590
+ className: "nb-dashboard-donut__svg",
591
+ children: [/* @__PURE__ */ jsx("circle", {
592
+ cx: "50",
593
+ cy: "50",
594
+ r: radius,
595
+ fill: "none",
596
+ stroke: "var(--surface-3)",
597
+ strokeWidth: stroke
598
+ }), arcs.map((arc) => /* @__PURE__ */ jsx("circle", {
599
+ cx: "50",
600
+ cy: "50",
601
+ r: radius,
602
+ fill: "none",
603
+ stroke: arc.color,
604
+ strokeWidth: stroke,
605
+ strokeDasharray: `${arc.dash} ${circumference - arc.dash}`,
606
+ strokeDashoffset: -arc.offset,
607
+ transform: "rotate(-90 50 50)",
608
+ strokeLinecap: "butt"
609
+ }, arc.key))]
610
+ }), /* @__PURE__ */ jsxs("div", {
611
+ className: "nb-dashboard-donut__center",
612
+ children: [widget.centerLabel && /* @__PURE__ */ jsx("span", {
613
+ className: "nb-dashboard-donut__center-label",
614
+ children: widget.centerLabel
615
+ }), /* @__PURE__ */ jsx("span", {
616
+ className: "nb-dashboard-donut__center-value",
617
+ children: formatDashboardValue(centerValue, widget.valueFormat)
618
+ })]
619
+ })]
620
+ }), widget.showLegend !== false && /* @__PURE__ */ jsx("ul", {
621
+ className: "nb-dashboard-donut__legend",
622
+ children: rows.map((row, index) => {
623
+ const label = String(row[widget.labelKey] ?? "");
624
+ const value = toNumber$1(row[widget.valueKey]);
625
+ const pct = total > 0 ? value / total * 100 : 0;
626
+ return /* @__PURE__ */ jsxs("li", {
627
+ className: "nb-dashboard-donut__legend-item",
628
+ children: [
629
+ /* @__PURE__ */ jsx("span", {
630
+ className: "nb-dashboard-donut__legend-swatch",
631
+ style: { background: colors[index % colors.length] }
632
+ }),
633
+ /* @__PURE__ */ jsx("span", {
634
+ className: "nb-dashboard-donut__legend-label",
635
+ children: label
636
+ }),
637
+ /* @__PURE__ */ jsxs("span", {
638
+ className: "nb-dashboard-donut__legend-value",
639
+ children: [pct.toFixed(0), "%"]
640
+ })
641
+ ]
642
+ }, `${label}-${index}`);
643
+ })
644
+ })]
645
+ })
646
+ });
647
+ }
648
+ //#endregion
649
+ //#region packages/dashboard/dashboard/widgets/LineChartWidgetView.tsx
650
+ function LineChartWidgetView({ widget, data, loading }) {
651
+ const rows = resolveArray(data, widget.dataPath);
652
+ const height = widget.height ?? 240;
653
+ return /* @__PURE__ */ jsx(StatCard, {
654
+ title: widget.title,
655
+ headerExtra: widget.subtitle ? /* @__PURE__ */ jsx("span", {
656
+ className: "nb-dashboard-widget__subtitle",
657
+ children: widget.subtitle
658
+ }) : void 0,
659
+ menuVisible: widget.menuVisible,
660
+ isLoading: loading,
661
+ className: "nb-dashboard-chart-card",
662
+ children: rows.length === 0 ? /* @__PURE__ */ jsx("div", {
663
+ className: "nb-dashboard-chart-empty",
664
+ children: "No data"
665
+ }) : /* @__PURE__ */ jsx("div", {
666
+ style: { height },
667
+ children: /* @__PURE__ */ jsx(ResponsiveContainer, {
668
+ width: "100%",
669
+ height: "100%",
670
+ children: /* @__PURE__ */ jsxs(LineChart, {
671
+ data: rows,
672
+ margin: {
673
+ top: 4,
674
+ right: 8,
675
+ bottom: 0,
676
+ left: 0
677
+ },
678
+ children: [
679
+ widget.showGrid !== false && /* @__PURE__ */ jsx(CartesianGrid, {
680
+ strokeDasharray: "3 3",
681
+ stroke: "var(--border-color)",
682
+ vertical: false
683
+ }),
684
+ /* @__PURE__ */ jsx(XAxis, {
685
+ dataKey: widget.xKey,
686
+ tick: {
687
+ fontSize: 11,
688
+ fill: "var(--text-tertiary)"
689
+ },
690
+ axisLine: { stroke: "var(--border-color)" },
691
+ tickLine: false
692
+ }),
693
+ /* @__PURE__ */ jsx(YAxis, {
694
+ tick: {
695
+ fontSize: 11,
696
+ fill: "var(--text-tertiary)"
697
+ },
698
+ axisLine: false,
699
+ tickLine: false,
700
+ width: 48,
701
+ tickFormatter: (v) => formatDashboardValue(v, widget.valueFormat)
702
+ }),
703
+ /* @__PURE__ */ jsx(Tooltip, { content: /* @__PURE__ */ jsx(ChartTooltip, {
704
+ labelFormat: widget.valueFormat,
705
+ series: widget.series
706
+ }) }),
707
+ widget.showLegend && /* @__PURE__ */ jsx(Legend, { wrapperStyle: { fontSize: 12 } }),
708
+ widget.series.map((series, index) => /* @__PURE__ */ jsx(Line, {
709
+ type: widget.curved === false ? "linear" : "monotone",
710
+ dataKey: series.key,
711
+ name: series.label ?? series.key,
712
+ stroke: series.color ?? DEFAULT_CHART_COLORS[index % DEFAULT_CHART_COLORS.length],
713
+ strokeWidth: 2,
714
+ dot: false,
715
+ activeDot: { r: 4 }
716
+ }, series.key))
717
+ ]
718
+ })
719
+ })
720
+ })
721
+ });
722
+ }
723
+ //#endregion
724
+ //#region packages/dashboard/dashboard/widgets/ProgressWidgetView.tsx
725
+ function toNumber(value, fallback) {
726
+ const numeric = typeof value === "number" ? value : Number(value);
727
+ return Number.isFinite(numeric) ? numeric : fallback;
728
+ }
729
+ function ProgressWidgetView({ widget, data, loading }) {
730
+ const value = toNumber(widget.valuePath !== void 0 ? resolvePath(data, widget.valuePath) : widget.value, 0);
731
+ const max = widget.maxPath !== void 0 ? toNumber(resolvePath(data, widget.maxPath), 100) : widget.max ?? 100;
732
+ const pct = max > 0 ? Math.min(100, Math.max(0, value / max * 100)) : 0;
733
+ const tone = widget.tone ?? "accent";
734
+ return /* @__PURE__ */ jsxs(StatCard, {
735
+ title: widget.title,
736
+ headerExtra: widget.subtitle ? /* @__PURE__ */ jsx("span", {
737
+ className: "nb-dashboard-widget__subtitle",
738
+ children: widget.subtitle
739
+ }) : void 0,
740
+ menuVisible: widget.menuVisible,
741
+ isLoading: loading,
742
+ className: `nb-dashboard-progress nb-dashboard-progress--${tone}`,
743
+ children: [/* @__PURE__ */ jsx("div", {
744
+ className: "nb-dashboard-progress__meter",
745
+ role: "progressbar",
746
+ "aria-valuenow": pct,
747
+ "aria-valuemin": 0,
748
+ "aria-valuemax": 100,
749
+ children: /* @__PURE__ */ jsx("div", {
750
+ className: "nb-dashboard-progress__fill",
751
+ style: { width: `${pct}%` }
752
+ })
753
+ }), /* @__PURE__ */ jsxs("div", {
754
+ className: "nb-dashboard-progress__labels",
755
+ children: [/* @__PURE__ */ jsx("span", {
756
+ className: "nb-dashboard-progress__value",
757
+ children: formatDashboardValue(value, widget.format ?? "number")
758
+ }), /* @__PURE__ */ jsxs("span", {
759
+ className: "nb-dashboard-progress__max",
760
+ children: ["/ ", formatDashboardValue(max, widget.format ?? "number")]
761
+ })]
762
+ })]
763
+ });
764
+ }
765
+ //#endregion
766
+ //#region packages/dashboard/dashboard/widgets/StatWidgetView.tsx
767
+ function resolveTrend(widget, data) {
768
+ if (!widget.trend) return { value: null };
769
+ const raw = widget.trend.valuePath !== void 0 ? resolvePath(data, widget.trend.valuePath) : widget.trend.value;
770
+ const numeric = typeof raw === "number" ? raw : Number(raw);
771
+ return {
772
+ value: Number.isFinite(numeric) ? numeric : null,
773
+ label: widget.trend.label,
774
+ invert: widget.trend.invertColors
775
+ };
776
+ }
777
+ function StatWidgetView({ widget, data, loading }) {
778
+ const valueText = formatDashboardValue(widget.valuePath !== void 0 ? resolvePath(data, widget.valuePath) : widget.value, widget.format ?? "text");
779
+ const trend = resolveTrend(widget, data);
780
+ const trendDirection = trend.value === null || trend.value === 0 ? "flat" : trend.value > 0 ? "up" : "down";
781
+ const trendClass = trendDirection === "flat" ? "nb-stat-card__delta--flat" : trendDirection === "up" ? trend.invert ? "nb-stat-card__delta--down" : "nb-stat-card__delta--up" : trend.invert ? "nb-stat-card__delta--up" : "nb-stat-card__delta--down";
782
+ const trendArrow = trendDirection === "up" ? "▲" : trendDirection === "down" ? "▼" : "—";
783
+ const trendText = trend.value === null ? null : `${trendArrow} ${formatDashboardValue(Math.abs(trend.value), "percent")}${trend.label ? ` ${trend.label}` : ""}`;
784
+ return /* @__PURE__ */ jsx(StatCard, {
785
+ title: widget.title,
786
+ menuVisible: widget.menuVisible,
787
+ isLoading: loading,
788
+ className: `nb-dashboard-stat nb-dashboard-stat--${widget.iconTone ?? "accent"}`,
789
+ children: /* @__PURE__ */ jsxs("div", {
790
+ className: "nb-dashboard-stat__content",
791
+ children: [widget.icon && /* @__PURE__ */ jsx("span", {
792
+ className: `nb-dashboard-stat__icon nb-dashboard-stat__icon--${widget.iconTone ?? "accent"}`,
793
+ children: /* @__PURE__ */ jsx("i", {
794
+ className: `ph ${widget.icon}`,
795
+ "aria-hidden": "true"
796
+ })
797
+ }), /* @__PURE__ */ jsxs("div", {
798
+ className: "nb-dashboard-stat__metrics",
799
+ children: [/* @__PURE__ */ jsx("span", {
800
+ className: "nb-stat-card__value nb-stat-card__value--2xl",
801
+ children: valueText
802
+ }), trendText && /* @__PURE__ */ jsx("span", {
803
+ className: `nb-stat-card__delta ${trendClass}`,
804
+ children: trendText
805
+ })]
806
+ })]
807
+ })
808
+ });
809
+ }
810
+ //#endregion
811
+ //#region packages/dashboard/dashboard/widgets/TableWidgetView.tsx
812
+ function renderCell(row, column) {
813
+ const raw = row[column.key];
814
+ if (column.badge) {
815
+ const key = raw == null ? "" : String(raw);
816
+ return /* @__PURE__ */ jsx(Badge, {
817
+ variant: column.badge[key] ?? "secondary",
818
+ size: "sm",
819
+ children: column.badgeLabels?.[key] ?? key.replace(/_/g, " ")
820
+ });
821
+ }
822
+ return formatDashboardValue(raw, column.format ?? "text");
823
+ }
824
+ function TableWidgetView({ widget, data, loading }) {
825
+ const rows = resolveArray(data, widget.dataPath);
826
+ return /* @__PURE__ */ jsx(StatCard, {
827
+ title: widget.title,
828
+ headerExtra: /* @__PURE__ */ jsxs("div", {
829
+ className: "nb-dashboard-table__header-extra",
830
+ children: [widget.subtitle && /* @__PURE__ */ jsx("span", {
831
+ className: "nb-dashboard-widget__subtitle",
832
+ children: widget.subtitle
833
+ }), widget.viewAll && /* @__PURE__ */ jsx(Link, {
834
+ to: widget.viewAll.to,
835
+ className: "nb-dashboard-table__view-all",
836
+ children: widget.viewAll.label ?? "View all"
837
+ })]
838
+ }),
839
+ menuVisible: widget.menuVisible,
840
+ isLoading: loading,
841
+ className: "nb-dashboard-table-card",
842
+ children: rows.length === 0 ? /* @__PURE__ */ jsx(EmptyState, {
843
+ size: "sm",
844
+ icon: "ph ph-table",
845
+ title: widget.emptyTitle ?? "No records yet",
846
+ description: widget.emptyDescription
847
+ }) : /* @__PURE__ */ jsx("div", {
848
+ className: "nb-dashboard-table-wrap",
849
+ children: /* @__PURE__ */ jsxs("table", {
850
+ className: "nb-dashboard-table",
851
+ children: [/* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsx("tr", { children: widget.columns.map((column) => /* @__PURE__ */ jsx("th", {
852
+ className: column.align ? `nb-dashboard-table__cell--${column.align}` : void 0,
853
+ children: column.label
854
+ }, column.key)) }) }), /* @__PURE__ */ jsx("tbody", { children: rows.map((row, rowIndex) => /* @__PURE__ */ jsx("tr", { children: widget.columns.map((column) => /* @__PURE__ */ jsx("td", {
855
+ className: column.align ? `nb-dashboard-table__cell--${column.align}` : void 0,
856
+ children: renderCell(row, column)
857
+ }, column.key)) }, rowIndex)) })]
858
+ })
859
+ })
860
+ });
861
+ }
862
+ //#endregion
863
+ //#region packages/dashboard/dashboard/widgets/registerBuiltinWidgets.ts
864
+ registerWidgetType("stat", StatWidgetView);
865
+ registerWidgetType("bar-chart", BarChartWidgetView);
866
+ registerWidgetType("donut-chart", DonutChartWidgetView);
867
+ registerWidgetType("line-chart", LineChartWidgetView);
868
+ registerWidgetType("area-chart", AreaChartWidgetView);
869
+ registerWidgetType("progress", ProgressWidgetView);
870
+ registerWidgetType("table", TableWidgetView);
871
+ //#endregion
366
872
  //#region packages/dashboard/dashboard/widgets/WidgetRenderer.tsx
367
873
  const isDev = typeof process !== "undefined" && process.env?.NODE_ENV !== "production";
368
874
  function WidgetRenderer({ widget, data, loading }) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nubitio/dashboard",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "type": "module",
5
5
  "description": "Declarative dashboard engine with stat cards, charts, and data tables for Nubit admin apps.",
6
6
  "license": "MIT",
@@ -40,7 +40,9 @@
40
40
  "dist"
41
41
  ],
42
42
  "sideEffects": [
43
- "**/*.css"
43
+ "**/*.css",
44
+ "./dashboard/widgets/registerBuiltinWidgets.ts",
45
+ "./dashboard/widgets/WidgetRenderer.tsx"
44
46
  ],
45
47
  "engines": {
46
48
  "node": ">=18.0.0"
@@ -54,8 +56,8 @@
54
56
  "react-dom": "^18.2.0 || ^19.0.0",
55
57
  "react-router-dom": "^6.0.0",
56
58
  "recharts": "^3.0.0",
57
- "@nubitio/core": "^0.8.0",
58
- "@nubitio/ui": "^0.8.0"
59
+ "@nubitio/core": "^0.8.1",
60
+ "@nubitio/ui": "^0.8.1"
59
61
  },
60
62
  "devDependencies": {
61
63
  "@tanstack/react-query": "^5.0.0",