@object-ui/react 0.5.0 → 2.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 +13 -0
- package/dist/LazyPluginLoader.d.ts +44 -0
- package/dist/LazyPluginLoader.js +92 -3
- package/dist/components/form/FieldFactory.js +44 -40
- package/dist/components/form/FormRenderer.js +45 -3
- package/dist/context/ActionContext.d.ts +76 -0
- package/dist/context/ActionContext.js +139 -0
- package/dist/context/ThemeContext.d.ts +94 -0
- package/dist/context/ThemeContext.js +215 -0
- package/dist/context/index.d.ts +2 -0
- package/dist/context/index.js +2 -0
- package/dist/hooks/index.d.ts +5 -0
- package/dist/hooks/index.js +5 -0
- package/dist/hooks/useActionRunner.d.ts +21 -7
- package/dist/hooks/useActionRunner.js +27 -7
- package/dist/hooks/useDiscovery.d.ts +78 -0
- package/dist/hooks/useDiscovery.js +96 -0
- package/dist/hooks/useDynamicApp.d.ts +70 -0
- package/dist/hooks/useDynamicApp.js +92 -0
- package/dist/hooks/useNavigationOverlay.d.ts +78 -0
- package/dist/hooks/useNavigationOverlay.js +112 -0
- package/dist/hooks/usePageVariables.d.ts +67 -0
- package/dist/hooks/usePageVariables.js +109 -0
- package/dist/hooks/useViewData.d.ts +73 -0
- package/dist/hooks/useViewData.js +135 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/utils/i18n.d.ts +10 -0
- package/dist/utils/i18n.js +12 -0
- package/package.json +6 -4
- package/src/LazyPluginLoader.tsx +129 -7
- package/src/__tests__/LazyPluginLoader.test.tsx +97 -1
- package/src/__tests__/ThemeProvider.test.tsx +451 -0
- package/src/__tests__/useNavigationOverlay.test.ts +416 -0
- package/src/__tests__/usePageVariables.test.tsx +334 -0
- package/src/__tests__/useViewData.test.tsx +422 -0
- package/src/components/form/FieldFactory.tsx +51 -46
- package/src/components/form/FormRenderer.test.tsx +145 -0
- package/src/components/form/FormRenderer.tsx +52 -3
- package/src/context/ActionContext.tsx +213 -0
- package/src/context/ThemeContext.tsx +325 -0
- package/src/context/__tests__/ActionContext.test.tsx +174 -0
- package/src/context/index.ts +2 -0
- package/src/hooks/__tests__/useActionRunner.test.ts +153 -0
- package/src/hooks/__tests__/useDiscovery.test.tsx +149 -0
- package/src/hooks/__tests__/useDynamicApp.test.ts +161 -0
- package/src/hooks/__tests__/useExpression.test.ts +86 -0
- package/src/hooks/index.ts +5 -0
- package/src/hooks/useActionRunner.ts +49 -11
- package/src/hooks/useDiscovery.ts +141 -0
- package/src/hooks/useDynamicApp.ts +142 -0
- package/src/hooks/useNavigationOverlay.ts +180 -0
- package/src/hooks/usePageVariables.tsx +157 -0
- package/src/hooks/useViewData.ts +201 -0
- package/src/index.ts +21 -0
- package/src/utils/i18n.ts +10 -0
- package/tsconfig.json +1 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/__tests__/LazyPluginLoader.test.d.ts +0 -8
- package/dist/__tests__/LazyPluginLoader.test.js +0 -49
- package/dist/components/form/FormRenderer.test.d.ts +0 -8
- package/dist/components/form/FormRenderer.test.js +0 -534
- package/dist/index.test.d.ts +0 -8
- package/dist/index.test.js +0 -13
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI — useViewData
|
|
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
|
+
* A React hook that resolves a ViewData configuration into a DataSource
|
|
9
|
+
* and provides reactive data fetching with loading/error states.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { useState, useEffect, useCallback, useMemo, useContext } from 'react';
|
|
13
|
+
import type { DataSource, ViewData, QueryParams, QueryResult } from '@object-ui/types';
|
|
14
|
+
import { resolveDataSource, type ResolveDataSourceOptions } from '@object-ui/core';
|
|
15
|
+
import { SchemaRendererContext } from '../context/SchemaRendererContext';
|
|
16
|
+
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
// Types
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
|
|
21
|
+
export interface UseViewDataOptions<T = any> {
|
|
22
|
+
/** The ViewData configuration from the schema */
|
|
23
|
+
viewData?: ViewData | null;
|
|
24
|
+
/** Resource name (object name, used for DataSource.find calls) */
|
|
25
|
+
resource?: string;
|
|
26
|
+
/** Initial query parameters */
|
|
27
|
+
params?: QueryParams;
|
|
28
|
+
/** Explicit data source override (bypasses context) */
|
|
29
|
+
dataSource?: DataSource<T> | null;
|
|
30
|
+
/** Options for adapter construction */
|
|
31
|
+
adapterOptions?: ResolveDataSourceOptions;
|
|
32
|
+
/** If true, skip the initial fetch (useful for forms in create mode) */
|
|
33
|
+
skip?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface UseViewDataResult<T = any> {
|
|
37
|
+
/** Resolved data array */
|
|
38
|
+
data: T[];
|
|
39
|
+
/** Total number of records (from server or array length) */
|
|
40
|
+
totalCount: number;
|
|
41
|
+
/** Whether data is being fetched */
|
|
42
|
+
loading: boolean;
|
|
43
|
+
/** Error from the last fetch attempt */
|
|
44
|
+
error: Error | null;
|
|
45
|
+
/** The resolved DataSource instance (can be used for create/update/delete) */
|
|
46
|
+
dataSource: DataSource<T> | null;
|
|
47
|
+
/** Re-fetch data with current or new params */
|
|
48
|
+
refresh: (newParams?: QueryParams) => Promise<void>;
|
|
49
|
+
/** Fetch a single record by ID */
|
|
50
|
+
fetchOne: (id: string | number) => Promise<T | null>;
|
|
51
|
+
/** Whether more records are available */
|
|
52
|
+
hasMore: boolean;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
// Hook
|
|
57
|
+
// ---------------------------------------------------------------------------
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* useViewData — resolves ViewData into a DataSource and provides reactive data.
|
|
61
|
+
*
|
|
62
|
+
* This hook centralizes the data resolution pattern that was previously
|
|
63
|
+
* duplicated across plugins (ObjectGrid, ObjectView, etc.). It:
|
|
64
|
+
*
|
|
65
|
+
* 1. Resolves the ViewData config to the right DataSource adapter
|
|
66
|
+
* - `provider: 'object'` → context DataSource (ObjectStackAdapter)
|
|
67
|
+
* - `provider: 'api'` → ApiDataSource (raw HTTP)
|
|
68
|
+
* - `provider: 'value'` → ValueDataSource (in-memory)
|
|
69
|
+
* 2. Fetches data automatically on mount
|
|
70
|
+
* 3. Provides loading/error states and refresh capability
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```tsx
|
|
74
|
+
* function MyGrid({ schema }) {
|
|
75
|
+
* const { data, loading, error, refresh, dataSource } = useViewData({
|
|
76
|
+
* viewData: schema.data,
|
|
77
|
+
* resource: schema.objectName,
|
|
78
|
+
* params: { $top: 50 },
|
|
79
|
+
* });
|
|
80
|
+
*
|
|
81
|
+
* if (loading) return <Spinner />;
|
|
82
|
+
* if (error) return <ErrorMessage error={error} />;
|
|
83
|
+
* return <Table data={data} />;
|
|
84
|
+
* }
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export function useViewData<T = any>(options: UseViewDataOptions<T>): UseViewDataResult<T> {
|
|
88
|
+
const {
|
|
89
|
+
viewData,
|
|
90
|
+
resource = '',
|
|
91
|
+
params,
|
|
92
|
+
dataSource: explicitDataSource,
|
|
93
|
+
adapterOptions,
|
|
94
|
+
skip = false,
|
|
95
|
+
} = options;
|
|
96
|
+
|
|
97
|
+
// Get context DataSource
|
|
98
|
+
const context = useContext(SchemaRendererContext);
|
|
99
|
+
const contextDataSource = context?.dataSource ?? null;
|
|
100
|
+
|
|
101
|
+
// Resolve the DataSource — memoize to prevent re-creation on every render.
|
|
102
|
+
// We key on the viewData provider + config identity.
|
|
103
|
+
const resolvedDataSource = useMemo(() => {
|
|
104
|
+
if (explicitDataSource) return explicitDataSource;
|
|
105
|
+
return resolveDataSource<T>(viewData, contextDataSource, adapterOptions);
|
|
106
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
107
|
+
}, [
|
|
108
|
+
explicitDataSource,
|
|
109
|
+
viewData?.provider,
|
|
110
|
+
// For 'api' provider, key on read/write URLs
|
|
111
|
+
viewData?.provider === 'api' ? (viewData as any).read?.url : undefined,
|
|
112
|
+
viewData?.provider === 'api' ? (viewData as any).write?.url : undefined,
|
|
113
|
+
// For 'value' provider, key on items reference (or length as proxy)
|
|
114
|
+
viewData?.provider === 'value' ? (viewData as any).items?.length : undefined,
|
|
115
|
+
contextDataSource,
|
|
116
|
+
]);
|
|
117
|
+
|
|
118
|
+
// State
|
|
119
|
+
const [data, setData] = useState<T[]>([]);
|
|
120
|
+
const [totalCount, setTotalCount] = useState(0);
|
|
121
|
+
const [loading, setLoading] = useState(!skip);
|
|
122
|
+
const [error, setError] = useState<Error | null>(null);
|
|
123
|
+
const [hasMore, setHasMore] = useState(false);
|
|
124
|
+
|
|
125
|
+
// Stable params reference
|
|
126
|
+
const paramsKey = useMemo(() => JSON.stringify(params ?? {}), [params]);
|
|
127
|
+
|
|
128
|
+
// Fetch function
|
|
129
|
+
const fetchData = useCallback(
|
|
130
|
+
async (fetchParams?: QueryParams) => {
|
|
131
|
+
if (!resolvedDataSource || !resource) {
|
|
132
|
+
setLoading(false);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
setLoading(true);
|
|
137
|
+
setError(null);
|
|
138
|
+
|
|
139
|
+
try {
|
|
140
|
+
const mergedParams = fetchParams ?? (params ? { ...params } : undefined);
|
|
141
|
+
const result: QueryResult<T> = await resolvedDataSource.find(resource, mergedParams);
|
|
142
|
+
setData(result.data ?? []);
|
|
143
|
+
setTotalCount(result.total ?? result.data?.length ?? 0);
|
|
144
|
+
setHasMore(result.hasMore ?? false);
|
|
145
|
+
} catch (err) {
|
|
146
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
147
|
+
setData([]);
|
|
148
|
+
setTotalCount(0);
|
|
149
|
+
} finally {
|
|
150
|
+
setLoading(false);
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
[resolvedDataSource, resource, params],
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
// Auto-fetch on mount / when deps change
|
|
157
|
+
useEffect(() => {
|
|
158
|
+
if (skip) {
|
|
159
|
+
setLoading(false);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// For value provider, data is synchronous — still go through fetchData
|
|
164
|
+
// for consistency, but it resolves immediately.
|
|
165
|
+
fetchData();
|
|
166
|
+
|
|
167
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
168
|
+
}, [resolvedDataSource, resource, paramsKey, skip]);
|
|
169
|
+
|
|
170
|
+
// Refresh function (can accept new params)
|
|
171
|
+
const refresh = useCallback(
|
|
172
|
+
async (newParams?: QueryParams) => {
|
|
173
|
+
await fetchData(newParams);
|
|
174
|
+
},
|
|
175
|
+
[fetchData],
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
// Fetch one record
|
|
179
|
+
const fetchOne = useCallback(
|
|
180
|
+
async (id: string | number): Promise<T | null> => {
|
|
181
|
+
if (!resolvedDataSource) return null;
|
|
182
|
+
try {
|
|
183
|
+
return await resolvedDataSource.findOne(resource, id);
|
|
184
|
+
} catch {
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
[resolvedDataSource, resource],
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
return {
|
|
192
|
+
data,
|
|
193
|
+
totalCount,
|
|
194
|
+
loading,
|
|
195
|
+
error,
|
|
196
|
+
dataSource: resolvedDataSource,
|
|
197
|
+
refresh,
|
|
198
|
+
fetchOne,
|
|
199
|
+
hasMore,
|
|
200
|
+
};
|
|
201
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -12,3 +12,24 @@ export * from './context'; // will be empty for now
|
|
|
12
12
|
export * from './components/form';
|
|
13
13
|
export * from './LazyPluginLoader';
|
|
14
14
|
|
|
15
|
+
// Built-in i18n support
|
|
16
|
+
export {
|
|
17
|
+
I18nProvider,
|
|
18
|
+
useObjectTranslation,
|
|
19
|
+
useI18nContext,
|
|
20
|
+
createI18n,
|
|
21
|
+
getDirection,
|
|
22
|
+
getAvailableLanguages,
|
|
23
|
+
formatDate,
|
|
24
|
+
formatDateTime,
|
|
25
|
+
formatRelativeTime,
|
|
26
|
+
formatCurrency,
|
|
27
|
+
formatNumber,
|
|
28
|
+
type I18nConfig,
|
|
29
|
+
type I18nProviderProps,
|
|
30
|
+
type TranslationKeys,
|
|
31
|
+
type DateFormatOptions,
|
|
32
|
+
type CurrencyFormatOptions,
|
|
33
|
+
type NumberFormatOptions,
|
|
34
|
+
} from '@object-ui/i18n';
|
|
35
|
+
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves an I18nLabel to a plain string.
|
|
3
|
+
* I18nLabel can be either a string or an object { key, defaultValue?, params? }.
|
|
4
|
+
* When it's an object, we return the defaultValue or the key as fallback.
|
|
5
|
+
*/
|
|
6
|
+
export function resolveI18nLabel(label: string | { key: string; defaultValue?: string; params?: Record<string, any> } | undefined): string | undefined {
|
|
7
|
+
if (label === undefined || label === null) return undefined;
|
|
8
|
+
if (typeof label === 'string') return label;
|
|
9
|
+
return label.defaultValue || label.key;
|
|
10
|
+
}
|