@object-ui/react 2.0.0 → 3.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.
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +28 -0
- package/dist/SchemaRenderer.d.ts +20 -1
- package/dist/SchemaRenderer.js +76 -13
- package/dist/context/DndContext.d.ts +135 -0
- package/dist/context/DndContext.js +120 -0
- package/dist/context/NotificationContext.d.ts +113 -0
- package/dist/context/NotificationContext.js +130 -0
- package/dist/context/index.d.ts +2 -0
- package/dist/context/index.js +2 -0
- package/dist/hooks/index.d.ts +17 -0
- package/dist/hooks/index.js +17 -0
- package/dist/hooks/useAnimation.d.ts +46 -0
- package/dist/hooks/useAnimation.js +59 -0
- package/dist/hooks/useClientNotifications.d.ts +53 -0
- package/dist/hooks/useClientNotifications.js +190 -0
- package/dist/hooks/useColumnSummary.d.ts +38 -0
- package/dist/hooks/useColumnSummary.js +54 -0
- package/dist/hooks/useCrudShortcuts.d.ts +39 -0
- package/dist/hooks/useCrudShortcuts.js +96 -0
- package/dist/hooks/useDensityMode.d.ts +48 -0
- package/dist/hooks/useDensityMode.js +60 -0
- package/dist/hooks/useETagCache.d.ts +75 -0
- package/dist/hooks/useETagCache.js +306 -0
- package/dist/hooks/useFocusManagement.d.ts +89 -0
- package/dist/hooks/useFocusManagement.js +188 -0
- package/dist/hooks/useFocusTrap.d.ts +41 -0
- package/dist/hooks/useFocusTrap.js +103 -0
- package/dist/hooks/useKeyboardShortcuts.d.ts +49 -0
- package/dist/hooks/useKeyboardShortcuts.js +86 -0
- package/dist/hooks/useOffline.d.ts +96 -0
- package/dist/hooks/useOffline.js +185 -0
- package/dist/hooks/usePageTransition.d.ts +60 -0
- package/dist/hooks/usePageTransition.js +111 -0
- package/dist/hooks/usePerformance.d.ts +87 -0
- package/dist/hooks/usePerformance.js +165 -0
- package/dist/hooks/usePerformanceBudget.d.ts +78 -0
- package/dist/hooks/usePerformanceBudget.js +209 -0
- package/dist/hooks/useReducedMotion.d.ts +28 -0
- package/dist/hooks/useReducedMotion.js +48 -0
- package/dist/hooks/useSchemaPersistence.d.ts +87 -0
- package/dist/hooks/useSchemaPersistence.js +173 -0
- package/dist/hooks/useViewSharing.d.ts +57 -0
- package/dist/hooks/useViewSharing.js +74 -0
- package/dist/hooks/useViewTransition.d.ts +54 -0
- package/dist/hooks/useViewTransition.js +83 -0
- package/package.json +7 -7
- package/src/SchemaRenderer.tsx +122 -13
- package/src/__tests__/SchemaRenderer.aria.test.tsx +118 -0
- package/src/__tests__/SchemaRenderer.errorBoundary.test.tsx +114 -0
- package/src/__tests__/SchemaRendererProvider.smoke.test.tsx +132 -0
- package/src/__tests__/useOffline.test.ts +198 -0
- package/src/__tests__/usePageTransition.test.ts +214 -0
- package/src/__tests__/usePerformance.test.ts +137 -0
- package/src/__tests__/view-switching.test.tsx +456 -0
- package/src/context/DndContext.tsx +260 -0
- package/src/context/NotificationContext.tsx +280 -0
- package/src/context/__tests__/DndContext.test.tsx +150 -0
- package/src/context/__tests__/NotificationContext.test.tsx +176 -0
- package/src/context/index.ts +2 -0
- package/src/hooks/__tests__/useAnimation.test.ts +82 -0
- package/src/hooks/__tests__/useColumnSummary.test.ts +93 -0
- package/src/hooks/__tests__/useDensityMode.test.ts +81 -0
- package/src/hooks/__tests__/useETagCache.test.ts +153 -0
- package/src/hooks/__tests__/useFocusTrap.test.ts +47 -0
- package/src/hooks/__tests__/useKeyboardShortcuts.test.ts +144 -0
- package/src/hooks/__tests__/usePerformanceBudget.test.ts +157 -0
- package/src/hooks/__tests__/useSchemaPersistence.test.ts +220 -0
- package/src/hooks/__tests__/useViewSharing.test.ts +121 -0
- package/src/hooks/__tests__/useViewTransition.test.ts +61 -0
- package/src/hooks/index.ts +17 -1
- package/src/hooks/useAnimation.ts +117 -0
- package/src/hooks/useClientNotifications.ts +251 -0
- package/src/hooks/useColumnSummary.ts +87 -0
- package/src/hooks/useCrudShortcuts.ts +126 -0
- package/src/hooks/useDensityMode.ts +102 -0
- package/src/hooks/useETagCache.ts +404 -0
- package/src/hooks/useFocusManagement.ts +248 -0
- package/src/hooks/useFocusTrap.ts +138 -0
- package/src/hooks/useKeyboardShortcuts.ts +119 -0
- package/src/hooks/useOffline.ts +301 -0
- package/src/hooks/usePageTransition.ts +187 -0
- package/src/hooks/usePerformance.ts +246 -0
- package/src/hooks/usePerformanceBudget.ts +313 -0
- package/src/hooks/useReducedMotion.ts +53 -0
- package/src/hooks/useSchemaPersistence.ts +222 -0
- package/src/hooks/useViewSharing.ts +140 -0
- package/src/hooks/useViewTransition.ts +144 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,209 @@
|
|
|
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
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Defaults
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
const DEFAULTS = {
|
|
13
|
+
maxLCP: 600,
|
|
14
|
+
maxFCP: 300,
|
|
15
|
+
maxRenderTime: 16,
|
|
16
|
+
maxMemoryMB: 128,
|
|
17
|
+
warnOnViolation: true,
|
|
18
|
+
};
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
// Helpers (same getFCP/getLCP pattern as usePerformance.ts)
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
function getFCP() {
|
|
23
|
+
if (typeof performance === 'undefined' || !performance.getEntriesByType)
|
|
24
|
+
return null;
|
|
25
|
+
try {
|
|
26
|
+
const entries = performance.getEntriesByType('paint');
|
|
27
|
+
const entry = entries.find((e) => e.name === 'first-contentful-paint');
|
|
28
|
+
return entry ? Math.round(entry.startTime) : null;
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function getLCP() {
|
|
35
|
+
if (typeof performance === 'undefined' || !performance.getEntriesByType)
|
|
36
|
+
return null;
|
|
37
|
+
try {
|
|
38
|
+
const entries = performance.getEntriesByType('largest-contentful-paint');
|
|
39
|
+
const entry = entries.length > 0 ? entries[entries.length - 1] : undefined;
|
|
40
|
+
return entry ? Math.round(entry.startTime) : null;
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** Chrome-only memory API (guarded). */
|
|
47
|
+
function getMemoryMB() {
|
|
48
|
+
try {
|
|
49
|
+
const mem = performance.memory;
|
|
50
|
+
if (mem?.usedJSHeapSize != null) {
|
|
51
|
+
return Math.round((mem.usedJSHeapSize / (1024 * 1024)) * 100) / 100;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
/* not available */
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
function buildViolationKey(metric) {
|
|
60
|
+
return metric;
|
|
61
|
+
}
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
// usePerformanceBudget hook
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
/**
|
|
66
|
+
* Hook for performance budget enforcement aligned with ROADMAP Q3 3.3.
|
|
67
|
+
*
|
|
68
|
+
* Monitors Web Vitals (LCP, FCP), memory usage, and component render times
|
|
69
|
+
* against configurable thresholds. Violations are recorded, deduplicated,
|
|
70
|
+
* and optionally logged to the console in development mode.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```tsx
|
|
74
|
+
* function App() {
|
|
75
|
+
* const budget = usePerformanceBudget({
|
|
76
|
+
* maxLCP: 500,
|
|
77
|
+
* maxRenderTime: 16,
|
|
78
|
+
* onViolation: (v) => analytics.track('perf_violation', v),
|
|
79
|
+
* });
|
|
80
|
+
*
|
|
81
|
+
* if (!budget.isWithinBudget) {
|
|
82
|
+
* console.warn('Performance budget exceeded:', budget.violations);
|
|
83
|
+
* }
|
|
84
|
+
*
|
|
85
|
+
* return <Dashboard />;
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export function usePerformanceBudget(userBudget = {}) {
|
|
90
|
+
const budget = useMemo(() => ({
|
|
91
|
+
maxLCP: userBudget.maxLCP ?? DEFAULTS.maxLCP,
|
|
92
|
+
maxFCP: userBudget.maxFCP ?? DEFAULTS.maxFCP,
|
|
93
|
+
maxRenderTime: userBudget.maxRenderTime ?? DEFAULTS.maxRenderTime,
|
|
94
|
+
maxMemoryMB: userBudget.maxMemoryMB ?? DEFAULTS.maxMemoryMB,
|
|
95
|
+
warnOnViolation: userBudget.warnOnViolation ?? DEFAULTS.warnOnViolation,
|
|
96
|
+
}), [
|
|
97
|
+
userBudget.maxLCP,
|
|
98
|
+
userBudget.maxFCP,
|
|
99
|
+
userBudget.maxRenderTime,
|
|
100
|
+
userBudget.maxMemoryMB,
|
|
101
|
+
userBudget.warnOnViolation,
|
|
102
|
+
]);
|
|
103
|
+
const onViolationRef = useRef(userBudget.onViolation);
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
onViolationRef.current = userBudget.onViolation;
|
|
106
|
+
}, [userBudget.onViolation]);
|
|
107
|
+
const [violations, setViolations] = useState([]);
|
|
108
|
+
const seenRef = useRef(new Set());
|
|
109
|
+
const [snapshot, setSnapshot] = useState({ lcp: null, fcp: null, memoryMB: null });
|
|
110
|
+
// -------------------------------------------------------------------
|
|
111
|
+
// Violation recording with deduplication
|
|
112
|
+
// -------------------------------------------------------------------
|
|
113
|
+
const recordViolation = useCallback((metric, budgetValue, actual) => {
|
|
114
|
+
const key = buildViolationKey(metric);
|
|
115
|
+
if (seenRef.current.has(key))
|
|
116
|
+
return;
|
|
117
|
+
seenRef.current.add(key);
|
|
118
|
+
const violation = {
|
|
119
|
+
metric,
|
|
120
|
+
budget: budgetValue,
|
|
121
|
+
actual: Math.round(actual * 100) / 100,
|
|
122
|
+
timestamp: Date.now(),
|
|
123
|
+
};
|
|
124
|
+
setViolations((prev) => [...prev, violation]);
|
|
125
|
+
if (budget.warnOnViolation && process.env.NODE_ENV !== 'production') {
|
|
126
|
+
console.warn(`[ObjectUI] Performance budget violated: ${metric} = ${violation.actual} (budget: ${budgetValue})`);
|
|
127
|
+
}
|
|
128
|
+
onViolationRef.current?.(violation);
|
|
129
|
+
}, [budget.warnOnViolation]);
|
|
130
|
+
// -------------------------------------------------------------------
|
|
131
|
+
// Collect Web Vitals & memory on mount
|
|
132
|
+
// -------------------------------------------------------------------
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
const timer = setTimeout(() => {
|
|
135
|
+
const fcp = getFCP();
|
|
136
|
+
const lcp = getLCP();
|
|
137
|
+
const memoryMB = getMemoryMB();
|
|
138
|
+
setSnapshot({ lcp, fcp, memoryMB });
|
|
139
|
+
if (fcp != null && fcp > budget.maxFCP) {
|
|
140
|
+
recordViolation('fcp', budget.maxFCP, fcp);
|
|
141
|
+
}
|
|
142
|
+
if (lcp != null && lcp > budget.maxLCP) {
|
|
143
|
+
recordViolation('lcp', budget.maxLCP, lcp);
|
|
144
|
+
}
|
|
145
|
+
if (memoryMB != null && memoryMB > budget.maxMemoryMB) {
|
|
146
|
+
recordViolation('memory', budget.maxMemoryMB, memoryMB);
|
|
147
|
+
}
|
|
148
|
+
}, 0);
|
|
149
|
+
return () => clearTimeout(timer);
|
|
150
|
+
}, [budget.maxFCP, budget.maxLCP, budget.maxMemoryMB, recordViolation]);
|
|
151
|
+
// -------------------------------------------------------------------
|
|
152
|
+
// Periodic memory monitoring
|
|
153
|
+
// -------------------------------------------------------------------
|
|
154
|
+
useEffect(() => {
|
|
155
|
+
// Only run when memory API is available
|
|
156
|
+
if (getMemoryMB() == null)
|
|
157
|
+
return;
|
|
158
|
+
const interval = setInterval(() => {
|
|
159
|
+
const memoryMB = getMemoryMB();
|
|
160
|
+
if (memoryMB != null) {
|
|
161
|
+
setSnapshot((prev) => ({ ...prev, memoryMB }));
|
|
162
|
+
if (memoryMB > budget.maxMemoryMB) {
|
|
163
|
+
recordViolation('memory', budget.maxMemoryMB, memoryMB);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}, 10000);
|
|
167
|
+
return () => clearInterval(interval);
|
|
168
|
+
}, [budget.maxMemoryMB, recordViolation]);
|
|
169
|
+
// -------------------------------------------------------------------
|
|
170
|
+
// Public API
|
|
171
|
+
// -------------------------------------------------------------------
|
|
172
|
+
const checkMetric = useCallback((metric, value) => {
|
|
173
|
+
let threshold;
|
|
174
|
+
switch (metric) {
|
|
175
|
+
case 'lcp':
|
|
176
|
+
threshold = budget.maxLCP;
|
|
177
|
+
break;
|
|
178
|
+
case 'fcp':
|
|
179
|
+
threshold = budget.maxFCP;
|
|
180
|
+
break;
|
|
181
|
+
case 'renderTime':
|
|
182
|
+
threshold = budget.maxRenderTime;
|
|
183
|
+
break;
|
|
184
|
+
case 'memory':
|
|
185
|
+
threshold = budget.maxMemoryMB;
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
const withinBudget = value <= threshold;
|
|
189
|
+
if (!withinBudget) {
|
|
190
|
+
recordViolation(metric, threshold, value);
|
|
191
|
+
}
|
|
192
|
+
return withinBudget;
|
|
193
|
+
}, [budget.maxLCP, budget.maxFCP, budget.maxRenderTime, budget.maxMemoryMB, recordViolation]);
|
|
194
|
+
const trackRender = useCallback((durationMs) => {
|
|
195
|
+
return checkMetric('renderTime', durationMs);
|
|
196
|
+
}, [checkMetric]);
|
|
197
|
+
const clearViolations = useCallback(() => {
|
|
198
|
+
setViolations([]);
|
|
199
|
+
seenRef.current.clear();
|
|
200
|
+
}, []);
|
|
201
|
+
return useMemo(() => ({
|
|
202
|
+
violations,
|
|
203
|
+
isWithinBudget: violations.length === 0,
|
|
204
|
+
checkMetric,
|
|
205
|
+
trackRender,
|
|
206
|
+
clearViolations,
|
|
207
|
+
snapshot,
|
|
208
|
+
}), [violations, checkMetric, trackRender, clearViolations, snapshot]);
|
|
209
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
* Hook that detects the user's motion preference.
|
|
10
|
+
* Implements MotionConfigSchema from @objectstack/spec v2.0.7.
|
|
11
|
+
*
|
|
12
|
+
* Respects the `prefers-reduced-motion` media query to provide
|
|
13
|
+
* accessible animation experiences.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* function AnimatedCard() {
|
|
18
|
+
* const prefersReducedMotion = useReducedMotion();
|
|
19
|
+
*
|
|
20
|
+
* return (
|
|
21
|
+
* <div className={prefersReducedMotion ? '' : 'animate-fade-in'}>
|
|
22
|
+
* Content
|
|
23
|
+
* </div>
|
|
24
|
+
* );
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function useReducedMotion(): boolean;
|
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
import { useState, useEffect } from 'react';
|
|
9
|
+
/**
|
|
10
|
+
* Hook that detects the user's motion preference.
|
|
11
|
+
* Implements MotionConfigSchema from @objectstack/spec v2.0.7.
|
|
12
|
+
*
|
|
13
|
+
* Respects the `prefers-reduced-motion` media query to provide
|
|
14
|
+
* accessible animation experiences.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* function AnimatedCard() {
|
|
19
|
+
* const prefersReducedMotion = useReducedMotion();
|
|
20
|
+
*
|
|
21
|
+
* return (
|
|
22
|
+
* <div className={prefersReducedMotion ? '' : 'animate-fade-in'}>
|
|
23
|
+
* Content
|
|
24
|
+
* </div>
|
|
25
|
+
* );
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function useReducedMotion() {
|
|
30
|
+
const [prefersReducedMotion, setPrefersReducedMotion] = useState(() => {
|
|
31
|
+
if (typeof window === 'undefined')
|
|
32
|
+
return false;
|
|
33
|
+
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
34
|
+
});
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (typeof window === 'undefined')
|
|
37
|
+
return;
|
|
38
|
+
const mediaQuery = window.matchMedia('(prefers-reduced-motion: reduce)');
|
|
39
|
+
const handleChange = (event) => {
|
|
40
|
+
setPrefersReducedMotion(event.matches);
|
|
41
|
+
};
|
|
42
|
+
mediaQuery.addEventListener('change', handleChange);
|
|
43
|
+
return () => {
|
|
44
|
+
mediaQuery.removeEventListener('change', handleChange);
|
|
45
|
+
};
|
|
46
|
+
}, []);
|
|
47
|
+
return prefersReducedMotion;
|
|
48
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
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
|
+
* Persistence adapter interface for schema save/load operations.
|
|
10
|
+
* Implement this to connect to any backend (REST API, GraphQL, localStorage, etc.).
|
|
11
|
+
*/
|
|
12
|
+
export interface SchemaPersistenceAdapter {
|
|
13
|
+
/** Save a schema to the backend. Returns the saved schema ID. */
|
|
14
|
+
save: (id: string, schema: Record<string, unknown>) => Promise<string>;
|
|
15
|
+
/** Load a schema from the backend by ID. */
|
|
16
|
+
load: (id: string) => Promise<Record<string, unknown> | null>;
|
|
17
|
+
/** List all available schemas. */
|
|
18
|
+
list?: () => Promise<Array<{
|
|
19
|
+
id: string;
|
|
20
|
+
name?: string;
|
|
21
|
+
updatedAt?: string;
|
|
22
|
+
}>>;
|
|
23
|
+
/** Delete a schema from the backend. */
|
|
24
|
+
delete?: (id: string) => Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
export interface SchemaPersistenceState {
|
|
27
|
+
/** Whether a save/load operation is in progress */
|
|
28
|
+
loading: boolean;
|
|
29
|
+
/** The last error from a save/load operation */
|
|
30
|
+
error: Error | null;
|
|
31
|
+
/** Whether the current schema has unsaved changes */
|
|
32
|
+
isDirty: boolean;
|
|
33
|
+
/** The last saved timestamp */
|
|
34
|
+
lastSavedAt: Date | null;
|
|
35
|
+
}
|
|
36
|
+
export interface SchemaPersistenceResult extends SchemaPersistenceState {
|
|
37
|
+
/** Save the current schema */
|
|
38
|
+
save: (id: string, schema: Record<string, unknown>) => Promise<string | null>;
|
|
39
|
+
/** Load a schema by ID */
|
|
40
|
+
load: (id: string) => Promise<Record<string, unknown> | null>;
|
|
41
|
+
/** List available schemas */
|
|
42
|
+
list: () => Promise<Array<{
|
|
43
|
+
id: string;
|
|
44
|
+
name?: string;
|
|
45
|
+
updatedAt?: string;
|
|
46
|
+
}>>;
|
|
47
|
+
/** Delete a schema */
|
|
48
|
+
remove: (id: string) => Promise<boolean>;
|
|
49
|
+
/** Mark the current schema as dirty (has unsaved changes) */
|
|
50
|
+
markDirty: () => void;
|
|
51
|
+
/** Clear the error state */
|
|
52
|
+
clearError: () => void;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Default localStorage adapter for schema persistence.
|
|
56
|
+
* Useful for development and demos.
|
|
57
|
+
*/
|
|
58
|
+
export declare function createLocalStorageAdapter(prefix?: string): SchemaPersistenceAdapter;
|
|
59
|
+
/**
|
|
60
|
+
* Hook for persisting designer schemas (save/load/list/delete).
|
|
61
|
+
* Implements schema persistence for @object-ui/plugin-designer.
|
|
62
|
+
*
|
|
63
|
+
* Accepts a pluggable adapter for connecting to any backend.
|
|
64
|
+
* Falls back to a localStorage adapter for development use.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```tsx
|
|
68
|
+
* // With default localStorage adapter
|
|
69
|
+
* const persistence = useSchemaPersistence();
|
|
70
|
+
*
|
|
71
|
+
* // Save current design
|
|
72
|
+
* await persistence.save('my-page', pageSchema);
|
|
73
|
+
*
|
|
74
|
+
* // Load a design
|
|
75
|
+
* const schema = await persistence.load('my-page');
|
|
76
|
+
*
|
|
77
|
+
* // With custom API adapter
|
|
78
|
+
* const apiAdapter: SchemaPersistenceAdapter = {
|
|
79
|
+
* save: (id, schema) => fetch(`/api/schemas/${id}`, { method: 'PUT', body: JSON.stringify(schema) }).then(r => r.json()),
|
|
80
|
+
* load: (id) => fetch(`/api/schemas/${id}`).then(r => r.json()),
|
|
81
|
+
* list: () => fetch('/api/schemas').then(r => r.json()),
|
|
82
|
+
* delete: (id) => fetch(`/api/schemas/${id}`, { method: 'DELETE' }).then(() => {}),
|
|
83
|
+
* };
|
|
84
|
+
* const persistence = useSchemaPersistence(apiAdapter);
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare function useSchemaPersistence(adapter?: SchemaPersistenceAdapter): SchemaPersistenceResult;
|
|
@@ -0,0 +1,173 @@
|
|
|
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
|
+
import { useState, useCallback, useRef, useMemo } from 'react';
|
|
9
|
+
/**
|
|
10
|
+
* Default localStorage adapter for schema persistence.
|
|
11
|
+
* Useful for development and demos.
|
|
12
|
+
*/
|
|
13
|
+
export function createLocalStorageAdapter(prefix = 'objectui-schema') {
|
|
14
|
+
return {
|
|
15
|
+
async save(id, schema) {
|
|
16
|
+
const key = `${prefix}:${id}`;
|
|
17
|
+
const entry = { schema, updatedAt: new Date().toISOString() };
|
|
18
|
+
localStorage.setItem(key, JSON.stringify(entry));
|
|
19
|
+
// Update the index
|
|
20
|
+
const indexKey = `${prefix}:__index__`;
|
|
21
|
+
const index = JSON.parse(localStorage.getItem(indexKey) || '[]');
|
|
22
|
+
if (!index.includes(id)) {
|
|
23
|
+
index.push(id);
|
|
24
|
+
localStorage.setItem(indexKey, JSON.stringify(index));
|
|
25
|
+
}
|
|
26
|
+
return id;
|
|
27
|
+
},
|
|
28
|
+
async load(id) {
|
|
29
|
+
const key = `${prefix}:${id}`;
|
|
30
|
+
const raw = localStorage.getItem(key);
|
|
31
|
+
if (!raw)
|
|
32
|
+
return null;
|
|
33
|
+
const entry = JSON.parse(raw);
|
|
34
|
+
return entry.schema ?? null;
|
|
35
|
+
},
|
|
36
|
+
async list() {
|
|
37
|
+
const indexKey = `${prefix}:__index__`;
|
|
38
|
+
const index = JSON.parse(localStorage.getItem(indexKey) || '[]');
|
|
39
|
+
return index.map((id) => {
|
|
40
|
+
const raw = localStorage.getItem(`${prefix}:${id}`);
|
|
41
|
+
const entry = raw ? JSON.parse(raw) : {};
|
|
42
|
+
return { id, name: id, updatedAt: entry.updatedAt };
|
|
43
|
+
});
|
|
44
|
+
},
|
|
45
|
+
async delete(id) {
|
|
46
|
+
const key = `${prefix}:${id}`;
|
|
47
|
+
localStorage.removeItem(key);
|
|
48
|
+
const indexKey = `${prefix}:__index__`;
|
|
49
|
+
const index = JSON.parse(localStorage.getItem(indexKey) || '[]');
|
|
50
|
+
localStorage.setItem(indexKey, JSON.stringify(index.filter((i) => i !== id)));
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Hook for persisting designer schemas (save/load/list/delete).
|
|
56
|
+
* Implements schema persistence for @object-ui/plugin-designer.
|
|
57
|
+
*
|
|
58
|
+
* Accepts a pluggable adapter for connecting to any backend.
|
|
59
|
+
* Falls back to a localStorage adapter for development use.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```tsx
|
|
63
|
+
* // With default localStorage adapter
|
|
64
|
+
* const persistence = useSchemaPersistence();
|
|
65
|
+
*
|
|
66
|
+
* // Save current design
|
|
67
|
+
* await persistence.save('my-page', pageSchema);
|
|
68
|
+
*
|
|
69
|
+
* // Load a design
|
|
70
|
+
* const schema = await persistence.load('my-page');
|
|
71
|
+
*
|
|
72
|
+
* // With custom API adapter
|
|
73
|
+
* const apiAdapter: SchemaPersistenceAdapter = {
|
|
74
|
+
* save: (id, schema) => fetch(`/api/schemas/${id}`, { method: 'PUT', body: JSON.stringify(schema) }).then(r => r.json()),
|
|
75
|
+
* load: (id) => fetch(`/api/schemas/${id}`).then(r => r.json()),
|
|
76
|
+
* list: () => fetch('/api/schemas').then(r => r.json()),
|
|
77
|
+
* delete: (id) => fetch(`/api/schemas/${id}`, { method: 'DELETE' }).then(() => {}),
|
|
78
|
+
* };
|
|
79
|
+
* const persistence = useSchemaPersistence(apiAdapter);
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export function useSchemaPersistence(adapter) {
|
|
83
|
+
const defaultAdapter = useRef(createLocalStorageAdapter());
|
|
84
|
+
const adapterRef = useRef(adapter ?? defaultAdapter.current);
|
|
85
|
+
// Keep the ref up to date if the adapter prop changes
|
|
86
|
+
adapterRef.current = adapter ?? defaultAdapter.current;
|
|
87
|
+
const [loading, setLoading] = useState(false);
|
|
88
|
+
const [error, setError] = useState(null);
|
|
89
|
+
const [isDirty, setIsDirty] = useState(false);
|
|
90
|
+
const [lastSavedAt, setLastSavedAt] = useState(null);
|
|
91
|
+
const save = useCallback(async (id, schema) => {
|
|
92
|
+
setLoading(true);
|
|
93
|
+
setError(null);
|
|
94
|
+
try {
|
|
95
|
+
const result = await adapterRef.current.save(id, schema);
|
|
96
|
+
setIsDirty(false);
|
|
97
|
+
setLastSavedAt(new Date());
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
catch (err) {
|
|
101
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
finally {
|
|
105
|
+
setLoading(false);
|
|
106
|
+
}
|
|
107
|
+
}, []);
|
|
108
|
+
const load = useCallback(async (id) => {
|
|
109
|
+
setLoading(true);
|
|
110
|
+
setError(null);
|
|
111
|
+
try {
|
|
112
|
+
const result = await adapterRef.current.load(id);
|
|
113
|
+
setIsDirty(false);
|
|
114
|
+
return result;
|
|
115
|
+
}
|
|
116
|
+
catch (err) {
|
|
117
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
finally {
|
|
121
|
+
setLoading(false);
|
|
122
|
+
}
|
|
123
|
+
}, []);
|
|
124
|
+
const list = useCallback(async () => {
|
|
125
|
+
setLoading(true);
|
|
126
|
+
setError(null);
|
|
127
|
+
try {
|
|
128
|
+
if (adapterRef.current.list) {
|
|
129
|
+
return await adapterRef.current.list();
|
|
130
|
+
}
|
|
131
|
+
return [];
|
|
132
|
+
}
|
|
133
|
+
catch (err) {
|
|
134
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
135
|
+
return [];
|
|
136
|
+
}
|
|
137
|
+
finally {
|
|
138
|
+
setLoading(false);
|
|
139
|
+
}
|
|
140
|
+
}, []);
|
|
141
|
+
const remove = useCallback(async (id) => {
|
|
142
|
+
setLoading(true);
|
|
143
|
+
setError(null);
|
|
144
|
+
try {
|
|
145
|
+
if (adapterRef.current.delete) {
|
|
146
|
+
await adapterRef.current.delete(id);
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
catch (err) {
|
|
152
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
setLoading(false);
|
|
157
|
+
}
|
|
158
|
+
}, []);
|
|
159
|
+
const markDirty = useCallback(() => setIsDirty(true), []);
|
|
160
|
+
const clearError = useCallback(() => setError(null), []);
|
|
161
|
+
return useMemo(() => ({
|
|
162
|
+
loading,
|
|
163
|
+
error,
|
|
164
|
+
isDirty,
|
|
165
|
+
lastSavedAt,
|
|
166
|
+
save,
|
|
167
|
+
load,
|
|
168
|
+
list,
|
|
169
|
+
remove,
|
|
170
|
+
markDirty,
|
|
171
|
+
clearError,
|
|
172
|
+
}), [loading, error, isDirty, lastSavedAt, save, load, list, remove, markDirty, clearError]);
|
|
173
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
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
|
+
/** View sharing visibility aligned with ViewSharingSchema */
|
|
9
|
+
export type ViewVisibility = 'private' | 'team' | 'organization' | 'public';
|
|
10
|
+
/** A saved view configuration */
|
|
11
|
+
export interface SavedView {
|
|
12
|
+
id: string;
|
|
13
|
+
name: string;
|
|
14
|
+
/** The view configuration (filters, sort, columns, etc.) */
|
|
15
|
+
config: Record<string, unknown>;
|
|
16
|
+
/** Who can see this view */
|
|
17
|
+
visibility: ViewVisibility;
|
|
18
|
+
/** Who created this view */
|
|
19
|
+
createdBy?: string;
|
|
20
|
+
/** Whether this is the default view */
|
|
21
|
+
isDefault?: boolean;
|
|
22
|
+
/** When the view was last modified */
|
|
23
|
+
updatedAt: Date;
|
|
24
|
+
}
|
|
25
|
+
export interface ViewSharingResult {
|
|
26
|
+
/** All available views */
|
|
27
|
+
views: SavedView[];
|
|
28
|
+
/** Currently active view */
|
|
29
|
+
activeView: SavedView | null;
|
|
30
|
+
/** Save a new view or update existing */
|
|
31
|
+
saveView: (view: Omit<SavedView, 'id' | 'updatedAt'>) => string;
|
|
32
|
+
/** Set the active view */
|
|
33
|
+
setActiveView: (viewId: string) => void;
|
|
34
|
+
/** Delete a view */
|
|
35
|
+
deleteView: (viewId: string) => void;
|
|
36
|
+
/** Duplicate a view */
|
|
37
|
+
duplicateView: (viewId: string, newName: string) => string;
|
|
38
|
+
/** Update view visibility */
|
|
39
|
+
setVisibility: (viewId: string, visibility: ViewVisibility) => void;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Hook for managing shared/personal view configurations.
|
|
43
|
+
* Implements ViewSharingSchema from @objectstack/spec v2.0.7.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```tsx
|
|
47
|
+
* const viewSharing = useViewSharing(initialViews);
|
|
48
|
+
*
|
|
49
|
+
* // Save current filters/sort as a named view
|
|
50
|
+
* viewSharing.saveView({
|
|
51
|
+
* name: 'My Open Tasks',
|
|
52
|
+
* config: { filters: currentFilters, sort: currentSort },
|
|
53
|
+
* visibility: 'private',
|
|
54
|
+
* });
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare function useViewSharing(initialViews?: SavedView[]): ViewSharingResult;
|
|
@@ -0,0 +1,74 @@
|
|
|
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
|
+
import { useState, useCallback, useMemo } from 'react';
|
|
9
|
+
let viewCounter = 0;
|
|
10
|
+
/**
|
|
11
|
+
* Hook for managing shared/personal view configurations.
|
|
12
|
+
* Implements ViewSharingSchema from @objectstack/spec v2.0.7.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```tsx
|
|
16
|
+
* const viewSharing = useViewSharing(initialViews);
|
|
17
|
+
*
|
|
18
|
+
* // Save current filters/sort as a named view
|
|
19
|
+
* viewSharing.saveView({
|
|
20
|
+
* name: 'My Open Tasks',
|
|
21
|
+
* config: { filters: currentFilters, sort: currentSort },
|
|
22
|
+
* visibility: 'private',
|
|
23
|
+
* });
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function useViewSharing(initialViews = []) {
|
|
27
|
+
const [views, setViews] = useState(initialViews);
|
|
28
|
+
const [activeViewId, setActiveViewId] = useState(initialViews.find((v) => v.isDefault)?.id ?? initialViews[0]?.id ?? null);
|
|
29
|
+
const activeView = useMemo(() => views.find((v) => v.id === activeViewId) ?? null, [views, activeViewId]);
|
|
30
|
+
const saveView = useCallback((input) => {
|
|
31
|
+
const id = `view-${++viewCounter}`;
|
|
32
|
+
const view = {
|
|
33
|
+
...input,
|
|
34
|
+
id,
|
|
35
|
+
updatedAt: new Date(),
|
|
36
|
+
};
|
|
37
|
+
setViews((prev) => [...prev, view]);
|
|
38
|
+
return id;
|
|
39
|
+
}, []);
|
|
40
|
+
const setActiveView = useCallback((viewId) => {
|
|
41
|
+
setActiveViewId(viewId);
|
|
42
|
+
}, []);
|
|
43
|
+
const deleteView = useCallback((viewId) => {
|
|
44
|
+
setViews((prev) => prev.filter((v) => v.id !== viewId));
|
|
45
|
+
setActiveViewId((current) => (current === viewId ? null : current));
|
|
46
|
+
}, []);
|
|
47
|
+
const duplicateView = useCallback((viewId, newName) => {
|
|
48
|
+
const original = views.find((v) => v.id === viewId);
|
|
49
|
+
if (!original)
|
|
50
|
+
return '';
|
|
51
|
+
const id = `view-${++viewCounter}`;
|
|
52
|
+
const duplicate = {
|
|
53
|
+
...original,
|
|
54
|
+
id,
|
|
55
|
+
name: newName,
|
|
56
|
+
isDefault: false,
|
|
57
|
+
updatedAt: new Date(),
|
|
58
|
+
};
|
|
59
|
+
setViews((prev) => [...prev, duplicate]);
|
|
60
|
+
return id;
|
|
61
|
+
}, [views]);
|
|
62
|
+
const setVisibility = useCallback((viewId, visibility) => {
|
|
63
|
+
setViews((prev) => prev.map((v) => v.id === viewId ? { ...v, visibility, updatedAt: new Date() } : v));
|
|
64
|
+
}, []);
|
|
65
|
+
return useMemo(() => ({
|
|
66
|
+
views,
|
|
67
|
+
activeView,
|
|
68
|
+
saveView,
|
|
69
|
+
setActiveView,
|
|
70
|
+
deleteView,
|
|
71
|
+
duplicateView,
|
|
72
|
+
setVisibility,
|
|
73
|
+
}), [views, activeView, saveView, setActiveView, deleteView, duplicateView, setVisibility]);
|
|
74
|
+
}
|