-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAppContent.tsx
More file actions
734 lines (686 loc) · 34 KB
/
Copy pathAppContent.tsx
File metadata and controls
734 lines (686 loc) · 34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/**
* AppContent — inner SPA rendered under /apps/:appName/*.
*
* Owns the per-app shell: ConsoleLayout, CommandPalette, KeyboardShortcutsDialog,
* route table for object/dashboard/report/page views, and the global ModalForm
* used by ObjectView edit actions. The outer routing skeleton (BrowserRouter,
* AuthGuard, AdapterProvider, MetadataProvider, theme/toaster, /home, /login,
* /organizations) is provided by `createConsole` from @object-ui/app-shell.
*/
import { Routes, Route, Navigate, useNavigate, useLocation, useParams } from 'react-router-dom';
import { useState, useEffect, useCallback, useRef, lazy, Suspense, useMemo, type ReactNode } from 'react';
import { useAssistant } from '../assistant/assistantBus';
import { ModalForm } from '@object-ui/plugin-form';
import { Empty, EmptyTitle, EmptyDescription, Button } from '@object-ui/components';
import { toast } from 'sonner';
import { useActionRunner, useGlobalUndo } from '@object-ui/react';
import { useObjectTranslation, useObjectLabel } from '@object-ui/i18n';
import type { ConnectionState } from '@object-ui/data-objectstack';
import { useAuth } from '@object-ui/auth';
import { useMetadata } from '../providers/MetadataProvider';
import { useAdapter } from '../providers/AdapterProvider';
import { usePreviewDrafts } from '../preview/PreviewModeContext';
import { PreviewDraftEmptyState } from '../preview/PreviewDraftEmptyState';
import { ExpressionProvider, evaluateVisibility } from '../providers/ExpressionProvider';
import { useTrackRouteAsRecent } from '../hooks/useTrackRouteAsRecent';
import { resolveRecordFormTarget, resolveNavigateCreateUrl, resolveNavigateEditUrl } from '../utils/recordFormNavigation';
import { matchAppBySegment } from '../utils/appRoute';
import { resolveHref, type NavTemplateContext } from '@object-ui/layout';
import { ExpressionEvaluator } from '@object-ui/core';
// Components (eagerly loaded — always needed)
import { ConsoleLayout } from '../layout/ConsoleLayout';
import { CommandPalette } from '../chrome/CommandPalette';
import { ErrorBoundary } from '../chrome/ErrorBoundary';
import { LoadingScreen } from '../chrome/LoadingScreen';
import { ObjectView } from '../views/ObjectView';
import { KeyboardShortcutsDialog } from '../chrome/KeyboardShortcutsDialog';
import { OnboardingWalkthrough } from '../chrome/OnboardingWalkthrough';
import { RouteFader } from '../chrome/RouteFader';
import { NavigationSyncEffect } from '../hooks/useNavigationSync';
// Route-based code splitting — lazy-load less-frequently-used routes
const RecordDetailView = lazy(() => import('../views/RecordDetailView').then(m => ({ default: m.RecordDetailView })));
const DashboardView = lazy(() => import('../views/DashboardView').then(m => ({ default: m.DashboardView })));
const PageView = lazy(() => import('../views/PageView').then(m => ({ default: m.PageView })));
const ReportView = lazy(() => import('../views/ReportView').then(m => ({ default: m.ReportView })));
const SearchResultsPage = lazy(() => import('../views/SearchResultsPage').then(m => ({ default: m.SearchResultsPage })));
const RecordFormPage = lazy(() => import('../views/RecordFormPage').then(m => ({ default: m.RecordFormPage })));
const ComponentNavView = lazy(() => import('../views/ComponentNavView').then(m => ({ default: m.ComponentNavView })));
// Metadata admin — mounted under /apps/:app/metadata. Lives at the top
// level so URLs read like a normal nested resource (RFC-style) instead of
// piggy-backing on the legacy ComponentRegistry fan-out.
const MetadataDirectoryPage = lazy(() => import('../views/metadata-admin').then(m => ({ default: m.MetadataDirectoryPage })));
const StudioHomePage = lazy(() => import('../views/metadata-admin').then(m => ({ default: m.StudioHomePage })));
const MetadataResourceListPage = lazy(() => import('../views/metadata-admin').then(m => ({ default: m.MetadataResourceListPage })));
const MetadataResourceEditPage = lazy(() => import('../views/metadata-admin').then(m => ({ default: m.MetadataResourceEditPage })));
const MetadataResourceHistoryPage = lazy(() => import('../views/metadata-admin').then(m => ({ default: m.MetadataResourceHistoryPage })));
const MetadataDiagnosticsPage = lazy(() => import('../views/metadata-admin').then(m => ({ default: m.MetadataDiagnosticsPage })));
// App authoring + dashboard editor pages — sourced from
// @object-ui/plugin-designer so third-party hosts can opt out by not
// registering these routes.
const CreateAppPage = lazy(() => import('@object-ui/plugin-designer').then(m => ({ default: m.CreateAppPage })));
const EditAppPage = lazy(() => import('@object-ui/plugin-designer').then(m => ({ default: m.EditAppPage })));
const DashboardDesignPage = lazy(() => import('@object-ui/plugin-designer').then(m => ({ default: m.DashboardDesignPage })));
// Marketplace pages — first-class platform feature; mounted at `system/marketplace`
// under any active app so admins can browse + install from inside the runtime.
const MarketplacePage = lazy(() => import('./marketplace/MarketplacePage').then(m => ({ default: m.MarketplacePage })));
const MarketplacePackagePage = lazy(() => import('./marketplace/MarketplacePackagePage').then(m => ({ default: m.MarketplacePackagePage })));
const MarketplaceInstalledPage = lazy(() => import('./marketplace/MarketplaceInstalledPage').then(m => ({ default: m.MarketplaceInstalledPage })));
interface AppContentProps {
/**
* Extra <Route> elements appended to the inner /apps/:appName/* router.
* Hosts can use this to mount console-specific routes (e.g. /system, legacy
* metadata editor) without forking AppContent.
*/
extraRoutes?: ReactNode;
/**
* Extra <Route> elements rendered when there is no active app but the URL
* matches a special path (create-app, system). Mirrors `extraRoutes` for
* the no-app branch.
*/
extraRoutesNoApp?: ReactNode;
}
/**
* Bridges the global chat's "Review N change(s)" affordance (ADR-0033 Phase B)
* to the metadata designer. The chat publishes a review target on `assistantBus`;
* this navigator — which lives inside the app router and knows the app base —
* routes to `/apps/:appName/metadata/:type/:name?review=1`, where the designer
* reloads the pending draft and opens its review/diff.
*/
function DraftReviewNavigator({ appName }: { appName: string | undefined }) {
const { reviewSeq, reviewTarget } = useAssistant();
const navigate = useNavigate();
const lastSeq = useRef(reviewSeq);
useEffect(() => {
if (reviewSeq === lastSeq.current || !reviewTarget || !appName) return;
lastSeq.current = reviewSeq;
const { type, name } = reviewTarget;
navigate(
`/apps/${appName}/metadata/${encodeURIComponent(type)}/${encodeURIComponent(name)}?review=1`,
);
}, [reviewSeq, reviewTarget, appName, navigate]);
return null;
}
export function AppContent({ extraRoutes, extraRoutesNoApp }: AppContentProps = {}) {
const [connectionState, setConnectionState] = useState<ConnectionState>('disconnected');
const { user, getAuthConfig } = useAuth();
const dataSource = useAdapter();
// Deployment-level feature flags from `/api/v1/auth/config`. Used by
// CEL predicates on metadata actions (e.g. `sys_organization`'s
// create button is hidden when `multiOrgEnabled === false`). We keep
// it empty until the fetch resolves so predicates default to "visible"
// and we don't briefly hide UI on slow networks.
const [features, setFeatures] = useState<Record<string, any>>({});
useEffect(() => {
let cancelled = false;
getAuthConfig()
.then((cfg) => {
if (cancelled) return;
setFeatures((cfg?.features as Record<string, any>) ?? {});
})
.catch(() => {
/* leave empty — predicates default to visible */
});
return () => {
cancelled = true;
};
}, [getAuthConfig]);
const navigate = useNavigate();
const location = useLocation();
const { appName } = useParams();
const { apps, objects: allObjects, loading: metadataLoading, ensureType, error: metadataError, refresh: refreshMetadata } = useMetadata();
const previewDrafts = usePreviewDrafts();
const { t } = useObjectTranslation();
const { objectLabel } = useObjectLabel();
// Preload the metadata buckets that the routes under /apps/:appName/* assume
// are fully loaded by render time (the lazy MetadataProvider only eagerly
// loads `app`).
const [scopeMetaReady, setScopeMetaReady] = useState(!ensureType);
useEffect(() => {
if (!ensureType) {
setScopeMetaReady(true);
return;
}
let cancelled = false;
Promise.all([
ensureType('object'),
ensureType('dashboard'),
ensureType('report'),
ensureType('page'),
]).finally(() => {
if (!cancelled) setScopeMetaReady(true);
});
return () => { cancelled = true; };
}, [ensureType]);
// Hidden apps (`App.hidden`) are excluded from app-listing surfaces
// (sidebar switcher, home grid, app switcher). The active app for the
// current route is still looked up across ALL apps so /apps/account
// resolves correctly; only the fallback (no appName → first app)
// skips hidden apps to avoid landing the user on a personal-settings
// app by default.
const activeApps = apps.filter((a: any) => a.active !== false);
const launcherApps = activeApps.filter((a: any) => a.hidden !== true);
const activeApp =
// ADR-0048 (A) — the route segment is the package id; resolve by it,
// falling back to the app name (legacy/alias URL).
matchAppBySegment(apps, appName) ||
launcherApps.find((a: any) => a.isDefault === true) ||
launcherApps[0];
// ADR-0037 — a draft preview is a window onto ONE requested app. When the
// overlay doesn't (yet) carry it, the default-app fallback above would
// silently preview the WRONG app; treat the request as "not ready" instead
// and let the preview-specific empty state below say so.
const requestedAppMissing =
previewDrafts && !!appName && !apps.some((a: any) => a.name === appName);
useEffect(() => {
if (!activeApp?.name) return;
const packageMetadataPath = `/apps/${activeApp.name}/metadata/package`;
if (
location.pathname === packageMetadataPath ||
location.pathname.startsWith(`${packageMetadataPath}/`)
) {
navigate(`/apps/${activeApp.name}/component/developer/packages`, { replace: true });
}
}, [activeApp?.name, location.pathname, navigate]);
const [isDialogOpen, setIsDialogOpen] = useState(false);
const [editingRecord, setEditingRecord] = useState<any>(null);
const [refreshKey, setRefreshKey] = useState(0);
const { execute: executeAction, runner } = useActionRunner();
useGlobalUndo({
dataSource: dataSource ?? undefined,
onUndo: (op: any) => {
toast.info(`Undo: ${op.description}`, { duration: 4000 });
setRefreshKey(k => k + 1);
},
onRedo: (op: any) => {
toast.info(`Redo: ${op.description}`, { duration: 3000 });
setRefreshKey(k => k + 1);
},
});
useEffect(() => {
runner.registerHandler('crud_success', async (action: any) => {
setIsDialogOpen(false);
setRefreshKey(k => k + 1);
toast.success(action.params?.message ?? 'Record saved successfully');
return { success: true, reload: true };
});
runner.registerHandler('dialog_cancel', async () => {
setIsDialogOpen(false);
return { success: true };
});
// Page-mode navigation handlers — declarative counterparts to the
// imperative `handleEdit` callback. These let JSON schemas open the
// full-screen create/edit pages directly via `<action:button>` without
// any custom code:
// { "action": "navigate_create", "params": { "objectName": "..." } }
// { "action": "navigate_edit",
// "params": { "objectName": "...", "recordId": "..." } }
// The `objectName` param falls back to the action context's
// `objectName` (set per view) so action buttons mounted inside an
// ObjectView can omit it.
// NOTE on duplication below: each handler reads `runner.getContext()`
// INSIDE its closure (at action-invocation time) rather than once at
// registration. Hoisting the call outside the registrations would
// freeze the context to whatever it was when the effect last ran,
// breaking dynamic per-view `runner.updateContext({ objectName, ... })`
// calls (used by ObjectView / RecordDetailView). Keep the call where
// it is.
runner.registerHandler('navigate_create', async (action: any) => {
const ctx = runner.getContext?.() ?? {};
const result = resolveNavigateCreateUrl({
action,
context: ctx,
defaultBaseUrl: `/apps/${appName ?? ''}`,
});
if (!result.success) return result;
navigate(result.url);
return { success: true };
});
runner.registerHandler('navigate_edit', async (action: any) => {
const ctx = runner.getContext?.() ?? {};
const result = resolveNavigateEditUrl({
action,
context: ctx,
defaultBaseUrl: `/apps/${appName ?? ''}`,
});
if (!result.success) return result;
navigate(result.url);
return { success: true };
});
// NOTE: `flow` actions are handled at the per-view ActionProvider level
// (RecordDetailView / ObjectView) so they share the same ActionRunner that
// <action:button> renderers consume via useAction(). Do NOT register a
// `flow` handler on this top-level useActionRunner — it lives on a
// different ActionRunner instance and would never be invoked from the
// record/list action buttons.
}, [runner, navigate, appName]);
useEffect(() => {
if (!dataSource) return;
const unsub = dataSource.onConnectionStateChange((event: any) => {
setConnectionState(event.state);
if (event.error) console.error('[Console] Connection error:', event.error);
});
setConnectionState(dataSource.getConnectionState());
return unsub;
}, [dataSource]);
const cleanParts = location.pathname.split('/').filter(Boolean);
let objectNameFromPath = cleanParts[2];
if (
objectNameFromPath === 'view' ||
objectNameFromPath === 'record' ||
objectNameFromPath === 'page' ||
objectNameFromPath === 'dashboard' ||
objectNameFromPath === 'design'
) {
objectNameFromPath = '';
}
const currentObjectDef = allObjects.find((o: any) => o.name === objectNameFromPath);
const handleCrudSuccess = useCallback(() => {
const label = currentObjectDef ? objectLabel(currentObjectDef as any) : t('common.record', { defaultValue: 'Record' });
executeAction({
type: 'crud_success',
params: {
message: editingRecord
? t('form.updateSuccess', { object: label, defaultValue: `${label} updated successfully` })
: t('form.createSuccess', { object: label, defaultValue: `${label} created successfully` }),
},
});
}, [executeAction, editingRecord, currentObjectDef, objectLabel, t]);
const handleDialogCancel = useCallback(() => {
executeAction({ type: 'dialog_cancel' });
}, [executeAction]);
// Track recent items on route change.
useTrackRouteAsRecent({
pathname: location.pathname,
appName: activeApp?.name,
objects: allObjects,
});
const handleEdit = (record: any) => {
// Page-mode opt-in: when the object metadata declares
// `editMode: 'page'`, route to the full-screen create/edit page instead
// of opening the global ModalForm. Default behavior (modal) is
// preserved for any object without the flag.
const target = resolveRecordFormTarget({
objectDef: currentObjectDef as any,
baseUrl: activeApp?.name ? `/apps/${activeApp.name}` : '',
record,
});
if (target.kind === 'page') {
navigate(target.url);
return;
}
setEditingRecord(record);
setIsDialogOpen(true);
};
const handleAppChange = (newAppName: string) => {
navigate(`/apps/${newAppName}`);
};
const expressionEvaluator = useMemo(
() => new ExpressionEvaluator({
user: user ? { name: user.name, email: user.email, role: user.role ?? 'user' } : {},
app: activeApp || {},
data: editingRecord || {},
}),
[user, activeApp, editingRecord],
);
if (!dataSource || metadataLoading || !scopeMetaReady) return <LoadingScreen />;
// ADR-0037 — preview mode renders its OWN empty/error states and never
// falls through to the generic "No Apps Configured" guard below: inside a
// draft preview (the Live Canvas iframe, or a hand-opened ?preview=draft
// URL) that screen both lies ("nothing has been registered") and misdirects
// ("Create Your First App") about an app the AI may have just drafted.
if (previewDrafts && (requestedAppMissing || !activeApp)) {
return (
<PreviewDraftEmptyState
appName={appName}
error={metadataError}
onRetry={() => void refreshMetadata()}
/>
);
}
const isCreateAppRoute = location.pathname.endsWith('/create-app');
const isSystemRoute = location.pathname.includes('/system');
// The metadata designer (Studio) must be reachable even with no active app —
// a brand-new env where AI just drafted everything has ZERO published apps,
// so without this exemption the "no apps configured" guard below would block
// the very surface you need to REVIEW & PUBLISH those first drafts (a
// chicken-and-egg that stranded the AI magic-moment loop).
const isMetadataRoute = location.pathname.includes('/metadata');
if (!activeApp && !isCreateAppRoute && !isSystemRoute && !isMetadataRoute) return (
<div className="h-screen flex items-center justify-center">
<Empty>
<EmptyTitle>{t('empty.noAppsConfigured')}</EmptyTitle>
<EmptyDescription>
{t('empty.noAppsConfiguredDescription')}
</EmptyDescription>
<div className="mt-4 flex flex-col sm:flex-row items-center gap-3">
<Button onClick={() => navigate('/create-app')} data-testid="create-first-app-btn">
{t('empty.createFirstApp')}
</Button>
<Button variant="outline" onClick={() => navigate('/apps/setup')} data-testid="go-to-settings-btn">
{t('empty.systemSettings')}
</Button>
</div>
</Empty>
</div>
);
if (!activeApp && (isCreateAppRoute || isSystemRoute || isMetadataRoute)) {
return (
<Suspense fallback={<LoadingScreen />}>
<Routes>
<Route path="create-app" element={<CreateAppPage />} />
<Route path="system/marketplace" element={<MarketplacePage />} />
<Route path="system/marketplace/installed" element={<MarketplaceInstalledPage />} />
<Route path="system/marketplace/:packageId" element={<MarketplacePackagePage />} />
{/* Studio / metadata designer — reachable with no active app so a
fresh env can review + publish its first (AI-authored) drafts. */}
<Route path="metadata" element={<MetadataDirectoryPage />} />
<Route path="metadata/_diagnostics" element={<MetadataDiagnosticsPage />} />
<Route path="metadata/:type" element={<MetadataResourceListPage />} />
<Route path="metadata/:type/new" element={<MetadataResourceEditPage createMode />} />
<Route path="metadata/:type/:name" element={<MetadataResourceEditPage />} />
<Route path="metadata/:type/:name/history" element={<MetadataResourceHistoryPage />} />
{extraRoutesNoApp}
</Routes>
</Suspense>
);
}
const expressionUser = user
? { name: user.name, email: user.email, role: user.role ?? 'user' }
: { name: 'Anonymous', email: '', role: 'guest' };
return (
<ExpressionProvider user={expressionUser} app={activeApp} data={{}} features={features}>
<NavigationSyncEffect />
<ConsoleLayout
activeAppName={activeApp.name}
activeApp={activeApp}
onAppChange={handleAppChange}
objects={allObjects}
connectionState={connectionState}
userId={user?.id}
>
<CommandPalette
apps={apps}
activeApp={activeApp}
objects={allObjects}
onAppChange={handleAppChange}
dataSource={dataSource}
/>
<KeyboardShortcutsDialog />
<OnboardingWalkthrough />
<DraftReviewNavigator appName={appName} />
<ErrorBoundary>
<Suspense fallback={<LoadingScreen />}>
<RouteFader className="h-full">
<Routes>
<Route
path="/"
element={(() => {
// When the app declares a landing target (home page or
// first nav route) honour it; otherwise — e.g. the
// metadata-admin "Studio" app whose nav is built from
// domains and has no single landing — render the rich
// overview instead of a blank `<Navigate to="">`.
const landing = resolveLandingRoute(activeApp, { currentUserId: user?.id ?? null });
return landing ? <Navigate to={landing} replace /> : <StudioHomePage />;
})()}
/>
{/* Metadata admin routes — declared BEFORE the generic
`:objectName/...` routes so the static `metadata` prefix
wins React Router's score tiebreaker (both
`metadata/:type/:name` and `:objectName/view/:viewId`
score 16; declaration order breaks the tie). */}
<Route
path="metadata/package/*"
element={<Navigate to={`/apps/${activeApp.name}/component/developer/packages`} replace />}
/>
<Route path="metadata">
<Route index element={<MetadataDirectoryPage />} />
<Route path="_diagnostics" element={<MetadataDiagnosticsPage />} />
<Route path=":type" element={<MetadataResourceListPage />} />
<Route path=":type/new" element={<MetadataResourceEditPage createMode />} />
<Route path=":type/:name" element={<MetadataResourceEditPage />} />
<Route path=":type/:name/history" element={<MetadataResourceHistoryPage />} />
</Route>
<Route path=":objectName" element={
<ObjectView dataSource={dataSource} objects={allObjects} onEdit={handleEdit} externalRefreshKey={refreshKey} />
} />
<Route path=":objectName/new" element={
<RecordFormPage mode="create" />
} />
<Route path=":objectName/view/:viewId" element={
<ObjectView dataSource={dataSource} objects={allObjects} onEdit={handleEdit} externalRefreshKey={refreshKey} />
} />
<Route path=":objectName/record/:recordId" element={
<RecordDetailView key={refreshKey} dataSource={dataSource} objects={allObjects} onEdit={handleEdit} />
} />
<Route path=":objectName/record/:recordId/edit" element={
<RecordFormPage mode="edit" />
} />
<Route path="dashboard/:dashboardName" element={<DashboardView dataSource={dataSource} />} />
<Route path="report/:reportName" element={<ReportView dataSource={dataSource} />} />
<Route path="page/:pageName" element={<PageView />} />
<Route path="component/:ns/:name/*" element={<ComponentNavView />} />
{/* Legacy: old metadata routes built before the REST-style nesting
landed. Redirect to the new /metadata/:type/... shape. */}
<Route path="component/metadata/directory" element={<LegacyMetadataRedirect mode="directory" />} />
<Route path="component/metadata/resource/*" element={<LegacyMetadataRedirect mode="resource" />} />
<Route path="design/dashboard/:dashboardName" element={<DashboardDesignPage />} />
<Route path="search" element={<SearchResultsPage />} />
<Route path="create-app" element={<CreateAppPage />} />
<Route path="edit-app/:editAppName" element={<EditAppPage />} />
<Route path="system/marketplace" element={<MarketplacePage />} />
<Route path="system/marketplace/installed" element={<MarketplaceInstalledPage />} />
<Route path="system/marketplace/:packageId" element={<MarketplacePackagePage />} />
{extraRoutes}
{/* Shorthand-deep-link redirect: a bare `/{:objectName}/:maybeRecordId`
URL is ambiguous — it could be a view id or a record id. When
the second segment matches a record-id shape (URL-safe, ≥6
chars, not a reserved word like `new` / `view` / `record`)
we forward it to the canonical record route. This catches:
- middle/Cmd-click links that legacy producers built before
the URL builder was fixed
- externally shared / pasted links (email, Slack)
- copy-paste of a record id appended to an object URL */}
<Route path=":objectName/:maybeRecordId" element={<ShorthandRecordRedirect />} />
{/* Catch-all: render an explicit "not found" instead of a blank
page so users always know when a URL didn't resolve. */}
<Route path="*" element={<RouteNotFound />} />
</Routes>
</RouteFader>
</Suspense>
</ErrorBoundary>
{currentObjectDef && (
<ModalForm
key={editingRecord?.id || 'new'}
schema={{
type: 'object-form',
formType: 'modal',
objectName: currentObjectDef.name,
mode: editingRecord ? 'edit' : 'create',
recordId: editingRecord?.id,
// Master-detail by config: if the object's form view declares
// inline child collections, the standard New/Edit modal renders
// them as an atomic master-detail form (no bespoke page).
subforms: (currentObjectDef as any).form?.subforms
?? (currentObjectDef as any).formViews?.default?.subforms,
title: editingRecord
? t('form.editTitle', { object: objectLabel(currentObjectDef as any) })
: t('form.createTitle', { object: objectLabel(currentObjectDef as any) }),
description: editingRecord
? t('form.editDescription', { object: objectLabel(currentObjectDef as any) })
: t('form.createDescription', { object: objectLabel(currentObjectDef as any) }),
open: isDialogOpen,
onOpenChange: setIsDialogOpen,
layout: 'vertical',
fields: currentObjectDef.fields
? (Array.isArray(currentObjectDef.fields)
? currentObjectDef.fields
.filter((f: any) => {
if (typeof f === 'string') return true;
return evaluateVisibility(f.visible, expressionEvaluator);
})
.map((f: any) => typeof f === 'string' ? f : f.name)
: Object.entries(currentObjectDef.fields)
.filter(([_, f]: [string, any]) => evaluateVisibility(f.visible, expressionEvaluator))
.map(([key]: [string, any]) => key))
: [],
onSuccess: handleCrudSuccess,
onCancel: handleDialogCancel,
showSubmit: true,
showCancel: true,
submitText: editingRecord
? t('form.update', { defaultValue: t('common.save', { defaultValue: 'Save' }) })
: t('form.create', { defaultValue: t('common.create', { defaultValue: 'Create' }) }),
cancelText: t('common.cancel'),
}}
dataSource={dataSource}
/>
)}
</ConsoleLayout>
</ExpressionProvider>
);
}
function findFirstRoute(items: any[], ctx?: NavTemplateContext): string {
if (!items || items.length === 0) return '';
for (const item of items) {
if (item.type === 'object' || item.type === 'page' || item.type === 'dashboard' || item.type === 'report') {
const route = buildItemRoute(item, ctx);
if (route) return route;
continue;
}
if (item.type === 'url') continue;
if (item.type === 'group' && item.children) {
const childRoute = findFirstRoute(item.children, ctx);
if (childRoute !== '') return childRoute;
}
}
return '';
}
// Build the per-item route segment without recursing through groups —
// used when `homePageId` resolved to an exact match and we just need to
// know how to address it. Delegates to the layout package's resolveHref()
// so `recordId`/`recordMode`/`componentRef` semantics stay consistent
// with the sidebar.
function buildItemRoute(item: any, ctx?: NavTemplateContext): string {
if (!item) return '';
if (item.type === 'url' || item.type === 'action' || item.type === 'separator' || item.type === 'group') return '';
const { href, external } = resolveHref(item, '', ctx);
if (external || !href || href === '#') return '';
// resolveHref returns leading-slash paths (`/sys_user/...`); the
// Navigate target inside <Routes basename="/apps/:appName"> wants a
// *relative* path so it composes with the app base.
return href.replace(/^\//, '');
}
function findNavItemById(items: any[], id: string): any | undefined {
if (!items) return undefined;
for (const item of items) {
if (item.id === id) return item;
if (item.type === 'group' && item.children) {
const hit = findNavItemById(item.children, id);
if (hit) return hit;
}
}
return undefined;
}
/**
* Resolves the route to navigate to when the user lands on the bare
* `/console/apps/:appName` URL. Honors the app's explicit
* `homePageId` (Salesforce-style "Default Landing"); falls back to the
* first reachable nav item only when no homePageId is set or it points
* at something that doesn't yield a route. This is what lets the CRM
* example open on the Sales Dashboard instead of the Lead list.
*/
function resolveLandingRoute(activeApp: any, ctx?: NavTemplateContext): string {
const homePageId: string | undefined = activeApp?.homePageId;
const navigation = activeApp?.navigation || [];
if (homePageId) {
const item = findNavItemById(navigation, homePageId);
const route = buildItemRoute(item, ctx);
if (route) return route;
}
return findFirstRoute(navigation, ctx);
}
/**
* Heuristic: distinguish a record id from a route fragment.
*
* Record ids in this system are URL-safe slugs (alnum + `_` / `-`), typically
* 8+ chars (often 16+). They never collide with reserved second-segment
* keywords used by the route table (`new`, `view`, `record`, `dashboard`,
* `report`, `page`, `design`, `search`, `create-app`, `edit-app`).
*/
const RESERVED_SECOND_SEGMENTS = new Set([
'new', 'view', 'record', 'edit',
'dashboard', 'report', 'page',
'design', 'search', 'create-app', 'edit-app',
'metadata',
]);
function looksLikeRecordId(segment: string | undefined): boolean {
if (!segment) return false;
if (RESERVED_SECOND_SEGMENTS.has(segment)) return false;
// Allow URL-safe slug chars; reject anything with `/`, `?`, `#`, spaces.
if (!/^[A-Za-z0-9_-]+$/.test(segment)) return false;
// Most record ids are at least 6 chars (UUID, ULID, nanoid all >=8).
return segment.length >= 6;
}
/**
* Translates pre-refactor metadata admin URLs
* (`/apps/:app/component/metadata/resource/:name?type=:type`,
* `/apps/:app/component/metadata/directory`) into the new REST-style
* shape (`/apps/:app/metadata/:type/:name`). Keeps bookmarks and any
* still-unmigrated link producers working.
*/
function LegacyMetadataRedirect({ mode }: { mode: 'directory' | 'resource' }) {
const location = useLocation();
const appBase = location.pathname.replace(/\/component\/metadata\/.*$/, '');
if (mode === 'directory') {
return <Navigate to={`${appBase}/metadata${location.search}${location.hash}`} replace />;
}
const sp = new URLSearchParams(location.search);
const type = sp.get('type') ?? '';
const tail = location.pathname.match(/\/component\/metadata\/resource(\/.*)?$/)?.[1] ?? '';
const target = type
? `${appBase}/metadata/${encodeURIComponent(type)}${tail}${location.hash}`
: `${appBase}/metadata${location.hash}`;
return <Navigate to={target} replace />;
}
/**
* Redirects `/apps/:appName/:objectName/:recordId` shorthand to the
* canonical `/apps/:appName/:objectName/record/:recordId` so externally
* shared / pasted links work, and legacy URL producers that built the
* shorthand keep functioning.
*/
function ShorthandRecordRedirect() {
const { objectName, maybeRecordId } = useParams();
const location = useLocation();
if (objectName && looksLikeRecordId(maybeRecordId)) {
const target = `${location.pathname.replace(/\/$/, '').replace(`/${maybeRecordId}`, `/record/${maybeRecordId}`)}${location.search}${location.hash}`;
return <Navigate to={target} replace />;
}
return <RouteNotFound />;
}
/**
* Visible "not found" fallback rendered for any unmatched URL inside the
* console app shell. Previously these URLs produced a fully blank content
* area with no indication that anything had gone wrong — users would think
* the app had crashed. An explicit Empty state with a "back to app home"
* action turns an opaque failure into a recoverable one.
*/
function RouteNotFound() {
const navigate = useNavigate();
const { t } = useObjectTranslation();
return (
<div className="flex h-full w-full items-center justify-center p-8">
<Empty>
<EmptyTitle>{t('console.notFound.title', { defaultValue: 'Page not found' })}</EmptyTitle>
<EmptyDescription>
{t('console.notFound.description', { defaultValue: 'The URL you followed does not match any view in this app.' })}
</EmptyDescription>
<div className="mt-4">
<Button variant="outline" onClick={() => navigate(-1)}>
{t('console.notFound.back', { defaultValue: 'Go back' })}
</Button>
</div>
</Empty>
</div>
);
}