@umituz/web-dashboard 8.11.0 → 9.0.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.
Files changed (46) hide show
  1. package/README.md +54 -7
  2. package/package.json +6 -2
  3. package/src/config/DashboardConfig.ts +4 -8
  4. package/src/domains/analytics/components/AnalyticsLayout.test.tsx +85 -0
  5. package/src/domains/analytics/components/AnalyticsLayout.tsx +103 -105
  6. package/src/domains/analytics/components/index.ts +0 -3
  7. package/src/domains/analytics/hooks/index.ts +1 -1
  8. package/src/domains/analytics/hooks/useAnalytics.test.tsx +125 -0
  9. package/src/domains/analytics/hooks/useAnalytics.ts +37 -56
  10. package/src/domains/analytics/index.ts +9 -26
  11. package/src/domains/analytics/services/index.ts +0 -1
  12. package/src/domains/analytics/types/analytics.ts +32 -149
  13. package/src/domains/analytics/types/index.ts +3 -15
  14. package/src/domains/analytics/utils/analytics.test.ts +82 -13
  15. package/src/domains/analytics/utils/analytics.ts +54 -77
  16. package/src/domains/analytics/utils/index.ts +5 -8
  17. package/src/domains/charts/charts.css +455 -0
  18. package/src/domains/charts/components/ActivityHeatmap.test.tsx +48 -0
  19. package/src/domains/charts/components/ActivityHeatmap.tsx +58 -0
  20. package/src/domains/charts/components/BarChart.test.tsx +91 -0
  21. package/src/domains/charts/components/BarChart.tsx +143 -0
  22. package/src/domains/charts/components/ChartCard.tsx +39 -0
  23. package/src/domains/charts/components/ChartPrimitives.test.tsx +90 -0
  24. package/src/domains/charts/components/ChartTip.tsx +18 -0
  25. package/src/domains/charts/components/Donut.test.tsx +51 -0
  26. package/src/domains/charts/components/Donut.tsx +97 -0
  27. package/src/domains/charts/components/HBars.tsx +31 -0
  28. package/src/domains/charts/components/Legend.tsx +21 -0
  29. package/src/domains/charts/components/LineChart.test.tsx +81 -0
  30. package/src/domains/charts/components/LineChart.tsx +244 -0
  31. package/src/domains/charts/components/Sparkline.tsx +35 -0
  32. package/src/domains/charts/components/index.ts +16 -0
  33. package/src/domains/charts/index.ts +16 -2
  34. package/src/domains/charts/ssr.test.tsx +35 -0
  35. package/src/domains/charts/types/index.ts +8 -0
  36. package/src/domains/charts/utils.ts +15 -0
  37. package/src/index.ts +6 -23
  38. package/src/styles/chrome.css +10 -490
  39. package/src/styles/dashboard.contract.test.ts +20 -5
  40. package/src/styles/dashboard.css +1 -0
  41. package/src/domains/analytics/components/AnalyticsCard.tsx +0 -87
  42. package/src/domains/analytics/components/AnalyticsChart.tsx +0 -387
  43. package/src/domains/analytics/components/MetricCard.tsx +0 -42
  44. package/src/domains/analytics/services/AnalyticsEngineService.ts +0 -300
  45. package/src/domains/charts/components.test.tsx +0 -141
  46. package/src/domains/charts/components.tsx +0 -683
@@ -1,387 +0,0 @@
1
- import { useMemo } from "react";
2
- import { cn } from "@umituz/web-design-system/utils/cn";
3
- import type { ChartConfig, ChartData, TimeSeriesData } from "../types/analytics";
4
- import { generateChartColors } from "../utils/analytics";
5
-
6
- const VIEWBOX_W = 600;
7
- const DEFAULT_CHART_HEIGHT_PX = 300;
8
- const DEFAULT_SERIES_COUNT = 5;
9
- const PAD = { top: 10, right: 12, bottom: 24, left: 44 } as const;
10
- const Y_TICKS = 4;
11
- const X_LABEL_SAMPLE = 8;
12
- const LINE_STROKE_WIDTH = 2;
13
- const DOT_RADIUS = 3;
14
- const AREA_FILL_OPACITY = 0.25;
15
-
16
- interface ChartRow {
17
- label: string;
18
- values: number[];
19
- }
20
-
21
- interface PieSlice {
22
- label: string;
23
- value: number;
24
- fraction: number;
25
- }
26
-
27
- const compactFmt = new Intl.NumberFormat(undefined, {
28
- notation: "compact",
29
- maximumFractionDigits: 1,
30
- });
31
-
32
- const toNumber = (value: unknown): number => {
33
- const num = Number(value);
34
- return Number.isFinite(num) ? num : 0;
35
- };
36
-
37
- const toRows = (
38
- data: TimeSeriesData[] | ChartData[],
39
- keys: string[],
40
- xAxisKey?: string,
41
- ): ChartRow[] =>
42
- data.map((point) => ({
43
- label: String(point[xAxisKey ?? "date"] ?? point.label ?? ""),
44
- values: keys.map((key) => toNumber(point[key])),
45
- }));
46
-
47
- const rowsDomain = (rows: ChartRow[]): { min: number; max: number } => {
48
- const all = rows.flatMap((row) => row.values);
49
- const dataMin = all.length > 0 ? Math.min(...all) : 0;
50
- const dataMax = all.length > 0 ? Math.max(...all) : 1;
51
- const min = Math.min(0, dataMin);
52
- const max = dataMax === min ? min + 1 : dataMax;
53
- return { min, max };
54
- };
55
-
56
- const shouldShowLabel = (index: number, count: number): boolean => {
57
- if (count <= X_LABEL_SAMPLE) return true;
58
- const step = Math.ceil((count - 1) / (X_LABEL_SAMPLE - 1));
59
- return index % step === 0 || index === count - 1;
60
- };
61
-
62
- const polarPoint = (cx: number, cy: number, r: number, angle: number): [number, number] => [
63
- cx + r * Math.cos(angle),
64
- cy + r * Math.sin(angle),
65
- ];
66
-
67
- interface AnalyticsChartProps {
68
- config: ChartConfig;
69
- unsupportedLabel?: string;
70
- className?: string;
71
- height?: number | string;
72
- }
73
-
74
- export const AnalyticsChart = ({ config, unsupportedLabel, className, height }: AnalyticsChartProps) => {
75
-
76
- const chartHeight = height ?? config.height ?? DEFAULT_CHART_HEIGHT_PX;
77
- const viewBoxH = typeof chartHeight === "number" ? chartHeight : DEFAULT_CHART_HEIGHT_PX;
78
-
79
- const keys = useMemo(
80
- () => config.yAxisKeys ?? ["value"],
81
- [config.yAxisKeys],
82
- );
83
-
84
- const colors = useMemo(
85
- () => config.colors ?? generateChartColors(keys.length || DEFAULT_SERIES_COUNT),
86
- [config.colors, keys.length],
87
- );
88
-
89
- const rows = useMemo(
90
- () => toRows(config.data as TimeSeriesData[] | ChartData[], keys, config.xAxisKey),
91
- [config.data, keys, config.xAxisKey],
92
- );
93
-
94
- const slices = useMemo<PieSlice[]>(() => {
95
- const entries = config.data as ChartData[];
96
- const total = entries.reduce((sum, entry) => sum + toNumber(entry.value), 0);
97
- return entries.map((entry) => ({
98
- label: String(entry.label ?? ""),
99
- value: toNumber(entry.value),
100
- fraction: total > 0 ? toNumber(entry.value) / total : 0,
101
- }));
102
- }, [config.data]);
103
-
104
- const legendItems = useMemo(() => {
105
- if (config.type === "pie" || config.type === "donut") {
106
- return slices.map((slice, index) => ({
107
- label: `${slice.label} (${Math.round(slice.fraction * 100)}%)`,
108
- color: colors[index % colors.length],
109
- }));
110
- }
111
- return keys.map((key, index) => ({ label: key, color: colors[index % colors.length] }));
112
- }, [config.type, keys, slices, colors]);
113
-
114
- const renderUnsupported = () => (
115
- <div className="udb-chart__empty" role="alert">
116
- {unsupportedLabel ?? config.type}
117
- </div>
118
-
119
- );
120
-
121
- const renderCartesian = () => {
122
- if (rows.length === 0) return renderUnsupported();
123
-
124
- const { min, max } = rowsDomain(rows);
125
- const plotW = VIEWBOX_W - PAD.left - PAD.right;
126
- const plotH = viewBoxH - PAD.top - PAD.bottom;
127
- const xAt = (index: number) =>
128
- PAD.left + (rows.length === 1 ? plotW / 2 : (index / (rows.length - 1)) * plotW);
129
- const yAt = (value: number) =>
130
- PAD.top + plotH - ((value - min) / (max - min)) * plotH;
131
-
132
- const seriesPath = (values: number[]) =>
133
- values
134
- .map((value, index) => `${index === 0 ? "M" : "L"}${xAt(index).toFixed(1)},${yAt(value).toFixed(1)}`)
135
- .join(" ");
136
-
137
- const areaPath = (values: number[]) =>
138
- values.length === 0
139
- ? ""
140
- : `${seriesPath(values)} L${xAt(values.length - 1).toFixed(1)},${yAt(min).toFixed(1)} L${xAt(0).toFixed(1)},${yAt(min).toFixed(1)} Z`;
141
-
142
- return (
143
- <svg
144
- className="udb-chart__svg"
145
- viewBox={`0 0 ${VIEWBOX_W} ${viewBoxH}`}
146
- width="100%"
147
- height="100%"
148
- role="img"
149
- >
150
- {config.showGrid !== false &&
151
- Array.from({ length: Y_TICKS + 1 }, (_, tick) => {
152
- const value = min + ((max - min) / Y_TICKS) * tick;
153
- const y = yAt(value);
154
- return (
155
- <g key={tick}>
156
- <line
157
- className="udb-chart__grid"
158
- x1={PAD.left}
159
- x2={VIEWBOX_W - PAD.right}
160
- y1={y}
161
- y2={y}
162
- />
163
- <text className="udb-chart__axis" x={PAD.left - 6} y={y + 4} textAnchor="end">
164
- {compactFmt.format(value)}
165
- </text>
166
-
167
- </g>
168
-
169
- );
170
- })}
171
-
172
- {rows.map((row, index) =>
173
- shouldShowLabel(index, rows.length) ? (
174
- <text
175
- key={`x-${row.label}-${index}`}
176
- className="udb-chart__axis"
177
- x={xAt(index)}
178
- y={viewBoxH - 6}
179
- textAnchor="middle"
180
- >
181
- {row.label}
182
- </text>
183
-
184
- ) : null,
185
- )}
186
-
187
- {(config.type === "line" || config.type === "area") &&
188
- keys.map((key, seriesIndex) => {
189
- const values = rows.map((row) => row.values[seriesIndex]);
190
- const color = colors[seriesIndex % colors.length];
191
- return (
192
- <g key={key}>
193
- {config.type === "area" && (
194
- <path d={areaPath(values)} fill={color} fillOpacity={AREA_FILL_OPACITY} />
195
- )}
196
- <path
197
- d={seriesPath(values)}
198
- fill="none"
199
- stroke={color}
200
- strokeWidth={LINE_STROKE_WIDTH}
201
- strokeLinecap="round"
202
- strokeLinejoin="round"
203
- />
204
- {config.showTooltip !== false &&
205
- values.map((value, index) => (
206
- <circle
207
- key={`${key}-${index}`}
208
- cx={xAt(index)}
209
- cy={yAt(value)}
210
- r={DOT_RADIUS}
211
- fill={color}
212
- >
213
- <title>{`${rows[index].label} — ${key}: ${value}`}</title>
214
-
215
- </circle>
216
-
217
- ))}
218
- </g>
219
-
220
- );
221
- })}
222
-
223
- {config.type === "bar" &&
224
- rows.map((row, rowIndex) => {
225
- const band = plotW / rows.length;
226
- const barW = (band * 0.6) / keys.length;
227
- const zero = yAt(Math.max(min, 0));
228
- return (
229
- <g key={`${row.label}-${rowIndex}`}>
230
- {keys.map((key, seriesIndex) => {
231
- const value = row.values[seriesIndex];
232
- const x = PAD.left + rowIndex * band + band * 0.2 + seriesIndex * barW;
233
- const y = yAt(Math.max(value, 0));
234
- const height = Math.max(Math.abs(yAt(value) - zero), 1);
235
- const color = colors[seriesIndex % colors.length];
236
- return (
237
- <rect
238
- key={key}
239
- x={x}
240
- y={Math.min(y, zero)}
241
- width={barW - 1}
242
- height={height}
243
- rx={2}
244
- fill={color}
245
- >
246
- <title>{`${row.label} — ${key}: ${value}`}</title>
247
-
248
- </rect>
249
-
250
- );
251
- })}
252
- </g>
253
-
254
- );
255
- })}
256
- </svg>
257
-
258
- );
259
- };
260
-
261
- const renderPie = (donut: boolean) => {
262
- if (slices.length === 0) return renderUnsupported();
263
-
264
- const cx = VIEWBOX_W / 2;
265
- const cy = viewBoxH / 2;
266
- const outerR = Math.min(viewBoxH / 2 - PAD.top, 120);
267
-
268
- if (donut) {
269
- const ringR = outerR * 0.7;
270
- const strokeWidth = outerR - ringR;
271
- const circumference = 2 * Math.PI * ringR;
272
- let offset = 0;
273
- return (
274
- <svg
275
- className="udb-chart__svg"
276
- viewBox={`0 0 ${VIEWBOX_W} ${viewBoxH}`}
277
- width="100%"
278
- height="100%"
279
- role="img"
280
- >
281
- <g transform={`rotate(-90 ${cx} ${cy})`}>
282
- {slices.map((slice, index) => {
283
- const dash = slice.fraction * circumference;
284
- const element = (
285
- <circle
286
- key={`${slice.label}-${index}`}
287
- cx={cx}
288
- cy={cy}
289
- r={ringR}
290
- fill="none"
291
- stroke={colors[index % colors.length]}
292
- strokeWidth={strokeWidth}
293
- strokeDasharray={`${dash} ${circumference - dash}`}
294
- strokeDashoffset={-offset}
295
- >
296
- <title>{`${slice.label}: ${Math.round(slice.fraction * 100)}%`}</title>
297
-
298
- </circle>
299
-
300
- );
301
- offset += dash;
302
- return element;
303
- })}
304
- </g>
305
-
306
- </svg>
307
-
308
- );
309
- }
310
-
311
- let angle = -Math.PI / 2;
312
- return (
313
- <svg
314
- className="udb-chart__svg"
315
- viewBox={`0 0 ${VIEWBOX_W} ${viewBoxH}`}
316
- width="100%"
317
- height="100%"
318
- role="img"
319
- >
320
- {slices.map((slice, index) => {
321
- const sweep = slice.fraction * 2 * Math.PI;
322
- const [x0, y0] = polarPoint(cx, cy, outerR, angle);
323
- const [x1, y1] = polarPoint(cx, cy, outerR, angle + sweep);
324
- const largeArc = sweep > Math.PI ? 1 : 0;
325
- const path =
326
- slice.fraction >= 1
327
- ? `M${cx - outerR},${cy} A${outerR},${outerR} 0 1 0 ${cx + outerR},${cy} A${outerR},${outerR} 0 1 0 ${cx - outerR},${cy} Z`
328
- : `M${cx},${cy} L${x0.toFixed(1)},${y0.toFixed(1)} A${outerR},${outerR} 0 ${largeArc} 1 ${x1.toFixed(1)},${y1.toFixed(1)} Z`;
329
- angle += sweep;
330
- return (
331
- <path key={`${slice.label}-${index}`} d={path} fill={colors[index % colors.length]}>
332
- <title>{`${slice.label}: ${Math.round(slice.fraction * 100)}%`}</title>
333
-
334
- </path>
335
-
336
- );
337
- })}
338
- </svg>
339
-
340
- );
341
- };
342
-
343
- const renderChart = () => {
344
- switch (config.type) {
345
- case "line":
346
- case "area":
347
- case "bar":
348
- return renderCartesian();
349
- case "pie":
350
- return renderPie(false);
351
- case "donut":
352
- return renderPie(true);
353
- default:
354
- return renderUnsupported();
355
- }
356
- };
357
-
358
- return (
359
- <div
360
- className={cn("udb-chart", config.aspectRatio, className)}
361
- style={{
362
- height: typeof chartHeight === "number" ? `${chartHeight}px` : chartHeight,
363
- }}
364
- >
365
- {renderChart()}
366
- {config.showLegend && legendItems.length > 0 && (
367
- <div className="udb-chart__legend">
368
- {legendItems.map((item) => (
369
- <span key={item.label} className="udb-chart__chip">
370
- <span
371
- className="udb-chart__chip-dot"
372
- style={{ background: item.color }}
373
- aria-hidden="true"
374
- />
375
- {item.label}
376
- </span>
377
-
378
- ))}
379
- </div>
380
-
381
- )}
382
- </div>
383
-
384
- );
385
- };
386
-
387
- export default AnalyticsChart;
@@ -1,42 +0,0 @@
1
- import {
2
- MetricCard as DSMetricCard,
3
- } from "@umituz/web-design-system/organisms";
4
- import type { MetricCardProps } from "../types/analytics";
5
- import { formatMetricValue, calculateGrowth, getTrend } from "../utils/analytics";
6
-
7
- export const MetricCard = ({
8
- metric,
9
- size = "md",
10
- showTrend = true,
11
- showIcon = true,
12
- lastPeriodLabel = "",
13
- className,
14
- onClick,
15
- }: MetricCardProps) => {
16
- const hasPrevious = metric.previousValue !== undefined;
17
- const growth = hasPrevious ? calculateGrowth(metric.value, metric.previousValue as number) : null;
18
- const trend = growth !== null ? getTrend(growth) : "stable";
19
-
20
- return (
21
- <DSMetricCard
22
- className={className}
23
- title={metric.name}
24
- value={formatMetricValue(metric)}
25
- icon={showIcon ? metric.icon : undefined}
26
- iconColor="text-primary"
27
- size={size}
28
- trend={
29
- showTrend && growth !== null && trend !== "stable"
30
- ? {
31
- value: growth,
32
- label: lastPeriodLabel,
33
- }
34
- : undefined
35
- }
36
- trendValueFormatter={(v) => `${Math.abs(v).toFixed(1)}%`}
37
- onClick={onClick}
38
- />
39
- );
40
- };
41
-
42
- export default MetricCard;