-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAppSchemaRenderer.tsx
More file actions
520 lines (468 loc) · 16.2 KB
/
Copy pathAppSchemaRenderer.tsx
File metadata and controls
520 lines (468 loc) · 16.2 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
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* @object-ui/layout - AppSchema Renderer
*
* Consumes an `AppSchema` JSON object and renders a complete application
* shell with branding, sidebar navigation (including area switching),
* and mobile navigation modes.
*
* This is the main P0.1 deliverable — it allows Console (or any consumer)
* to render a fully-functional AppShell from a single JSON document.
*
* @module AppSchemaRenderer
*/
import React, { useState, useEffect, useMemo } from 'react';
import { Link, useLocation } from 'react-router-dom';
import { Layers } from 'lucide-react';
import {
Sidebar,
SidebarHeader,
SidebarContent,
SidebarFooter,
SidebarMenu,
SidebarMenuItem,
SidebarMenuButton,
SidebarGroup,
SidebarGroupLabel,
SidebarGroupContent,
SidebarInput,
useSidebar,
} from '@object-ui/components';
import type { AppSchema, NavigationItem, NavigationArea } from '@object-ui/types';
import { menuItemToNavigationItem } from '@object-ui/types';
import { AppShell, type AppShellBranding } from './AppShell';
import {
NavigationRenderer,
resolveIcon,
resolveLabel,
type VisibilityEvaluator,
type PermissionChecker,
type CapabilityChecker,
} from './NavigationRenderer';
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
/** Mobile navigation display mode */
export type MobileNavMode = 'drawer' | 'bottom_nav' | 'hamburger';
export interface AppSchemaRendererProps {
/** The AppSchema JSON to render */
schema: AppSchema;
/** Base URL prefix for generated hrefs (e.g. "/apps/crm") */
basePath?: string;
/** Mobile navigation mode @default "drawer" */
mobileNavMode?: MobileNavMode;
/** Optional visibility evaluator passed to NavigationRenderer */
evaluateVisibility?: VisibilityEvaluator;
/** Optional permission checker passed to NavigationRenderer */
checkPermission?: PermissionChecker;
/** Optional capability checker passed to NavigationRenderer (gates `requiresObject` / `requiresService`) */
checkCapability?: CapabilityChecker;
/** Called when an action-type navigation item is clicked */
onAction?: (item: NavigationItem) => void;
/** Slot: top navbar content (rendered beside the sidebar trigger) */
navbar?: React.ReactNode;
/** Slot: sidebar header (e.g. app switcher dropdown). Replaces default branding header when provided. */
sidebarHeader?: React.ReactNode;
/** Slot: sidebar footer (e.g. user profile menu) */
sidebarFooter?: React.ReactNode;
/** Slot: extra sidebar content rendered after navigation (e.g. favorites, recent items) */
sidebarExtra?: React.ReactNode;
/** Page content */
children: React.ReactNode;
/** Extra class on the <main> content area */
className?: string;
/** Whether the sidebar starts open @default true */
defaultOpen?: boolean;
// --- P1.7 Navigation Enhancements ---
/** Show a search input in the sidebar to filter navigation items */
enableSearch?: boolean;
/** Enable pin/favorite toggle on navigation items */
enablePinning?: boolean;
/** Called when a navigation item is pinned or unpinned */
onPinToggle?: (itemId: string, pinned: boolean, item?: NavigationItem, basePath?: string) => void;
/** Enable drag-to-reorder for navigation items */
enableReorder?: boolean;
/** Called when navigation items are reordered via drag */
onReorder?: (reorderedItems: NavigationItem[]) => void;
}
// ---------------------------------------------------------------------------
// AreaSwitcher
// ---------------------------------------------------------------------------
function AreaSwitcher({
areas,
activeAreaId,
onAreaChange,
evalVis,
checkPerm,
}: {
areas: NavigationArea[];
activeAreaId: string;
onAreaChange: (id: string) => void;
evalVis: VisibilityEvaluator;
checkPerm: PermissionChecker;
}) {
// Filter areas by visibility & permissions
const visibleAreas = areas.filter((a) => {
if (!evalVis(a.visible)) return false;
if (a.requiredPermissions?.length && !checkPerm(a.requiredPermissions)) return false;
return true;
});
if (visibleAreas.length <= 1) return null;
return (
<SidebarGroup>
<SidebarGroupLabel className="flex items-center gap-1.5">
<Layers className="h-3.5 w-3.5" />
Area
</SidebarGroupLabel>
<SidebarGroupContent>
<SidebarMenu>
{visibleAreas.map((area) => {
const AreaIcon = resolveIcon(area.icon);
return (
<SidebarMenuItem key={area.id}>
<SidebarMenuButton
isActive={area.id === activeAreaId}
tooltip={resolveLabel(area.label)}
onClick={() => onAreaChange(area.id)}
>
<AreaIcon className="h-4 w-4" />
<span>{resolveLabel(area.label)}</span>
</SidebarMenuButton>
</SidebarMenuItem>
);
})}
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
);
}
// ---------------------------------------------------------------------------
// MobileBottomNav
// ---------------------------------------------------------------------------
function MobileBottomNav({
items,
basePath,
}: {
items: NavigationItem[];
basePath: string;
}) {
const location = useLocation();
// Show up to 5 non-group leaf items. Flatten group children so apps that
// organise navigation into groups (e.g. Setup → Overview / Administration /
// …) still surface real links in the mobile bottom nav.
const collectLeaves = (list: typeof items): typeof items => {
const out: typeof items = [];
for (const item of list) {
if (item.type === 'separator') continue;
if (item.type === 'group') {
out.push(...collectLeaves(item.children || []));
} else {
out.push(item);
}
}
return out;
};
const leaves = collectLeaves(items).slice(0, 5);
if (leaves.length === 0) return null;
return (
<div
className="fixed bottom-0 left-0 right-0 z-50 flex items-center justify-around border-t bg-background/95 backdrop-blur-sm px-2 py-1 sm:hidden safe-area-bottom"
role="navigation"
aria-label="Mobile navigation"
>
{leaves.map((item) => {
const NavIcon = resolveIcon(item.icon);
let href = '#';
if (item.type === 'object') {
href = `${basePath}/${item.objectName}`;
if (item.viewName) href += `/view/${item.viewName}`;
}
else if (item.type === 'dashboard') href = item.dashboardName ? `${basePath}/dashboard/${item.dashboardName}` : '#';
else if (item.type === 'page') href = item.pageName ? `${basePath}/page/${item.pageName}` : '#';
else if (item.type === 'report') href = item.reportName ? `${basePath}/report/${item.reportName}` : '#';
else if (item.type === 'url') href = item.url ?? '#';
else if (item.type === 'component') {
const ref = item.componentRef;
if (ref) {
const segs = ref.split(':').filter(Boolean);
href = `${basePath}/component/${segs.join('/')}`;
const navParams = item.params;
if (navParams) {
const usp = new URLSearchParams();
for (const [k, v] of Object.entries(navParams)) {
if (v === undefined || v === null) continue;
usp.set(k, typeof v === 'string' ? v : JSON.stringify(v));
}
const qs = usp.toString();
if (qs) href += `?${qs}`;
}
}
}
const isActive = href !== '#' && location.pathname.startsWith(href);
return (
<Link
key={item.id}
to={href}
className={`flex flex-col items-center gap-0.5 px-2 py-1.5 transition-colors min-w-[44px] min-h-[44px] justify-center ${
isActive ? 'text-primary' : 'text-muted-foreground hover:text-foreground'
}`}
>
<NavIcon className="h-5 w-5" />
<span className="text-[10px] truncate max-w-[60px]">{resolveLabel(item.label)}</span>
</Link>
);
})}
</div>
);
}
// ---------------------------------------------------------------------------
// InternalSidebar (wraps Sidebar primitive + header + navigation)
// ---------------------------------------------------------------------------
function InternalSidebar({
schema,
basePath,
evalVis,
checkPerm,
checkCap,
onAction,
sidebarHeader,
sidebarFooter,
sidebarExtra,
activeAreaId,
setActiveAreaId,
resolvedNavigation,
enableSearch,
enablePinning,
onPinToggle,
enableReorder,
onReorder,
}: {
schema: AppSchema;
basePath: string;
evalVis: VisibilityEvaluator;
checkPerm: PermissionChecker;
checkCap: CapabilityChecker;
onAction?: (item: NavigationItem) => void;
sidebarHeader?: React.ReactNode;
sidebarFooter?: React.ReactNode;
sidebarExtra?: React.ReactNode;
activeAreaId: string | null;
setActiveAreaId: (id: string) => void;
resolvedNavigation: NavigationItem[];
enableSearch?: boolean;
enablePinning?: boolean;
onPinToggle?: (itemId: string, pinned: boolean, item?: NavigationItem, basePath?: string) => void;
enableReorder?: boolean;
onReorder?: (reorderedItems: NavigationItem[]) => void;
}) {
const Icon = resolveIcon(schema.logo);
const areas = schema.areas ?? [];
const [searchQuery, setSearchQuery] = useState('');
return (
<Sidebar collapsible="icon">
{/* Header: custom slot or default branding */}
<SidebarHeader>
{sidebarHeader ?? (
<SidebarMenu>
<SidebarMenuItem>
<SidebarMenuButton size="lg" tooltip={schema.title ?? schema.name}>
<div className="flex aspect-square size-8 items-center justify-center rounded-lg bg-primary text-primary-foreground">
{schema.logo && schema.logo.startsWith('http') ? (
<img
src={schema.logo}
alt={schema.title ?? ''}
className="size-6 object-contain"
/>
) : (
// eslint-disable-next-line react-hooks/static-components -- resolveIcon returns a stable icon component from a static registry, not a component created during render
<Icon className="size-4" />
)}
</div>
<div className="grid flex-1 text-left text-sm leading-tight">
<span className="truncate font-semibold">
{schema.title ?? schema.name ?? 'App'}
</span>
{schema.description && (
<span className="truncate text-xs text-muted-foreground">
{schema.description}
</span>
)}
</div>
</SidebarMenuButton>
</SidebarMenuItem>
</SidebarMenu>
)}
{/* Search input */}
{enableSearch && (
<SidebarInput
placeholder="Search navigation…"
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
aria-label="Search navigation"
/>
)}
</SidebarHeader>
<SidebarContent>
{/* Area Switcher */}
{areas.length > 1 && activeAreaId && (
<AreaSwitcher
areas={areas}
activeAreaId={activeAreaId}
onAreaChange={setActiveAreaId}
evalVis={evalVis}
checkPerm={checkPerm}
/>
)}
{/* Navigation tree */}
<NavigationRenderer
items={resolvedNavigation}
basePath={basePath}
evaluateVisibility={evalVis}
checkPermission={checkPerm}
checkCapability={checkCap}
onAction={onAction}
searchQuery={searchQuery}
enablePinning={enablePinning}
onPinToggle={onPinToggle}
enableReorder={enableReorder}
onReorder={onReorder}
/>
{/* Extra sidebar content slot (e.g. favorites, recent items) */}
{sidebarExtra}
</SidebarContent>
{/* Optional footer slot */}
{sidebarFooter && <SidebarFooter>{sidebarFooter}</SidebarFooter>}
</Sidebar>
);
}
// ---------------------------------------------------------------------------
// AppSchemaRenderer (main export)
// ---------------------------------------------------------------------------
/**
* Renders a complete application shell from an `AppSchema` JSON document.
*
* Responsibilities:
* - Reads `name`, `title`, `description`, `logo`, `favicon` for branding
* - Renders sidebar navigation from `navigation` or `areas[].navigation`
* - Area switcher when multiple `areas` are defined
* - Mobile modes: `drawer` (sheet overlay, default), `bottom_nav` (fixed
* bottom bar), `hamburger` (collapsed sidebar)
* - Evaluates `visible` expressions and `requiredPermissions` on every item
*
* @example
* ```tsx
* <AppSchemaRenderer
* schema={appJson}
* basePath="/apps/sales"
* mobileNavMode="bottom_nav"
* evaluateVisibility={(expr) => evaluateVisibility(expr, evaluator)}
* checkPermission={(perms) => perms.every(p => can(p))}
* >
* <Outlet />
* </AppSchemaRenderer>
* ```
*/
export function AppSchemaRenderer({
schema,
basePath = '',
mobileNavMode = 'drawer',
evaluateVisibility: evalVisProp,
checkPermission: checkPermProp,
checkCapability: checkCapProp,
onAction,
navbar,
sidebarHeader,
sidebarFooter,
sidebarExtra,
children,
className,
defaultOpen = true,
enableSearch,
enablePinning,
onPinToggle,
enableReorder,
onReorder,
}: AppSchemaRendererProps) {
// Default evaluators
const evalVis: VisibilityEvaluator = evalVisProp ?? ((expr) => {
if (expr === false || expr === 'false') return false;
return true;
});
const checkPerm: PermissionChecker = checkPermProp ?? (() => true);
const checkCap: CapabilityChecker = checkCapProp ?? (() => true);
// --- Resolve navigation from legacy `menu` or modern `navigation`/`areas` ---
const legacyNavigation = useMemo(
() => (schema.menu ?? []).map((m, i) => menuItemToNavigationItem(m, i)),
[schema.menu],
);
const flatNavigation = schema.navigation ?? legacyNavigation;
// --- Area management ---
const areas = schema.areas ?? [];
const [activeAreaId, setActiveAreaId] = useState<string | null>(
() => areas.length > 0 ? areas[0].id : null,
);
const areaIds = areas.map((a) => a.id).join(',');
useEffect(() => {
if (areas.length > 0) {
setActiveAreaId((prev) =>
areas.some((a) => a.id === prev) ? prev : areas[0].id,
);
} else {
setActiveAreaId(null);
}
}, [schema.name, areaIds]);
const activeArea = areas.find((a) => a.id === activeAreaId);
const resolvedNavigation: NavigationItem[] = activeArea?.navigation ?? flatNavigation;
// --- Branding ---
const branding: AppShellBranding = {
title: schema.title,
favicon: schema.favicon,
logo: schema.logo,
};
// --- Build sidebar element ---
const sidebarElement = (
<InternalSidebar
schema={schema}
basePath={basePath}
evalVis={evalVis}
checkPerm={checkPerm}
checkCap={checkCap}
onAction={onAction}
sidebarHeader={sidebarHeader}
sidebarFooter={sidebarFooter}
sidebarExtra={sidebarExtra}
activeAreaId={activeAreaId}
setActiveAreaId={setActiveAreaId}
resolvedNavigation={resolvedNavigation}
enableSearch={enableSearch}
enablePinning={enablePinning}
onPinToggle={onPinToggle}
enableReorder={enableReorder}
onReorder={onReorder}
/>
);
// --- Mobile bottom nav (shown alongside drawer sidebar on mobile) ---
const showBottomNav = mobileNavMode === 'bottom_nav';
return (
<>
<AppShell
sidebar={sidebarElement}
navbar={navbar}
className={className}
defaultOpen={defaultOpen}
branding={branding}
>
{children}
</AppShell>
{showBottomNav && (
<MobileBottomNav items={resolvedNavigation} basePath={basePath} />
)}
</>
);
}