@aventoapp/framework 0.1.0 → 0.2.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 (169) hide show
  1. package/README.md +260 -0
  2. package/dist/index.js.map +1 -1
  3. package/dist/native/index.js.map +1 -1
  4. package/dist/offline/index.js +399 -0
  5. package/dist/offline/index.js.map +7 -0
  6. package/dist/server/index.js +75 -0
  7. package/dist/server/index.js.map +3 -3
  8. package/dist/server/workflow/index.js.map +1 -1
  9. package/dist/shared/index.js.map +1 -1
  10. package/dist/web/index.js.map +1 -1
  11. package/package.json +10 -7
  12. package/schemas/Workflow.json +14 -14
  13. package/schemas/WorkflowHistory.json +22 -22
  14. package/schemas/WorkflowInstance.json +16 -16
  15. package/schemas/WorkflowState.json +18 -18
  16. package/schemas/WorkflowTransition.json +27 -27
  17. package/src/index.js +8 -8
  18. package/src/native/components/NativeAuthProvider.jsx +249 -249
  19. package/src/native/components/NativeDetail.jsx +270 -270
  20. package/src/native/components/NativeForm.jsx +543 -543
  21. package/src/native/components/NativeGrid.jsx +90 -90
  22. package/src/native/components/NativeJsonGrid.jsx +213 -213
  23. package/src/native/components/NativeJsonView.jsx +454 -454
  24. package/src/native/components/NativeJsonViewContext.js +17 -17
  25. package/src/native/components/NativeShell.jsx +68 -68
  26. package/src/native/components/NativeViewComponent.jsx +351 -351
  27. package/src/native/extensions/NativeExtensionRegistry.js +81 -81
  28. package/src/native/hooks/useNativeStore.js +72 -72
  29. package/src/native/index.js +15 -15
  30. package/src/native/layouts/NativeStackLayout.jsx +30 -30
  31. package/src/native/layouts/NativeTabLayout.jsx +96 -96
  32. package/src/native/storage.js +13 -13
  33. package/src/native/stores/NativeBaseStore.js +348 -348
  34. package/src/native/stores/NativeStoreContext.jsx +53 -53
  35. package/src/offline/clock.js +12 -0
  36. package/src/offline/coalesce.js +45 -0
  37. package/src/offline/diff.js +17 -0
  38. package/src/offline/idb.js +52 -0
  39. package/src/offline/index.js +13 -0
  40. package/src/offline/kit.js +121 -0
  41. package/src/offline/outbox.js +83 -0
  42. package/src/offline/store.js +26 -0
  43. package/src/offline/syncer.js +55 -0
  44. package/src/server/ai/capabilities-routes.js +106 -106
  45. package/src/server/ai/index.js +1 -1
  46. package/src/server/cache/MemoryCache.js +153 -153
  47. package/src/server/cache/index.js +2 -2
  48. package/src/server/cache/middleware.js +12 -12
  49. package/src/server/db/index.js +14 -14
  50. package/src/server/db/mongo.js +137 -137
  51. package/src/server/db/postgres.js +144 -144
  52. package/src/server/index.js +42 -38
  53. package/src/server/middleware/auth.js +682 -682
  54. package/src/server/middleware/can-flags.js +132 -132
  55. package/src/server/middleware/entityRoles.js +105 -105
  56. package/src/server/middleware/index.js +58 -58
  57. package/src/server/middleware/permissions.js +240 -240
  58. package/src/server/middleware/tenant.js +203 -203
  59. package/src/server/offline/field-lww.js +66 -0
  60. package/src/server/offline/index.js +1 -0
  61. package/src/server/realtime/EventBus.js +45 -45
  62. package/src/server/realtime/WebSocketServer.js +105 -105
  63. package/src/server/realtime/index.js +3 -3
  64. package/src/server/realtime/middleware.js +18 -18
  65. package/src/server/services/email-templates.js +157 -157
  66. package/src/server/services/email.js +78 -78
  67. package/src/server/services/index.js +15 -15
  68. package/src/server/services/storage.js +119 -119
  69. package/src/server/utils/index.js +16 -16
  70. package/src/server/utils/paginate.js +65 -65
  71. package/src/server/utils/query-builder.js +293 -293
  72. package/src/server/utils/route-loader.js +61 -61
  73. package/src/server/workflow/WorkflowEngine.js +494 -494
  74. package/src/server/workflow/index.js +2 -2
  75. package/src/server/workflow/workflow-routes.js +174 -174
  76. package/src/shared/index.js +10 -10
  77. package/src/shared/utils/field-accessor.js +108 -108
  78. package/src/shared/utils/index.js +7 -7
  79. package/src/shared/utils/validation-utils.js +83 -83
  80. package/src/web/components/AppRouter/AppRouter.jsx +130 -130
  81. package/src/web/components/AppRouter/index.js +1 -1
  82. package/src/web/components/AppShell/AppShell.jsx +101 -101
  83. package/src/web/components/AppShell/DefaultUserMenu.jsx +54 -54
  84. package/src/web/components/AppShell/ShellConfig.js +53 -53
  85. package/src/web/components/AppShell/index.js +3 -3
  86. package/src/web/components/AuthProvider.jsx +426 -426
  87. package/src/web/components/CardList/CardList.jsx +272 -272
  88. package/src/web/components/CardList/index.js +2 -2
  89. package/src/web/components/DetailView/DetailView.jsx +281 -281
  90. package/src/web/components/DetailView/index.js +2 -2
  91. package/src/web/components/Form/Form.jsx +300 -300
  92. package/src/web/components/Form/FormField.jsx +226 -226
  93. package/src/web/components/Form/index.js +3 -3
  94. package/src/web/components/Grid/Grid.jsx +304 -304
  95. package/src/web/components/Grid/index.js +2 -2
  96. package/src/web/components/IconRegistry/IconRegistry.jsx +127 -127
  97. package/src/web/components/IconRegistry/index.js +1 -1
  98. package/src/web/components/JsonView/JsonView.jsx +647 -647
  99. package/src/web/components/JsonView/ViewComponent.jsx +1045 -1045
  100. package/src/web/components/JsonView/ViewToolbar.jsx +141 -141
  101. package/src/web/components/JsonView/index.js +14 -14
  102. package/src/web/components/JsonView/layouts/SingleLayout.jsx +41 -41
  103. package/src/web/components/JsonView/layouts/SplitLayout.jsx +122 -122
  104. package/src/web/components/JsonView/layouts/TabLayout.jsx +112 -112
  105. package/src/web/components/JsonView/layouts/index.js +8 -8
  106. package/src/web/components/Layout.jsx +265 -265
  107. package/src/web/components/MobileLayout/MobileLayout.jsx +385 -385
  108. package/src/web/components/MobileLayout/index.js +1 -1
  109. package/src/web/components/Navigation/Navigation.jsx +248 -248
  110. package/src/web/components/Navigation/index.js +1 -1
  111. package/src/web/components/NotificationProvider.jsx +55 -55
  112. package/src/web/components/Pagination/InfiniteScroll.jsx +82 -82
  113. package/src/web/components/Pagination/Pagination.jsx +73 -73
  114. package/src/web/components/Pagination/index.js +8 -8
  115. package/src/web/components/SimpleList/SimpleList.jsx +177 -177
  116. package/src/web/components/SimpleList/index.js +2 -2
  117. package/src/web/components/TenantProvider.jsx +121 -121
  118. package/src/web/components/WorkflowActions/WorkflowActions.jsx +71 -71
  119. package/src/web/components/WorkflowActions/index.js +1 -1
  120. package/src/web/components/WorkflowField/WorkflowField.jsx +176 -176
  121. package/src/web/components/WorkflowField/index.js +2 -2
  122. package/src/web/components/WorkflowField/workflowColors.js +32 -32
  123. package/src/web/components/auth/DevTenantSelector.jsx +90 -90
  124. package/src/web/components/auth/ForgotPasswordForm.jsx +94 -94
  125. package/src/web/components/auth/RegisterForm.jsx +168 -168
  126. package/src/web/components/auth/ResetPasswordForm.jsx +121 -121
  127. package/src/web/components/auth/VerifyEmailView.jsx +85 -85
  128. package/src/web/components/auth/index.js +5 -5
  129. package/src/web/components/index.js +39 -39
  130. package/src/web/components/layouts/CardListLayout.jsx +68 -68
  131. package/src/web/components/layouts/FullscreenLayout.jsx +30 -30
  132. package/src/web/components/layouts/MobileStackLayout.jsx +28 -28
  133. package/src/web/components/layouts/WizardLayout.jsx +86 -86
  134. package/src/web/components/layouts/index.js +4 -4
  135. package/src/web/components/mobile/Badge.jsx +105 -105
  136. package/src/web/components/mobile/BadgeThemeEditor.jsx +104 -104
  137. package/src/web/components/mobile/EmptyState.jsx +30 -30
  138. package/src/web/components/mobile/QuickActionButton.jsx +77 -77
  139. package/src/web/components/mobile/ServerAutocomplete.jsx +123 -123
  140. package/src/web/components/mobile/StarRating.jsx +42 -42
  141. package/src/web/components/mobile/StatCard.jsx +65 -65
  142. package/src/web/components/mobile/index.js +7 -7
  143. package/src/web/extensions/ExtensionRegistry.js +79 -79
  144. package/src/web/extensions/index.js +7 -7
  145. package/src/web/hooks/index.js +15 -15
  146. package/src/web/hooks/useApi.js +76 -76
  147. package/src/web/hooks/useAuth.js +37 -37
  148. package/src/web/hooks/useDebouncedSave.js +158 -158
  149. package/src/web/hooks/useIsMobile.js +14 -14
  150. package/src/web/hooks/useOfflineQueue.js +99 -99
  151. package/src/web/hooks/useSelectionModel.js +143 -143
  152. package/src/web/hooks/useServerAutocomplete.js +146 -146
  153. package/src/web/hooks/useStore.js +107 -107
  154. package/src/web/hooks/useSubscription.js +130 -130
  155. package/src/web/hooks/useTenant.js +39 -39
  156. package/src/web/hooks/useTenantResolver.js +65 -65
  157. package/src/web/hooks/useUndoRedo.js +77 -77
  158. package/src/web/hooks/useViews.js +113 -113
  159. package/src/web/hooks/useWorkflow.js +190 -190
  160. package/src/web/index.js +117 -117
  161. package/src/web/stores/BaseStore.js +439 -439
  162. package/src/web/stores/StoreContext.jsx +93 -93
  163. package/src/web/stores/index.js +3 -3
  164. package/src/web/theme/index.js +3 -3
  165. package/src/web/theme/mui-theme.js +228 -228
  166. package/src/web/utils/formatTimeAgo.js +22 -22
  167. package/src/web/utils/index.js +1 -1
  168. package/src/web/views/ViewRegistry.js +235 -235
  169. package/src/web/views/index.js +2 -2
@@ -1,454 +1,454 @@
1
- /**
2
- * NativeJsonView
3
- *
4
- * Main orchestrator for JSON-declared views on React Native.
5
- * Port of web JsonView — same reducer, same context shape, RN rendering.
6
- */
7
- import React, { useReducer, useEffect, useMemo, useCallback } from 'react';
8
- import { View, Text, ActivityIndicator, StyleSheet } from 'react-native';
9
- import { useNativeStore } from '../hooks/useNativeStore.js';
10
- import { NativeStackLayout } from '../layouts/NativeStackLayout.jsx';
11
- import { NativeTabLayout } from '../layouts/NativeTabLayout.jsx';
12
- import { nativeExtensionRegistry } from '../extensions/NativeExtensionRegistry.js';
13
- import { NativeJsonViewContext, useNativeJsonView } from './NativeJsonViewContext.js';
14
-
15
- const ActionTypes = {
16
- SET_SELECTION: 'SET_SELECTION',
17
- CLEAR_SELECTION: 'CLEAR_SELECTION',
18
- SET_EDIT_ITEM: 'SET_EDIT_ITEM',
19
- CLEAR_EDIT_ITEM: 'CLEAR_EDIT_ITEM',
20
- OPEN_DIALOG: 'OPEN_DIALOG',
21
- CLOSE_DIALOG: 'CLOSE_DIALOG',
22
- SET_COMPONENT_STATE: 'SET_COMPONENT_STATE',
23
- SET_PAGINATION: 'SET_PAGINATION',
24
- };
25
-
26
- function getInitialPaginationState(paginationConfig) {
27
- if (!paginationConfig || paginationConfig.mode === 'none') {
28
- return null;
29
- }
30
- return {
31
- mode: paginationConfig.mode || 'infinite',
32
- page: 1,
33
- pageSize: paginationConfig.pageSize || 25,
34
- total: 0,
35
- totalPages: 0,
36
- hasNext: false,
37
- hasPrev: false
38
- };
39
- }
40
-
41
- const initialState = {
42
- selections: {},
43
- editItems: {},
44
- dialogs: {},
45
- componentStates: {},
46
- pagination: null,
47
- };
48
-
49
- function viewReducer(state, action) {
50
- switch (action.type) {
51
- case ActionTypes.SET_SELECTION:
52
- return {
53
- ...state,
54
- selections: { ...state.selections, [action.componentId]: action.data }
55
- };
56
-
57
- case ActionTypes.CLEAR_SELECTION:
58
- return {
59
- ...state,
60
- selections: { ...state.selections, [action.componentId]: null }
61
- };
62
-
63
- case ActionTypes.SET_EDIT_ITEM:
64
- return {
65
- ...state,
66
- editItems: { ...state.editItems, [action.componentId]: action.data }
67
- };
68
-
69
- case ActionTypes.CLEAR_EDIT_ITEM:
70
- return {
71
- ...state,
72
- editItems: { ...state.editItems, [action.componentId]: null }
73
- };
74
-
75
- case ActionTypes.OPEN_DIALOG:
76
- return {
77
- ...state,
78
- dialogs: { ...state.dialogs, [action.dialogId]: { open: true, data: action.data } }
79
- };
80
-
81
- case ActionTypes.CLOSE_DIALOG:
82
- return {
83
- ...state,
84
- dialogs: { ...state.dialogs, [action.dialogId]: { open: false, data: null } }
85
- };
86
-
87
- case ActionTypes.SET_COMPONENT_STATE:
88
- return {
89
- ...state,
90
- componentStates: {
91
- ...state.componentStates,
92
- [action.componentId]: {
93
- ...state.componentStates[action.componentId],
94
- ...action.state
95
- }
96
- }
97
- };
98
-
99
- case ActionTypes.SET_PAGINATION:
100
- return {
101
- ...state,
102
- pagination: { ...state.pagination, ...action.pagination }
103
- };
104
-
105
- default:
106
- return state;
107
- }
108
- }
109
-
110
- const LAYOUTS = {
111
- stack: NativeStackLayout,
112
- tabs: NativeTabLayout,
113
- single: NativeStackLayout,
114
- split: NativeStackLayout,
115
- };
116
-
117
- function NativeJsonViewWithStore({ config, storeName }) {
118
- const store = useNativeStore(storeName);
119
-
120
- const {
121
- id: viewId,
122
- layout = 'stack',
123
- layoutConfig = {},
124
- dataSource = {},
125
- components = [],
126
- extension: extensionName
127
- } = config || {};
128
-
129
- // Get extension hook if specified
130
- const useExtension = extensionName ? nativeExtensionRegistry.get(extensionName) : null;
131
-
132
- const initialStateWithPagination = useMemo(() => ({
133
- ...initialState,
134
- pagination: getInitialPaginationState(dataSource?.pagination)
135
- }), []);
136
-
137
- const [viewState, dispatch] = useReducer(viewReducer, initialStateWithPagination);
138
-
139
- const storeLoading = store.loading;
140
- const storeError = store.error;
141
- const storePagination = store.pagination;
142
-
143
- const storeActions = useMemo(() => ({
144
- list: store.list,
145
- get: store.get,
146
- create: store.create,
147
- update: store.update,
148
- delete: store.delete,
149
- select: store.select,
150
- clearSelection: store.clearSelection,
151
- loadMore: store.loadMore,
152
- setPageSize: store.setPageSize
153
- }), [store]);
154
-
155
- // Sync store pagination to view state
156
- useEffect(() => {
157
- if (viewState.pagination && storePagination) {
158
- dispatch({
159
- type: ActionTypes.SET_PAGINATION,
160
- pagination: {
161
- page: storePagination.page,
162
- pageSize: storePagination.pageSize,
163
- total: storePagination.total,
164
- totalPages: storePagination.totalPages,
165
- hasNext: storePagination.hasNext,
166
- hasPrev: storePagination.hasPrev
167
- }
168
- });
169
- }
170
- }, [storePagination, viewState.pagination !== null]);
171
-
172
- // Sync selections with store items after edits
173
- const storeItems = store.items;
174
- useEffect(() => {
175
- if (!storeItems?.length) return;
176
-
177
- Object.entries(viewState.selections).forEach(([componentId, selectedItem]) => {
178
- if (!selectedItem?.id) return;
179
-
180
- const updatedItem = storeItems.find(item => item.id === selectedItem.id);
181
- if (updatedItem && updatedItem !== selectedItem) {
182
- dispatch({
183
- type: ActionTypes.SET_SELECTION,
184
- componentId,
185
- data: updatedItem
186
- });
187
- }
188
- });
189
- }, [storeItems]);
190
-
191
- // Auto-load data on mount
192
- useEffect(() => {
193
- if (dataSource?.autoLoad) {
194
- const loadOptions = {};
195
- if (viewState.pagination) {
196
- loadOptions.page = viewState.pagination.page;
197
- loadOptions.pageSize = viewState.pagination.pageSize;
198
- }
199
- if (dataSource.sliceId) {
200
- loadOptions.sliceId = dataSource.sliceId;
201
- }
202
- store.list(loadOptions);
203
- }
204
- }, []);
205
-
206
- return (
207
- <NativeJsonViewInner
208
- config={config}
209
- viewId={viewId}
210
- layout={layout}
211
- layoutConfig={layoutConfig}
212
- dataSource={dataSource}
213
- components={components}
214
- viewState={viewState}
215
- dispatch={dispatch}
216
- storeData={store}
217
- storeLoading={storeLoading}
218
- storeError={storeError}
219
- storeActions={storeActions}
220
- useExtension={useExtension}
221
- />
222
- );
223
- }
224
-
225
- function NativeJsonViewWithoutStore({ config }) {
226
- const [viewState, dispatch] = useReducer(viewReducer, initialState);
227
-
228
- const {
229
- id: viewId,
230
- layout = 'stack',
231
- layoutConfig = {},
232
- dataSource = {},
233
- components = [],
234
- extension: extensionName
235
- } = config || {};
236
-
237
- const useExtension = extensionName ? nativeExtensionRegistry.get(extensionName) : null;
238
-
239
- return (
240
- <NativeJsonViewInner
241
- config={config}
242
- viewId={viewId}
243
- layout={layout}
244
- layoutConfig={layoutConfig}
245
- dataSource={dataSource}
246
- components={components}
247
- viewState={viewState}
248
- dispatch={dispatch}
249
- storeData={null}
250
- storeLoading={false}
251
- storeError={null}
252
- storeActions={{}}
253
- useExtension={useExtension}
254
- />
255
- );
256
- }
257
-
258
- export function NativeJsonView({ config }) {
259
- const storeName = config?.dataSource?.store;
260
-
261
- if (storeName) {
262
- return <NativeJsonViewWithStore config={config} storeName={storeName} />;
263
- }
264
- return <NativeJsonViewWithoutStore config={config} />;
265
- }
266
-
267
- function NativeJsonViewInner({
268
- config,
269
- viewId,
270
- layout,
271
- layoutConfig,
272
- dataSource,
273
- components,
274
- viewState,
275
- dispatch,
276
- storeData,
277
- storeLoading,
278
- storeError,
279
- storeActions,
280
- useExtension,
281
- }) {
282
- const setSelection = useCallback((componentId, data) => {
283
- dispatch({ type: ActionTypes.SET_SELECTION, componentId, data });
284
- }, []);
285
-
286
- const clearSelection = useCallback((componentId) => {
287
- dispatch({ type: ActionTypes.CLEAR_SELECTION, componentId });
288
- }, []);
289
-
290
- const setEditItem = useCallback((componentId, data) => {
291
- dispatch({ type: ActionTypes.SET_EDIT_ITEM, componentId, data });
292
- }, []);
293
-
294
- const clearEditItem = useCallback((componentId) => {
295
- dispatch({ type: ActionTypes.CLEAR_EDIT_ITEM, componentId });
296
- }, []);
297
-
298
- const openDialog = useCallback((dialogId, data = null) => {
299
- dispatch({ type: ActionTypes.OPEN_DIALOG, dialogId, data });
300
- }, []);
301
-
302
- const closeDialog = useCallback((dialogId) => {
303
- dispatch({ type: ActionTypes.CLOSE_DIALOG, dialogId });
304
- }, []);
305
-
306
- const setComponentState = useCallback((componentId, state) => {
307
- dispatch({ type: ActionTypes.SET_COMPONENT_STATE, componentId, state });
308
- }, []);
309
-
310
- const handleLoadMore = useCallback(() => {
311
- storeActions.loadMore?.();
312
- }, [storeActions]);
313
-
314
- // Call extension hook if provided
315
- const extensionContext = useMemo(() => ({
316
- store: storeData,
317
- storeActions,
318
- viewState,
319
- openDialog,
320
- closeDialog,
321
- setSelection,
322
- setEditItem
323
- }), [storeData, storeActions, viewState, openDialog, closeDialog, setSelection, setEditItem]);
324
-
325
- const extension = useExtension ? useExtension(extensionContext) : null;
326
- const extensionActions = extension?.actions || {};
327
- const extensionComponents = extension?.components || {};
328
-
329
- // Execute a custom action (from extension or server-side)
330
- const executeCustomAction = useCallback(async (actionConfig, data) => {
331
- if (actionConfig.handler && extensionActions[actionConfig.handler]) {
332
- return extensionActions[actionConfig.handler](data);
333
- }
334
-
335
- if (actionConfig.serverAction) {
336
- const endpoint = actionConfig.endpoint.replace(':id', data?.id);
337
- const method = actionConfig.method || 'POST';
338
-
339
- const response = await fetch(endpoint, {
340
- method,
341
- headers: { 'Content-Type': 'application/json' },
342
- body: method !== 'GET' ? JSON.stringify(data) : undefined
343
- });
344
-
345
- if (!response.ok) {
346
- throw new Error(`Server action failed: ${response.statusText}`);
347
- }
348
-
349
- if (actionConfig.refreshAfter !== false) {
350
- storeActions.list?.(dataSource?.sliceId ? { sliceId: dataSource.sliceId } : undefined);
351
- }
352
-
353
- return response.json().catch(() => ({}));
354
- }
355
- }, [extensionActions, storeActions, dataSource?.sliceId]);
356
-
357
- const contextValue = useMemo(() => ({
358
- viewId,
359
- config,
360
- components,
361
-
362
- viewState,
363
- selections: viewState.selections,
364
- editItems: viewState.editItems,
365
- dialogs: viewState.dialogs,
366
-
367
- pagination: viewState.pagination,
368
- paginationConfig: dataSource?.pagination,
369
- onLoadMore: handleLoadMore,
370
-
371
- setSelection,
372
- clearSelection,
373
- setEditItem,
374
- clearEditItem,
375
- openDialog,
376
- closeDialog,
377
- setComponentState,
378
-
379
- store: storeData,
380
- storeActions,
381
- items: storeData?.items || [],
382
- loading: storeLoading,
383
- error: storeError,
384
-
385
- // Extension data
386
- extension,
387
- extensionActions,
388
- extensionComponents,
389
- executeCustomAction,
390
- }), [
391
- viewId,
392
- config,
393
- components,
394
- viewState,
395
- dataSource?.pagination,
396
- storeData,
397
- storeLoading,
398
- storeError,
399
- setSelection,
400
- clearSelection,
401
- setEditItem,
402
- clearEditItem,
403
- openDialog,
404
- closeDialog,
405
- setComponentState,
406
- storeActions,
407
- handleLoadMore,
408
- extension,
409
- extensionActions,
410
- extensionComponents,
411
- executeCustomAction,
412
- ]);
413
-
414
- if (storeError && !storeData?.items?.length) {
415
- return (
416
- <View style={styles.errorContainer}>
417
- <Text style={styles.errorText}>{storeError}</Text>
418
- </View>
419
- );
420
- }
421
-
422
- const LayoutComponent = LAYOUTS[layout] || NativeStackLayout;
423
-
424
- return (
425
- <NativeJsonViewContext.Provider value={contextValue}>
426
- <View style={styles.container}>
427
- <LayoutComponent
428
- config={layoutConfig}
429
- components={components}
430
- />
431
- </View>
432
- </NativeJsonViewContext.Provider>
433
- );
434
- }
435
-
436
- const styles = StyleSheet.create({
437
- container: {
438
- flex: 1,
439
- backgroundColor: '#f5f5f5',
440
- },
441
- errorContainer: {
442
- flex: 1,
443
- justifyContent: 'center',
444
- alignItems: 'center',
445
- padding: 24,
446
- },
447
- errorText: {
448
- fontSize: 16,
449
- color: '#d32f2f',
450
- textAlign: 'center',
451
- },
452
- });
453
-
454
- export default NativeJsonView;
1
+ /**
2
+ * NativeJsonView
3
+ *
4
+ * Main orchestrator for JSON-declared views on React Native.
5
+ * Port of web JsonView — same reducer, same context shape, RN rendering.
6
+ */
7
+ import React, { useReducer, useEffect, useMemo, useCallback } from 'react';
8
+ import { View, Text, ActivityIndicator, StyleSheet } from 'react-native';
9
+ import { useNativeStore } from '../hooks/useNativeStore.js';
10
+ import { NativeStackLayout } from '../layouts/NativeStackLayout.jsx';
11
+ import { NativeTabLayout } from '../layouts/NativeTabLayout.jsx';
12
+ import { nativeExtensionRegistry } from '../extensions/NativeExtensionRegistry.js';
13
+ import { NativeJsonViewContext, useNativeJsonView } from './NativeJsonViewContext.js';
14
+
15
+ const ActionTypes = {
16
+ SET_SELECTION: 'SET_SELECTION',
17
+ CLEAR_SELECTION: 'CLEAR_SELECTION',
18
+ SET_EDIT_ITEM: 'SET_EDIT_ITEM',
19
+ CLEAR_EDIT_ITEM: 'CLEAR_EDIT_ITEM',
20
+ OPEN_DIALOG: 'OPEN_DIALOG',
21
+ CLOSE_DIALOG: 'CLOSE_DIALOG',
22
+ SET_COMPONENT_STATE: 'SET_COMPONENT_STATE',
23
+ SET_PAGINATION: 'SET_PAGINATION',
24
+ };
25
+
26
+ function getInitialPaginationState(paginationConfig) {
27
+ if (!paginationConfig || paginationConfig.mode === 'none') {
28
+ return null;
29
+ }
30
+ return {
31
+ mode: paginationConfig.mode || 'infinite',
32
+ page: 1,
33
+ pageSize: paginationConfig.pageSize || 25,
34
+ total: 0,
35
+ totalPages: 0,
36
+ hasNext: false,
37
+ hasPrev: false
38
+ };
39
+ }
40
+
41
+ const initialState = {
42
+ selections: {},
43
+ editItems: {},
44
+ dialogs: {},
45
+ componentStates: {},
46
+ pagination: null,
47
+ };
48
+
49
+ function viewReducer(state, action) {
50
+ switch (action.type) {
51
+ case ActionTypes.SET_SELECTION:
52
+ return {
53
+ ...state,
54
+ selections: { ...state.selections, [action.componentId]: action.data }
55
+ };
56
+
57
+ case ActionTypes.CLEAR_SELECTION:
58
+ return {
59
+ ...state,
60
+ selections: { ...state.selections, [action.componentId]: null }
61
+ };
62
+
63
+ case ActionTypes.SET_EDIT_ITEM:
64
+ return {
65
+ ...state,
66
+ editItems: { ...state.editItems, [action.componentId]: action.data }
67
+ };
68
+
69
+ case ActionTypes.CLEAR_EDIT_ITEM:
70
+ return {
71
+ ...state,
72
+ editItems: { ...state.editItems, [action.componentId]: null }
73
+ };
74
+
75
+ case ActionTypes.OPEN_DIALOG:
76
+ return {
77
+ ...state,
78
+ dialogs: { ...state.dialogs, [action.dialogId]: { open: true, data: action.data } }
79
+ };
80
+
81
+ case ActionTypes.CLOSE_DIALOG:
82
+ return {
83
+ ...state,
84
+ dialogs: { ...state.dialogs, [action.dialogId]: { open: false, data: null } }
85
+ };
86
+
87
+ case ActionTypes.SET_COMPONENT_STATE:
88
+ return {
89
+ ...state,
90
+ componentStates: {
91
+ ...state.componentStates,
92
+ [action.componentId]: {
93
+ ...state.componentStates[action.componentId],
94
+ ...action.state
95
+ }
96
+ }
97
+ };
98
+
99
+ case ActionTypes.SET_PAGINATION:
100
+ return {
101
+ ...state,
102
+ pagination: { ...state.pagination, ...action.pagination }
103
+ };
104
+
105
+ default:
106
+ return state;
107
+ }
108
+ }
109
+
110
+ const LAYOUTS = {
111
+ stack: NativeStackLayout,
112
+ tabs: NativeTabLayout,
113
+ single: NativeStackLayout,
114
+ split: NativeStackLayout,
115
+ };
116
+
117
+ function NativeJsonViewWithStore({ config, storeName }) {
118
+ const store = useNativeStore(storeName);
119
+
120
+ const {
121
+ id: viewId,
122
+ layout = 'stack',
123
+ layoutConfig = {},
124
+ dataSource = {},
125
+ components = [],
126
+ extension: extensionName
127
+ } = config || {};
128
+
129
+ // Get extension hook if specified
130
+ const useExtension = extensionName ? nativeExtensionRegistry.get(extensionName) : null;
131
+
132
+ const initialStateWithPagination = useMemo(() => ({
133
+ ...initialState,
134
+ pagination: getInitialPaginationState(dataSource?.pagination)
135
+ }), []);
136
+
137
+ const [viewState, dispatch] = useReducer(viewReducer, initialStateWithPagination);
138
+
139
+ const storeLoading = store.loading;
140
+ const storeError = store.error;
141
+ const storePagination = store.pagination;
142
+
143
+ const storeActions = useMemo(() => ({
144
+ list: store.list,
145
+ get: store.get,
146
+ create: store.create,
147
+ update: store.update,
148
+ delete: store.delete,
149
+ select: store.select,
150
+ clearSelection: store.clearSelection,
151
+ loadMore: store.loadMore,
152
+ setPageSize: store.setPageSize
153
+ }), [store]);
154
+
155
+ // Sync store pagination to view state
156
+ useEffect(() => {
157
+ if (viewState.pagination && storePagination) {
158
+ dispatch({
159
+ type: ActionTypes.SET_PAGINATION,
160
+ pagination: {
161
+ page: storePagination.page,
162
+ pageSize: storePagination.pageSize,
163
+ total: storePagination.total,
164
+ totalPages: storePagination.totalPages,
165
+ hasNext: storePagination.hasNext,
166
+ hasPrev: storePagination.hasPrev
167
+ }
168
+ });
169
+ }
170
+ }, [storePagination, viewState.pagination !== null]);
171
+
172
+ // Sync selections with store items after edits
173
+ const storeItems = store.items;
174
+ useEffect(() => {
175
+ if (!storeItems?.length) return;
176
+
177
+ Object.entries(viewState.selections).forEach(([componentId, selectedItem]) => {
178
+ if (!selectedItem?.id) return;
179
+
180
+ const updatedItem = storeItems.find(item => item.id === selectedItem.id);
181
+ if (updatedItem && updatedItem !== selectedItem) {
182
+ dispatch({
183
+ type: ActionTypes.SET_SELECTION,
184
+ componentId,
185
+ data: updatedItem
186
+ });
187
+ }
188
+ });
189
+ }, [storeItems]);
190
+
191
+ // Auto-load data on mount
192
+ useEffect(() => {
193
+ if (dataSource?.autoLoad) {
194
+ const loadOptions = {};
195
+ if (viewState.pagination) {
196
+ loadOptions.page = viewState.pagination.page;
197
+ loadOptions.pageSize = viewState.pagination.pageSize;
198
+ }
199
+ if (dataSource.sliceId) {
200
+ loadOptions.sliceId = dataSource.sliceId;
201
+ }
202
+ store.list(loadOptions);
203
+ }
204
+ }, []);
205
+
206
+ return (
207
+ <NativeJsonViewInner
208
+ config={config}
209
+ viewId={viewId}
210
+ layout={layout}
211
+ layoutConfig={layoutConfig}
212
+ dataSource={dataSource}
213
+ components={components}
214
+ viewState={viewState}
215
+ dispatch={dispatch}
216
+ storeData={store}
217
+ storeLoading={storeLoading}
218
+ storeError={storeError}
219
+ storeActions={storeActions}
220
+ useExtension={useExtension}
221
+ />
222
+ );
223
+ }
224
+
225
+ function NativeJsonViewWithoutStore({ config }) {
226
+ const [viewState, dispatch] = useReducer(viewReducer, initialState);
227
+
228
+ const {
229
+ id: viewId,
230
+ layout = 'stack',
231
+ layoutConfig = {},
232
+ dataSource = {},
233
+ components = [],
234
+ extension: extensionName
235
+ } = config || {};
236
+
237
+ const useExtension = extensionName ? nativeExtensionRegistry.get(extensionName) : null;
238
+
239
+ return (
240
+ <NativeJsonViewInner
241
+ config={config}
242
+ viewId={viewId}
243
+ layout={layout}
244
+ layoutConfig={layoutConfig}
245
+ dataSource={dataSource}
246
+ components={components}
247
+ viewState={viewState}
248
+ dispatch={dispatch}
249
+ storeData={null}
250
+ storeLoading={false}
251
+ storeError={null}
252
+ storeActions={{}}
253
+ useExtension={useExtension}
254
+ />
255
+ );
256
+ }
257
+
258
+ export function NativeJsonView({ config }) {
259
+ const storeName = config?.dataSource?.store;
260
+
261
+ if (storeName) {
262
+ return <NativeJsonViewWithStore config={config} storeName={storeName} />;
263
+ }
264
+ return <NativeJsonViewWithoutStore config={config} />;
265
+ }
266
+
267
+ function NativeJsonViewInner({
268
+ config,
269
+ viewId,
270
+ layout,
271
+ layoutConfig,
272
+ dataSource,
273
+ components,
274
+ viewState,
275
+ dispatch,
276
+ storeData,
277
+ storeLoading,
278
+ storeError,
279
+ storeActions,
280
+ useExtension,
281
+ }) {
282
+ const setSelection = useCallback((componentId, data) => {
283
+ dispatch({ type: ActionTypes.SET_SELECTION, componentId, data });
284
+ }, []);
285
+
286
+ const clearSelection = useCallback((componentId) => {
287
+ dispatch({ type: ActionTypes.CLEAR_SELECTION, componentId });
288
+ }, []);
289
+
290
+ const setEditItem = useCallback((componentId, data) => {
291
+ dispatch({ type: ActionTypes.SET_EDIT_ITEM, componentId, data });
292
+ }, []);
293
+
294
+ const clearEditItem = useCallback((componentId) => {
295
+ dispatch({ type: ActionTypes.CLEAR_EDIT_ITEM, componentId });
296
+ }, []);
297
+
298
+ const openDialog = useCallback((dialogId, data = null) => {
299
+ dispatch({ type: ActionTypes.OPEN_DIALOG, dialogId, data });
300
+ }, []);
301
+
302
+ const closeDialog = useCallback((dialogId) => {
303
+ dispatch({ type: ActionTypes.CLOSE_DIALOG, dialogId });
304
+ }, []);
305
+
306
+ const setComponentState = useCallback((componentId, state) => {
307
+ dispatch({ type: ActionTypes.SET_COMPONENT_STATE, componentId, state });
308
+ }, []);
309
+
310
+ const handleLoadMore = useCallback(() => {
311
+ storeActions.loadMore?.();
312
+ }, [storeActions]);
313
+
314
+ // Call extension hook if provided
315
+ const extensionContext = useMemo(() => ({
316
+ store: storeData,
317
+ storeActions,
318
+ viewState,
319
+ openDialog,
320
+ closeDialog,
321
+ setSelection,
322
+ setEditItem
323
+ }), [storeData, storeActions, viewState, openDialog, closeDialog, setSelection, setEditItem]);
324
+
325
+ const extension = useExtension ? useExtension(extensionContext) : null;
326
+ const extensionActions = extension?.actions || {};
327
+ const extensionComponents = extension?.components || {};
328
+
329
+ // Execute a custom action (from extension or server-side)
330
+ const executeCustomAction = useCallback(async (actionConfig, data) => {
331
+ if (actionConfig.handler && extensionActions[actionConfig.handler]) {
332
+ return extensionActions[actionConfig.handler](data);
333
+ }
334
+
335
+ if (actionConfig.serverAction) {
336
+ const endpoint = actionConfig.endpoint.replace(':id', data?.id);
337
+ const method = actionConfig.method || 'POST';
338
+
339
+ const response = await fetch(endpoint, {
340
+ method,
341
+ headers: { 'Content-Type': 'application/json' },
342
+ body: method !== 'GET' ? JSON.stringify(data) : undefined
343
+ });
344
+
345
+ if (!response.ok) {
346
+ throw new Error(`Server action failed: ${response.statusText}`);
347
+ }
348
+
349
+ if (actionConfig.refreshAfter !== false) {
350
+ storeActions.list?.(dataSource?.sliceId ? { sliceId: dataSource.sliceId } : undefined);
351
+ }
352
+
353
+ return response.json().catch(() => ({}));
354
+ }
355
+ }, [extensionActions, storeActions, dataSource?.sliceId]);
356
+
357
+ const contextValue = useMemo(() => ({
358
+ viewId,
359
+ config,
360
+ components,
361
+
362
+ viewState,
363
+ selections: viewState.selections,
364
+ editItems: viewState.editItems,
365
+ dialogs: viewState.dialogs,
366
+
367
+ pagination: viewState.pagination,
368
+ paginationConfig: dataSource?.pagination,
369
+ onLoadMore: handleLoadMore,
370
+
371
+ setSelection,
372
+ clearSelection,
373
+ setEditItem,
374
+ clearEditItem,
375
+ openDialog,
376
+ closeDialog,
377
+ setComponentState,
378
+
379
+ store: storeData,
380
+ storeActions,
381
+ items: storeData?.items || [],
382
+ loading: storeLoading,
383
+ error: storeError,
384
+
385
+ // Extension data
386
+ extension,
387
+ extensionActions,
388
+ extensionComponents,
389
+ executeCustomAction,
390
+ }), [
391
+ viewId,
392
+ config,
393
+ components,
394
+ viewState,
395
+ dataSource?.pagination,
396
+ storeData,
397
+ storeLoading,
398
+ storeError,
399
+ setSelection,
400
+ clearSelection,
401
+ setEditItem,
402
+ clearEditItem,
403
+ openDialog,
404
+ closeDialog,
405
+ setComponentState,
406
+ storeActions,
407
+ handleLoadMore,
408
+ extension,
409
+ extensionActions,
410
+ extensionComponents,
411
+ executeCustomAction,
412
+ ]);
413
+
414
+ if (storeError && !storeData?.items?.length) {
415
+ return (
416
+ <View style={styles.errorContainer}>
417
+ <Text style={styles.errorText}>{storeError}</Text>
418
+ </View>
419
+ );
420
+ }
421
+
422
+ const LayoutComponent = LAYOUTS[layout] || NativeStackLayout;
423
+
424
+ return (
425
+ <NativeJsonViewContext.Provider value={contextValue}>
426
+ <View style={styles.container}>
427
+ <LayoutComponent
428
+ config={layoutConfig}
429
+ components={components}
430
+ />
431
+ </View>
432
+ </NativeJsonViewContext.Provider>
433
+ );
434
+ }
435
+
436
+ const styles = StyleSheet.create({
437
+ container: {
438
+ flex: 1,
439
+ backgroundColor: '#f5f5f5',
440
+ },
441
+ errorContainer: {
442
+ flex: 1,
443
+ justifyContent: 'center',
444
+ alignItems: 'center',
445
+ padding: 24,
446
+ },
447
+ errorText: {
448
+ fontSize: 16,
449
+ color: '#d32f2f',
450
+ textAlign: 'center',
451
+ },
452
+ });
453
+
454
+ export default NativeJsonView;