@object-ui/react 2.0.0 → 3.0.0
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 +19 -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,187 @@
|
|
|
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 { useReducedMotion } from './useReducedMotion';
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Types aligned with @objectstack/spec v2.0.7 PageTransitionSchema
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
/** Page transition type. */
|
|
17
|
+
export type PageTransitionType =
|
|
18
|
+
| 'none'
|
|
19
|
+
| 'fade'
|
|
20
|
+
| 'slide_up'
|
|
21
|
+
| 'slide_down'
|
|
22
|
+
| 'slide_left'
|
|
23
|
+
| 'slide_right'
|
|
24
|
+
| 'scale'
|
|
25
|
+
| 'rotate'
|
|
26
|
+
| 'flip';
|
|
27
|
+
|
|
28
|
+
/** Easing function for page transitions. */
|
|
29
|
+
export type PageTransitionEasing =
|
|
30
|
+
| 'linear'
|
|
31
|
+
| 'ease'
|
|
32
|
+
| 'ease_in'
|
|
33
|
+
| 'ease_out'
|
|
34
|
+
| 'ease_in_out'
|
|
35
|
+
| 'spring';
|
|
36
|
+
|
|
37
|
+
/** Page transition configuration aligned with PageTransitionSchema. */
|
|
38
|
+
export interface PageTransitionConfig {
|
|
39
|
+
/** Transition type. */
|
|
40
|
+
type?: PageTransitionType;
|
|
41
|
+
/** Duration in milliseconds. */
|
|
42
|
+
duration?: number;
|
|
43
|
+
/** Easing function. */
|
|
44
|
+
easing?: PageTransitionEasing;
|
|
45
|
+
/** Whether entering and exiting pages cross-fade. */
|
|
46
|
+
crossFade?: boolean;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Resolved CSS classes and styles for a page transition. */
|
|
50
|
+
export interface PageTransitionResult {
|
|
51
|
+
/** Tailwind CSS classes for the enter transition. */
|
|
52
|
+
enterClassName: string;
|
|
53
|
+
/** Tailwind CSS classes for the exit transition. */
|
|
54
|
+
exitClassName: string;
|
|
55
|
+
/** Inline styles for the enter transition. */
|
|
56
|
+
enterStyle: React.CSSProperties;
|
|
57
|
+
/** Inline styles for the exit transition. */
|
|
58
|
+
exitStyle: React.CSSProperties;
|
|
59
|
+
/** Whether the transition is active (not 'none' and motion not reduced). */
|
|
60
|
+
isActive: boolean;
|
|
61
|
+
/** The resolved transition type. */
|
|
62
|
+
type: PageTransitionType;
|
|
63
|
+
/** The resolved duration in ms. */
|
|
64
|
+
duration: number;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ---------------------------------------------------------------------------
|
|
68
|
+
// Constants
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
|
|
71
|
+
const EASING_CSS: Record<PageTransitionEasing, string> = {
|
|
72
|
+
linear: 'linear',
|
|
73
|
+
ease: 'ease',
|
|
74
|
+
ease_in: 'ease-in',
|
|
75
|
+
ease_out: 'ease-out',
|
|
76
|
+
ease_in_out: 'ease-in-out',
|
|
77
|
+
spring: 'cubic-bezier(0.34, 1.56, 0.64, 1)',
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const ENTER_CLASSES: Record<PageTransitionType, string> = {
|
|
81
|
+
none: '',
|
|
82
|
+
fade: 'animate-in fade-in',
|
|
83
|
+
slide_up: 'animate-in slide-in-from-bottom-4',
|
|
84
|
+
slide_down: 'animate-in slide-in-from-top-4',
|
|
85
|
+
slide_left: 'animate-in slide-in-from-right-4',
|
|
86
|
+
slide_right: 'animate-in slide-in-from-left-4',
|
|
87
|
+
scale: 'animate-in zoom-in-95',
|
|
88
|
+
rotate: 'animate-in spin-in-90',
|
|
89
|
+
flip: 'animate-in fade-in zoom-in-95',
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const EXIT_CLASSES: Record<PageTransitionType, string> = {
|
|
93
|
+
none: '',
|
|
94
|
+
fade: 'animate-out fade-out',
|
|
95
|
+
slide_up: 'animate-out slide-out-to-top-4',
|
|
96
|
+
slide_down: 'animate-out slide-out-to-bottom-4',
|
|
97
|
+
slide_left: 'animate-out slide-out-to-left-4',
|
|
98
|
+
slide_right: 'animate-out slide-out-to-right-4',
|
|
99
|
+
scale: 'animate-out zoom-out-95',
|
|
100
|
+
rotate: 'animate-out spin-out-90',
|
|
101
|
+
flip: 'animate-out fade-out zoom-out-95',
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const DEFAULT_DURATION = 300;
|
|
105
|
+
const DEFAULT_EASING: PageTransitionEasing = 'ease_in_out';
|
|
106
|
+
|
|
107
|
+
// ---------------------------------------------------------------------------
|
|
108
|
+
// usePageTransition hook
|
|
109
|
+
// ---------------------------------------------------------------------------
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Hook for page-level transition animations aligned with
|
|
113
|
+
* PageTransitionSchema from @objectstack/spec v2.0.7.
|
|
114
|
+
*
|
|
115
|
+
* Generates Tailwind CSS animate-in / animate-out classes and inline styles
|
|
116
|
+
* for enter/exit page transitions. Respects `prefers-reduced-motion`.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```tsx
|
|
120
|
+
* function PageWrapper({ children }: { children: React.ReactNode }) {
|
|
121
|
+
* const transition = usePageTransition({ type: 'fade', duration: 200 });
|
|
122
|
+
*
|
|
123
|
+
* return (
|
|
124
|
+
* <div className={transition.enterClassName} style={transition.enterStyle}>
|
|
125
|
+
* {children}
|
|
126
|
+
* </div>
|
|
127
|
+
* );
|
|
128
|
+
* }
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
export function usePageTransition(config: PageTransitionConfig = {}): PageTransitionResult {
|
|
132
|
+
const {
|
|
133
|
+
type = 'none',
|
|
134
|
+
duration = DEFAULT_DURATION,
|
|
135
|
+
easing = DEFAULT_EASING,
|
|
136
|
+
crossFade = false,
|
|
137
|
+
} = config;
|
|
138
|
+
|
|
139
|
+
const reducedMotion = useReducedMotion();
|
|
140
|
+
|
|
141
|
+
return useMemo(() => {
|
|
142
|
+
const isActive = type !== 'none' && !reducedMotion;
|
|
143
|
+
|
|
144
|
+
if (!isActive) {
|
|
145
|
+
return {
|
|
146
|
+
enterClassName: '',
|
|
147
|
+
exitClassName: '',
|
|
148
|
+
enterStyle: {},
|
|
149
|
+
exitStyle: {},
|
|
150
|
+
isActive: false,
|
|
151
|
+
type,
|
|
152
|
+
duration,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const enterClassName = ENTER_CLASSES[type] || '';
|
|
157
|
+
const exitClassName = EXIT_CLASSES[type] || '';
|
|
158
|
+
|
|
159
|
+
const baseStyle: React.CSSProperties = {
|
|
160
|
+
animationDuration: `${duration}ms`,
|
|
161
|
+
animationTimingFunction: EASING_CSS[easing] || EASING_CSS.ease_in_out,
|
|
162
|
+
animationFillMode: 'both',
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const enterStyle: React.CSSProperties = { ...baseStyle };
|
|
166
|
+
const exitStyle: React.CSSProperties = { ...baseStyle };
|
|
167
|
+
|
|
168
|
+
// When crossFade is enabled, both the entering and exiting pages
|
|
169
|
+
// should overlap and transition opacity simultaneously.
|
|
170
|
+
if (crossFade) {
|
|
171
|
+
enterStyle.position = 'absolute';
|
|
172
|
+
enterStyle.inset = '0';
|
|
173
|
+
exitStyle.position = 'absolute';
|
|
174
|
+
exitStyle.inset = '0';
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
enterClassName,
|
|
179
|
+
exitClassName,
|
|
180
|
+
enterStyle,
|
|
181
|
+
exitStyle,
|
|
182
|
+
isActive: true,
|
|
183
|
+
type,
|
|
184
|
+
duration,
|
|
185
|
+
};
|
|
186
|
+
}, [type, duration, easing, crossFade, reducedMotion]);
|
|
187
|
+
}
|
|
@@ -0,0 +1,246 @@
|
|
|
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 { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
10
|
+
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
// Types aligned with @objectstack/spec v2.0.7 PerformanceConfigSchema
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
|
|
15
|
+
/** Cache strategy for data fetching. */
|
|
16
|
+
export type CacheStrategyType =
|
|
17
|
+
| 'none'
|
|
18
|
+
| 'cache-first'
|
|
19
|
+
| 'network-first'
|
|
20
|
+
| 'stale-while-revalidate';
|
|
21
|
+
|
|
22
|
+
/** Virtual scroll configuration. */
|
|
23
|
+
export interface VirtualScrollConfig {
|
|
24
|
+
enabled?: boolean;
|
|
25
|
+
itemHeight?: number;
|
|
26
|
+
overscan?: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Performance configuration aligned with PerformanceConfigSchema. */
|
|
30
|
+
export interface PerformanceConfig {
|
|
31
|
+
/** Whether to lazy-load components/data. */
|
|
32
|
+
lazyLoad?: boolean;
|
|
33
|
+
/** Virtual scroll settings for large lists. */
|
|
34
|
+
virtualScroll?: VirtualScrollConfig;
|
|
35
|
+
/** Cache strategy for data fetching. */
|
|
36
|
+
cacheStrategy?: CacheStrategyType;
|
|
37
|
+
/** Whether to prefetch linked resources. */
|
|
38
|
+
prefetch?: boolean;
|
|
39
|
+
/** Default page size for paginated views. */
|
|
40
|
+
pageSize?: number;
|
|
41
|
+
/** Debounce interval in milliseconds for user input. */
|
|
42
|
+
debounceMs?: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Web Vitals metrics snapshot. */
|
|
46
|
+
export interface PerformanceMetrics {
|
|
47
|
+
/** Largest Contentful Paint (ms). */
|
|
48
|
+
lcp: number | null;
|
|
49
|
+
/** First Contentful Paint (ms). */
|
|
50
|
+
fcp: number | null;
|
|
51
|
+
/** Time to Interactive (ms). */
|
|
52
|
+
tti: number | null;
|
|
53
|
+
/** Total render count since mount. */
|
|
54
|
+
renderCount: number;
|
|
55
|
+
/** Last render duration (ms). */
|
|
56
|
+
lastRenderDuration: number | null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Result returned by the usePerformance hook. */
|
|
60
|
+
export interface PerformanceResult {
|
|
61
|
+
/** The resolved performance configuration (with defaults). */
|
|
62
|
+
config: Required<Omit<PerformanceConfig, 'virtualScroll'>> & {
|
|
63
|
+
virtualScroll: Required<VirtualScrollConfig>;
|
|
64
|
+
};
|
|
65
|
+
/** Current performance metrics. */
|
|
66
|
+
metrics: PerformanceMetrics;
|
|
67
|
+
/** Mark a rendering start (returns stop function). */
|
|
68
|
+
markRenderStart: () => () => void;
|
|
69
|
+
/** Create a debounced version of a callback. */
|
|
70
|
+
debounce: <T extends (...args: unknown[]) => void>(fn: T) => T;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ---------------------------------------------------------------------------
|
|
74
|
+
// Defaults
|
|
75
|
+
// ---------------------------------------------------------------------------
|
|
76
|
+
|
|
77
|
+
const DEFAULTS = {
|
|
78
|
+
lazyLoad: true,
|
|
79
|
+
cacheStrategy: 'stale-while-revalidate' as CacheStrategyType,
|
|
80
|
+
prefetch: false,
|
|
81
|
+
pageSize: 50,
|
|
82
|
+
debounceMs: 300,
|
|
83
|
+
virtualScroll: {
|
|
84
|
+
enabled: false,
|
|
85
|
+
itemHeight: 40,
|
|
86
|
+
overscan: 5,
|
|
87
|
+
},
|
|
88
|
+
} as const;
|
|
89
|
+
|
|
90
|
+
// ---------------------------------------------------------------------------
|
|
91
|
+
// Helpers
|
|
92
|
+
// ---------------------------------------------------------------------------
|
|
93
|
+
|
|
94
|
+
function getFCP(): number | null {
|
|
95
|
+
if (typeof performance === 'undefined' || !performance.getEntriesByType) return null;
|
|
96
|
+
try {
|
|
97
|
+
const entries = performance.getEntriesByType('paint');
|
|
98
|
+
const entry = entries.find((e) => e.name === 'first-contentful-paint');
|
|
99
|
+
return entry ? Math.round(entry.startTime) : null;
|
|
100
|
+
} catch {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function getLCP(): number | null {
|
|
106
|
+
if (typeof performance === 'undefined' || !performance.getEntriesByType) return null;
|
|
107
|
+
try {
|
|
108
|
+
const entries = performance.getEntriesByType('largest-contentful-paint');
|
|
109
|
+
const entry = entries.length > 0 ? entries[entries.length - 1] : undefined;
|
|
110
|
+
return entry ? Math.round(entry.startTime) : null;
|
|
111
|
+
} catch {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ---------------------------------------------------------------------------
|
|
117
|
+
// usePerformance hook
|
|
118
|
+
// ---------------------------------------------------------------------------
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Hook for performance monitoring and configuration aligned with
|
|
122
|
+
* PerformanceConfigSchema from @objectstack/spec v2.0.7.
|
|
123
|
+
*
|
|
124
|
+
* Provides resolved config values, Web Vitals metrics, and utility functions
|
|
125
|
+
* (debounce, render marking) for performance-aware components.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```tsx
|
|
129
|
+
* function DataGrid({ data }: { data: unknown[] }) {
|
|
130
|
+
* const perf = usePerformance({
|
|
131
|
+
* lazyLoad: true,
|
|
132
|
+
* virtualScroll: { enabled: true, itemHeight: 40 },
|
|
133
|
+
* debounceMs: 200,
|
|
134
|
+
* });
|
|
135
|
+
*
|
|
136
|
+
* const handleSearch = perf.debounce((query: string) => {
|
|
137
|
+
* // debounced search
|
|
138
|
+
* });
|
|
139
|
+
*
|
|
140
|
+
* return (
|
|
141
|
+
* <div>
|
|
142
|
+
* {perf.config.virtualScroll.enabled
|
|
143
|
+
* ? <VirtualList itemHeight={perf.config.virtualScroll.itemHeight} />
|
|
144
|
+
* : <NormalList />}
|
|
145
|
+
* </div>
|
|
146
|
+
* );
|
|
147
|
+
* }
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
export function usePerformance(userConfig: PerformanceConfig = {}): PerformanceResult {
|
|
151
|
+
const config = useMemo(
|
|
152
|
+
() => ({
|
|
153
|
+
lazyLoad: userConfig.lazyLoad ?? DEFAULTS.lazyLoad,
|
|
154
|
+
cacheStrategy: userConfig.cacheStrategy ?? DEFAULTS.cacheStrategy,
|
|
155
|
+
prefetch: userConfig.prefetch ?? DEFAULTS.prefetch,
|
|
156
|
+
pageSize: userConfig.pageSize ?? DEFAULTS.pageSize,
|
|
157
|
+
debounceMs: userConfig.debounceMs ?? DEFAULTS.debounceMs,
|
|
158
|
+
virtualScroll: {
|
|
159
|
+
enabled: userConfig.virtualScroll?.enabled ?? DEFAULTS.virtualScroll.enabled,
|
|
160
|
+
itemHeight: userConfig.virtualScroll?.itemHeight ?? DEFAULTS.virtualScroll.itemHeight,
|
|
161
|
+
overscan: userConfig.virtualScroll?.overscan ?? DEFAULTS.virtualScroll.overscan,
|
|
162
|
+
},
|
|
163
|
+
}),
|
|
164
|
+
[
|
|
165
|
+
userConfig.lazyLoad,
|
|
166
|
+
userConfig.cacheStrategy,
|
|
167
|
+
userConfig.prefetch,
|
|
168
|
+
userConfig.pageSize,
|
|
169
|
+
userConfig.debounceMs,
|
|
170
|
+
userConfig.virtualScroll?.enabled,
|
|
171
|
+
userConfig.virtualScroll?.itemHeight,
|
|
172
|
+
userConfig.virtualScroll?.overscan,
|
|
173
|
+
],
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
const [metrics, setMetrics] = useState<PerformanceMetrics>({
|
|
177
|
+
lcp: null,
|
|
178
|
+
fcp: null,
|
|
179
|
+
tti: null,
|
|
180
|
+
renderCount: 0,
|
|
181
|
+
lastRenderDuration: null,
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
// Collect paint metrics on mount
|
|
185
|
+
useEffect(() => {
|
|
186
|
+
// Defer metric collection to allow paint entries to populate
|
|
187
|
+
const timer = setTimeout(() => {
|
|
188
|
+
setMetrics((prev) => ({
|
|
189
|
+
...prev,
|
|
190
|
+
fcp: getFCP(),
|
|
191
|
+
lcp: getLCP(),
|
|
192
|
+
}));
|
|
193
|
+
}, 0);
|
|
194
|
+
return () => clearTimeout(timer);
|
|
195
|
+
}, []);
|
|
196
|
+
|
|
197
|
+
const markRenderStart = useCallback((): (() => void) => {
|
|
198
|
+
const start = typeof performance !== 'undefined' ? performance.now() : Date.now();
|
|
199
|
+
return () => {
|
|
200
|
+
const end = typeof performance !== 'undefined' ? performance.now() : Date.now();
|
|
201
|
+
const duration = Math.round((end - start) * 100) / 100;
|
|
202
|
+
setMetrics((prev) => ({ ...prev, lastRenderDuration: duration, renderCount: prev.renderCount + 1 }));
|
|
203
|
+
};
|
|
204
|
+
}, []);
|
|
205
|
+
|
|
206
|
+
const debounceMs = config.debounceMs;
|
|
207
|
+
const timersRef = useRef<Set<ReturnType<typeof setTimeout>>>(new Set());
|
|
208
|
+
|
|
209
|
+
const debounce = useCallback(
|
|
210
|
+
<T extends (...args: unknown[]) => void>(fn: T): T => {
|
|
211
|
+
let timer: ReturnType<typeof setTimeout> | null = null;
|
|
212
|
+
const debounced = (...args: unknown[]) => {
|
|
213
|
+
if (timer) {
|
|
214
|
+
clearTimeout(timer);
|
|
215
|
+
timersRef.current.delete(timer);
|
|
216
|
+
}
|
|
217
|
+
timer = setTimeout(() => {
|
|
218
|
+
fn(...args);
|
|
219
|
+
if (timer) timersRef.current.delete(timer);
|
|
220
|
+
}, debounceMs);
|
|
221
|
+
timersRef.current.add(timer);
|
|
222
|
+
};
|
|
223
|
+
return debounced as unknown as T;
|
|
224
|
+
},
|
|
225
|
+
[debounceMs],
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
// Cleanup all debounce timers
|
|
229
|
+
useEffect(() => {
|
|
230
|
+
const timers = timersRef.current;
|
|
231
|
+
return () => {
|
|
232
|
+
timers.forEach((t) => clearTimeout(t));
|
|
233
|
+
timers.clear();
|
|
234
|
+
};
|
|
235
|
+
}, []);
|
|
236
|
+
|
|
237
|
+
return useMemo(
|
|
238
|
+
() => ({
|
|
239
|
+
config,
|
|
240
|
+
metrics,
|
|
241
|
+
markRenderStart,
|
|
242
|
+
debounce,
|
|
243
|
+
}),
|
|
244
|
+
[config, metrics, markRenderStart, debounce],
|
|
245
|
+
);
|
|
246
|
+
}
|