@object-ui/react 3.3.0 → 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 +9 -0
- package/README.md +21 -1
- package/dist/context/AppShellContext.d.ts +26 -0
- package/dist/context/AppShellContext.js +67 -0
- package/dist/context/index.d.ts +1 -7
- package/dist/context/index.js +1 -7
- package/package.json +32 -9
- package/.turbo/turbo-build.log +0 -4
- package/src/LazyPluginLoader.tsx +0 -174
- package/src/SchemaRenderer.tsx +0 -266
- package/src/__tests__/LazyPluginLoader.test.tsx +0 -176
- package/src/__tests__/SchemaRenderer.aria.test.tsx +0 -118
- package/src/__tests__/SchemaRenderer.debug.test.tsx +0 -69
- package/src/__tests__/SchemaRenderer.errorBoundary.test.tsx +0 -114
- package/src/__tests__/SchemaRenderer.expressions.test.tsx +0 -131
- package/src/__tests__/SchemaRendererProvider.smoke.test.tsx +0 -132
- package/src/__tests__/ThemeProvider.test.tsx +0 -451
- package/src/__tests__/useNavigationOverlay.test.ts +0 -578
- package/src/__tests__/useOffline.test.ts +0 -198
- package/src/__tests__/usePageTransition.test.ts +0 -214
- package/src/__tests__/usePageVariables.test.tsx +0 -334
- package/src/__tests__/usePerformance.test.ts +0 -137
- package/src/__tests__/useViewData.test.tsx +0 -422
- package/src/__tests__/view-switching.test.tsx +0 -456
- package/src/components/form/FieldFactory.tsx +0 -516
- package/src/components/form/FormRenderer.test.tsx +0 -744
- package/src/components/form/FormRenderer.tsx +0 -258
- package/src/components/form/README.md +0 -352
- package/src/components/form/index.ts +0 -12
- package/src/context/ActionContext.tsx +0 -213
- package/src/context/DndContext.tsx +0 -260
- package/src/context/NotificationContext.tsx +0 -280
- package/src/context/SchemaRendererContext.tsx +0 -48
- package/src/context/ThemeContext.tsx +0 -325
- package/src/context/__tests__/ActionContext.test.tsx +0 -174
- package/src/context/__tests__/DndContext.test.tsx +0 -150
- package/src/context/__tests__/NotificationContext.test.tsx +0 -176
- package/src/context/__tests__/useDataScope.test.tsx +0 -91
- package/src/context/index.ts +0 -13
- package/src/hooks/__tests__/useActionEngine.test.ts +0 -170
- package/src/hooks/__tests__/useActionRunner.test.ts +0 -153
- package/src/hooks/__tests__/useAnimation.test.ts +0 -82
- package/src/hooks/__tests__/useColumnSummary.test.ts +0 -93
- package/src/hooks/__tests__/useCrudShortcuts.test.ts +0 -85
- package/src/hooks/__tests__/useDataRefresh.test.ts +0 -115
- package/src/hooks/__tests__/useDebugMode.test.ts +0 -83
- package/src/hooks/__tests__/useDensityMode.test.ts +0 -81
- package/src/hooks/__tests__/useDiscovery.test.tsx +0 -247
- package/src/hooks/__tests__/useDynamicApp.test.ts +0 -161
- package/src/hooks/__tests__/useETagCache.test.ts +0 -153
- package/src/hooks/__tests__/useExpression.test.ts +0 -86
- package/src/hooks/__tests__/useFocusTrap.test.ts +0 -294
- package/src/hooks/__tests__/useKeyboardShortcuts.test.ts +0 -144
- package/src/hooks/__tests__/usePageTransition.test.ts +0 -158
- package/src/hooks/__tests__/usePerformanceBudget.test.ts +0 -157
- package/src/hooks/__tests__/useReducedMotion.test.ts +0 -104
- package/src/hooks/__tests__/useSchemaPersistence.test.ts +0 -220
- package/src/hooks/__tests__/useViewSharing.test.ts +0 -121
- package/src/hooks/__tests__/useViewTransition.test.ts +0 -61
- package/src/hooks/index.ts +0 -36
- package/src/hooks/useActionEngine.ts +0 -84
- package/src/hooks/useActionRunner.ts +0 -119
- package/src/hooks/useAnimation.ts +0 -117
- package/src/hooks/useClientNotifications.ts +0 -251
- package/src/hooks/useColumnSummary.ts +0 -87
- package/src/hooks/useCrudShortcuts.ts +0 -126
- package/src/hooks/useDataRefresh.ts +0 -58
- package/src/hooks/useDebugMode.ts +0 -95
- package/src/hooks/useDensityMode.ts +0 -102
- package/src/hooks/useDiscovery.ts +0 -168
- package/src/hooks/useDynamicApp.ts +0 -142
- package/src/hooks/useETagCache.ts +0 -404
- package/src/hooks/useExpression.ts +0 -56
- package/src/hooks/useFocusManagement.ts +0 -248
- package/src/hooks/useFocusTrap.ts +0 -138
- package/src/hooks/useGlobalUndo.ts +0 -98
- package/src/hooks/useKeyboardShortcuts.ts +0 -119
- package/src/hooks/useNavigationOverlay.ts +0 -190
- package/src/hooks/useOffline.ts +0 -301
- package/src/hooks/usePageTransition.ts +0 -187
- package/src/hooks/usePageVariables.tsx +0 -157
- package/src/hooks/usePerformance.ts +0 -246
- package/src/hooks/usePerformanceBudget.ts +0 -313
- package/src/hooks/useReducedMotion.ts +0 -53
- package/src/hooks/useSchemaPersistence.ts +0 -222
- package/src/hooks/useViewData.ts +0 -201
- package/src/hooks/useViewSharing.ts +0 -140
- package/src/hooks/useViewTransition.ts +0 -144
- package/src/index.ts +0 -41
- package/src/spec-bridge/SpecBridge.ts +0 -64
- package/src/spec-bridge/__tests__/P1SpecBridge.test.ts +0 -855
- package/src/spec-bridge/__tests__/SpecBridge.test.ts +0 -550
- package/src/spec-bridge/bridges/dashboard.ts +0 -180
- package/src/spec-bridge/bridges/form-view.ts +0 -109
- package/src/spec-bridge/bridges/list-view.ts +0 -187
- package/src/spec-bridge/bridges/page.ts +0 -136
- package/src/spec-bridge/index.ts +0 -14
- package/src/spec-bridge/types.ts +0 -38
- package/src/utils/i18n.ts +0 -10
- package/tsconfig.json +0 -17
- package/tsconfig.tsbuildinfo +0 -1
- package/vitest.config.ts +0 -2
|
@@ -1,251 +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
|
-
/**
|
|
10
|
-
* @object-ui/react - useClientNotifications
|
|
11
|
-
*
|
|
12
|
-
* Bridge hook between the @objectstack/client notifications API
|
|
13
|
-
* and the local NotificationContext state. Fetches server-side
|
|
14
|
-
* notifications and surfaces them through the existing provider.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
import { useCallback, useContext, useEffect, useRef, useState } from 'react';
|
|
18
|
-
import { SchemaRendererContext } from '../context/SchemaRendererContext';
|
|
19
|
-
import { useNotifications } from '../context/NotificationContext';
|
|
20
|
-
import type { NotificationSeverityLevel } from '../context/NotificationContext';
|
|
21
|
-
|
|
22
|
-
/* ------------------------------------------------------------------ */
|
|
23
|
-
/* Public types */
|
|
24
|
-
/* ------------------------------------------------------------------ */
|
|
25
|
-
|
|
26
|
-
export interface UseClientNotificationsOptions {
|
|
27
|
-
/** ObjectStack client instance (optional, from context if available) */
|
|
28
|
-
client?: any;
|
|
29
|
-
/** Whether to auto-fetch notifications on mount */
|
|
30
|
-
autoFetch?: boolean;
|
|
31
|
-
/** Polling interval in ms (0 to disable) */
|
|
32
|
-
pollInterval?: number;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface UseClientNotificationsResult {
|
|
36
|
-
/** Fetch notifications from server */
|
|
37
|
-
fetchNotifications: () => Promise<void>;
|
|
38
|
-
/** Register device for push notifications */
|
|
39
|
-
registerDevice: (token: string, platform: string) => Promise<void>;
|
|
40
|
-
/** Get notification preferences */
|
|
41
|
-
getPreferences: () => Promise<any>;
|
|
42
|
-
/** Update notification preferences */
|
|
43
|
-
updatePreferences: (prefs: any) => Promise<void>;
|
|
44
|
-
/** Mark a notification as read on the server */
|
|
45
|
-
markAsRead: (id: string) => Promise<void>;
|
|
46
|
-
/** Loading state */
|
|
47
|
-
loading: boolean;
|
|
48
|
-
/** Error state */
|
|
49
|
-
error: Error | null;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/* ------------------------------------------------------------------ */
|
|
53
|
-
/* Helpers */
|
|
54
|
-
/* ------------------------------------------------------------------ */
|
|
55
|
-
|
|
56
|
-
/** Normalise severity coming from the server into a valid local level. */
|
|
57
|
-
function toSeverity(value: unknown): NotificationSeverityLevel {
|
|
58
|
-
const valid: NotificationSeverityLevel[] = ['info', 'success', 'warning', 'error'];
|
|
59
|
-
if (typeof value === 'string' && valid.includes(value as NotificationSeverityLevel)) {
|
|
60
|
-
return value as NotificationSeverityLevel;
|
|
61
|
-
}
|
|
62
|
-
return 'info';
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Resolve the ObjectStack client from explicit option or SchemaRendererContext.
|
|
67
|
-
*
|
|
68
|
-
* The dataSource stored in context is typically an ObjectStackAdapter which
|
|
69
|
-
* exposes `getClient()`. When the caller passes a client directly we use
|
|
70
|
-
* that instead.
|
|
71
|
-
*/
|
|
72
|
-
function useResolvedClient(explicit?: any): any {
|
|
73
|
-
const rendererCtx = useContext(SchemaRendererContext);
|
|
74
|
-
if (explicit) return explicit;
|
|
75
|
-
|
|
76
|
-
const dataSource = rendererCtx?.dataSource;
|
|
77
|
-
if (dataSource && typeof dataSource.getClient === 'function') {
|
|
78
|
-
return dataSource.getClient();
|
|
79
|
-
}
|
|
80
|
-
return null;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/* ------------------------------------------------------------------ */
|
|
84
|
-
/* Hook */
|
|
85
|
-
/* ------------------------------------------------------------------ */
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Bridge between the `@objectstack/client` notifications API and the
|
|
89
|
-
* local {@link NotificationContext}.
|
|
90
|
-
*
|
|
91
|
-
* The hook resolves the ObjectStack client either from the explicit
|
|
92
|
-
* `client` option or from the `SchemaRendererContext` dataSource, then
|
|
93
|
-
* delegates to `client.notifications.*` methods. Fetched notifications
|
|
94
|
-
* are fed into the local provider via `useNotifications().notify()`.
|
|
95
|
-
*
|
|
96
|
-
* @example
|
|
97
|
-
* ```tsx
|
|
98
|
-
* function NotificationBell() {
|
|
99
|
-
* const { fetchNotifications, loading } = useClientNotifications({
|
|
100
|
-
* autoFetch: true,
|
|
101
|
-
* pollInterval: 30_000,
|
|
102
|
-
* });
|
|
103
|
-
*
|
|
104
|
-
* return <Button onClick={fetchNotifications} disabled={loading}>🔔</Button>;
|
|
105
|
-
* }
|
|
106
|
-
* ```
|
|
107
|
-
*/
|
|
108
|
-
export function useClientNotifications(
|
|
109
|
-
options: UseClientNotificationsOptions = {},
|
|
110
|
-
): UseClientNotificationsResult {
|
|
111
|
-
const { client: explicitClient, autoFetch = false, pollInterval = 0 } = options;
|
|
112
|
-
|
|
113
|
-
const client = useResolvedClient(explicitClient);
|
|
114
|
-
const { notify, markAsRead: localMarkAsRead } = useNotifications();
|
|
115
|
-
|
|
116
|
-
const [loading, setLoading] = useState(false);
|
|
117
|
-
const [error, setError] = useState<Error | null>(null);
|
|
118
|
-
|
|
119
|
-
// Track IDs that have already been pushed into the local context so we
|
|
120
|
-
// don't add duplicates on subsequent fetches / polls.
|
|
121
|
-
const seenIdsRef = useRef<Set<string>>(new Set());
|
|
122
|
-
|
|
123
|
-
/* ---- fetchNotifications ---- */
|
|
124
|
-
|
|
125
|
-
const fetchNotifications = useCallback(async () => {
|
|
126
|
-
if (!client?.notifications) return;
|
|
127
|
-
|
|
128
|
-
setLoading(true);
|
|
129
|
-
setError(null);
|
|
130
|
-
|
|
131
|
-
try {
|
|
132
|
-
const result = await client.notifications.list({});
|
|
133
|
-
const items: any[] = Array.isArray(result) ? result : result?.data ?? result?.items ?? [];
|
|
134
|
-
|
|
135
|
-
for (const item of items) {
|
|
136
|
-
const id = String(item.id ?? '');
|
|
137
|
-
if (!id || seenIdsRef.current.has(id)) continue;
|
|
138
|
-
|
|
139
|
-
seenIdsRef.current.add(id);
|
|
140
|
-
notify({
|
|
141
|
-
title: item.title ?? 'Notification',
|
|
142
|
-
message: item.message ?? item.body,
|
|
143
|
-
severity: toSeverity(item.severity ?? item.level),
|
|
144
|
-
duration: 0, // server-driven notifications are persistent
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
} catch (err) {
|
|
148
|
-
setError(err instanceof Error ? err : new Error('Failed to fetch notifications'));
|
|
149
|
-
} finally {
|
|
150
|
-
setLoading(false);
|
|
151
|
-
}
|
|
152
|
-
}, [client, notify]);
|
|
153
|
-
|
|
154
|
-
/* ---- registerDevice ---- */
|
|
155
|
-
|
|
156
|
-
const registerDevice = useCallback(
|
|
157
|
-
async (token: string, platform: string) => {
|
|
158
|
-
if (!client?.notifications) return;
|
|
159
|
-
setError(null);
|
|
160
|
-
try {
|
|
161
|
-
await client.notifications.registerDevice(token, platform);
|
|
162
|
-
} catch (err) {
|
|
163
|
-
const wrapped = err instanceof Error ? err : new Error('Failed to register device');
|
|
164
|
-
setError(wrapped);
|
|
165
|
-
throw wrapped;
|
|
166
|
-
}
|
|
167
|
-
},
|
|
168
|
-
[client],
|
|
169
|
-
);
|
|
170
|
-
|
|
171
|
-
/* ---- getPreferences ---- */
|
|
172
|
-
|
|
173
|
-
const getPreferences = useCallback(async () => {
|
|
174
|
-
if (!client?.notifications) return null;
|
|
175
|
-
setError(null);
|
|
176
|
-
try {
|
|
177
|
-
return await client.notifications.getPreferences();
|
|
178
|
-
} catch (err) {
|
|
179
|
-
const wrapped = err instanceof Error ? err : new Error('Failed to get preferences');
|
|
180
|
-
setError(wrapped);
|
|
181
|
-
throw wrapped;
|
|
182
|
-
}
|
|
183
|
-
}, [client]);
|
|
184
|
-
|
|
185
|
-
/* ---- updatePreferences ---- */
|
|
186
|
-
|
|
187
|
-
const updatePreferences = useCallback(
|
|
188
|
-
async (prefs: any) => {
|
|
189
|
-
if (!client?.notifications) return;
|
|
190
|
-
setError(null);
|
|
191
|
-
try {
|
|
192
|
-
await client.notifications.updatePreferences(prefs);
|
|
193
|
-
} catch (err) {
|
|
194
|
-
const wrapped = err instanceof Error ? err : new Error('Failed to update preferences');
|
|
195
|
-
setError(wrapped);
|
|
196
|
-
throw wrapped;
|
|
197
|
-
}
|
|
198
|
-
},
|
|
199
|
-
[client],
|
|
200
|
-
);
|
|
201
|
-
|
|
202
|
-
/* ---- markAsRead (server + local) ---- */
|
|
203
|
-
|
|
204
|
-
const markAsRead = useCallback(
|
|
205
|
-
async (id: string) => {
|
|
206
|
-
// Optimistically mark locally first
|
|
207
|
-
localMarkAsRead(id);
|
|
208
|
-
|
|
209
|
-
if (!client?.notifications) return;
|
|
210
|
-
setError(null);
|
|
211
|
-
try {
|
|
212
|
-
await client.notifications.markAsRead(id);
|
|
213
|
-
} catch (err) {
|
|
214
|
-
const wrapped = err instanceof Error ? err : new Error('Failed to mark as read');
|
|
215
|
-
setError(wrapped);
|
|
216
|
-
throw wrapped;
|
|
217
|
-
}
|
|
218
|
-
},
|
|
219
|
-
[client, localMarkAsRead],
|
|
220
|
-
);
|
|
221
|
-
|
|
222
|
-
/* ---- auto-fetch on mount ---- */
|
|
223
|
-
|
|
224
|
-
useEffect(() => {
|
|
225
|
-
if (autoFetch && client?.notifications) {
|
|
226
|
-
fetchNotifications();
|
|
227
|
-
}
|
|
228
|
-
}, [autoFetch, client, fetchNotifications]);
|
|
229
|
-
|
|
230
|
-
/* ---- polling ---- */
|
|
231
|
-
|
|
232
|
-
useEffect(() => {
|
|
233
|
-
if (!pollInterval || pollInterval <= 0 || !client?.notifications) return;
|
|
234
|
-
|
|
235
|
-
const id = setInterval(() => {
|
|
236
|
-
fetchNotifications();
|
|
237
|
-
}, pollInterval);
|
|
238
|
-
|
|
239
|
-
return () => clearInterval(id);
|
|
240
|
-
}, [pollInterval, client, fetchNotifications]);
|
|
241
|
-
|
|
242
|
-
return {
|
|
243
|
-
fetchNotifications,
|
|
244
|
-
registerDevice,
|
|
245
|
-
getPreferences,
|
|
246
|
-
updatePreferences,
|
|
247
|
-
markAsRead,
|
|
248
|
-
loading,
|
|
249
|
-
error,
|
|
250
|
-
};
|
|
251
|
-
}
|
|
@@ -1,87 +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 { useMemo } from 'react';
|
|
10
|
-
|
|
11
|
-
/** Summary function types aligned with ColumnSummarySchema */
|
|
12
|
-
export type SummaryFunction = 'sum' | 'avg' | 'count' | 'min' | 'max' | 'distinct';
|
|
13
|
-
|
|
14
|
-
export interface ColumnSummaryConfig {
|
|
15
|
-
/** The field/column to aggregate */
|
|
16
|
-
field: string;
|
|
17
|
-
/** The aggregation function */
|
|
18
|
-
function: SummaryFunction;
|
|
19
|
-
/** Optional label for the summary */
|
|
20
|
-
label?: string;
|
|
21
|
-
/** Number format options */
|
|
22
|
-
format?: Intl.NumberFormatOptions;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface ColumnSummaryResult {
|
|
26
|
-
/** The computed value */
|
|
27
|
-
value: number;
|
|
28
|
-
/** Formatted display string */
|
|
29
|
-
formatted: string;
|
|
30
|
-
/** The label */
|
|
31
|
-
label: string;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Hook for computing column-level summaries (SUM, AVG, COUNT, MIN, MAX, DISTINCT).
|
|
36
|
-
* Implements ColumnSummarySchema from @objectstack/spec v2.0.7.
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* ```tsx
|
|
40
|
-
* const summary = useColumnSummary(data, { field: 'amount', function: 'sum', label: 'Total' });
|
|
41
|
-
* // summary.formatted => "$12,345.67"
|
|
42
|
-
* ```
|
|
43
|
-
*/
|
|
44
|
-
export function useColumnSummary(
|
|
45
|
-
data: Record<string, unknown>[],
|
|
46
|
-
config: ColumnSummaryConfig,
|
|
47
|
-
locale?: string
|
|
48
|
-
): ColumnSummaryResult {
|
|
49
|
-
return useMemo(() => {
|
|
50
|
-
const values = data
|
|
51
|
-
.map((row) => {
|
|
52
|
-
const val = row[config.field];
|
|
53
|
-
return typeof val === 'number' ? val : Number(val);
|
|
54
|
-
})
|
|
55
|
-
.filter((v) => !isNaN(v));
|
|
56
|
-
|
|
57
|
-
let value: number;
|
|
58
|
-
|
|
59
|
-
switch (config.function) {
|
|
60
|
-
case 'sum':
|
|
61
|
-
value = values.reduce((acc, v) => acc + v, 0);
|
|
62
|
-
break;
|
|
63
|
-
case 'avg':
|
|
64
|
-
value = values.length > 0 ? values.reduce((acc, v) => acc + v, 0) / values.length : 0;
|
|
65
|
-
break;
|
|
66
|
-
case 'count':
|
|
67
|
-
value = data.length;
|
|
68
|
-
break;
|
|
69
|
-
case 'min':
|
|
70
|
-
value = values.length > 0 ? Math.min(...values) : 0;
|
|
71
|
-
break;
|
|
72
|
-
case 'max':
|
|
73
|
-
value = values.length > 0 ? Math.max(...values) : 0;
|
|
74
|
-
break;
|
|
75
|
-
case 'distinct':
|
|
76
|
-
value = new Set(data.map((row) => row[config.field])).size;
|
|
77
|
-
break;
|
|
78
|
-
default:
|
|
79
|
-
value = 0;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const formatted = new Intl.NumberFormat(locale, config.format).format(value);
|
|
83
|
-
const label = config.label ?? config.function.toUpperCase();
|
|
84
|
-
|
|
85
|
-
return { value, formatted, label };
|
|
86
|
-
}, [data, config.field, config.function, config.label, config.format, locale]);
|
|
87
|
-
}
|
|
@@ -1,126 +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 { useMemo } from 'react';
|
|
10
|
-
import {
|
|
11
|
-
useKeyboardShortcuts,
|
|
12
|
-
type KeyboardShortcutDefinition,
|
|
13
|
-
} from './useKeyboardShortcuts';
|
|
14
|
-
|
|
15
|
-
export interface CrudShortcutCallbacks {
|
|
16
|
-
onCreate?: () => void;
|
|
17
|
-
onEdit?: () => void;
|
|
18
|
-
onDelete?: () => void;
|
|
19
|
-
onSave?: () => void;
|
|
20
|
-
onDuplicate?: () => void;
|
|
21
|
-
onCancel?: () => void;
|
|
22
|
-
onSearch?: () => void;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Registers standard CRUD keyboard shortcuts using the useKeyboardShortcuts hook.
|
|
27
|
-
*
|
|
28
|
-
* Shortcuts:
|
|
29
|
-
* - Ctrl/Cmd+N: Create new record
|
|
30
|
-
* - Ctrl/Cmd+E: Edit selected record
|
|
31
|
-
* - Delete / Backspace: Delete selected record(s)
|
|
32
|
-
* - Ctrl/Cmd+S: Save current record
|
|
33
|
-
* - Ctrl/Cmd+D: Duplicate selected record
|
|
34
|
-
* - Escape: Cancel/close current operation
|
|
35
|
-
* - Ctrl/Cmd+F: Focus search
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```tsx
|
|
39
|
-
* useCrudShortcuts({
|
|
40
|
-
* onCreate: () => openCreateDialog(),
|
|
41
|
-
* onSave: () => saveRecord(),
|
|
42
|
-
* onDelete: () => deleteSelected(),
|
|
43
|
-
* onCancel: () => closeDialog(),
|
|
44
|
-
* });
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
export function useCrudShortcuts(
|
|
48
|
-
callbacks: CrudShortcutCallbacks,
|
|
49
|
-
enabled = true
|
|
50
|
-
): void {
|
|
51
|
-
const shortcuts = useMemo<KeyboardShortcutDefinition[]>(() => {
|
|
52
|
-
const defs: KeyboardShortcutDefinition[] = [];
|
|
53
|
-
|
|
54
|
-
if (callbacks.onCreate) {
|
|
55
|
-
defs.push({
|
|
56
|
-
key: 'n',
|
|
57
|
-
ctrlOrMeta: true,
|
|
58
|
-
handler: () => callbacks.onCreate!(),
|
|
59
|
-
description: 'New record',
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (callbacks.onEdit) {
|
|
64
|
-
defs.push({
|
|
65
|
-
key: 'e',
|
|
66
|
-
ctrlOrMeta: true,
|
|
67
|
-
handler: () => callbacks.onEdit!(),
|
|
68
|
-
description: 'Edit selected record',
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (callbacks.onDelete) {
|
|
73
|
-
defs.push(
|
|
74
|
-
{
|
|
75
|
-
key: 'Delete',
|
|
76
|
-
handler: () => callbacks.onDelete!(),
|
|
77
|
-
description: 'Delete selected record(s)',
|
|
78
|
-
},
|
|
79
|
-
{
|
|
80
|
-
key: 'Backspace',
|
|
81
|
-
handler: () => callbacks.onDelete!(),
|
|
82
|
-
description: 'Delete selected record(s)',
|
|
83
|
-
}
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (callbacks.onSave) {
|
|
88
|
-
defs.push({
|
|
89
|
-
key: 's',
|
|
90
|
-
ctrlOrMeta: true,
|
|
91
|
-
handler: () => callbacks.onSave!(),
|
|
92
|
-
description: 'Save current record',
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (callbacks.onDuplicate) {
|
|
97
|
-
defs.push({
|
|
98
|
-
key: 'd',
|
|
99
|
-
ctrlOrMeta: true,
|
|
100
|
-
handler: () => callbacks.onDuplicate!(),
|
|
101
|
-
description: 'Duplicate selected record',
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (callbacks.onCancel) {
|
|
106
|
-
defs.push({
|
|
107
|
-
key: 'Escape',
|
|
108
|
-
handler: () => callbacks.onCancel!(),
|
|
109
|
-
description: 'Cancel/close current operation',
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (callbacks.onSearch) {
|
|
114
|
-
defs.push({
|
|
115
|
-
key: 'f',
|
|
116
|
-
ctrlOrMeta: true,
|
|
117
|
-
handler: () => callbacks.onSearch!(),
|
|
118
|
-
description: 'Focus search',
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return defs;
|
|
123
|
-
}, [callbacks]);
|
|
124
|
-
|
|
125
|
-
useKeyboardShortcuts(shortcuts, { enabled });
|
|
126
|
-
}
|
|
@@ -1,58 +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 { useState, useCallback, useEffect } from 'react';
|
|
10
|
-
import type { DataSource } from '@object-ui/types';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Reusable hook that encapsulates the data-refresh pattern:
|
|
14
|
-
* 1. A numeric `refreshKey` that triggers React effect re-runs when incremented.
|
|
15
|
-
* 2. An imperative `refresh()` function to trigger a re-fetch on demand.
|
|
16
|
-
* 3. Auto-subscription to `DataSource.onMutation()` — if the DataSource
|
|
17
|
-
* implements the optional mutation event bus, the hook auto-refreshes
|
|
18
|
-
* whenever a mutation occurs on the matching `objectName`.
|
|
19
|
-
*
|
|
20
|
-
* @param dataSource - The DataSource instance (may be undefined during initial render)
|
|
21
|
-
* @param objectName - The resource/object name to watch for mutations
|
|
22
|
-
* @returns `{ refreshKey, refresh }` — include `refreshKey` in your effect deps
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```tsx
|
|
26
|
-
* const { refreshKey, refresh } = useDataRefresh(dataSource, schema.objectName);
|
|
27
|
-
*
|
|
28
|
-
* useEffect(() => {
|
|
29
|
-
* dataSource.find(objectName, params).then(setData);
|
|
30
|
-
* }, [objectName, refreshKey]);
|
|
31
|
-
*
|
|
32
|
-
* // Or trigger manually:
|
|
33
|
-
* <button onClick={refresh}>Refresh</button>
|
|
34
|
-
* ```
|
|
35
|
-
*/
|
|
36
|
-
export function useDataRefresh(
|
|
37
|
-
dataSource: DataSource | undefined,
|
|
38
|
-
objectName: string | undefined,
|
|
39
|
-
): { refreshKey: number; refresh: () => void } {
|
|
40
|
-
const [refreshKey, setRefreshKey] = useState(0);
|
|
41
|
-
|
|
42
|
-
const refresh = useCallback(() => {
|
|
43
|
-
setRefreshKey(k => k + 1);
|
|
44
|
-
}, []);
|
|
45
|
-
|
|
46
|
-
// Auto-subscribe to DataSource mutation events
|
|
47
|
-
useEffect(() => {
|
|
48
|
-
if (!dataSource?.onMutation || !objectName) return;
|
|
49
|
-
const unsub = dataSource.onMutation((event) => {
|
|
50
|
-
if (event.resource === objectName) {
|
|
51
|
-
setRefreshKey(k => k + 1);
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
return unsub;
|
|
55
|
-
}, [dataSource, objectName]);
|
|
56
|
-
|
|
57
|
-
return { refreshKey, refresh };
|
|
58
|
-
}
|
|
@@ -1,95 +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 { useState, useEffect, useCallback, useMemo } from 'react';
|
|
10
|
-
import { parseDebugFlags, type DebugFlags } from '@object-ui/core';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Result returned by {@link useDebugMode}.
|
|
14
|
-
*/
|
|
15
|
-
export interface UseDebugModeResult {
|
|
16
|
-
/** Whether debug mode is currently enabled */
|
|
17
|
-
enabled: boolean;
|
|
18
|
-
/** Fine-grained debug flags parsed from the URL */
|
|
19
|
-
flags: DebugFlags;
|
|
20
|
-
/** Toggle debug mode on/off */
|
|
21
|
-
toggle: () => void;
|
|
22
|
-
/** Explicitly set debug mode */
|
|
23
|
-
setEnabled: (value: boolean) => void;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Hook for managing debug mode.
|
|
28
|
-
*
|
|
29
|
-
* Automatically detects `?__debug` (and sub-flags like `?__debug_schema`)
|
|
30
|
-
* from the URL. Supports manual toggle and Ctrl+Shift+D keyboard shortcut.
|
|
31
|
-
*
|
|
32
|
-
* SSR-safe — returns disabled state when `window` is unavailable.
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```tsx
|
|
36
|
-
* function App() {
|
|
37
|
-
* const debug = useDebugMode();
|
|
38
|
-
* return (
|
|
39
|
-
* <>
|
|
40
|
-
* {debug.enabled && <DebugPanel flags={debug.flags} />}
|
|
41
|
-
* <button onClick={debug.toggle}>Toggle Debug</button>
|
|
42
|
-
* </>
|
|
43
|
-
* );
|
|
44
|
-
* }
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
export function useDebugMode(): UseDebugModeResult {
|
|
48
|
-
const urlFlags = useMemo(() => {
|
|
49
|
-
try {
|
|
50
|
-
return typeof window !== 'undefined'
|
|
51
|
-
? parseDebugFlags(window.location.search)
|
|
52
|
-
: { enabled: false } as DebugFlags;
|
|
53
|
-
} catch {
|
|
54
|
-
return { enabled: false } as DebugFlags;
|
|
55
|
-
}
|
|
56
|
-
}, []);
|
|
57
|
-
|
|
58
|
-
const [manualEnabled, setManualEnabled] = useState<boolean | null>(null);
|
|
59
|
-
|
|
60
|
-
// Effective enabled: manual override wins, then URL flags
|
|
61
|
-
const enabled = manualEnabled !== null ? manualEnabled : urlFlags.enabled;
|
|
62
|
-
|
|
63
|
-
// Flags reflect URL-parsed flags plus effective enabled state
|
|
64
|
-
const flags: DebugFlags = useMemo(
|
|
65
|
-
() => ({ ...urlFlags, enabled }),
|
|
66
|
-
[urlFlags, enabled],
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
const toggle = useCallback(() => {
|
|
70
|
-
setManualEnabled((prev) => {
|
|
71
|
-
if (prev === null) return !urlFlags.enabled;
|
|
72
|
-
return !prev;
|
|
73
|
-
});
|
|
74
|
-
}, [urlFlags.enabled]);
|
|
75
|
-
|
|
76
|
-
const setEnabled = useCallback((value: boolean) => {
|
|
77
|
-
setManualEnabled(value);
|
|
78
|
-
}, []);
|
|
79
|
-
|
|
80
|
-
// Ctrl+Shift+D keyboard shortcut
|
|
81
|
-
useEffect(() => {
|
|
82
|
-
if (typeof window === 'undefined') return;
|
|
83
|
-
|
|
84
|
-
const handler = (e: KeyboardEvent) => {
|
|
85
|
-
if (e.ctrlKey && e.shiftKey && e.key === 'D') {
|
|
86
|
-
e.preventDefault();
|
|
87
|
-
toggle();
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
document.addEventListener('keydown', handler);
|
|
91
|
-
return () => document.removeEventListener('keydown', handler);
|
|
92
|
-
}, [toggle]);
|
|
93
|
-
|
|
94
|
-
return { enabled, flags, toggle, setEnabled };
|
|
95
|
-
}
|