-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.ts
More file actions
607 lines (504 loc) · 14.9 KB
/
Copy pathapp.ts
File metadata and controls
607 lines (504 loc) · 14.9 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
/**
* 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/types - Application Schema
*
* Defines the metadata structure for a complete application, including
* global layout, navigation menus, and routing configuration.
*
* ## Navigation Model
*
* ObjectUI uses a unified `NavigationItem` model aligned with @objectstack/spec.
* The legacy `MenuItem` type is retained for backward compatibility but new
* configurations should use `NavigationItem` and the `navigation` / `areas` fields.
*/
import type { BaseSchema } from './base';
// ============================================================================
// Unified Navigation Model (aligned with @objectstack/spec)
// ============================================================================
/**
* Navigation item type — determines the target and required fields.
*/
export type NavigationItemType =
| 'object'
| 'dashboard'
| 'page'
| 'report'
| 'url'
| 'component'
| 'group'
| 'separator'
| 'action';
/**
* Unified Navigation Item
*
* The single navigation primitive used across ObjectUI and @objectstack/spec.
* Replaces the legacy `MenuItem` for application navigation trees.
*
* Supports typed navigation targets (object, dashboard, page, report, url),
* nested groups, visibility expressions, RBAC permissions, and UX enhancements
* like badges, pinning, and sort ordering.
*/
export interface NavigationItem {
/** Unique identifier */
id: string;
/** Navigation item type */
type: NavigationItemType;
/** Display label (plain string per @objectstack/spec v4 protocol) */
label: string;
/** Icon name (Lucide) */
icon?: string;
// -- Type-specific target fields --
/** Target object name (for type: 'object') */
objectName?: string;
/** Target view name (for type: 'object') — opens a specific named list view e.g. 'calendar', 'pipeline' */
viewName?: string;
/**
* Target record id (for type: 'object') — when set, the nav item
* opens a single record's detail page instead of a list view.
*
* Supports template variables resolved at render time by the shell:
* - `{current_user_id}` → currently signed-in user's id
* - `{current_org_id}` → currently active organization's id
*
* If the template can't be resolved (e.g. signed-out pre-render),
* the item falls back to opening the list view.
*
* When both `recordId` and `viewName` are set, `recordId` wins.
*/
recordId?: string;
/**
* Record opening mode when `recordId` is set. Defaults to `'view'`.
* Use `'edit'` to land directly on the edit form (e.g. "Edit my profile").
*/
recordMode?: 'view' | 'edit';
/**
* URL filter conditions (for type: 'object') — the entry targets the
* parameterized bare data surface `/:objectName/data` with each entry
* serialized as a `filter[<field>]=<value>` search param (equality),
* instead of anchoring to a saved view. Use for one-off / parameterized
* slices ("My open tickets" without authoring a view); slices worth
* curating belong in a named view via `viewName`.
*
* Values support the same template variables as `recordId`
* (`{current_user_id}`, `{current_org_id}`); entries whose template can't
* be resolved are dropped from the URL.
*
* Precedence within `type: 'object'`: `recordId` → `filters` → `viewName`.
*/
filters?: Record<string, string>;
/** Target dashboard name (for type: 'dashboard') */
dashboardName?: string;
/** Target page name (for type: 'page') */
pageName?: string;
/** Target report name (for type: 'report') */
reportName?: string;
/** Target URL (for type: 'url') */
url?: string;
/** Link target (for type: 'url') */
target?: '_blank' | '_self';
/**
* Target component reference (for type: 'component') — a colon-joined
* `ComponentRegistry` key (e.g. `metadata:resource`, `setup:permission_matrix`)
* identifying a first-party UI shipped with the platform. Routed to
* `/component/<ns>/<name>`. Mirrors `@objectstack/spec` `ComponentNavItem`.
*/
componentRef?: string;
/**
* Extra parameters (for type: 'component' | 'page') — serialised as
* querystring so the same component/page can be reused across nav entries
* with different inputs (e.g. `params: { type: 'object' }`). String values
* support the same template variables as `recordId`.
*/
params?: Record<string, unknown>;
// -- Grouping --
/** Child navigation items (for type: 'group') */
children?: NavigationItem[];
// -- Visibility & Permissions --
/** Visibility expression — boolean or expression string e.g. "${user.role === 'admin'}" */
visible?: boolean | string;
/** Required permissions to see/access this item */
requiredPermissions?: string[];
/**
* Runtime capability gate — name of an object that must be registered
* in the runtime's SchemaRegistry for this entry to render. Used to
* hide cloud-only nav entries (e.g. `sys_app`, `sys_package`) in
* single-project runtimes that don't register those objects.
*/
requiresObject?: string;
/**
* Runtime capability gate — name of a kernel service that must be
* registered for this entry to render. Mirrors `requiresObject` for
* service-bound features.
*/
requiresService?: string;
// -- UX Enhancements --
/** Badge text or count */
badge?: string | number;
/** Badge visual variant */
badgeVariant?: 'default' | 'destructive' | 'outline';
/** Whether group is expanded by default (for type: 'group') */
defaultOpen?: boolean;
/** Whether this item is pinned */
pinned?: boolean;
/** Sort order weight (lower = higher) */
order?: number;
}
/**
* Navigation Area — a business-domain partition of navigation items.
*
* Inspired by Salesforce Lightning App → Area → Tab model and
* Microsoft Power Apps Area → Group → Subarea pattern.
*
* Each area contains an independent navigation tree, allowing large
* enterprise applications to organise navigation by domain (e.g.
* Sales, Service, Marketing).
*/
export interface NavigationArea {
/** Unique identifier */
id: string;
/** Display label (plain string per @objectstack/spec v4 protocol) */
label: string;
/** Icon name (Lucide) */
icon?: string;
/** Navigation items within this area */
navigation: NavigationItem[];
/** Visibility expression */
visible?: boolean | string;
/** Required permissions to see this area */
requiredPermissions?: string[];
}
// ============================================================================
// Application Schema
// ============================================================================
/**
* Top-level Application Configuration (app.json)
*/
export interface AppSchema extends BaseSchema {
type: 'app';
/**
* Application Name (System ID)
*/
name?: string;
/**
* Display Title
*/
title?: string;
/**
* Display Label (used in navigation and app switcher)
*/
label?: string;
/**
* Application Description
*/
description?: string;
/**
* Icon name (Lucide) for app switcher and navigation
*/
icon?: string;
/**
* Logo URL or Icon name
*/
logo?: string;
/**
* Favicon URL
*/
favicon?: string;
/**
* Branding configuration
*/
branding?: BrandingConfig;
/**
* Whether the application is active (visible in app switcher)
* @default true
*/
active?: boolean;
/**
* Global Layout Strategy
* - sidebar: Standard admin layout with left sidebar
* - header: Top navigation bar only
* - empty: No layout, pages are responsible for their own structure
* @default "sidebar"
*/
layout?: 'sidebar' | 'header' | 'empty';
/**
* Global Navigation Menu
* @deprecated Use `navigation` instead. Retained for backward compatibility.
*/
menu?: MenuItem[];
/**
* Unified navigation tree (aligned with @objectstack/spec NavigationItem model).
* Takes precedence over `menu` when both are present.
*/
navigation?: NavigationItem[];
/**
* Navigation areas / business-domain partitions.
* When provided, the sidebar displays an area switcher and renders
* the selected area's navigation tree.
*/
areas?: NavigationArea[];
/**
* Global Actions (User Profile, Settings, etc)
*/
actions?: AppAction[];
/**
* Home page ID (ObjectStack Spec v2.0.1)
* Default page to navigate to after login
*/
homePageId?: string;
/**
* Required permissions (ObjectStack Spec v2.0.1)
* Permissions required to access this application
*/
requiredPermissions?: string[];
}
// ============================================================================
// Legacy MenuItem (backward compat — prefer NavigationItem)
// ============================================================================
/**
* Navigation Menu Item
* @deprecated Use `NavigationItem` instead.
*/
export interface MenuItem {
/**
* Item Type
*/
type?: 'item' | 'group' | 'separator';
/**
* Display Label
*/
label?: string;
/**
* Icon Name (Lucide)
*/
icon?: string;
/**
* Target Path (Route)
*/
path?: string;
/**
* External Link
*/
href?: string;
/**
* Child Items (Submenu)
*/
children?: MenuItem[];
/**
* Badge / Count
*/
badge?: string | number;
/**
* Visibility Condition
*/
hidden?: boolean | string;
}
// ============================================================================
// MenuItem → NavigationItem Transform
// ============================================================================
/**
* Convert a legacy `MenuItem` to a `NavigationItem`.
*
* Mapping rules:
* - `type: 'item'` → inferred from `href` (url) or `path` (page)
* - `type: 'group'` → `type: 'group'`
* - `type: 'separator'` → `type: 'separator'`
* - `hidden` → `visible` (inverted)
* - `path` → `pageName` (last segment) or kept as-is for url
* - `href` → `url` with `target: '_blank'`
*/
export function menuItemToNavigationItem(
item: MenuItem,
index: number = 0,
): NavigationItem {
const id = `migrated_${index}`;
if (item.type === 'separator') {
return {
id,
type: 'separator',
label: item.label || '',
};
}
if (item.type === 'group') {
return {
id,
type: 'group',
label: item.label || '',
icon: item.icon,
children: (item.children || []).map((child, i) =>
menuItemToNavigationItem(child, index * 100 + i),
),
visible: item.hidden !== undefined ? !item.hidden : undefined,
badge: item.badge,
defaultOpen: true,
};
}
// Default: 'item' type — infer target from href / path
if (item.href) {
return {
id,
type: 'url',
label: item.label || '',
icon: item.icon,
url: item.href,
target: '_blank',
visible: item.hidden !== undefined ? !item.hidden : undefined,
badge: item.badge,
};
}
// Path-based item → treat as page navigation
return {
id,
type: 'page',
label: item.label || '',
icon: item.icon,
pageName: item.path || '',
visible: item.hidden !== undefined ? !item.hidden : undefined,
badge: item.badge,
};
}
// ============================================================================
// App Creation Wizard Types
// ============================================================================
/**
* Wizard step identifier for app creation flow.
*/
export type AppWizardStepId = 'basic' | 'objects' | 'navigation' | 'branding';
/**
* App wizard step definition.
*/
export interface AppWizardStep {
/** Step identifier */
id: AppWizardStepId;
/** Display label */
label: string;
/** Step description */
description?: string;
/** Icon name (Lucide) */
icon?: string;
/** Whether the step is optional */
optional?: boolean;
}
/**
* Branding configuration for an application.
*/
export interface BrandingConfig {
/** Logo URL or base64 data URI */
logo?: string;
/** Primary brand color (hex) */
primaryColor?: string;
/** Favicon URL */
favicon?: string;
/** Font family override */
fontFamily?: string;
}
/**
* Object selection entry for the wizard.
*/
export interface ObjectSelection {
/** Object name (snake_case) */
name: string;
/** Display label (singular) */
label: string;
/** Plural display label — preferred for list-style nav entries (falls back to `label`) */
pluralLabel?: string;
/** Icon name (Lucide) */
icon?: string;
/** Whether this object is selected */
selected: boolean;
}
/**
* App creation wizard draft state — represents the in-progress
* application configuration before it is finalized into an AppSchema.
*/
export interface AppWizardDraft {
/** App name (snake_case, validated) */
name: string;
/** Display title */
title: string;
/** Description */
description?: string;
/** App icon name (Lucide) */
icon?: string;
/** Template to start from */
template?: string;
/** Layout strategy */
layout: 'sidebar' | 'header' | 'empty';
/** Selected business objects */
objects: ObjectSelection[];
/** Navigation tree being built */
navigation: NavigationItem[];
/** Branding configuration */
branding: BrandingConfig;
}
/**
* Editor mode for the app designer.
*/
export type EditorMode = 'edit' | 'preview' | 'code';
/**
* Validate an app name is snake_case.
* Pattern: starts with lowercase letter, followed by lowercase letters/digits,
* with optional underscore-separated segments (no trailing/leading/double underscores).
*/
export function isValidAppName(name: string): boolean {
return /^[a-z][a-z0-9]*(_[a-z0-9]+)*$/.test(name);
}
/**
* Convert an AppWizardDraft to an AppSchema.
*/
export function wizardDraftToAppSchema(draft: AppWizardDraft): AppSchema {
return {
type: 'app',
name: draft.name,
title: draft.title,
label: draft.title,
description: draft.description,
icon: draft.icon,
logo: draft.branding.logo,
favicon: draft.branding.favicon,
branding: draft.branding,
layout: draft.layout,
navigation: draft.navigation,
};
}
// ============================================================================
// Application Actions
// ============================================================================
/**
* Application Header/Toolbar Action
*/
export interface AppAction {
type: 'button' | 'dropdown' | 'user';
label?: string;
icon?: string;
onClick?: string;
/**
* User Avatar URL (for type='user')
*/
avatar?: string;
/**
* Additional description (e.g. email for user)
*/
description?: string;
/**
* Dropdown Menu Items (for type='dropdown' or 'user')
*/
items?: MenuItem[];
/**
* Keyboard shortcut
*/
shortcut?: string;
/**
* Button variant
*/
variant?: 'default' | 'destructive' | 'outline' | 'secondary' | 'ghost' | 'link';
/**
* Button size
*/
size?: 'default' | 'sm' | 'lg' | 'icon';
}