@hexclave/dashboard-ui-components 1.0.2 → 1.0.5
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/components/button.d.ts +4 -4
- package/dist/components/pill-toggle.js +2 -2
- package/dist/components/pill-toggle.js.map +1 -1
- package/dist/dashboard-ui-components.global.js +7447 -9242
- package/dist/dashboard-ui-components.global.js.map +4 -4
- package/dist/esm/components/button.d.ts +4 -4
- package/dist/esm/components/pill-toggle.js +3 -3
- package/dist/esm/components/pill-toggle.js.map +1 -1
- package/package.json +4 -3
- package/src/components/alert.tsx +120 -0
- package/src/components/analytics-chart/analytics-chart-pie.tsx +369 -0
- package/src/components/analytics-chart/analytics-chart.tsx +1585 -0
- package/src/components/analytics-chart/default-analytics-chart-tooltip.tsx +265 -0
- package/src/components/analytics-chart/format.ts +101 -0
- package/src/components/analytics-chart/index.ts +75 -0
- package/src/components/analytics-chart/palette.ts +68 -0
- package/src/components/analytics-chart/render-data-series.tsx +169 -0
- package/src/components/analytics-chart/state.ts +165 -0
- package/src/components/analytics-chart/strings.ts +72 -0
- package/src/components/analytics-chart/types.ts +220 -0
- package/src/components/badge.tsx +108 -0
- package/src/components/button.tsx +104 -0
- package/src/components/card.tsx +263 -0
- package/src/components/chart-card.tsx +101 -0
- package/src/components/chart-container.tsx +155 -0
- package/src/components/chart-legend.tsx +65 -0
- package/src/components/chart-theme.tsx +52 -0
- package/src/components/chart-tooltip.tsx +165 -0
- package/src/components/cursor-blast-effect.tsx +334 -0
- package/src/components/data-grid/data-grid-sizing.ts +51 -0
- package/src/components/data-grid/data-grid-toolbar.tsx +373 -0
- package/src/components/data-grid/data-grid.test.tsx +366 -0
- package/src/components/data-grid/data-grid.tsx +1220 -0
- package/src/components/data-grid/index.ts +64 -0
- package/src/components/data-grid/state.ts +235 -0
- package/src/components/data-grid/strings.ts +45 -0
- package/src/components/data-grid/types.ts +401 -0
- package/src/components/data-grid/use-data-source.ts +413 -0
- package/src/components/data-grid/use-url-state.test.tsx +88 -0
- package/src/components/data-grid/use-url-state.ts +298 -0
- package/src/components/dialog.tsx +207 -0
- package/src/components/edit-mode.tsx +17 -0
- package/src/components/empty-state.tsx +64 -0
- package/src/components/input.tsx +85 -0
- package/src/components/metric-card.tsx +142 -0
- package/src/components/pill-toggle.tsx +159 -0
- package/src/components/progress-bar.tsx +82 -0
- package/src/components/separator.tsx +36 -0
- package/src/components/skeleton.tsx +30 -0
- package/src/components/table.tsx +113 -0
- package/src/components/tabs.tsx +214 -0
- package/src/index.ts +69 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { DEFAULT_FORMAT_KIND } from "./format";
|
|
2
|
+
import type {
|
|
3
|
+
AnalyticsChartAnnotationsLayer,
|
|
4
|
+
AnalyticsChartDataLayer,
|
|
5
|
+
AnalyticsChartLayer,
|
|
6
|
+
AnalyticsChartLayers,
|
|
7
|
+
AnalyticsChartLayerType,
|
|
8
|
+
AnalyticsChartSeries,
|
|
9
|
+
AnalyticsChartState,
|
|
10
|
+
AnalyticsChartStrokeStyle,
|
|
11
|
+
AnalyticsChartTimeseriesState,
|
|
12
|
+
} from "./types";
|
|
13
|
+
|
|
14
|
+
export const STROKE_DASHARRAY: Record<AnalyticsChartStrokeStyle, string | undefined> = {
|
|
15
|
+
solid: undefined,
|
|
16
|
+
dashed: "5 4",
|
|
17
|
+
dotted: "1 4",
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const EMPTY_SERIES: readonly AnalyticsChartSeries[] = [];
|
|
21
|
+
export const EMPTY_MATRIX: readonly (readonly number[])[] = [];
|
|
22
|
+
|
|
23
|
+
/** Generic non-segmented defaults; demos swap in segment data. */
|
|
24
|
+
export const ANALYTICS_CHART_DEFAULT_LAYERS: AnalyticsChartLayers = [
|
|
25
|
+
{
|
|
26
|
+
id: "primary",
|
|
27
|
+
kind: "primary",
|
|
28
|
+
label: "Current",
|
|
29
|
+
visible: true,
|
|
30
|
+
color: "#2563eb",
|
|
31
|
+
segmented: false,
|
|
32
|
+
type: "area",
|
|
33
|
+
strokeStyle: "solid",
|
|
34
|
+
fillOpacity: 0.22,
|
|
35
|
+
inProgressFromIndex: null,
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
id: "compare",
|
|
39
|
+
kind: "compare",
|
|
40
|
+
label: "Previous period",
|
|
41
|
+
visible: true,
|
|
42
|
+
color: "#f59e0b",
|
|
43
|
+
segmented: false,
|
|
44
|
+
type: "line",
|
|
45
|
+
strokeStyle: "dashed",
|
|
46
|
+
inProgressFromIndex: null,
|
|
47
|
+
},
|
|
48
|
+
{ id: "annotations", kind: "annotations", label: "Annotations", visible: true, color: "#f59e0b" },
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Default state for `AnalyticsChart`. ALWAYS spread from this when
|
|
53
|
+
* initializing state; never build the state object by hand. Ships with
|
|
54
|
+
* three pre-configured layers (primary, compare, annotations) — map over
|
|
55
|
+
* `layers` to override individual ones.
|
|
56
|
+
*
|
|
57
|
+
* ```tsx
|
|
58
|
+
* const [state, setState] = React.useState({
|
|
59
|
+
* ...ANALYTICS_CHART_DEFAULT_STATE,
|
|
60
|
+
* layers: ANALYTICS_CHART_DEFAULT_STATE.layers.map(l =>
|
|
61
|
+
* l.kind === "compare" ? { ...l, visible: false } : l
|
|
62
|
+
* ),
|
|
63
|
+
* });
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* See the JSDoc on `AnalyticsChart` for the full contract, examples, and
|
|
67
|
+
* the segment data format.
|
|
68
|
+
*/
|
|
69
|
+
export const ANALYTICS_CHART_DEFAULT_STATE: AnalyticsChartState = {
|
|
70
|
+
view: "timeseries",
|
|
71
|
+
layers: ANALYTICS_CHART_DEFAULT_LAYERS,
|
|
72
|
+
xFormatKind: DEFAULT_FORMAT_KIND.datetime,
|
|
73
|
+
yFormatKind: DEFAULT_FORMAT_KIND.short,
|
|
74
|
+
showGrid: true,
|
|
75
|
+
showXAxis: true,
|
|
76
|
+
showYAxis: true,
|
|
77
|
+
zoomRange: null,
|
|
78
|
+
pinnedIndex: null,
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export function findPrimaryLayer(layers: AnalyticsChartLayers): AnalyticsChartDataLayer | undefined {
|
|
82
|
+
const l = layers.find((x) => x.kind === "primary");
|
|
83
|
+
return l as AnalyticsChartDataLayer | undefined;
|
|
84
|
+
}
|
|
85
|
+
export function findCompareLayer(layers: AnalyticsChartLayers): AnalyticsChartDataLayer | undefined {
|
|
86
|
+
const l = layers.find((x) => x.kind === "compare");
|
|
87
|
+
return l as AnalyticsChartDataLayer | undefined;
|
|
88
|
+
}
|
|
89
|
+
export function findAnnotationsLayer(layers: AnalyticsChartLayers): AnalyticsChartAnnotationsLayer | undefined {
|
|
90
|
+
const l = layers.find((x) => x.kind === "annotations");
|
|
91
|
+
return l as AnalyticsChartAnnotationsLayer | undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function findLayerById(
|
|
95
|
+
layers: AnalyticsChartLayers,
|
|
96
|
+
id: string,
|
|
97
|
+
): AnalyticsChartLayer | undefined {
|
|
98
|
+
return layers.find((l) => l.id === id);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function isAnalyticsChartDataLayer(l: AnalyticsChartLayer): l is AnalyticsChartDataLayer {
|
|
102
|
+
return l.kind === "primary" || l.kind === "compare";
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function isTimeseriesState(
|
|
106
|
+
state: AnalyticsChartState,
|
|
107
|
+
): state is AnalyticsChartTimeseriesState {
|
|
108
|
+
return state.view === "timeseries";
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Replace a single layer (looked up by id) with a new layer object. */
|
|
112
|
+
export function setLayerById(
|
|
113
|
+
layers: AnalyticsChartLayers,
|
|
114
|
+
id: string,
|
|
115
|
+
next: AnalyticsChartLayer,
|
|
116
|
+
): AnalyticsChartLayers {
|
|
117
|
+
return layers.map((l) => (l.id === id ? next : l));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Shallow-patch fields on a layer by id. The patch type is deliberately
|
|
121
|
+
* loose — callers are trusted to supply only fields the layer's
|
|
122
|
+
* `kind`/`type` actually owns. */
|
|
123
|
+
export function patchLayerById(
|
|
124
|
+
layers: AnalyticsChartLayers,
|
|
125
|
+
id: string,
|
|
126
|
+
patch: Record<string, unknown>,
|
|
127
|
+
): AnalyticsChartLayers {
|
|
128
|
+
return layers.map((l) => (l.id === id ? ({ ...l, ...patch } as AnalyticsChartLayer) : l));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export type ResolvedDataLayerStyle = {
|
|
132
|
+
color: string,
|
|
133
|
+
type: AnalyticsChartLayerType,
|
|
134
|
+
strokeStyle: AnalyticsChartStrokeStyle,
|
|
135
|
+
fillOpacity: number,
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
export function resolveDataLayerStyle(
|
|
139
|
+
layer: AnalyticsChartDataLayer,
|
|
140
|
+
): ResolvedDataLayerStyle {
|
|
141
|
+
return {
|
|
142
|
+
color: layer.color,
|
|
143
|
+
type: layer.type,
|
|
144
|
+
// Bars have no stroke pattern — default to solid for the underline.
|
|
145
|
+
strokeStyle: layer.type === "bar" ? "solid" : layer.strokeStyle,
|
|
146
|
+
// Lines have no fill — default to 0 so gradient overlays sit flat.
|
|
147
|
+
fillOpacity: layer.type === "line" ? 0 : layer.fillOpacity,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Translate a layer's absolute `inProgressFromIndex` into a local index
|
|
152
|
+
* inside the visible window. Returns `null` when the marker sits beyond
|
|
153
|
+
* the visible window, `0` when it sits before the window (whole window
|
|
154
|
+
* is dashed), or the clamped local index otherwise. */
|
|
155
|
+
export function computeLocalInProgressIdx(
|
|
156
|
+
absIdx: number | null | undefined,
|
|
157
|
+
visibleStart: number,
|
|
158
|
+
visibleEnd: number,
|
|
159
|
+
): number | null {
|
|
160
|
+
if (absIdx == null) return null;
|
|
161
|
+
const local = absIdx - visibleStart;
|
|
162
|
+
if (local >= visibleEnd - visibleStart + 1) return null; // beyond window
|
|
163
|
+
if (local < 0) return 0; // before window — whole window is dashed
|
|
164
|
+
return local;
|
|
165
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export type AnalyticsChartStrings = {
|
|
2
|
+
/** Reset-zoom badge in the top-right corner when `state.zoomRange` is set. */
|
|
3
|
+
resetZoom: string,
|
|
4
|
+
/** Header label above the timestamps in the live range-brush popup. */
|
|
5
|
+
rangeLabel: string,
|
|
6
|
+
/** Formatted day-count suffix used by the brush + action-bar. */
|
|
7
|
+
daysShort: (days: number) => string,
|
|
8
|
+
/** "Zoom in" button inside the committed-range action bar. */
|
|
9
|
+
zoomIn: string,
|
|
10
|
+
/** "Annotate" button inside the committed-range action bar. */
|
|
11
|
+
annotate: string,
|
|
12
|
+
/** Placeholder for the annotation label input. */
|
|
13
|
+
annotationPlaceholder: string,
|
|
14
|
+
/** `aria-label` for the annotation label input. */
|
|
15
|
+
annotationLabelAria: string,
|
|
16
|
+
/** Save button in the annotation form. */
|
|
17
|
+
save: string,
|
|
18
|
+
/** Cancel button in the annotation form. */
|
|
19
|
+
cancel: string,
|
|
20
|
+
/** `aria-label` for the X button that clears the committed range. */
|
|
21
|
+
clearSelection: string,
|
|
22
|
+
/** Pinned badge shown in the tooltip header when the tooltip is pinned. */
|
|
23
|
+
pinnedBadge: string,
|
|
24
|
+
/** "Δ vs prev" row label in the tooltip (both segmented and flat modes). */
|
|
25
|
+
deltaVsPrev: string,
|
|
26
|
+
/** Suffix appended to a layer's label in the per-layer totals section
|
|
27
|
+
* (e.g. "Sign-ups" → "Sign-ups total"). */
|
|
28
|
+
layerTotalSuffix: string,
|
|
29
|
+
/** Hint row shown in the tooltip while it is floating (not pinned). */
|
|
30
|
+
hintClickToPin: string,
|
|
31
|
+
/** Hint row shown in the tooltip while it is pinned. */
|
|
32
|
+
hintClickAnywhereUnpin: string,
|
|
33
|
+
/** Center-stat heading shown in the pie when no segment is hovered. */
|
|
34
|
+
pieTotalCenter: string,
|
|
35
|
+
/** Label on the TrendPill in the pie center. */
|
|
36
|
+
pieVsPrev: string,
|
|
37
|
+
/** `aria-label` for the PieChart SVG. */
|
|
38
|
+
pieAriaLabel: (ctx: { segmentCount: number, windowDays: number }) => string,
|
|
39
|
+
/** Percentage-of-total caption shown under an active pie slice. */
|
|
40
|
+
piePercentOfTotal: (pct: number) => string,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const ANALYTICS_CHART_DEFAULT_STRINGS: AnalyticsChartStrings = {
|
|
44
|
+
resetZoom: "Reset zoom",
|
|
45
|
+
rangeLabel: "Range",
|
|
46
|
+
daysShort: (days) => `${days}d`,
|
|
47
|
+
zoomIn: "Zoom in",
|
|
48
|
+
annotate: "Annotate",
|
|
49
|
+
annotationPlaceholder: "Label this range…",
|
|
50
|
+
annotationLabelAria: "Annotation label",
|
|
51
|
+
save: "Save",
|
|
52
|
+
cancel: "Cancel",
|
|
53
|
+
clearSelection: "Clear selection",
|
|
54
|
+
pinnedBadge: "Pinned",
|
|
55
|
+
deltaVsPrev: "Δ vs prev",
|
|
56
|
+
layerTotalSuffix: " total",
|
|
57
|
+
hintClickToPin: "Click to pin this point",
|
|
58
|
+
hintClickAnywhereUnpin: "Click anywhere · Esc\u00A0to unpin",
|
|
59
|
+
pieTotalCenter: "Total",
|
|
60
|
+
pieVsPrev: "vs prev",
|
|
61
|
+
pieAriaLabel: ({ segmentCount, windowDays }) =>
|
|
62
|
+
`${segmentCount} segment share-of-total over the visible ${windowDays}-day range`,
|
|
63
|
+
piePercentOfTotal: (pct) => `${(pct * 100).toFixed(1)}% of total`,
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/** Shallow merge — every field is a primitive or a flat function. */
|
|
67
|
+
export function resolveAnalyticsChartStrings(
|
|
68
|
+
override: Partial<AnalyticsChartStrings> | undefined,
|
|
69
|
+
): AnalyticsChartStrings {
|
|
70
|
+
if (!override) return ANALYTICS_CHART_DEFAULT_STRINGS;
|
|
71
|
+
return { ...ANALYTICS_CHART_DEFAULT_STRINGS, ...override };
|
|
72
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/** Time-series point. `values` is keyed by layer id. */
|
|
2
|
+
export type Point = {
|
|
3
|
+
ts: number,
|
|
4
|
+
values: Record<string, number>,
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
/** Missing or non-finite values become 0. */
|
|
8
|
+
export function pointValue(p: Point, id: string): number {
|
|
9
|
+
const v = p.values[id];
|
|
10
|
+
return typeof v === "number" && Number.isFinite(v) ? v : 0;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Sanitize a string into a valid CSS `<ident>` token.
|
|
14
|
+
* Replaces characters not allowed in CSS identifiers (like `$`, spaces,
|
|
15
|
+
* slashes, dots) with underscores. Used to build safe `var(--color-xxx)`
|
|
16
|
+
* custom property names from arbitrary segment keys. */
|
|
17
|
+
export function cssIdent(raw: string): string {
|
|
18
|
+
// Replace everything that isn't a letter, digit, hyphen, or underscore.
|
|
19
|
+
// Prefix with `_` if the result starts with a digit (not valid as ident start).
|
|
20
|
+
const cleaned = raw.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
21
|
+
return /^\d/.test(cleaned) ? `_${cleaned}` : cleaned;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type Annotation = {
|
|
25
|
+
index: number,
|
|
26
|
+
label: string,
|
|
27
|
+
description: string,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** Breakdown category definition — `{ key, label }` tuple. */
|
|
31
|
+
export type AnalyticsChartSeries = {
|
|
32
|
+
key: string,
|
|
33
|
+
label: string,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export type FormatKindType =
|
|
37
|
+
| "numeric"
|
|
38
|
+
| "short"
|
|
39
|
+
| "currency"
|
|
40
|
+
| "duration"
|
|
41
|
+
| "datetime"
|
|
42
|
+
| "percent";
|
|
43
|
+
|
|
44
|
+
export type FormatKindNumeric = {
|
|
45
|
+
type: "numeric",
|
|
46
|
+
/** Locale used for grouping and digit separators. Defaults to "en-US". */
|
|
47
|
+
locale?: string,
|
|
48
|
+
/** Fixed decimal places (0-4). Defaults to 0. */
|
|
49
|
+
decimals?: number,
|
|
50
|
+
};
|
|
51
|
+
export type FormatKindShort = {
|
|
52
|
+
type: "short",
|
|
53
|
+
/** Decimal places after the unit suffix (1.2K vs 1.20K). Defaults to 1. */
|
|
54
|
+
precision?: number,
|
|
55
|
+
locale?: string,
|
|
56
|
+
};
|
|
57
|
+
export type FormatKindCurrency = {
|
|
58
|
+
type: "currency",
|
|
59
|
+
/** ISO 4217 code. Defaults to "USD". */
|
|
60
|
+
currency?: string,
|
|
61
|
+
/** Divisor applied before formatting — e.g. 100 for cents → dollars. Defaults to 1. */
|
|
62
|
+
divisor?: number,
|
|
63
|
+
locale?: string,
|
|
64
|
+
};
|
|
65
|
+
export type FormatKindDuration = {
|
|
66
|
+
type: "duration",
|
|
67
|
+
/** Source unit of the input value. Defaults to "s". */
|
|
68
|
+
unit?: "ms" | "s" | "m" | "h",
|
|
69
|
+
/** Show the smallest unit even when zero. Defaults to false. */
|
|
70
|
+
showZero?: boolean,
|
|
71
|
+
};
|
|
72
|
+
export type FormatKindDatetime = {
|
|
73
|
+
type: "datetime",
|
|
74
|
+
/** Render style. Defaults to "short". */
|
|
75
|
+
style?: "short" | "long" | "iso" | "relative",
|
|
76
|
+
locale?: string,
|
|
77
|
+
};
|
|
78
|
+
export type FormatKindPercent = {
|
|
79
|
+
type: "percent",
|
|
80
|
+
/** How to interpret the input value:
|
|
81
|
+
* - "fraction" → 0..1 → multiply by 100 (default)
|
|
82
|
+
* - "basis" → 0..10000 → divide by 100
|
|
83
|
+
* - "whole" → 0..100 → no scaling
|
|
84
|
+
*/
|
|
85
|
+
source?: "fraction" | "basis" | "whole",
|
|
86
|
+
/** Decimal places. Defaults to 1. */
|
|
87
|
+
decimals?: number,
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export type FormatKind =
|
|
91
|
+
| FormatKindNumeric
|
|
92
|
+
| FormatKindShort
|
|
93
|
+
| FormatKindCurrency
|
|
94
|
+
| FormatKindDuration
|
|
95
|
+
| FormatKindDatetime
|
|
96
|
+
| FormatKindPercent;
|
|
97
|
+
|
|
98
|
+
export type AnalyticsChartView = "timeseries" | "pie";
|
|
99
|
+
export type AnalyticsChartLayerType = "line" | "area" | "bar";
|
|
100
|
+
export type AnalyticsChartStrokeStyle = "solid" | "dashed" | "dotted";
|
|
101
|
+
|
|
102
|
+
type AnalyticsChartDataLayerCommon = {
|
|
103
|
+
id: string,
|
|
104
|
+
kind: "primary" | "compare",
|
|
105
|
+
label: string,
|
|
106
|
+
visible: boolean,
|
|
107
|
+
color: string,
|
|
108
|
+
segmented: boolean,
|
|
109
|
+
/** Per-day per-category values. Outer index is the day (matches the
|
|
110
|
+
* chart data array index); inner index is the category (matches
|
|
111
|
+
* `segmentSeries`). Rows should sum to `point.values[layer.id]`. */
|
|
112
|
+
segments?: readonly (readonly number[])[],
|
|
113
|
+
/** Breakdown category definitions ordered to match the inner index of
|
|
114
|
+
* `segments`. */
|
|
115
|
+
segmentSeries?: readonly AnalyticsChartSeries[],
|
|
116
|
+
/** Absolute index into the full data array at which this layer's values
|
|
117
|
+
* become "in progress" (incomplete and still changing). Points from this
|
|
118
|
+
* index onward render with a dashed overlay so users don't panic at a
|
|
119
|
+
* half-filled bucket. Applies to line and area rendering only. */
|
|
120
|
+
inProgressFromIndex?: number | null,
|
|
121
|
+
};
|
|
122
|
+
export type AnalyticsChartLineLayer = AnalyticsChartDataLayerCommon & {
|
|
123
|
+
type: "line",
|
|
124
|
+
strokeStyle: AnalyticsChartStrokeStyle,
|
|
125
|
+
};
|
|
126
|
+
export type AnalyticsChartAreaLayer = AnalyticsChartDataLayerCommon & {
|
|
127
|
+
type: "area",
|
|
128
|
+
strokeStyle: AnalyticsChartStrokeStyle,
|
|
129
|
+
fillOpacity: number,
|
|
130
|
+
};
|
|
131
|
+
export type AnalyticsChartBarLayer = AnalyticsChartDataLayerCommon & {
|
|
132
|
+
type: "bar",
|
|
133
|
+
fillOpacity: number,
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export type AnalyticsChartDataLayer =
|
|
137
|
+
| AnalyticsChartLineLayer
|
|
138
|
+
| AnalyticsChartAreaLayer
|
|
139
|
+
| AnalyticsChartBarLayer;
|
|
140
|
+
|
|
141
|
+
export type AnalyticsChartAnnotationsLayer = {
|
|
142
|
+
id: string,
|
|
143
|
+
kind: "annotations",
|
|
144
|
+
label: string,
|
|
145
|
+
visible: boolean,
|
|
146
|
+
color: string,
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
export type AnalyticsChartLayer =
|
|
150
|
+
| AnalyticsChartDataLayer
|
|
151
|
+
| AnalyticsChartAnnotationsLayer;
|
|
152
|
+
|
|
153
|
+
export type AnalyticsChartLayers = readonly AnalyticsChartLayer[];
|
|
154
|
+
|
|
155
|
+
export type AnalyticsChartTimeseriesState = {
|
|
156
|
+
view: "timeseries",
|
|
157
|
+
layers: AnalyticsChartLayers,
|
|
158
|
+
/** Format applied to every x-axis value. Defaults to `datetime / short`. */
|
|
159
|
+
xFormatKind: FormatKind,
|
|
160
|
+
/** Format applied to every y-axis value. Defaults to `short`. */
|
|
161
|
+
yFormatKind: FormatKind,
|
|
162
|
+
showGrid: boolean,
|
|
163
|
+
showXAxis: boolean,
|
|
164
|
+
showYAxis: boolean,
|
|
165
|
+
zoomRange: [number, number] | null,
|
|
166
|
+
pinnedIndex: number | null,
|
|
167
|
+
};
|
|
168
|
+
export type AnalyticsChartPieState = {
|
|
169
|
+
view: "pie",
|
|
170
|
+
layers: AnalyticsChartLayers,
|
|
171
|
+
xFormatKind: FormatKind,
|
|
172
|
+
yFormatKind: FormatKind,
|
|
173
|
+
};
|
|
174
|
+
export type AnalyticsChartState =
|
|
175
|
+
| AnalyticsChartTimeseriesState
|
|
176
|
+
| AnalyticsChartPieState;
|
|
177
|
+
|
|
178
|
+
export type AnalyticsChartDelta = {
|
|
179
|
+
pct: number | null,
|
|
180
|
+
sign: "up" | "down" | "flat" | "na",
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/** Grouped pie config — collapses the formerly-separate
|
|
184
|
+
* `pieInnerRadius` / `pieOuterRadius` / `pieCompareInnerRadius` /
|
|
185
|
+
* `pieCompareOuterRadius` / `pieContainerClassName` props into one object. */
|
|
186
|
+
export type AnalyticsChartPieProps = {
|
|
187
|
+
innerRadius?: number,
|
|
188
|
+
outerRadius?: number,
|
|
189
|
+
compareInnerRadius?: number,
|
|
190
|
+
compareOuterRadius?: number,
|
|
191
|
+
className?: string,
|
|
192
|
+
showDateRange?: boolean,
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
export type AnalyticsChartSegmentRamp =
|
|
196
|
+
| {
|
|
197
|
+
kind: "procedural",
|
|
198
|
+
/** HSL hue (0-360). */
|
|
199
|
+
hue: number,
|
|
200
|
+
/** HSL saturation percent (0-100). */
|
|
201
|
+
sat: number,
|
|
202
|
+
/** Lightness range `[start, end]` for the light theme (0-100). */
|
|
203
|
+
shadeRangeLight: [number, number],
|
|
204
|
+
/** Lightness range `[start, end]` for the dark theme (0-100). */
|
|
205
|
+
shadeRangeDark: [number, number],
|
|
206
|
+
}
|
|
207
|
+
| {
|
|
208
|
+
kind: "explicit",
|
|
209
|
+
/** Concrete light-theme color list. Indexed by segment. */
|
|
210
|
+
light: readonly string[],
|
|
211
|
+
/** Concrete dark-theme color list. Indexed by segment. */
|
|
212
|
+
dark: readonly string[],
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
export type AnalyticsChartPalette = {
|
|
216
|
+
/** Color ramp for the primary data layer (current period). */
|
|
217
|
+
primary: AnalyticsChartSegmentRamp,
|
|
218
|
+
/** Color ramp for the compare data layer (previous period). */
|
|
219
|
+
compare: AnalyticsChartSegmentRamp,
|
|
220
|
+
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { cn } from "@hexclave/ui";
|
|
4
|
+
|
|
5
|
+
export type DesignBadgeColor = "blue" | "cyan" | "purple" | "green" | "orange" | "red";
|
|
6
|
+
export type DesignBadgeSize = "sm" | "md";
|
|
7
|
+
|
|
8
|
+
const badgeStyles = new Map<DesignBadgeColor, string>([
|
|
9
|
+
["blue", "text-blue-700 dark:text-blue-400 bg-blue-500/20 dark:bg-blue-500/10 ring-1 ring-blue-500/30 dark:ring-blue-500/20"],
|
|
10
|
+
["cyan", "text-cyan-700 dark:text-cyan-400 bg-cyan-500/20 dark:bg-cyan-500/10 ring-1 ring-cyan-500/30 dark:ring-cyan-500/20"],
|
|
11
|
+
["purple", "text-purple-700 dark:text-purple-400 bg-purple-500/20 dark:bg-purple-500/10 ring-1 ring-purple-500/30 dark:ring-purple-500/20"],
|
|
12
|
+
["green", "text-emerald-700 dark:text-emerald-400 bg-emerald-500/20 dark:bg-emerald-500/10 ring-1 ring-emerald-500/30 dark:ring-emerald-500/20"],
|
|
13
|
+
["orange", "text-amber-700 dark:text-amber-300 bg-amber-500/20 dark:bg-amber-500/10 ring-1 ring-amber-500/30 dark:ring-amber-500/20"],
|
|
14
|
+
["red", "text-red-700 dark:text-red-400 bg-red-500/20 dark:bg-red-500/10 ring-1 ring-red-500/30 dark:ring-red-500/20"],
|
|
15
|
+
]);
|
|
16
|
+
|
|
17
|
+
function getMapValueOrThrow<TKey, TValue>(map: Map<TKey, TValue>, key: TKey, mapName: string) {
|
|
18
|
+
const value = map.get(key);
|
|
19
|
+
if (!value) {
|
|
20
|
+
throw new Error(`Missing ${mapName} entry for key "${String(key)}"`);
|
|
21
|
+
}
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** At least one of showLabel or showIcon must be true. */
|
|
26
|
+
export type DesignBadgeContentMode = "both" | "text" | "icon";
|
|
27
|
+
|
|
28
|
+
export type DesignBadgeProps = {
|
|
29
|
+
label: string,
|
|
30
|
+
color: DesignBadgeColor,
|
|
31
|
+
icon?: React.ElementType,
|
|
32
|
+
size?: DesignBadgeSize,
|
|
33
|
+
/** What to display: "both" (default), "text" (label only), or "icon" (icon only; requires icon prop). */
|
|
34
|
+
contentMode?: DesignBadgeContentMode,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
function getShowLabelShowIcon(
|
|
38
|
+
contentMode: DesignBadgeContentMode,
|
|
39
|
+
hasIcon: boolean,
|
|
40
|
+
): { showLabel: boolean, showIcon: boolean } {
|
|
41
|
+
switch (contentMode) {
|
|
42
|
+
case "both": {
|
|
43
|
+
return { showLabel: true, showIcon: hasIcon };
|
|
44
|
+
}
|
|
45
|
+
case "text": {
|
|
46
|
+
return { showLabel: true, showIcon: false };
|
|
47
|
+
}
|
|
48
|
+
case "icon": {
|
|
49
|
+
if (!hasIcon) {
|
|
50
|
+
throw new Error("DesignBadge contentMode 'icon' requires the icon prop to be provided.");
|
|
51
|
+
}
|
|
52
|
+
return { showLabel: false, showIcon: true };
|
|
53
|
+
}
|
|
54
|
+
default: {
|
|
55
|
+
const _exhaustive: never = contentMode;
|
|
56
|
+
throw new Error(`Unknown contentMode: ${String(_exhaustive)}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Small pill used for status tags, roles, categories, and other short
|
|
63
|
+
* labels. Not a variant-based component — pick a semantic `color` and
|
|
64
|
+
* optionally pass an `icon` (as a component type, not a rendered node).
|
|
65
|
+
*
|
|
66
|
+
* ```tsx
|
|
67
|
+
* <DesignBadge label="Verified" color="green" icon={CheckIcon} />
|
|
68
|
+
* <DesignBadge label="Beta" color="purple" />
|
|
69
|
+
* <DesignBadge label="Error" color="red" size="sm" />
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* Notes:
|
|
73
|
+
* - Props are `label` + `color`, NOT `variant` + children.
|
|
74
|
+
* - `color` is one of: `"blue" | "cyan" | "purple" | "green" | "orange" | "red"`.
|
|
75
|
+
* - `icon` is optional but, if set via `contentMode: "icon"`, is required.
|
|
76
|
+
*/
|
|
77
|
+
export function DesignBadge({
|
|
78
|
+
label,
|
|
79
|
+
color,
|
|
80
|
+
icon,
|
|
81
|
+
size = "md",
|
|
82
|
+
contentMode = "both",
|
|
83
|
+
}: DesignBadgeProps) {
|
|
84
|
+
const Icon = icon;
|
|
85
|
+
const { showLabel, showIcon } = getShowLabelShowIcon(contentMode, !!Icon);
|
|
86
|
+
if (!showLabel && !showIcon) {
|
|
87
|
+
throw new Error("DesignBadge must show at least label or icon.");
|
|
88
|
+
}
|
|
89
|
+
const sizeClasses = size === "sm"
|
|
90
|
+
? "px-2 py-0.5 text-[10px]"
|
|
91
|
+
: "px-2.5 py-1 text-[11px]";
|
|
92
|
+
const colorClasses = getMapValueOrThrow(badgeStyles, color, "badgeStyles");
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
<div
|
|
96
|
+
className={cn(
|
|
97
|
+
"inline-flex items-center gap-1.5 rounded-full font-medium",
|
|
98
|
+
colorClasses,
|
|
99
|
+
sizeClasses
|
|
100
|
+
)}
|
|
101
|
+
title={!showLabel ? label : undefined}
|
|
102
|
+
aria-label={label}
|
|
103
|
+
>
|
|
104
|
+
{showIcon && Icon && <Icon className="h-3 w-3" />}
|
|
105
|
+
{showLabel ? label : null}
|
|
106
|
+
</div>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { Slot, Slottable } from "@radix-ui/react-slot";
|
|
2
|
+
import { forwardRefIfNeeded } from "@hexclave/shared/dist/utils/react";
|
|
3
|
+
import { cva, type VariantProps } from "class-variance-authority";
|
|
4
|
+
import React from "react";
|
|
5
|
+
|
|
6
|
+
import { cn, Spinner } from "@hexclave/ui";
|
|
7
|
+
import { useAsyncCallback } from "@hexclave/shared/dist/hooks/use-async-callback";
|
|
8
|
+
import { runAsynchronouslyWithAlert } from "@hexclave/shared/dist/utils/promises";
|
|
9
|
+
|
|
10
|
+
const designButtonVariants = cva(
|
|
11
|
+
"stack-scope inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50",
|
|
12
|
+
{
|
|
13
|
+
variants: {
|
|
14
|
+
variant: {
|
|
15
|
+
default:
|
|
16
|
+
"bg-primary text-primary-foreground hover:bg-primary/90",
|
|
17
|
+
destructive:
|
|
18
|
+
"bg-destructive text-destructive-foreground hover:bg-destructive/90",
|
|
19
|
+
outline:
|
|
20
|
+
"border border-input bg-white/85 dark:bg-background hover:bg-white dark:hover:bg-accent hover:text-accent-foreground",
|
|
21
|
+
secondary:
|
|
22
|
+
"bg-secondary text-secondary-foreground hover:bg-secondary/80",
|
|
23
|
+
ghost: "hover:bg-accent hover:text-accent-foreground",
|
|
24
|
+
link: "text-primary underline-offset-4 hover:underline",
|
|
25
|
+
plain: "",
|
|
26
|
+
},
|
|
27
|
+
size: {
|
|
28
|
+
default: "h-9 px-4 py-2",
|
|
29
|
+
sm: "h-8 rounded-md px-3 text-xs",
|
|
30
|
+
lg: "h-10 rounded-md px-8",
|
|
31
|
+
icon: "h-9 w-9",
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
defaultVariants: {
|
|
35
|
+
variant: "default",
|
|
36
|
+
size: "default",
|
|
37
|
+
},
|
|
38
|
+
}
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
export type DesignOriginalButtonProps = {
|
|
42
|
+
asChild?: boolean,
|
|
43
|
+
} & React.ButtonHTMLAttributes<HTMLButtonElement> & VariantProps<typeof designButtonVariants>;
|
|
44
|
+
|
|
45
|
+
const DesignOriginalButton = forwardRefIfNeeded<HTMLButtonElement, DesignOriginalButtonProps>(
|
|
46
|
+
({ className, variant, size, asChild = false, ...props }, ref) => {
|
|
47
|
+
const Comp = asChild ? Slot : "button";
|
|
48
|
+
return (
|
|
49
|
+
<Comp
|
|
50
|
+
className={cn(designButtonVariants({ variant, size, className }))}
|
|
51
|
+
ref={ref}
|
|
52
|
+
{...props}
|
|
53
|
+
/>
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
DesignOriginalButton.displayName = "DesignButton";
|
|
58
|
+
|
|
59
|
+
export type DesignButtonProps = {
|
|
60
|
+
onClick?: (e: React.MouseEvent<HTMLButtonElement>) => void | Promise<void>,
|
|
61
|
+
loading?: boolean,
|
|
62
|
+
loadingStyle?: "spinner" | "disabled",
|
|
63
|
+
} & DesignOriginalButtonProps;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Standard button. Variants: `default | destructive | outline | secondary | ghost | link | plain`.
|
|
67
|
+
* Sizes: `default | sm | lg | icon`.
|
|
68
|
+
*
|
|
69
|
+
* ```tsx
|
|
70
|
+
* <DesignButton variant="default" onClick={handleSave}>Save</DesignButton>
|
|
71
|
+
* <DesignButton variant="outline" size="sm" onClick={handleCancel}>Cancel</DesignButton>
|
|
72
|
+
* <DesignButton variant="ghost" size="icon"><PlusIcon className="h-4 w-4" /></DesignButton>
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* Pass an async `onClick` and the button will automatically show a spinner
|
|
76
|
+
* while the promise is pending (set `loadingStyle="disabled"` if you prefer
|
|
77
|
+
* a simple disabled state instead).
|
|
78
|
+
*/
|
|
79
|
+
export const DesignButton = forwardRefIfNeeded<HTMLButtonElement, DesignButtonProps>(
|
|
80
|
+
({ onClick, loading: loadingProp, loadingStyle = "spinner", children, size, ...props }, ref) => {
|
|
81
|
+
const [handleClick, isLoading] = useAsyncCallback(async (e: React.MouseEvent<HTMLButtonElement>) => {
|
|
82
|
+
await onClick?.(e);
|
|
83
|
+
}, [onClick]);
|
|
84
|
+
|
|
85
|
+
const loading = loadingProp || isLoading;
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
<DesignOriginalButton
|
|
89
|
+
{...props}
|
|
90
|
+
ref={ref}
|
|
91
|
+
disabled={props.disabled || loading}
|
|
92
|
+
onClick={(e) => runAsynchronouslyWithAlert(handleClick(e))}
|
|
93
|
+
size={size}
|
|
94
|
+
className={cn("relative", loading && "[&>:not(.stack-button-do-not-hide-when-siblings-are)]:invisible", props.className)}
|
|
95
|
+
>
|
|
96
|
+
{loadingStyle === "spinner" && <Spinner className={cn("absolute inset-0 flex items-center justify-center stack-button-do-not-hide-when-siblings-are", !loading && "invisible")} />}
|
|
97
|
+
<Slottable>
|
|
98
|
+
{typeof children === "string" ? <span>{children}</span> : children}
|
|
99
|
+
</Slottable>
|
|
100
|
+
</DesignOriginalButton>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
);
|
|
104
|
+
DesignButton.displayName = "DesignButton";
|