@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,385 +1,385 @@
1
- /**
2
- * MobileLayout Component
3
- *
4
- * Core mobile shell with bottom navigation (mobile viewport) or
5
- * top AppBar with horizontal tabs (desktop viewport).
6
- *
7
- * Derives tabs from ViewRegistry, resolves icons via IconRegistry,
8
- * and supports fullscreen view detection.
9
- *
10
- * Bottom navigation follows a ForeFlight-inspired pattern:
11
- * [pinned tabs] [recently visited tabs] [...overflow]
12
- *
13
- * - Pinned tabs: always shown first (configured via shell.mobile.pinnedTabs)
14
- * - Recent tabs: recently visited views fill remaining slots
15
- * - Overflow "...": opens a drawer with all views (when overflowEnabled)
16
- */
17
- import React, { useMemo, useState, useEffect, useCallback, useRef } from 'react';
18
- import { useNavigate, useLocation, matchPath } from 'react-router-dom';
19
- import {
20
- Box,
21
- BottomNavigation,
22
- BottomNavigationAction,
23
- AppBar,
24
- Toolbar,
25
- Typography,
26
- } from '@mui/material';
27
- import MoreHorizIcon from '@mui/icons-material/MoreHoriz';
28
-
29
- import { useViews } from '../../hooks/useViews.js';
30
- import { useIsMobile } from '../../hooks/useIsMobile.js';
31
- import { resolveIcon } from '../IconRegistry/index.js';
32
- import { useShellConfig } from '../AppShell/ShellConfig.js';
33
-
34
- // localStorage key for persisting recent tabs
35
- const RECENT_TABS_KEY = 'avento_recent_tabs';
36
- const MAX_STORED_RECENT = 20;
37
-
38
- function loadRecentViewIds() {
39
- try {
40
- const stored = localStorage.getItem(RECENT_TABS_KEY);
41
- return stored ? JSON.parse(stored) : [];
42
- } catch {
43
- return [];
44
- }
45
- }
46
-
47
- function saveRecentViewIds(ids) {
48
- try {
49
- localStorage.setItem(RECENT_TABS_KEY, JSON.stringify(ids.slice(0, MAX_STORED_RECENT)));
50
- } catch {
51
- // Ignore storage errors
52
- }
53
- }
54
-
55
- /**
56
- * Build the full tab list from ViewRegistry views.
57
- */
58
- function useAllTabs() {
59
- const { views: allViews } = useViews({ includeHidden: false });
60
-
61
- return useMemo(() => {
62
- return allViews
63
- .filter(v => !v.parent && !v.hidden && v.icon)
64
- .sort((a, b) => (a.order || 100) - (b.order || 100))
65
- .map(v => ({
66
- id: v.id,
67
- label: v.label,
68
- icon: resolveIcon(v.icon),
69
- path: v.route,
70
- meta: v.meta || {},
71
- }));
72
- }, [allViews]);
73
- }
74
-
75
- /**
76
- * Determine which view ID is active based on current pathname.
77
- */
78
- function useActiveViewId(allTabs) {
79
- const location = useLocation();
80
- const { views: allViews } = useViews({ includeHidden: true });
81
-
82
- return useMemo(() => {
83
- const pathname = location.pathname;
84
-
85
- // Check direct child views (match child route → parent tab)
86
- const tabIdSet = new Set(allTabs.map(t => t.id));
87
- for (const view of allViews) {
88
- if (view.parent && tabIdSet.has(view.parent)) {
89
- if (matchPath({ path: view.route, end: true }, pathname)) {
90
- return view.parent;
91
- }
92
- }
93
- }
94
-
95
- // Check tab routes: exact for '/', prefix for others
96
- for (const tab of allTabs) {
97
- if (tab.path === '/') {
98
- if (pathname === '/') return tab.id;
99
- } else {
100
- if (pathname.startsWith(tab.path)) return tab.id;
101
- }
102
- }
103
-
104
- return allTabs[0]?.id || null;
105
- }, [location.pathname, allTabs, allViews]);
106
- }
107
-
108
- /**
109
- * Detect if the current route is a fullscreen view.
110
- */
111
- function useIsFullscreen(shellConfig) {
112
- const location = useLocation();
113
- const { views: allViews } = useViews({ includeHidden: true });
114
-
115
- return useMemo(() => {
116
- const pathname = location.pathname;
117
-
118
- for (const view of allViews) {
119
- if (view.meta?.fullscreen) {
120
- if (matchPath({ path: view.route, end: true }, pathname)) {
121
- return true;
122
- }
123
- }
124
- }
125
-
126
- const routes = shellConfig?.fullscreenRoutes || [];
127
- for (const route of routes) {
128
- if (pathname.startsWith(route)) return true;
129
- }
130
-
131
- return false;
132
- }, [location.pathname, allViews, shellConfig]);
133
- }
134
-
135
- /**
136
- * Build the visible bottom-nav tab set using ForeFlight-style logic:
137
- * [pinned...] [recent...] [...overflow]
138
- *
139
- * Tabs with meta.overflowHub=true are excluded from all visible slots and
140
- * from recent history — they are represented by the "..." overflow button,
141
- * and their children populate the overflow drawer.
142
- */
143
- function useVisibleTabs(allTabs, mobileConfig, recentViewIds) {
144
- const pinnedIds = mobileConfig?.pinnedTabs || [];
145
- const maxTabs = mobileConfig?.maxTabs || 5;
146
- const recentTabsCount = mobileConfig?.recentTabsCount ?? 1;
147
- const overflowEnabled = mobileConfig?.overflowEnabled !== false;
148
-
149
- return useMemo(() => {
150
- // Separate overflowHub tabs from navigable tabs
151
- const navigableTabs = allTabs.filter(t => !t.meta?.overflowHub);
152
- const navigableById = new Map(navigableTabs.map(t => [t.id, t]));
153
- const hasOverflowHub = allTabs.some(t => t.meta?.overflowHub);
154
-
155
- // 1. Pinned tabs (config order, from navigable tabs only)
156
- const pinnedTabs = pinnedIds.map(id => navigableById.get(id)).filter(Boolean);
157
-
158
- // 2. Slots for recent tabs
159
- const overflowSlot = overflowEnabled ? 1 : 0;
160
- const availableForRecent = Math.max(0, maxTabs - pinnedTabs.length - overflowSlot);
161
- const recentSlots = Math.min(recentTabsCount, availableForRecent);
162
-
163
- // 3. Recent tabs: from history, excluding pinned and overflowHub
164
- const pinnedIdSet = new Set(pinnedIds);
165
- const recentTabs = recentViewIds
166
- .filter(id => !pinnedIdSet.has(id) && navigableById.has(id))
167
- .slice(0, recentSlots)
168
- .map(id => navigableById.get(id));
169
-
170
- const visible = [...pinnedTabs, ...recentTabs];
171
-
172
- // 4. Determine if overflow is needed
173
- const visibleIdSet = new Set(visible.map(t => t.id));
174
- const hasOverflow = overflowEnabled && (
175
- hasOverflowHub || navigableTabs.some(t => !visibleIdSet.has(t.id))
176
- );
177
-
178
- return { visibleTabs: visible, hasOverflow };
179
- }, [allTabs, pinnedIds, maxTabs, recentTabsCount, overflowEnabled, recentViewIds]);
180
- }
181
-
182
-
183
- /**
184
- * MobileLayout Component
185
- */
186
- export function MobileLayout({
187
- children,
188
- title: titleProp,
189
- logo: logoProp,
190
- userMenu,
191
- shellConfig: shellConfigProp,
192
- }) {
193
- const contextConfig = useShellConfig();
194
- const shellConfig = shellConfigProp || contextConfig;
195
- const title = titleProp || shellConfig.title || 'Avento App';
196
- const logo = logoProp || shellConfig.logo;
197
- const mobileConfig = shellConfig.mobile || {};
198
-
199
- const allTabs = useAllTabs();
200
- const [recentViewIds, setRecentViewIds] = useState(() => loadRecentViewIds());
201
-
202
- const { visibleTabs, hasOverflow } = useVisibleTabs(allTabs, mobileConfig, recentViewIds);
203
- const activeViewId = useActiveViewId(allTabs);
204
- const isFullscreen = useIsFullscreen(shellConfig);
205
- const isMobile = useIsMobile(shellConfig.breakpoint);
206
- const navigate = useNavigate();
207
- const location = useLocation();
208
-
209
- // Track recently visited views (skip overflowHub tabs)
210
- const prevPathnameRef = useRef(null);
211
- useEffect(() => {
212
- if (prevPathnameRef.current === location.pathname) return;
213
- prevPathnameRef.current = location.pathname;
214
- if (!activeViewId) return;
215
-
216
- // Don't track overflowHub views in recent history
217
- const activeTab = allTabs.find(t => t.id === activeViewId);
218
- if (activeTab?.meta?.overflowHub) return;
219
-
220
- setRecentViewIds(prev => {
221
- const filtered = prev.filter(id => id !== activeViewId);
222
- const next = [activeViewId, ...filtered];
223
- saveRecentViewIds(next);
224
- return next;
225
- });
226
- }, [location.pathname, activeViewId, allTabs]);
227
-
228
- // Prevent body scroll on mobile (iOS rubber-band prevention)
229
- useEffect(() => {
230
- if (!isMobile) return;
231
- document.documentElement.style.overflow = 'hidden';
232
- document.body.style.overflow = 'hidden';
233
- return () => {
234
- document.documentElement.style.overflow = '';
235
- document.body.style.overflow = '';
236
- };
237
- }, [isMobile]);
238
-
239
- const handleNavigate = useCallback((path) => {
240
- navigate(path);
241
- }, [navigate]);
242
-
243
- // Active tab index in visibleTabs
244
- const activeTabIndex = useMemo(() => {
245
- const idx = visibleTabs.findIndex(t => t.id === activeViewId);
246
- return idx >= 0 ? idx : false;
247
- }, [visibleTabs, activeViewId]);
248
-
249
- // Fullscreen: no chrome
250
- if (isFullscreen) {
251
- return <>{children}</>;
252
- }
253
-
254
- // Mobile viewport: bottom navigation
255
- if (isMobile) {
256
- return (
257
- <Box sx={{ display: 'flex', flexDirection: 'column', height: '100dvh', position: 'fixed', inset: 0 }}>
258
- {/* Main content */}
259
- <Box sx={{
260
- flex: 1,
261
- overflow: 'auto',
262
- WebkitOverflowScrolling: 'touch',
263
- // Padding for fixed bottom nav + iOS safe area
264
- pb: 'calc(56px + env(safe-area-inset-bottom, 0px))',
265
- }}>
266
- {children}
267
- </Box>
268
-
269
- {/* Fixed bottom navigation */}
270
- <Box sx={{
271
- position: 'fixed',
272
- bottom: 0,
273
- left: 0,
274
- right: 0,
275
- zIndex: 1200,
276
- borderTop: 1,
277
- borderColor: 'divider',
278
- bgcolor: 'background.paper',
279
- paddingBottom: 'env(safe-area-inset-bottom, 0px)',
280
- overflow: 'visible',
281
- }}>
282
- <BottomNavigation
283
- value={activeTabIndex}
284
- onChange={(_, newValue) => {
285
- if (newValue >= 0 && newValue < visibleTabs.length) {
286
- handleNavigate(visibleTabs[newValue].path);
287
- }
288
- }}
289
- showLabels
290
- sx={{ height: 56, overflow: 'visible' }}
291
- >
292
- {visibleTabs.map((tab) => (
293
- <BottomNavigationAction
294
- key={tab.id}
295
- label={tab.meta?.fab ? undefined : tab.label}
296
- icon={
297
- tab.meta?.fab ? (
298
- <Box sx={{
299
- bgcolor: 'primary.main',
300
- color: 'white',
301
- borderRadius: '50%',
302
- width: 56,
303
- height: 56,
304
- display: 'flex',
305
- flexDirection: 'column',
306
- alignItems: 'center',
307
- justifyContent: 'center',
308
- mt: -1.5,
309
- boxShadow: 3,
310
- gap: 0.25,
311
- }}>
312
- {tab.icon}
313
- <Typography variant="caption" sx={{ fontSize: '0.6rem', lineHeight: 1, color: 'inherit' }}>
314
- {tab.label}
315
- </Typography>
316
- </Box>
317
- ) : tab.icon
318
- }
319
- sx={tab.meta?.fab ? { '&.Mui-selected': { color: 'text.secondary' } } : { minWidth: 0, px: 0.5 }}
320
- />
321
- ))}
322
-
323
- {/* Overflow "..." tab — navigates to the overflowHub's page */}
324
- {hasOverflow && (() => {
325
- const hubTab = allTabs.find(t => t.meta?.overflowHub);
326
- if (!hubTab) return null;
327
- return (
328
- <BottomNavigationAction
329
- label={hubTab.label}
330
- icon={<MoreHorizIcon />}
331
- value={-1}
332
- onClick={() => handleNavigate(hubTab.path)}
333
- sx={{ minWidth: 0, px: 0.5 }}
334
- />
335
- );
336
- })()}
337
- </BottomNavigation>
338
- </Box>
339
- </Box>
340
- );
341
- }
342
-
343
- // Desktop viewport: top AppBar with horizontal tabs
344
- const desktopTabs = allTabs.slice(0, mobileConfig.maxTabs || 8);
345
- const activeDesktopIdx = desktopTabs.findIndex(t => t.id === activeViewId);
346
-
347
- return (
348
- <Box sx={{ display: 'flex', flexDirection: 'column', height: '100vh' }}>
349
- <AppBar position="fixed" color="primary" elevation={1}>
350
- <Toolbar>
351
- {logo || (
352
- <Typography variant="h6" sx={{ mr: 2 }}>
353
- {title}
354
- </Typography>
355
- )}
356
- <Box sx={{ flexGrow: 1, display: 'flex', overflow: 'hidden' }}>
357
- {desktopTabs.map((tab, idx) => (
358
- <Box
359
- key={tab.id}
360
- onClick={() => navigate(tab.path)}
361
- sx={{
362
- px: 2,
363
- py: 1,
364
- cursor: 'pointer',
365
- whiteSpace: 'nowrap',
366
- borderBottom: activeDesktopIdx === idx ? '2px solid white' : '2px solid transparent',
367
- '&:hover': { bgcolor: 'rgba(255,255,255,0.1)' },
368
- }}
369
- >
370
- <Typography variant="body2" color="inherit">{tab.label}</Typography>
371
- </Box>
372
- ))}
373
- </Box>
374
- {userMenu}
375
- </Toolbar>
376
- </AppBar>
377
- <Toolbar /> {/* Spacer for fixed AppBar */}
378
- <Box sx={{ flex: 1, overflow: 'auto' }}>
379
- {children}
380
- </Box>
381
- </Box>
382
- );
383
- }
384
-
385
- export default MobileLayout;
1
+ /**
2
+ * MobileLayout Component
3
+ *
4
+ * Core mobile shell with bottom navigation (mobile viewport) or
5
+ * top AppBar with horizontal tabs (desktop viewport).
6
+ *
7
+ * Derives tabs from ViewRegistry, resolves icons via IconRegistry,
8
+ * and supports fullscreen view detection.
9
+ *
10
+ * Bottom navigation follows a ForeFlight-inspired pattern:
11
+ * [pinned tabs] [recently visited tabs] [...overflow]
12
+ *
13
+ * - Pinned tabs: always shown first (configured via shell.mobile.pinnedTabs)
14
+ * - Recent tabs: recently visited views fill remaining slots
15
+ * - Overflow "...": opens a drawer with all views (when overflowEnabled)
16
+ */
17
+ import React, { useMemo, useState, useEffect, useCallback, useRef } from 'react';
18
+ import { useNavigate, useLocation, matchPath } from 'react-router-dom';
19
+ import {
20
+ Box,
21
+ BottomNavigation,
22
+ BottomNavigationAction,
23
+ AppBar,
24
+ Toolbar,
25
+ Typography,
26
+ } from '@mui/material';
27
+ import MoreHorizIcon from '@mui/icons-material/MoreHoriz';
28
+
29
+ import { useViews } from '../../hooks/useViews.js';
30
+ import { useIsMobile } from '../../hooks/useIsMobile.js';
31
+ import { resolveIcon } from '../IconRegistry/index.js';
32
+ import { useShellConfig } from '../AppShell/ShellConfig.js';
33
+
34
+ // localStorage key for persisting recent tabs
35
+ const RECENT_TABS_KEY = 'avento_recent_tabs';
36
+ const MAX_STORED_RECENT = 20;
37
+
38
+ function loadRecentViewIds() {
39
+ try {
40
+ const stored = localStorage.getItem(RECENT_TABS_KEY);
41
+ return stored ? JSON.parse(stored) : [];
42
+ } catch {
43
+ return [];
44
+ }
45
+ }
46
+
47
+ function saveRecentViewIds(ids) {
48
+ try {
49
+ localStorage.setItem(RECENT_TABS_KEY, JSON.stringify(ids.slice(0, MAX_STORED_RECENT)));
50
+ } catch {
51
+ // Ignore storage errors
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Build the full tab list from ViewRegistry views.
57
+ */
58
+ function useAllTabs() {
59
+ const { views: allViews } = useViews({ includeHidden: false });
60
+
61
+ return useMemo(() => {
62
+ return allViews
63
+ .filter(v => !v.parent && !v.hidden && v.icon)
64
+ .sort((a, b) => (a.order || 100) - (b.order || 100))
65
+ .map(v => ({
66
+ id: v.id,
67
+ label: v.label,
68
+ icon: resolveIcon(v.icon),
69
+ path: v.route,
70
+ meta: v.meta || {},
71
+ }));
72
+ }, [allViews]);
73
+ }
74
+
75
+ /**
76
+ * Determine which view ID is active based on current pathname.
77
+ */
78
+ function useActiveViewId(allTabs) {
79
+ const location = useLocation();
80
+ const { views: allViews } = useViews({ includeHidden: true });
81
+
82
+ return useMemo(() => {
83
+ const pathname = location.pathname;
84
+
85
+ // Check direct child views (match child route → parent tab)
86
+ const tabIdSet = new Set(allTabs.map(t => t.id));
87
+ for (const view of allViews) {
88
+ if (view.parent && tabIdSet.has(view.parent)) {
89
+ if (matchPath({ path: view.route, end: true }, pathname)) {
90
+ return view.parent;
91
+ }
92
+ }
93
+ }
94
+
95
+ // Check tab routes: exact for '/', prefix for others
96
+ for (const tab of allTabs) {
97
+ if (tab.path === '/') {
98
+ if (pathname === '/') return tab.id;
99
+ } else {
100
+ if (pathname.startsWith(tab.path)) return tab.id;
101
+ }
102
+ }
103
+
104
+ return allTabs[0]?.id || null;
105
+ }, [location.pathname, allTabs, allViews]);
106
+ }
107
+
108
+ /**
109
+ * Detect if the current route is a fullscreen view.
110
+ */
111
+ function useIsFullscreen(shellConfig) {
112
+ const location = useLocation();
113
+ const { views: allViews } = useViews({ includeHidden: true });
114
+
115
+ return useMemo(() => {
116
+ const pathname = location.pathname;
117
+
118
+ for (const view of allViews) {
119
+ if (view.meta?.fullscreen) {
120
+ if (matchPath({ path: view.route, end: true }, pathname)) {
121
+ return true;
122
+ }
123
+ }
124
+ }
125
+
126
+ const routes = shellConfig?.fullscreenRoutes || [];
127
+ for (const route of routes) {
128
+ if (pathname.startsWith(route)) return true;
129
+ }
130
+
131
+ return false;
132
+ }, [location.pathname, allViews, shellConfig]);
133
+ }
134
+
135
+ /**
136
+ * Build the visible bottom-nav tab set using ForeFlight-style logic:
137
+ * [pinned...] [recent...] [...overflow]
138
+ *
139
+ * Tabs with meta.overflowHub=true are excluded from all visible slots and
140
+ * from recent history — they are represented by the "..." overflow button,
141
+ * and their children populate the overflow drawer.
142
+ */
143
+ function useVisibleTabs(allTabs, mobileConfig, recentViewIds) {
144
+ const pinnedIds = mobileConfig?.pinnedTabs || [];
145
+ const maxTabs = mobileConfig?.maxTabs || 5;
146
+ const recentTabsCount = mobileConfig?.recentTabsCount ?? 1;
147
+ const overflowEnabled = mobileConfig?.overflowEnabled !== false;
148
+
149
+ return useMemo(() => {
150
+ // Separate overflowHub tabs from navigable tabs
151
+ const navigableTabs = allTabs.filter(t => !t.meta?.overflowHub);
152
+ const navigableById = new Map(navigableTabs.map(t => [t.id, t]));
153
+ const hasOverflowHub = allTabs.some(t => t.meta?.overflowHub);
154
+
155
+ // 1. Pinned tabs (config order, from navigable tabs only)
156
+ const pinnedTabs = pinnedIds.map(id => navigableById.get(id)).filter(Boolean);
157
+
158
+ // 2. Slots for recent tabs
159
+ const overflowSlot = overflowEnabled ? 1 : 0;
160
+ const availableForRecent = Math.max(0, maxTabs - pinnedTabs.length - overflowSlot);
161
+ const recentSlots = Math.min(recentTabsCount, availableForRecent);
162
+
163
+ // 3. Recent tabs: from history, excluding pinned and overflowHub
164
+ const pinnedIdSet = new Set(pinnedIds);
165
+ const recentTabs = recentViewIds
166
+ .filter(id => !pinnedIdSet.has(id) && navigableById.has(id))
167
+ .slice(0, recentSlots)
168
+ .map(id => navigableById.get(id));
169
+
170
+ const visible = [...pinnedTabs, ...recentTabs];
171
+
172
+ // 4. Determine if overflow is needed
173
+ const visibleIdSet = new Set(visible.map(t => t.id));
174
+ const hasOverflow = overflowEnabled && (
175
+ hasOverflowHub || navigableTabs.some(t => !visibleIdSet.has(t.id))
176
+ );
177
+
178
+ return { visibleTabs: visible, hasOverflow };
179
+ }, [allTabs, pinnedIds, maxTabs, recentTabsCount, overflowEnabled, recentViewIds]);
180
+ }
181
+
182
+
183
+ /**
184
+ * MobileLayout Component
185
+ */
186
+ export function MobileLayout({
187
+ children,
188
+ title: titleProp,
189
+ logo: logoProp,
190
+ userMenu,
191
+ shellConfig: shellConfigProp,
192
+ }) {
193
+ const contextConfig = useShellConfig();
194
+ const shellConfig = shellConfigProp || contextConfig;
195
+ const title = titleProp || shellConfig.title || 'Avento App';
196
+ const logo = logoProp || shellConfig.logo;
197
+ const mobileConfig = shellConfig.mobile || {};
198
+
199
+ const allTabs = useAllTabs();
200
+ const [recentViewIds, setRecentViewIds] = useState(() => loadRecentViewIds());
201
+
202
+ const { visibleTabs, hasOverflow } = useVisibleTabs(allTabs, mobileConfig, recentViewIds);
203
+ const activeViewId = useActiveViewId(allTabs);
204
+ const isFullscreen = useIsFullscreen(shellConfig);
205
+ const isMobile = useIsMobile(shellConfig.breakpoint);
206
+ const navigate = useNavigate();
207
+ const location = useLocation();
208
+
209
+ // Track recently visited views (skip overflowHub tabs)
210
+ const prevPathnameRef = useRef(null);
211
+ useEffect(() => {
212
+ if (prevPathnameRef.current === location.pathname) return;
213
+ prevPathnameRef.current = location.pathname;
214
+ if (!activeViewId) return;
215
+
216
+ // Don't track overflowHub views in recent history
217
+ const activeTab = allTabs.find(t => t.id === activeViewId);
218
+ if (activeTab?.meta?.overflowHub) return;
219
+
220
+ setRecentViewIds(prev => {
221
+ const filtered = prev.filter(id => id !== activeViewId);
222
+ const next = [activeViewId, ...filtered];
223
+ saveRecentViewIds(next);
224
+ return next;
225
+ });
226
+ }, [location.pathname, activeViewId, allTabs]);
227
+
228
+ // Prevent body scroll on mobile (iOS rubber-band prevention)
229
+ useEffect(() => {
230
+ if (!isMobile) return;
231
+ document.documentElement.style.overflow = 'hidden';
232
+ document.body.style.overflow = 'hidden';
233
+ return () => {
234
+ document.documentElement.style.overflow = '';
235
+ document.body.style.overflow = '';
236
+ };
237
+ }, [isMobile]);
238
+
239
+ const handleNavigate = useCallback((path) => {
240
+ navigate(path);
241
+ }, [navigate]);
242
+
243
+ // Active tab index in visibleTabs
244
+ const activeTabIndex = useMemo(() => {
245
+ const idx = visibleTabs.findIndex(t => t.id === activeViewId);
246
+ return idx >= 0 ? idx : false;
247
+ }, [visibleTabs, activeViewId]);
248
+
249
+ // Fullscreen: no chrome
250
+ if (isFullscreen) {
251
+ return <>{children}</>;
252
+ }
253
+
254
+ // Mobile viewport: bottom navigation
255
+ if (isMobile) {
256
+ return (
257
+ <Box sx={{ display: 'flex', flexDirection: 'column', height: '100dvh', position: 'fixed', inset: 0 }}>
258
+ {/* Main content */}
259
+ <Box sx={{
260
+ flex: 1,
261
+ overflow: 'auto',
262
+ WebkitOverflowScrolling: 'touch',
263
+ // Padding for fixed bottom nav + iOS safe area
264
+ pb: 'calc(56px + env(safe-area-inset-bottom, 0px))',
265
+ }}>
266
+ {children}
267
+ </Box>
268
+
269
+ {/* Fixed bottom navigation */}
270
+ <Box sx={{
271
+ position: 'fixed',
272
+ bottom: 0,
273
+ left: 0,
274
+ right: 0,
275
+ zIndex: 1200,
276
+ borderTop: 1,
277
+ borderColor: 'divider',
278
+ bgcolor: 'background.paper',
279
+ paddingBottom: 'env(safe-area-inset-bottom, 0px)',
280
+ overflow: 'visible',
281
+ }}>
282
+ <BottomNavigation
283
+ value={activeTabIndex}
284
+ onChange={(_, newValue) => {
285
+ if (newValue >= 0 && newValue < visibleTabs.length) {
286
+ handleNavigate(visibleTabs[newValue].path);
287
+ }
288
+ }}
289
+ showLabels
290
+ sx={{ height: 56, overflow: 'visible' }}
291
+ >
292
+ {visibleTabs.map((tab) => (
293
+ <BottomNavigationAction
294
+ key={tab.id}
295
+ label={tab.meta?.fab ? undefined : tab.label}
296
+ icon={
297
+ tab.meta?.fab ? (
298
+ <Box sx={{
299
+ bgcolor: 'primary.main',
300
+ color: 'white',
301
+ borderRadius: '50%',
302
+ width: 56,
303
+ height: 56,
304
+ display: 'flex',
305
+ flexDirection: 'column',
306
+ alignItems: 'center',
307
+ justifyContent: 'center',
308
+ mt: -1.5,
309
+ boxShadow: 3,
310
+ gap: 0.25,
311
+ }}>
312
+ {tab.icon}
313
+ <Typography variant="caption" sx={{ fontSize: '0.6rem', lineHeight: 1, color: 'inherit' }}>
314
+ {tab.label}
315
+ </Typography>
316
+ </Box>
317
+ ) : tab.icon
318
+ }
319
+ sx={tab.meta?.fab ? { '&.Mui-selected': { color: 'text.secondary' } } : { minWidth: 0, px: 0.5 }}
320
+ />
321
+ ))}
322
+
323
+ {/* Overflow "..." tab — navigates to the overflowHub's page */}
324
+ {hasOverflow && (() => {
325
+ const hubTab = allTabs.find(t => t.meta?.overflowHub);
326
+ if (!hubTab) return null;
327
+ return (
328
+ <BottomNavigationAction
329
+ label={hubTab.label}
330
+ icon={<MoreHorizIcon />}
331
+ value={-1}
332
+ onClick={() => handleNavigate(hubTab.path)}
333
+ sx={{ minWidth: 0, px: 0.5 }}
334
+ />
335
+ );
336
+ })()}
337
+ </BottomNavigation>
338
+ </Box>
339
+ </Box>
340
+ );
341
+ }
342
+
343
+ // Desktop viewport: top AppBar with horizontal tabs
344
+ const desktopTabs = allTabs.slice(0, mobileConfig.maxTabs || 8);
345
+ const activeDesktopIdx = desktopTabs.findIndex(t => t.id === activeViewId);
346
+
347
+ return (
348
+ <Box sx={{ display: 'flex', flexDirection: 'column', height: '100vh' }}>
349
+ <AppBar position="fixed" color="primary" elevation={1}>
350
+ <Toolbar>
351
+ {logo || (
352
+ <Typography variant="h6" sx={{ mr: 2 }}>
353
+ {title}
354
+ </Typography>
355
+ )}
356
+ <Box sx={{ flexGrow: 1, display: 'flex', overflow: 'hidden' }}>
357
+ {desktopTabs.map((tab, idx) => (
358
+ <Box
359
+ key={tab.id}
360
+ onClick={() => navigate(tab.path)}
361
+ sx={{
362
+ px: 2,
363
+ py: 1,
364
+ cursor: 'pointer',
365
+ whiteSpace: 'nowrap',
366
+ borderBottom: activeDesktopIdx === idx ? '2px solid white' : '2px solid transparent',
367
+ '&:hover': { bgcolor: 'rgba(255,255,255,0.1)' },
368
+ }}
369
+ >
370
+ <Typography variant="body2" color="inherit">{tab.label}</Typography>
371
+ </Box>
372
+ ))}
373
+ </Box>
374
+ {userMenu}
375
+ </Toolbar>
376
+ </AppBar>
377
+ <Toolbar /> {/* Spacer for fixed AppBar */}
378
+ <Box sx={{ flex: 1, overflow: 'auto' }}>
379
+ {children}
380
+ </Box>
381
+ </Box>
382
+ );
383
+ }
384
+
385
+ export default MobileLayout;