@object-ui/plugin-dashboard 3.1.5 → 3.3.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/CHANGELOG.md +28 -0
- package/README.md +21 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1162 -939
- package/dist/index.umd.cjs +4 -4
- package/dist/packages/plugin-dashboard/src/DashboardConfigPanel.d.ts.map +1 -0
- package/dist/packages/plugin-dashboard/src/DashboardConfigPanel.stories.d.ts.map +1 -0
- package/dist/packages/plugin-dashboard/src/DashboardGridLayout.d.ts.map +1 -0
- package/dist/{src → packages/plugin-dashboard/src}/DashboardRenderer.d.ts +5 -0
- package/dist/packages/plugin-dashboard/src/DashboardRenderer.d.ts.map +1 -0
- package/dist/packages/plugin-dashboard/src/DashboardRenderer.stories.d.ts.map +1 -0
- package/dist/packages/plugin-dashboard/src/DashboardWithConfig.d.ts.map +1 -0
- package/dist/{src → packages/plugin-dashboard/src}/MetricCard.d.ts +4 -0
- package/dist/packages/plugin-dashboard/src/MetricCard.d.ts.map +1 -0
- package/dist/packages/plugin-dashboard/src/MetricWidget.d.ts +31 -0
- package/dist/packages/plugin-dashboard/src/MetricWidget.d.ts.map +1 -0
- package/dist/packages/plugin-dashboard/src/ObjectDataTable.d.ts.map +1 -0
- package/dist/packages/plugin-dashboard/src/ObjectMetricWidget.d.ts +59 -0
- package/dist/packages/plugin-dashboard/src/ObjectMetricWidget.d.ts.map +1 -0
- package/dist/packages/plugin-dashboard/src/ObjectPivotTable.d.ts.map +1 -0
- package/dist/packages/plugin-dashboard/src/PivotTable.d.ts.map +1 -0
- package/dist/packages/plugin-dashboard/src/WidgetConfigPanel.d.ts.map +1 -0
- package/dist/{src → packages/plugin-dashboard/src}/index.d.ts +4 -2
- package/dist/packages/plugin-dashboard/src/index.d.ts.map +1 -0
- package/dist/packages/plugin-dashboard/src/utils.d.ts.map +1 -0
- package/package.json +44 -11
- package/.turbo/turbo-build.log +0 -34
- package/dist/src/DashboardConfigPanel.d.ts.map +0 -1
- package/dist/src/DashboardConfigPanel.stories.d.ts.map +0 -1
- package/dist/src/DashboardGridLayout.d.ts.map +0 -1
- package/dist/src/DashboardRenderer.d.ts.map +0 -1
- package/dist/src/DashboardRenderer.stories.d.ts.map +0 -1
- package/dist/src/DashboardWithConfig.d.ts.map +0 -1
- package/dist/src/MetricCard.d.ts.map +0 -1
- package/dist/src/MetricWidget.d.ts +0 -24
- package/dist/src/MetricWidget.d.ts.map +0 -1
- package/dist/src/ObjectDataTable.d.ts.map +0 -1
- package/dist/src/ObjectPivotTable.d.ts.map +0 -1
- package/dist/src/PivotTable.d.ts.map +0 -1
- package/dist/src/WidgetConfigPanel.d.ts.map +0 -1
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/utils.d.ts.map +0 -1
- package/src/DashboardConfigPanel.stories.tsx +0 -164
- package/src/DashboardConfigPanel.tsx +0 -158
- package/src/DashboardGridLayout.tsx +0 -367
- package/src/DashboardRenderer.stories.tsx +0 -173
- package/src/DashboardRenderer.tsx +0 -445
- package/src/DashboardWithConfig.tsx +0 -211
- package/src/MetricCard.tsx +0 -82
- package/src/MetricWidget.tsx +0 -76
- package/src/ObjectDataTable.tsx +0 -226
- package/src/ObjectPivotTable.tsx +0 -160
- package/src/PivotTable.tsx +0 -262
- package/src/WidgetConfigPanel.tsx +0 -540
- package/src/__tests__/DashboardConfigPanel.test.tsx +0 -206
- package/src/__tests__/DashboardGridLayout.test.tsx +0 -199
- package/src/__tests__/DashboardRenderer.autoRefresh.test.tsx +0 -124
- package/src/__tests__/DashboardRenderer.designMode.test.tsx +0 -386
- package/src/__tests__/DashboardRenderer.header.test.tsx +0 -114
- package/src/__tests__/DashboardRenderer.mobile.test.tsx +0 -214
- package/src/__tests__/DashboardRenderer.widgetData.test.tsx +0 -1283
- package/src/__tests__/DashboardWithConfig.test.tsx +0 -276
- package/src/__tests__/MetricCard.test.tsx +0 -82
- package/src/__tests__/ObjectDataTable.test.tsx +0 -211
- package/src/__tests__/ObjectPivotTable.test.tsx +0 -192
- package/src/__tests__/PivotTable.test.tsx +0 -162
- package/src/__tests__/WidgetConfigPanel.test.tsx +0 -492
- package/src/__tests__/ensureWidgetIds.test.tsx +0 -103
- package/src/index.tsx +0 -214
- package/src/utils.ts +0 -17
- package/tsconfig.json +0 -19
- package/vite.config.ts +0 -63
- package/vitest.config.ts +0 -9
- package/vitest.setup.tsx +0 -18
- /package/dist/{src → packages/plugin-dashboard/src}/DashboardConfigPanel.d.ts +0 -0
- /package/dist/{src → packages/plugin-dashboard/src}/DashboardConfigPanel.stories.d.ts +0 -0
- /package/dist/{src → packages/plugin-dashboard/src}/DashboardGridLayout.d.ts +0 -0
- /package/dist/{src → packages/plugin-dashboard/src}/DashboardRenderer.stories.d.ts +0 -0
- /package/dist/{src → packages/plugin-dashboard/src}/DashboardWithConfig.d.ts +0 -0
- /package/dist/{src → packages/plugin-dashboard/src}/ObjectDataTable.d.ts +0 -0
- /package/dist/{src → packages/plugin-dashboard/src}/ObjectPivotTable.d.ts +0 -0
- /package/dist/{src → packages/plugin-dashboard/src}/PivotTable.d.ts +0 -0
- /package/dist/{src → packages/plugin-dashboard/src}/WidgetConfigPanel.d.ts +0 -0
- /package/dist/{src → packages/plugin-dashboard/src}/utils.d.ts +0 -0
|
@@ -1,445 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ObjectUI
|
|
3
|
-
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
-
*
|
|
5
|
-
* This source code is licensed under the MIT license found in the
|
|
6
|
-
* LICENSE file in the root directory of this source tree.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { DashboardSchema, DashboardWidgetSchema } from '@object-ui/types';
|
|
10
|
-
import { SchemaRenderer } from '@object-ui/react';
|
|
11
|
-
import { cn, Card, CardHeader, CardTitle, CardContent, Button } from '@object-ui/components';
|
|
12
|
-
import { forwardRef, useState, useEffect, useCallback, useRef } from 'react';
|
|
13
|
-
import { RefreshCw } from 'lucide-react';
|
|
14
|
-
import { isObjectProvider } from './utils';
|
|
15
|
-
|
|
16
|
-
/** Resolve an I18nLabel (string or {key, defaultValue}) to a plain string. */
|
|
17
|
-
function resolveLabel(label: string | { key?: string; defaultValue?: string } | undefined): string | undefined {
|
|
18
|
-
if (label === undefined || label === null) return undefined;
|
|
19
|
-
if (typeof label === 'string') return label;
|
|
20
|
-
return label.defaultValue || label.key;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// Color palette for charts
|
|
24
|
-
const CHART_COLORS = [
|
|
25
|
-
'hsl(var(--chart-1))',
|
|
26
|
-
'hsl(var(--chart-2))',
|
|
27
|
-
'hsl(var(--chart-3))',
|
|
28
|
-
'hsl(var(--chart-4))',
|
|
29
|
-
'hsl(var(--chart-5))',
|
|
30
|
-
];
|
|
31
|
-
|
|
32
|
-
export interface DashboardRendererProps {
|
|
33
|
-
schema: DashboardSchema;
|
|
34
|
-
className?: string;
|
|
35
|
-
/** Callback invoked when dashboard refresh is triggered (manual or auto) */
|
|
36
|
-
onRefresh?: () => void;
|
|
37
|
-
/** Total record count to display */
|
|
38
|
-
recordCount?: number;
|
|
39
|
-
/** User actions configuration */
|
|
40
|
-
userActions?: { sort?: boolean; search?: boolean; filter?: boolean };
|
|
41
|
-
/** Enable design mode — shows selection affordances on widgets */
|
|
42
|
-
designMode?: boolean;
|
|
43
|
-
/** Currently selected widget ID (controlled) */
|
|
44
|
-
selectedWidgetId?: string | null;
|
|
45
|
-
/** Callback when a widget is clicked in design mode */
|
|
46
|
-
onWidgetClick?: (widgetId: string | null) => void;
|
|
47
|
-
[key: string]: any;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export const DashboardRenderer = forwardRef<HTMLDivElement, DashboardRendererProps>(
|
|
51
|
-
({ schema, className, dataSource, onRefresh, recordCount, userActions, designMode, selectedWidgetId, onWidgetClick, ...props }, ref) => {
|
|
52
|
-
const columns = schema.columns || 4; // Default to 4 columns for better density
|
|
53
|
-
const gap = schema.gap || 4;
|
|
54
|
-
const [refreshing, setRefreshing] = useState(false);
|
|
55
|
-
const [isMobile, setIsMobile] = useState(false);
|
|
56
|
-
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
|
57
|
-
|
|
58
|
-
useEffect(() => {
|
|
59
|
-
const checkMobile = () => setIsMobile(window.innerWidth < 768);
|
|
60
|
-
checkMobile();
|
|
61
|
-
window.addEventListener('resize', checkMobile);
|
|
62
|
-
return () => window.removeEventListener('resize', checkMobile);
|
|
63
|
-
}, []);
|
|
64
|
-
|
|
65
|
-
const handleRefresh = useCallback(() => {
|
|
66
|
-
if (!onRefresh) return;
|
|
67
|
-
setRefreshing(true);
|
|
68
|
-
onRefresh();
|
|
69
|
-
// Reset refreshing indicator after a short delay
|
|
70
|
-
setTimeout(() => setRefreshing(false), 600);
|
|
71
|
-
}, [onRefresh]);
|
|
72
|
-
|
|
73
|
-
// Auto-refresh interval
|
|
74
|
-
useEffect(() => {
|
|
75
|
-
if (!schema.refreshInterval || schema.refreshInterval <= 0 || !onRefresh) return;
|
|
76
|
-
intervalRef.current = setInterval(handleRefresh, schema.refreshInterval * 1000);
|
|
77
|
-
return () => {
|
|
78
|
-
if (intervalRef.current) clearInterval(intervalRef.current);
|
|
79
|
-
};
|
|
80
|
-
}, [schema.refreshInterval, onRefresh, handleRefresh]);
|
|
81
|
-
|
|
82
|
-
const handleWidgetClick = useCallback((e: React.MouseEvent, widgetId: string | undefined) => {
|
|
83
|
-
if (!designMode || !onWidgetClick || !widgetId) return;
|
|
84
|
-
e.stopPropagation();
|
|
85
|
-
onWidgetClick(widgetId);
|
|
86
|
-
}, [designMode, onWidgetClick]);
|
|
87
|
-
|
|
88
|
-
const handleWidgetKeyDown = useCallback((e: React.KeyboardEvent, widgetId: string | undefined, index: number) => {
|
|
89
|
-
if (!designMode || !onWidgetClick) return;
|
|
90
|
-
const widgets = schema.widgets || [];
|
|
91
|
-
if (e.key === 'Enter' || e.key === ' ') {
|
|
92
|
-
e.preventDefault();
|
|
93
|
-
onWidgetClick(widgetId ?? null);
|
|
94
|
-
} else if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
|
|
95
|
-
e.preventDefault();
|
|
96
|
-
const next = index + 1 < widgets.length ? widgets[index + 1] : null;
|
|
97
|
-
if (next?.id) onWidgetClick(next.id);
|
|
98
|
-
} else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
|
|
99
|
-
e.preventDefault();
|
|
100
|
-
const prev = index - 1 >= 0 ? widgets[index - 1] : null;
|
|
101
|
-
if (prev?.id) onWidgetClick(prev.id);
|
|
102
|
-
} else if (e.key === 'Escape') {
|
|
103
|
-
e.preventDefault();
|
|
104
|
-
onWidgetClick(null);
|
|
105
|
-
}
|
|
106
|
-
}, [designMode, onWidgetClick, schema.widgets]);
|
|
107
|
-
|
|
108
|
-
const handleBackgroundClick = useCallback((e: React.MouseEvent) => {
|
|
109
|
-
if (!designMode || !onWidgetClick) return;
|
|
110
|
-
if (e.target === e.currentTarget) {
|
|
111
|
-
onWidgetClick(null);
|
|
112
|
-
}
|
|
113
|
-
}, [designMode, onWidgetClick]);
|
|
114
|
-
|
|
115
|
-
const renderWidget = (widget: DashboardWidgetSchema, index: number, forceMobileFullWidth?: boolean) => {
|
|
116
|
-
// Clamp widget span to grid columns to prevent overflow
|
|
117
|
-
const clampedLayout = widget.layout
|
|
118
|
-
? { ...widget.layout, w: Math.min(widget.layout.w, columns) }
|
|
119
|
-
: undefined;
|
|
120
|
-
|
|
121
|
-
const getComponentSchema = () => {
|
|
122
|
-
if (widget.component) return widget.component;
|
|
123
|
-
|
|
124
|
-
// Handle Shorthand Registry Mappings
|
|
125
|
-
const widgetType = widget.type;
|
|
126
|
-
const options = (widget.options || {}) as Record<string, any>;
|
|
127
|
-
if (widgetType === 'bar' || widgetType === 'line' || widgetType === 'area' || widgetType === 'pie' || widgetType === 'donut' || widgetType === 'scatter') {
|
|
128
|
-
// Support data at widget level or nested inside options
|
|
129
|
-
const widgetData = (widget as any).data || options.data;
|
|
130
|
-
// Widget-level fields (from config panel) override options-level fields
|
|
131
|
-
const xAxisKey = widget.categoryField || options.xField || 'name';
|
|
132
|
-
const yField = widget.valueField || options.yField || 'value';
|
|
133
|
-
|
|
134
|
-
// provider: 'object' — delegate to ObjectChart for async data loading
|
|
135
|
-
if (isObjectProvider(widgetData)) {
|
|
136
|
-
// Merge widget-level fields with data provider config.
|
|
137
|
-
// Widget-level fields take precedence so that config panel
|
|
138
|
-
// edits are immediately reflected in the live preview.
|
|
139
|
-
const providerAgg = widgetData.aggregate;
|
|
140
|
-
const effectiveAggregate = providerAgg ? {
|
|
141
|
-
field: widget.valueField || providerAgg.field,
|
|
142
|
-
function: widget.aggregate || providerAgg.function,
|
|
143
|
-
groupBy: widget.categoryField || providerAgg.groupBy,
|
|
144
|
-
} : undefined;
|
|
145
|
-
const effectiveYField = effectiveAggregate?.field || yField;
|
|
146
|
-
return {
|
|
147
|
-
type: 'object-chart',
|
|
148
|
-
chartType: widgetType,
|
|
149
|
-
objectName: widget.object || widgetData.object,
|
|
150
|
-
aggregate: effectiveAggregate,
|
|
151
|
-
xAxisKey: xAxisKey,
|
|
152
|
-
series: [{ dataKey: effectiveYField }],
|
|
153
|
-
colors: CHART_COLORS,
|
|
154
|
-
className: "h-[200px] sm:h-[250px] md:h-[300px]"
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// No explicit data provider but widget has object binding
|
|
159
|
-
// (e.g. newly created widget via config panel) — build object-chart
|
|
160
|
-
if (!widgetData && widget.object) {
|
|
161
|
-
const aggregate = widget.aggregate ? {
|
|
162
|
-
field: widget.valueField || 'value',
|
|
163
|
-
function: widget.aggregate,
|
|
164
|
-
groupBy: widget.categoryField || 'name',
|
|
165
|
-
} : undefined;
|
|
166
|
-
return {
|
|
167
|
-
type: 'object-chart',
|
|
168
|
-
chartType: widgetType,
|
|
169
|
-
objectName: widget.object,
|
|
170
|
-
aggregate,
|
|
171
|
-
xAxisKey: xAxisKey,
|
|
172
|
-
series: [{ dataKey: widget.valueField || 'value' }],
|
|
173
|
-
colors: CHART_COLORS,
|
|
174
|
-
className: "h-[200px] sm:h-[250px] md:h-[300px]"
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const dataItems = Array.isArray(widgetData) ? widgetData : widgetData?.items || [];
|
|
179
|
-
|
|
180
|
-
return {
|
|
181
|
-
type: 'chart',
|
|
182
|
-
chartType: widgetType,
|
|
183
|
-
data: dataItems,
|
|
184
|
-
xAxisKey: xAxisKey,
|
|
185
|
-
series: [{ dataKey: yField }],
|
|
186
|
-
colors: CHART_COLORS,
|
|
187
|
-
className: "h-[200px] sm:h-[250px] md:h-[300px]"
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (widgetType === 'table') {
|
|
192
|
-
// Support data at widget level or nested inside options
|
|
193
|
-
const widgetData = (widget as any).data || options.data;
|
|
194
|
-
|
|
195
|
-
// provider: 'object' — use ObjectDataTable for async data loading
|
|
196
|
-
if (isObjectProvider(widgetData)) {
|
|
197
|
-
const { data: _data, ...restOptions } = options;
|
|
198
|
-
return {
|
|
199
|
-
type: 'object-data-table',
|
|
200
|
-
...restOptions,
|
|
201
|
-
objectName: widget.object || widgetData.object,
|
|
202
|
-
dataProvider: widgetData,
|
|
203
|
-
filter: widgetData.filter || widget.filter,
|
|
204
|
-
searchable: widget.searchable ?? false,
|
|
205
|
-
pagination: widget.pagination ?? false,
|
|
206
|
-
className: "border-0"
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// No explicit data provider but widget has object binding
|
|
211
|
-
if (!widgetData && widget.object) {
|
|
212
|
-
return {
|
|
213
|
-
type: 'object-data-table',
|
|
214
|
-
...options,
|
|
215
|
-
objectName: widget.object,
|
|
216
|
-
filter: widget.filter,
|
|
217
|
-
searchable: widget.searchable ?? false,
|
|
218
|
-
pagination: widget.pagination ?? false,
|
|
219
|
-
className: "border-0"
|
|
220
|
-
};
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
return {
|
|
224
|
-
type: 'data-table',
|
|
225
|
-
...options,
|
|
226
|
-
data: widgetData?.items || [],
|
|
227
|
-
searchable: false,
|
|
228
|
-
pagination: false,
|
|
229
|
-
className: "border-0"
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
if (widgetType === 'pivot') {
|
|
234
|
-
const widgetData = (widget as any).data || options.data;
|
|
235
|
-
|
|
236
|
-
// provider: 'object' — use ObjectPivotTable for async data loading
|
|
237
|
-
if (isObjectProvider(widgetData)) {
|
|
238
|
-
const { data: _data, ...restOptions } = options;
|
|
239
|
-
return {
|
|
240
|
-
type: 'object-pivot',
|
|
241
|
-
...restOptions,
|
|
242
|
-
objectName: widget.object || widgetData.object,
|
|
243
|
-
dataProvider: widgetData,
|
|
244
|
-
filter: widgetData.filter || widget.filter,
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// No explicit data provider but widget has object binding
|
|
249
|
-
if (!widgetData && widget.object) {
|
|
250
|
-
return {
|
|
251
|
-
type: 'object-pivot',
|
|
252
|
-
...options,
|
|
253
|
-
objectName: widget.object,
|
|
254
|
-
filter: widget.filter,
|
|
255
|
-
};
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
return {
|
|
259
|
-
type: 'pivot',
|
|
260
|
-
...options,
|
|
261
|
-
data: Array.isArray(widgetData) ? widgetData : widgetData?.items || [],
|
|
262
|
-
};
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
return {
|
|
266
|
-
...widget,
|
|
267
|
-
...options
|
|
268
|
-
};
|
|
269
|
-
};
|
|
270
|
-
|
|
271
|
-
const componentSchema = getComponentSchema();
|
|
272
|
-
const isSelfContained = widget.type === 'metric';
|
|
273
|
-
const resolvedTitle = resolveLabel(widget.title);
|
|
274
|
-
const resolvedDescription = resolveLabel(widget.description);
|
|
275
|
-
const widgetKey = widget.id || resolvedTitle || `widget-${index}`;
|
|
276
|
-
const isSelected = designMode && selectedWidgetId === widget.id;
|
|
277
|
-
|
|
278
|
-
const designModeProps = designMode ? {
|
|
279
|
-
'data-testid': `dashboard-preview-widget-${widget.id}`,
|
|
280
|
-
'data-widget-id': widget.id,
|
|
281
|
-
role: 'button' as const,
|
|
282
|
-
tabIndex: 0,
|
|
283
|
-
'aria-selected': isSelected,
|
|
284
|
-
'aria-label': `Widget: ${resolvedTitle || `Widget ${index + 1}`}`,
|
|
285
|
-
onClick: (e: React.MouseEvent) => handleWidgetClick(e, widget.id),
|
|
286
|
-
onKeyDown: (e: React.KeyboardEvent) => handleWidgetKeyDown(e, widget.id, index),
|
|
287
|
-
} : {};
|
|
288
|
-
|
|
289
|
-
const selectionClasses = designMode
|
|
290
|
-
? cn(
|
|
291
|
-
"cursor-pointer rounded-lg transition-all outline-none",
|
|
292
|
-
"focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
|
|
293
|
-
isSelected
|
|
294
|
-
? "ring-2 ring-primary shadow-md bg-primary/5 dark:bg-primary/10"
|
|
295
|
-
: "hover:ring-2 hover:ring-primary/40 hover:shadow-sm"
|
|
296
|
-
)
|
|
297
|
-
: undefined;
|
|
298
|
-
|
|
299
|
-
if (isSelfContained) {
|
|
300
|
-
return (
|
|
301
|
-
<div
|
|
302
|
-
key={widgetKey}
|
|
303
|
-
className={cn("h-full w-full", designMode && "relative", selectionClasses)}
|
|
304
|
-
style={!isMobile && clampedLayout ? {
|
|
305
|
-
gridColumn: `span ${clampedLayout.w}`,
|
|
306
|
-
gridRow: `span ${clampedLayout.h}`
|
|
307
|
-
}: undefined}
|
|
308
|
-
{...designModeProps}
|
|
309
|
-
>
|
|
310
|
-
<SchemaRenderer schema={componentSchema} className={cn("h-full w-full", designMode && "pointer-events-none")} />
|
|
311
|
-
{designMode && <div className="absolute inset-0 z-10" aria-hidden="true" data-testid="widget-click-overlay" />}
|
|
312
|
-
</div>
|
|
313
|
-
);
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
return (
|
|
317
|
-
<Card
|
|
318
|
-
key={widgetKey}
|
|
319
|
-
className={cn(
|
|
320
|
-
"overflow-hidden border-border/50 shadow-sm transition-all hover:shadow-md",
|
|
321
|
-
"bg-card/50 backdrop-blur-sm",
|
|
322
|
-
forceMobileFullWidth && "w-full",
|
|
323
|
-
designMode && "relative",
|
|
324
|
-
selectionClasses
|
|
325
|
-
)}
|
|
326
|
-
style={!isMobile && clampedLayout ? {
|
|
327
|
-
gridColumn: `span ${clampedLayout.w}`,
|
|
328
|
-
gridRow: `span ${clampedLayout.h}`
|
|
329
|
-
}: undefined}
|
|
330
|
-
{...designModeProps}
|
|
331
|
-
>
|
|
332
|
-
{resolvedTitle && (
|
|
333
|
-
<CardHeader className="pb-2 border-b border-border/40 bg-muted/20 px-3 sm:px-6">
|
|
334
|
-
<CardTitle className="text-sm sm:text-base font-medium tracking-tight truncate" title={resolvedTitle}>
|
|
335
|
-
{resolvedTitle}
|
|
336
|
-
</CardTitle>
|
|
337
|
-
{resolvedDescription && (
|
|
338
|
-
<p className="text-xs text-muted-foreground mt-0.5 line-clamp-2">{resolvedDescription}</p>
|
|
339
|
-
)}
|
|
340
|
-
</CardHeader>
|
|
341
|
-
)}
|
|
342
|
-
<CardContent className="p-0">
|
|
343
|
-
<div className={cn("h-full w-full", "p-3 sm:p-4 md:p-6", designMode && "pointer-events-none")}>
|
|
344
|
-
<SchemaRenderer schema={componentSchema} />
|
|
345
|
-
</div>
|
|
346
|
-
</CardContent>
|
|
347
|
-
{designMode && <div className="absolute inset-0 z-10" aria-hidden="true" data-testid="widget-click-overlay" />}
|
|
348
|
-
</Card>
|
|
349
|
-
);
|
|
350
|
-
};
|
|
351
|
-
|
|
352
|
-
const headerSection = schema.header && (
|
|
353
|
-
<div className="col-span-full mb-4">
|
|
354
|
-
{schema.header.showTitle !== false && schema.title && (
|
|
355
|
-
<h2 className="text-lg font-semibold tracking-tight">{resolveLabel(schema.title)}</h2>
|
|
356
|
-
)}
|
|
357
|
-
{schema.header.showDescription !== false && schema.description && (
|
|
358
|
-
<p className="text-sm text-muted-foreground mt-1">{resolveLabel(schema.description)}</p>
|
|
359
|
-
)}
|
|
360
|
-
{schema.header.actions && schema.header.actions.length > 0 && (
|
|
361
|
-
<div className="flex gap-2 mt-3">
|
|
362
|
-
{schema.header.actions.map((action: { label: string; actionUrl?: string; actionType?: string; icon?: string }, i: number) => (
|
|
363
|
-
<Button key={i} variant="outline" size="sm">
|
|
364
|
-
{action.label}
|
|
365
|
-
</Button>
|
|
366
|
-
))}
|
|
367
|
-
</div>
|
|
368
|
-
)}
|
|
369
|
-
</div>
|
|
370
|
-
);
|
|
371
|
-
|
|
372
|
-
const recordCountBadge = recordCount !== undefined && (
|
|
373
|
-
<span className="text-xs text-muted-foreground">
|
|
374
|
-
{recordCount.toLocaleString()} records
|
|
375
|
-
</span>
|
|
376
|
-
);
|
|
377
|
-
|
|
378
|
-
const userActionsAttr = userActions ? JSON.stringify(userActions) : undefined;
|
|
379
|
-
|
|
380
|
-
const refreshButton = onRefresh && (
|
|
381
|
-
<div className={cn("flex items-center justify-end gap-3 mb-2", !isMobile && "col-span-full")}>
|
|
382
|
-
{recordCountBadge}
|
|
383
|
-
<Button
|
|
384
|
-
variant="outline"
|
|
385
|
-
size="sm"
|
|
386
|
-
onClick={handleRefresh}
|
|
387
|
-
disabled={refreshing}
|
|
388
|
-
aria-label="Refresh dashboard"
|
|
389
|
-
>
|
|
390
|
-
<RefreshCw className={cn("h-4 w-4 mr-2", refreshing && "animate-spin")} />
|
|
391
|
-
{refreshing ? 'Refreshing…' : 'Refresh All'}
|
|
392
|
-
</Button>
|
|
393
|
-
</div>
|
|
394
|
-
);
|
|
395
|
-
|
|
396
|
-
if (isMobile) {
|
|
397
|
-
// Separate metric widgets from other widgets for better mobile layout
|
|
398
|
-
const metricWidgets = schema.widgets?.filter((w: DashboardWidgetSchema) => w.type === 'metric') || [];
|
|
399
|
-
const otherWidgets = schema.widgets?.filter((w: DashboardWidgetSchema) => w.type !== 'metric') || [];
|
|
400
|
-
|
|
401
|
-
return (
|
|
402
|
-
<div ref={ref} className={cn("flex flex-col gap-4 px-4", className)} data-user-actions={userActionsAttr} onClick={handleBackgroundClick} {...props}>
|
|
403
|
-
{headerSection}
|
|
404
|
-
{refreshButton}
|
|
405
|
-
|
|
406
|
-
{/* Metric cards: 2-column grid */}
|
|
407
|
-
{metricWidgets.length > 0 && (
|
|
408
|
-
<div className="grid grid-cols-2 gap-3" onClick={handleBackgroundClick}>
|
|
409
|
-
{metricWidgets.map((widget: DashboardWidgetSchema, index: number) => renderWidget(widget, index))}
|
|
410
|
-
</div>
|
|
411
|
-
)}
|
|
412
|
-
|
|
413
|
-
{/* Other widgets (charts, tables): full-width vertical stack */}
|
|
414
|
-
{otherWidgets.length > 0 && (
|
|
415
|
-
<div className="flex flex-col gap-4" onClick={handleBackgroundClick}>
|
|
416
|
-
{otherWidgets.map((widget: DashboardWidgetSchema, index: number) => renderWidget(widget, index, true))}
|
|
417
|
-
</div>
|
|
418
|
-
)}
|
|
419
|
-
</div>
|
|
420
|
-
);
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
return (
|
|
424
|
-
<div
|
|
425
|
-
ref={ref}
|
|
426
|
-
className={cn(
|
|
427
|
-
"grid auto-rows-min",
|
|
428
|
-
"grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4",
|
|
429
|
-
className
|
|
430
|
-
)}
|
|
431
|
-
style={{
|
|
432
|
-
...(columns > 4 && { gridTemplateColumns: `repeat(${columns}, minmax(0, 1fr))` }),
|
|
433
|
-
gap: `${gap * 0.25}rem`
|
|
434
|
-
}}
|
|
435
|
-
data-user-actions={userActionsAttr}
|
|
436
|
-
onClick={handleBackgroundClick}
|
|
437
|
-
{...props}
|
|
438
|
-
>
|
|
439
|
-
{headerSection}
|
|
440
|
-
{refreshButton}
|
|
441
|
-
{schema.widgets?.map((widget: DashboardWidgetSchema, index: number) => renderWidget(widget, index))}
|
|
442
|
-
</div>
|
|
443
|
-
);
|
|
444
|
-
}
|
|
445
|
-
);
|
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ObjectUI
|
|
3
|
-
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
-
*
|
|
5
|
-
* This source code is licensed under the MIT license found in the
|
|
6
|
-
* LICENSE file in the root directory of this source tree.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import * as React from 'react';
|
|
10
|
-
import { useState, useCallback, useEffect } from 'react';
|
|
11
|
-
import { Settings } from 'lucide-react';
|
|
12
|
-
import { cn, Button } from '@object-ui/components';
|
|
13
|
-
import type { DashboardSchema, DashboardWidgetSchema } from '@object-ui/types';
|
|
14
|
-
|
|
15
|
-
import { DashboardRenderer } from './DashboardRenderer';
|
|
16
|
-
import { DashboardConfigPanel } from './DashboardConfigPanel';
|
|
17
|
-
import { WidgetConfigPanel } from './WidgetConfigPanel';
|
|
18
|
-
|
|
19
|
-
// ---------------------------------------------------------------------------
|
|
20
|
-
// Props
|
|
21
|
-
// ---------------------------------------------------------------------------
|
|
22
|
-
|
|
23
|
-
export interface DashboardWithConfigProps {
|
|
24
|
-
/** Dashboard schema for rendering */
|
|
25
|
-
schema: DashboardSchema;
|
|
26
|
-
/** Current dashboard configuration (for the config panel) */
|
|
27
|
-
config: Record<string, any>;
|
|
28
|
-
/** Called when config panel saves dashboard-level changes */
|
|
29
|
-
onConfigSave: (config: Record<string, any>) => void;
|
|
30
|
-
/** Called when widget config panel saves widget-level changes */
|
|
31
|
-
onWidgetSave?: (widgetId: string, config: Record<string, any>) => void;
|
|
32
|
-
/** Callback invoked when dashboard refresh is triggered */
|
|
33
|
-
onRefresh?: () => void;
|
|
34
|
-
/** Total record count */
|
|
35
|
-
recordCount?: number;
|
|
36
|
-
/** Whether the config panel is open initially */
|
|
37
|
-
defaultConfigOpen?: boolean;
|
|
38
|
-
/** Additional CSS class name for the container */
|
|
39
|
-
className?: string;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// ---------------------------------------------------------------------------
|
|
43
|
-
// Component
|
|
44
|
-
// ---------------------------------------------------------------------------
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* DashboardWithConfig — Composite component combining a DashboardRenderer
|
|
48
|
-
* with a DashboardConfigPanel sidebar.
|
|
49
|
-
*
|
|
50
|
-
* Supports:
|
|
51
|
-
* - Toggle config panel visibility via a Settings button
|
|
52
|
-
* - Dashboard-level configuration editing
|
|
53
|
-
* - Click-to-select a widget → sidebar switches to WidgetConfigPanel
|
|
54
|
-
* - Back navigation from widget config to dashboard config
|
|
55
|
-
* - Live preview: widget config changes are reflected in real time
|
|
56
|
-
*/
|
|
57
|
-
export function DashboardWithConfig({
|
|
58
|
-
schema,
|
|
59
|
-
config,
|
|
60
|
-
onConfigSave,
|
|
61
|
-
onWidgetSave,
|
|
62
|
-
onRefresh,
|
|
63
|
-
recordCount,
|
|
64
|
-
defaultConfigOpen = false,
|
|
65
|
-
className,
|
|
66
|
-
}: DashboardWithConfigProps) {
|
|
67
|
-
const [configOpen, setConfigOpen] = useState(defaultConfigOpen);
|
|
68
|
-
const [selectedWidgetId, setSelectedWidgetId] = useState<string | null>(null);
|
|
69
|
-
|
|
70
|
-
// Internal schema state for live preview during widget editing.
|
|
71
|
-
// Updated on every field change; reset when external schema prop changes.
|
|
72
|
-
const [liveSchema, setLiveSchema] = useState<DashboardSchema>(schema);
|
|
73
|
-
const [configVersion, setConfigVersion] = useState(0);
|
|
74
|
-
|
|
75
|
-
useEffect(() => {
|
|
76
|
-
setLiveSchema(schema);
|
|
77
|
-
setConfigVersion((v) => v + 1);
|
|
78
|
-
}, [schema]);
|
|
79
|
-
|
|
80
|
-
// Stable widget config for the config panel — only recomputed on
|
|
81
|
-
// widget selection change or save (configVersion), NOT on every live
|
|
82
|
-
// field change. This prevents useConfigDraft from resetting the draft.
|
|
83
|
-
const selectedWidgetConfig = React.useMemo(() => {
|
|
84
|
-
if (!selectedWidgetId || !liveSchema.widgets) return null;
|
|
85
|
-
const widget = liveSchema.widgets.find(
|
|
86
|
-
(w) => (w.id || w.title) === selectedWidgetId,
|
|
87
|
-
);
|
|
88
|
-
if (!widget) return null;
|
|
89
|
-
return {
|
|
90
|
-
id: widget.id ?? '',
|
|
91
|
-
title: widget.title ?? '',
|
|
92
|
-
description: widget.description ?? '',
|
|
93
|
-
type: widget.type ?? '',
|
|
94
|
-
object: widget.object ?? '',
|
|
95
|
-
categoryField: widget.categoryField ?? '',
|
|
96
|
-
valueField: widget.valueField ?? '',
|
|
97
|
-
aggregate: widget.aggregate ?? '',
|
|
98
|
-
colorVariant: widget.colorVariant ?? 'default',
|
|
99
|
-
actionUrl: widget.actionUrl ?? '',
|
|
100
|
-
layoutW: widget.layout?.w ?? 1,
|
|
101
|
-
layoutH: widget.layout?.h ?? 1,
|
|
102
|
-
};
|
|
103
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
104
|
-
}, [selectedWidgetId, configVersion]);
|
|
105
|
-
|
|
106
|
-
const handleWidgetSelect = useCallback(
|
|
107
|
-
(widgetId: string) => {
|
|
108
|
-
setSelectedWidgetId(widgetId);
|
|
109
|
-
setConfigOpen(true);
|
|
110
|
-
},
|
|
111
|
-
[],
|
|
112
|
-
);
|
|
113
|
-
|
|
114
|
-
const handleWidgetClose = useCallback(() => {
|
|
115
|
-
setSelectedWidgetId(null);
|
|
116
|
-
}, []);
|
|
117
|
-
|
|
118
|
-
// Live-update handler: updates liveSchema so DashboardRenderer re-renders.
|
|
119
|
-
const handleWidgetFieldChange = useCallback(
|
|
120
|
-
(field: string, value: any) => {
|
|
121
|
-
if (!selectedWidgetId) return;
|
|
122
|
-
setLiveSchema((prev) => {
|
|
123
|
-
if (!prev.widgets) return prev;
|
|
124
|
-
return {
|
|
125
|
-
...prev,
|
|
126
|
-
widgets: prev.widgets.map((w) => {
|
|
127
|
-
if ((w.id || w.title) !== selectedWidgetId) return w;
|
|
128
|
-
if (field === 'layoutW') {
|
|
129
|
-
return { ...w, layout: { ...(w.layout || {}), w: value } as DashboardWidgetSchema['layout'] };
|
|
130
|
-
}
|
|
131
|
-
if (field === 'layoutH') {
|
|
132
|
-
return { ...w, layout: { ...(w.layout || {}), h: value } as DashboardWidgetSchema['layout'] };
|
|
133
|
-
}
|
|
134
|
-
return { ...w, [field]: value };
|
|
135
|
-
}),
|
|
136
|
-
};
|
|
137
|
-
});
|
|
138
|
-
},
|
|
139
|
-
[selectedWidgetId],
|
|
140
|
-
);
|
|
141
|
-
|
|
142
|
-
const handleWidgetSave = useCallback(
|
|
143
|
-
(widgetConfig: Record<string, any>) => {
|
|
144
|
-
if (selectedWidgetId && onWidgetSave) {
|
|
145
|
-
onWidgetSave(selectedWidgetId, widgetConfig);
|
|
146
|
-
}
|
|
147
|
-
setSelectedWidgetId(null);
|
|
148
|
-
setConfigVersion((v) => v + 1);
|
|
149
|
-
},
|
|
150
|
-
[selectedWidgetId, onWidgetSave],
|
|
151
|
-
);
|
|
152
|
-
|
|
153
|
-
const handleToggleConfig = useCallback(() => {
|
|
154
|
-
setConfigOpen((prev) => !prev);
|
|
155
|
-
setSelectedWidgetId(null);
|
|
156
|
-
}, []);
|
|
157
|
-
|
|
158
|
-
return (
|
|
159
|
-
<div
|
|
160
|
-
className={cn('flex h-full w-full', className)}
|
|
161
|
-
data-testid="dashboard-with-config"
|
|
162
|
-
>
|
|
163
|
-
{/* Main dashboard area */}
|
|
164
|
-
<div className="flex-1 min-w-0 overflow-auto relative">
|
|
165
|
-
{/* Settings toggle button */}
|
|
166
|
-
<div className="absolute top-2 right-2 z-10">
|
|
167
|
-
<Button
|
|
168
|
-
size="sm"
|
|
169
|
-
variant={configOpen ? 'default' : 'outline'}
|
|
170
|
-
onClick={handleToggleConfig}
|
|
171
|
-
data-testid="dashboard-config-toggle"
|
|
172
|
-
>
|
|
173
|
-
<Settings className="h-3.5 w-3.5 mr-1" />
|
|
174
|
-
Settings
|
|
175
|
-
</Button>
|
|
176
|
-
</div>
|
|
177
|
-
|
|
178
|
-
<DashboardRenderer
|
|
179
|
-
schema={liveSchema}
|
|
180
|
-
onRefresh={onRefresh}
|
|
181
|
-
recordCount={recordCount}
|
|
182
|
-
designMode={configOpen}
|
|
183
|
-
selectedWidgetId={selectedWidgetId}
|
|
184
|
-
onWidgetClick={handleWidgetSelect}
|
|
185
|
-
/>
|
|
186
|
-
</div>
|
|
187
|
-
|
|
188
|
-
{/* Config panel sidebar */}
|
|
189
|
-
{configOpen && (
|
|
190
|
-
<div className="relative shrink-0">
|
|
191
|
-
{selectedWidgetId && selectedWidgetConfig ? (
|
|
192
|
-
<WidgetConfigPanel
|
|
193
|
-
open={true}
|
|
194
|
-
onClose={handleWidgetClose}
|
|
195
|
-
config={selectedWidgetConfig}
|
|
196
|
-
onSave={handleWidgetSave}
|
|
197
|
-
onFieldChange={handleWidgetFieldChange}
|
|
198
|
-
/>
|
|
199
|
-
) : (
|
|
200
|
-
<DashboardConfigPanel
|
|
201
|
-
open={true}
|
|
202
|
-
onClose={() => setConfigOpen(false)}
|
|
203
|
-
config={config}
|
|
204
|
-
onSave={onConfigSave}
|
|
205
|
-
/>
|
|
206
|
-
)}
|
|
207
|
-
</div>
|
|
208
|
-
)}
|
|
209
|
-
</div>
|
|
210
|
-
);
|
|
211
|
-
}
|