@object-ui/react 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.
Files changed (109) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.md +28 -3
  3. package/dist/SchemaRenderer.js +2 -2
  4. package/dist/components/form/FieldFactory.js +5 -2
  5. package/dist/context/AppShellContext.d.ts +26 -0
  6. package/dist/context/AppShellContext.js +67 -0
  7. package/dist/context/index.d.ts +1 -7
  8. package/dist/context/index.js +1 -7
  9. package/dist/hooks/index.d.ts +1 -0
  10. package/dist/hooks/index.js +1 -0
  11. package/dist/hooks/useDataRefresh.d.ts +36 -0
  12. package/dist/hooks/useDataRefresh.js +50 -0
  13. package/dist/hooks/useDiscovery.d.ts +12 -0
  14. package/dist/hooks/useDiscovery.js +6 -0
  15. package/dist/hooks/usePerformanceBudget.js +1 -1
  16. package/package.json +32 -9
  17. package/.turbo/turbo-build.log +0 -4
  18. package/src/LazyPluginLoader.tsx +0 -174
  19. package/src/SchemaRenderer.tsx +0 -266
  20. package/src/__tests__/LazyPluginLoader.test.tsx +0 -176
  21. package/src/__tests__/SchemaRenderer.aria.test.tsx +0 -118
  22. package/src/__tests__/SchemaRenderer.debug.test.tsx +0 -69
  23. package/src/__tests__/SchemaRenderer.errorBoundary.test.tsx +0 -114
  24. package/src/__tests__/SchemaRenderer.expressions.test.tsx +0 -131
  25. package/src/__tests__/SchemaRendererProvider.smoke.test.tsx +0 -132
  26. package/src/__tests__/ThemeProvider.test.tsx +0 -451
  27. package/src/__tests__/useNavigationOverlay.test.ts +0 -578
  28. package/src/__tests__/useOffline.test.ts +0 -198
  29. package/src/__tests__/usePageTransition.test.ts +0 -214
  30. package/src/__tests__/usePageVariables.test.tsx +0 -334
  31. package/src/__tests__/usePerformance.test.ts +0 -137
  32. package/src/__tests__/useViewData.test.tsx +0 -422
  33. package/src/__tests__/view-switching.test.tsx +0 -456
  34. package/src/components/form/FieldFactory.tsx +0 -509
  35. package/src/components/form/FormRenderer.test.tsx +0 -744
  36. package/src/components/form/FormRenderer.tsx +0 -258
  37. package/src/components/form/README.md +0 -352
  38. package/src/components/form/index.ts +0 -12
  39. package/src/context/ActionContext.tsx +0 -213
  40. package/src/context/DndContext.tsx +0 -260
  41. package/src/context/NotificationContext.tsx +0 -280
  42. package/src/context/SchemaRendererContext.tsx +0 -48
  43. package/src/context/ThemeContext.tsx +0 -325
  44. package/src/context/__tests__/ActionContext.test.tsx +0 -174
  45. package/src/context/__tests__/DndContext.test.tsx +0 -150
  46. package/src/context/__tests__/NotificationContext.test.tsx +0 -176
  47. package/src/context/__tests__/useDataScope.test.tsx +0 -91
  48. package/src/context/index.ts +0 -13
  49. package/src/hooks/__tests__/useActionEngine.test.ts +0 -170
  50. package/src/hooks/__tests__/useActionRunner.test.ts +0 -153
  51. package/src/hooks/__tests__/useAnimation.test.ts +0 -82
  52. package/src/hooks/__tests__/useColumnSummary.test.ts +0 -93
  53. package/src/hooks/__tests__/useCrudShortcuts.test.ts +0 -85
  54. package/src/hooks/__tests__/useDebugMode.test.ts +0 -83
  55. package/src/hooks/__tests__/useDensityMode.test.ts +0 -81
  56. package/src/hooks/__tests__/useDiscovery.test.tsx +0 -149
  57. package/src/hooks/__tests__/useDynamicApp.test.ts +0 -161
  58. package/src/hooks/__tests__/useETagCache.test.ts +0 -153
  59. package/src/hooks/__tests__/useExpression.test.ts +0 -86
  60. package/src/hooks/__tests__/useFocusTrap.test.ts +0 -294
  61. package/src/hooks/__tests__/useKeyboardShortcuts.test.ts +0 -144
  62. package/src/hooks/__tests__/usePageTransition.test.ts +0 -158
  63. package/src/hooks/__tests__/usePerformanceBudget.test.ts +0 -157
  64. package/src/hooks/__tests__/useReducedMotion.test.ts +0 -104
  65. package/src/hooks/__tests__/useSchemaPersistence.test.ts +0 -220
  66. package/src/hooks/__tests__/useViewSharing.test.ts +0 -121
  67. package/src/hooks/__tests__/useViewTransition.test.ts +0 -61
  68. package/src/hooks/index.ts +0 -35
  69. package/src/hooks/useActionEngine.ts +0 -84
  70. package/src/hooks/useActionRunner.ts +0 -119
  71. package/src/hooks/useAnimation.ts +0 -117
  72. package/src/hooks/useClientNotifications.ts +0 -251
  73. package/src/hooks/useColumnSummary.ts +0 -87
  74. package/src/hooks/useCrudShortcuts.ts +0 -126
  75. package/src/hooks/useDebugMode.ts +0 -95
  76. package/src/hooks/useDensityMode.ts +0 -102
  77. package/src/hooks/useDiscovery.ts +0 -154
  78. package/src/hooks/useDynamicApp.ts +0 -142
  79. package/src/hooks/useETagCache.ts +0 -404
  80. package/src/hooks/useExpression.ts +0 -56
  81. package/src/hooks/useFocusManagement.ts +0 -248
  82. package/src/hooks/useFocusTrap.ts +0 -138
  83. package/src/hooks/useGlobalUndo.ts +0 -98
  84. package/src/hooks/useKeyboardShortcuts.ts +0 -119
  85. package/src/hooks/useNavigationOverlay.ts +0 -190
  86. package/src/hooks/useOffline.ts +0 -301
  87. package/src/hooks/usePageTransition.ts +0 -187
  88. package/src/hooks/usePageVariables.tsx +0 -157
  89. package/src/hooks/usePerformance.ts +0 -246
  90. package/src/hooks/usePerformanceBudget.ts +0 -313
  91. package/src/hooks/useReducedMotion.ts +0 -53
  92. package/src/hooks/useSchemaPersistence.ts +0 -222
  93. package/src/hooks/useViewData.ts +0 -201
  94. package/src/hooks/useViewSharing.ts +0 -140
  95. package/src/hooks/useViewTransition.ts +0 -144
  96. package/src/index.ts +0 -41
  97. package/src/spec-bridge/SpecBridge.ts +0 -64
  98. package/src/spec-bridge/__tests__/P1SpecBridge.test.ts +0 -855
  99. package/src/spec-bridge/__tests__/SpecBridge.test.ts +0 -550
  100. package/src/spec-bridge/bridges/dashboard.ts +0 -180
  101. package/src/spec-bridge/bridges/form-view.ts +0 -109
  102. package/src/spec-bridge/bridges/list-view.ts +0 -187
  103. package/src/spec-bridge/bridges/page.ts +0 -136
  104. package/src/spec-bridge/index.ts +0 -14
  105. package/src/spec-bridge/types.ts +0 -38
  106. package/src/utils/i18n.ts +0 -10
  107. package/tsconfig.json +0 -17
  108. package/tsconfig.tsbuildinfo +0 -1
  109. package/vitest.config.ts +0 -2
@@ -1,138 +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 { useEffect, useRef, useCallback } from 'react';
10
-
11
- export interface FocusTrapOptions {
12
- /** Whether the focus trap is currently active */
13
- enabled?: boolean;
14
- /** Whether to auto-focus the first focusable element on mount */
15
- autoFocus?: boolean;
16
- /** Whether to restore focus to the previously focused element on unmount */
17
- restoreFocus?: boolean;
18
- /** Whether to release the trap on Escape key */
19
- escapeDeactivates?: boolean;
20
- /** Callback when Escape is pressed (if escapeDeactivates is true) */
21
- onEscape?: () => void;
22
- /** Initial element to focus (CSS selector) */
23
- initialFocus?: string;
24
- }
25
-
26
- const FOCUSABLE_SELECTOR = [
27
- 'a[href]',
28
- 'button:not([disabled])',
29
- 'input:not([disabled])',
30
- 'select:not([disabled])',
31
- 'textarea:not([disabled])',
32
- '[tabindex]:not([tabindex="-1"])',
33
- ].join(', ');
34
-
35
- /**
36
- * Hook that traps focus within a container element.
37
- * Implements FocusTrapConfigSchema from @objectstack/spec v2.0.7.
38
- *
39
- * @example
40
- * ```tsx
41
- * function Modal({ onClose }: { onClose: () => void }) {
42
- * const trapRef = useFocusTrap({
43
- * enabled: true,
44
- * autoFocus: true,
45
- * restoreFocus: true,
46
- * escapeDeactivates: true,
47
- * onEscape: onClose,
48
- * });
49
- *
50
- * return <div ref={trapRef}>...</div>;
51
- * }
52
- * ```
53
- */
54
- export function useFocusTrap<T extends HTMLElement = HTMLDivElement>(
55
- options: FocusTrapOptions = {}
56
- ): React.RefObject<T | null> {
57
- const {
58
- enabled = true,
59
- autoFocus = true,
60
- restoreFocus = true,
61
- escapeDeactivates = false,
62
- onEscape,
63
- initialFocus,
64
- } = options;
65
-
66
- const containerRef = useRef<T | null>(null);
67
- const previousFocusRef = useRef<HTMLElement | null>(null);
68
-
69
- const getFocusableElements = useCallback(() => {
70
- if (!containerRef.current) return [];
71
- return Array.from(
72
- containerRef.current.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR)
73
- ).filter((el) => !el.hasAttribute('disabled') && el.tabIndex >= 0);
74
- }, []);
75
-
76
- useEffect(() => {
77
- if (!enabled || !containerRef.current) return;
78
-
79
- // Save previously focused element
80
- previousFocusRef.current = document.activeElement as HTMLElement;
81
-
82
- // Auto-focus
83
- if (autoFocus) {
84
- const focusableElements = getFocusableElements();
85
- if (initialFocus) {
86
- const target = containerRef.current.querySelector<HTMLElement>(initialFocus);
87
- if (target) {
88
- target.focus();
89
- } else if (focusableElements.length > 0) {
90
- focusableElements[0].focus();
91
- }
92
- } else if (focusableElements.length > 0) {
93
- focusableElements[0].focus();
94
- }
95
- }
96
-
97
- const handleKeyDown = (event: KeyboardEvent) => {
98
- if (!containerRef.current) return;
99
-
100
- if (event.key === 'Escape' && escapeDeactivates) {
101
- onEscape?.();
102
- return;
103
- }
104
-
105
- if (event.key !== 'Tab') return;
106
-
107
- const focusableElements = getFocusableElements();
108
- if (focusableElements.length === 0) return;
109
-
110
- const firstElement = focusableElements[0];
111
- const lastElement = focusableElements[focusableElements.length - 1];
112
-
113
- if (event.shiftKey) {
114
- if (document.activeElement === firstElement) {
115
- event.preventDefault();
116
- lastElement.focus();
117
- }
118
- } else {
119
- if (document.activeElement === lastElement) {
120
- event.preventDefault();
121
- firstElement.focus();
122
- }
123
- }
124
- };
125
-
126
- const container = containerRef.current;
127
- container.addEventListener('keydown', handleKeyDown);
128
-
129
- return () => {
130
- container.removeEventListener('keydown', handleKeyDown);
131
- if (restoreFocus && previousFocusRef.current) {
132
- previousFocusRef.current.focus();
133
- }
134
- };
135
- }, [enabled, autoFocus, restoreFocus, escapeDeactivates, onEscape, initialFocus, getFocusableElements]);
136
-
137
- return containerRef;
138
- }
@@ -1,98 +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 { useCallback, useEffect, useMemo, useRef, useSyncExternalStore } from 'react';
10
- import { globalUndoManager, type UndoableOperation } from '@object-ui/core';
11
-
12
- export interface UseGlobalUndoOptions {
13
- /** DataSource to execute undo/redo operations. */
14
- dataSource?: {
15
- create(objectName: string, data: Record<string, unknown>): Promise<unknown>;
16
- update(objectName: string, recordId: string, data: Record<string, unknown>): Promise<unknown>;
17
- delete(objectName: string, recordId: string): Promise<unknown>;
18
- };
19
- /** Callback after successful undo. */
20
- onUndo?: (op: UndoableOperation) => void;
21
- /** Callback after successful redo. */
22
- onRedo?: (op: UndoableOperation) => void;
23
- }
24
-
25
- function getSnapshot() {
26
- return {
27
- canUndo: globalUndoManager.canUndo,
28
- canRedo: globalUndoManager.canRedo,
29
- undoDescription: globalUndoManager.peekUndo()?.description,
30
- redoDescription: globalUndoManager.peekRedo()?.description,
31
- history: globalUndoManager.getHistory(),
32
- };
33
- }
34
-
35
- function getServerSnapshot() {
36
- return { canUndo: false, canRedo: false, undoDescription: undefined, redoDescription: undefined, history: [] as UndoableOperation[] };
37
- }
38
-
39
- // Cache reference to avoid re-renders when nothing changed
40
- let cachedSnapshot = getSnapshot();
41
- function subscribe(callback: () => void) {
42
- return globalUndoManager.subscribe(() => {
43
- cachedSnapshot = getSnapshot();
44
- callback();
45
- });
46
- }
47
- function getCachedSnapshot() { return cachedSnapshot; }
48
-
49
- /**
50
- * React hook that wraps the global UndoManager for use in console components.
51
- *
52
- * Provides reactive undo/redo state, executes data operations through the
53
- * supplied dataSource, and registers Ctrl+Z / Ctrl+Shift+Z keyboard shortcuts.
54
- */
55
- export function useGlobalUndo(options: UseGlobalUndoOptions = {}) {
56
- const optionsRef = useRef(options);
57
- optionsRef.current = options;
58
-
59
- const state = useSyncExternalStore(subscribe, getCachedSnapshot, getServerSnapshot);
60
-
61
- const executeOp = useCallback(async (op: UndoableOperation, data: Record<string, unknown>, mode: 'undo' | 'redo') => {
62
- const ds = optionsRef.current.dataSource;
63
- if (!ds) return;
64
- const action = mode === 'undo'
65
- ? ({ create: 'delete', update: 'update', delete: 'create' } as const)[op.type]
66
- : op.type;
67
- if (action === 'delete') await ds.delete(op.objectName, op.recordId);
68
- else if (action === 'update') await ds.update(op.objectName, op.recordId, data);
69
- else await ds.create(op.objectName, data);
70
- }, []);
71
-
72
- const undo = useCallback(async () => {
73
- const op = globalUndoManager.popUndo();
74
- if (!op) return;
75
- await executeOp(op, op.undoData, 'undo');
76
- optionsRef.current.onUndo?.(op);
77
- }, [executeOp]);
78
-
79
- const redo = useCallback(async () => {
80
- const op = globalUndoManager.popRedo();
81
- if (!op) return;
82
- await executeOp(op, op.redoData, 'redo');
83
- optionsRef.current.onRedo?.(op);
84
- }, [executeOp]);
85
-
86
- // Keyboard shortcuts: Ctrl+Z (undo), Ctrl+Shift+Z (redo)
87
- useEffect(() => {
88
- const handler = (e: KeyboardEvent) => {
89
- if (!(e.ctrlKey || e.metaKey) || e.key.toLowerCase() !== 'z') return;
90
- e.preventDefault();
91
- if (e.shiftKey) { void redo(); } else { void undo(); }
92
- };
93
- window.addEventListener('keydown', handler);
94
- return () => window.removeEventListener('keydown', handler);
95
- }, [undo, redo]);
96
-
97
- return useMemo(() => ({ ...state, undo, redo }), [state, undo, redo]);
98
- }
@@ -1,119 +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 { useEffect, useRef, useCallback } from 'react';
10
-
11
- export interface KeyboardShortcutDefinition {
12
- /** The key to listen for (e.g., 'k', 'Enter', 'Escape') */
13
- key: string;
14
- /** Whether Ctrl/Cmd is required */
15
- ctrlOrMeta?: boolean;
16
- /** Whether Shift is required */
17
- shift?: boolean;
18
- /** Whether Alt is required */
19
- alt?: boolean;
20
- /** Handler to call when the shortcut is triggered */
21
- handler: (event: KeyboardEvent) => void;
22
- /** Human-readable description for the help dialog */
23
- description?: string;
24
- /** Whether to prevent default browser behavior */
25
- preventDefault?: boolean;
26
- /** Whether the shortcut is currently enabled */
27
- enabled?: boolean;
28
- }
29
-
30
- /**
31
- * Hook for registering keyboard shortcuts.
32
- * Implements KeyboardShortcutSchema from @objectstack/spec v2.0.7.
33
- *
34
- * @example
35
- * ```tsx
36
- * useKeyboardShortcuts([
37
- * { key: 'k', ctrlOrMeta: true, handler: () => openSearch(), description: 'Open search' },
38
- * { key: 'n', ctrlOrMeta: true, handler: () => createNew(), description: 'Create new record' },
39
- * { key: 'Escape', handler: () => closeModal(), description: 'Close dialog' },
40
- * ]);
41
- * ```
42
- */
43
- export function useKeyboardShortcuts(
44
- shortcuts: KeyboardShortcutDefinition[],
45
- options: { enabled?: boolean } = {}
46
- ): void {
47
- const { enabled = true } = options;
48
- const shortcutsRef = useRef(shortcuts);
49
- useEffect(() => {
50
- shortcutsRef.current = shortcuts;
51
- }, [shortcuts]);
52
-
53
- const handleKeyDown = useCallback(
54
- (event: KeyboardEvent) => {
55
- // Don't trigger shortcuts when typing in form elements
56
- const target = event.target as HTMLElement;
57
- const isFormElement =
58
- target.tagName === 'INPUT' ||
59
- target.tagName === 'TEXTAREA' ||
60
- target.tagName === 'SELECT' ||
61
- target.isContentEditable;
62
-
63
- for (const shortcut of shortcutsRef.current) {
64
- if (shortcut.enabled === false) continue;
65
-
66
- const keyMatch =
67
- event.key.toLowerCase() === shortcut.key.toLowerCase();
68
- const ctrlMatch = shortcut.ctrlOrMeta
69
- ? event.ctrlKey || event.metaKey
70
- : !event.ctrlKey && !event.metaKey;
71
- const shiftMatch = shortcut.shift ? event.shiftKey : !event.shiftKey;
72
- const altMatch = shortcut.alt ? event.altKey : !event.altKey;
73
-
74
- if (keyMatch && ctrlMatch && shiftMatch && altMatch) {
75
- // Allow shortcuts with modifiers even in form elements
76
- if (isFormElement && !shortcut.ctrlOrMeta && !shortcut.alt) continue;
77
-
78
- if (shortcut.preventDefault !== false) {
79
- event.preventDefault();
80
- }
81
- shortcut.handler(event);
82
- return;
83
- }
84
- }
85
- },
86
- []
87
- );
88
-
89
- useEffect(() => {
90
- if (!enabled) return;
91
-
92
- document.addEventListener('keydown', handleKeyDown);
93
- return () => {
94
- document.removeEventListener('keydown', handleKeyDown);
95
- };
96
- }, [enabled, handleKeyDown]);
97
- }
98
-
99
- /**
100
- * Returns a list of all registered shortcut descriptions.
101
- * Useful for building a help dialog (? key).
102
- */
103
- export function getShortcutDescriptions(
104
- shortcuts: KeyboardShortcutDefinition[]
105
- ): Array<{ keys: string; description: string }> {
106
- return shortcuts
107
- .filter((s) => s.description && s.enabled !== false)
108
- .map((s) => {
109
- const parts: string[] = [];
110
- if (s.ctrlOrMeta) parts.push('⌘/Ctrl');
111
- if (s.shift) parts.push('Shift');
112
- if (s.alt) parts.push('Alt');
113
- parts.push(s.key.length === 1 ? s.key.toUpperCase() : s.key);
114
- return {
115
- keys: parts.join(' + '),
116
- description: s.description!,
117
- };
118
- });
119
- }
@@ -1,190 +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
- * useNavigationOverlay
11
- *
12
- * A reusable hook for handling NavigationConfig-driven row/item click behavior.
13
- * Manages overlay state (drawer/modal/split/popover) and generates click handlers
14
- * that respect the ViewNavigationConfig specification.
15
- *
16
- * Used by plugin-grid, plugin-list, plugin-detail and any component that needs
17
- * NavigationConfig support.
18
- */
19
-
20
- import { useState, useCallback, useMemo } from 'react';
21
-
22
- /**
23
- * Inline ViewNavigationConfig to avoid importing from @object-ui/types
24
- * (which may not be a direct dependency of @object-ui/react).
25
- * Mirrors the canonical definition in @object-ui/types/objectql.ts.
26
- */
27
- export interface NavigationConfig {
28
- mode: 'page' | 'drawer' | 'modal' | 'split' | 'popover' | 'new_window' | 'none';
29
- view?: string;
30
- preventNavigation?: boolean;
31
- openNewTab?: boolean;
32
- width?: string | number;
33
- }
34
-
35
- export type NavigationMode = NavigationConfig['mode'];
36
-
37
- export interface UseNavigationOverlayOptions {
38
- /** The navigation configuration from the schema */
39
- navigation?: NavigationConfig;
40
- /** Object name — used to build default URLs for page/new_window modes */
41
- objectName?: string;
42
- /** External onNavigate callback (e.g., from ActionProvider or parent) */
43
- onNavigate?: (recordId: string | number, action?: string) => void;
44
- /** External onRowClick callback — if set, takes full priority */
45
- onRowClick?: (record: Record<string, unknown>) => void;
46
- }
47
-
48
- export interface NavigationOverlayState {
49
- /** Whether the overlay (drawer/modal/split/popover) is open */
50
- isOpen: boolean;
51
- /** The record that triggered the navigation */
52
- selectedRecord: Record<string, unknown> | null;
53
- /** The resolved navigation mode */
54
- mode: NavigationMode;
55
- /** Close the overlay */
56
- close: () => void;
57
- /** Open the overlay with a specific record */
58
- open: (record: Record<string, unknown>) => void;
59
- /** Set the open state (for controlled Sheet/Dialog `onOpenChange`) */
60
- setIsOpen: (open: boolean) => void;
61
- /** The click handler to attach to rows/items */
62
- handleClick: (record: Record<string, unknown>) => void;
63
- /** The width from NavigationConfig (for drawer/modal/split sizing) */
64
- width: string | number | undefined;
65
- /** Whether navigation is an overlay mode (drawer/modal/split/popover) */
66
- isOverlay: boolean;
67
- /** The target view/form name from NavigationConfig */
68
- view: string | undefined;
69
- }
70
-
71
- /**
72
- * Hook for NavigationConfig-driven navigation overlay.
73
- *
74
- * @example
75
- * ```tsx
76
- * const { handleClick, isOpen, selectedRecord, mode, close, width } =
77
- * useNavigationOverlay({
78
- * navigation: schema.navigation,
79
- * objectName: schema.objectName,
80
- * onNavigate: schema.onNavigate,
81
- * onRowClick: props.onRowClick,
82
- * });
83
- *
84
- * return (
85
- * <>
86
- * <DataTable onRowClick={handleClick} ... />
87
- * {isOpen && mode === 'drawer' && (
88
- * <Sheet open onOpenChange={() => close()}>
89
- * <SheetContent style={{ maxWidth: width }}>
90
- * <RecordDetail record={selectedRecord} />
91
- * </SheetContent>
92
- * </Sheet>
93
- * )}
94
- * </>
95
- * );
96
- * ```
97
- */
98
- export function useNavigationOverlay(
99
- options: UseNavigationOverlayOptions
100
- ): NavigationOverlayState {
101
- const { navigation, objectName, onNavigate, onRowClick } = options;
102
- const [isOpen, setIsOpen] = useState(false);
103
- const [selectedRecord, setSelectedRecord] = useState<Record<string, unknown> | null>(null);
104
-
105
- const mode: NavigationMode = navigation?.mode ?? 'page';
106
- const width = navigation?.width;
107
- const view = navigation?.view;
108
- const isOverlay = mode === 'drawer' || mode === 'modal' || mode === 'split' || mode === 'popover';
109
-
110
- const close = useCallback(() => {
111
- setIsOpen(false);
112
- setSelectedRecord(null);
113
- }, []);
114
-
115
- const open = useCallback((record: Record<string, unknown>) => {
116
- setSelectedRecord(record);
117
- setIsOpen(true);
118
- }, []);
119
-
120
- const handleClick = useCallback(
121
- (record: Record<string, unknown>) => {
122
- // External onRowClick takes full priority
123
- if (onRowClick) {
124
- onRowClick(record);
125
- return;
126
- }
127
-
128
- // No navigation config — default to page navigation
129
- if (!navigation) {
130
- const recordId = record.id || record._id;
131
- if (onNavigate && recordId != null) {
132
- onNavigate(recordId as string | number, view ?? 'view');
133
- }
134
- return;
135
- }
136
-
137
- // 'none' or preventNavigation — do nothing
138
- if (mode === 'none' || navigation.preventNavigation) {
139
- return;
140
- }
141
-
142
- // new_window / openNewTab — delegate to onNavigate when available, else open directly
143
- if (mode === 'new_window' || navigation.openNewTab) {
144
- const recordId = record.id || record._id;
145
- if (onNavigate && recordId != null) {
146
- onNavigate(recordId as string | number, 'new_window');
147
- return;
148
- }
149
- const viewPath = view ? `/${view}` : '';
150
- const encodedId = encodeURIComponent(String(recordId));
151
- const url = objectName ? `/${objectName}/${encodedId}${viewPath}` : `/${encodedId}${viewPath}`;
152
- window.open(url, '_blank');
153
- return;
154
- }
155
-
156
- // page — delegate to onNavigate callback
157
- if (mode === 'page') {
158
- const recordId = record.id || record._id;
159
- if (onNavigate && recordId != null) {
160
- onNavigate(recordId as string | number, view ?? 'view');
161
- }
162
- return;
163
- }
164
-
165
- // Overlay modes: drawer, modal, split, popover
166
- if (isOverlay) {
167
- setSelectedRecord(record);
168
- setIsOpen(true);
169
- return;
170
- }
171
- },
172
- [onRowClick, navigation, mode, objectName, onNavigate, isOverlay]
173
- );
174
-
175
- return useMemo(
176
- () => ({
177
- isOpen,
178
- selectedRecord,
179
- mode,
180
- close,
181
- open,
182
- setIsOpen,
183
- handleClick,
184
- width,
185
- view,
186
- isOverlay,
187
- }),
188
- [isOpen, selectedRecord, mode, close, open, handleClick, width, view, isOverlay]
189
- );
190
- }