Repository navigation
Expand file tree
/
Copy pathappRoute.ts
More file actions
443 lines (423 loc) · 21.3 KB
/
Copy pathappRoute.ts
File metadata and controls
443 lines (423 loc) · 21.3 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
/**
* ADR-0048 (option A) — the `/apps/<segment>` route is keyed on the **package
* id** (reverse-domain, globally unique), not the app's display name. This makes
* two vendors' same-named apps unambiguous and self-describing in the URL.
*
* - `appRouteSegment(app)` — the canonical route segment for linking TO an app
* (its package id; falls back to `name` for runtime/DB apps with no
* `_packageId`).
* - `matchAppBySegment(apps, seg)` — resolve a route segment back to its app,
* preferring the package id and falling back to the app name. The name
* fallback doubles as a per-tenant friendly alias and keeps legacy
* name-based URLs/bookmarks working. Callers keep their own
* default/first-app fallback around this match.
* - `resolveHostAppSegment(apps, preferred)` — which app should HOST a
* framework-owned, app-independent page for this user (see below).
*/
import { studioItemPath, studioMetadataItemAddress } from '../views/studio-design/studioAddress.js';
import { studioPackageOverviewPath } from '../views/studio-design/studioOverviewAddress.js';
/**
* The minimal app shape THESE ROUTE HELPERS read.
*
* ⚠️ Renamed off `AppLike` at objectui#7265, and the rename is the fix rather
* than a workaround. `@objectstack/spec/system` exports an `AppLike` of its own:
* the minimal app document `translateApp` consumes — `{ name, label,
* description, navigation }` with `name` REQUIRED — i.e. the keys the
* TRANSLATOR reads. These helpers read a disjoint set for a different purpose,
* and adopting the spec's shape would make this module worse in both
* directions:
*
* - `_packageId` is the ADR-0048 route key and the FIRST thing every function
* here reads. The spec's shape does not declare it; it would arrive through
* that shape's `[key: string]: any`, i.e. as `any`, taking the
* `typeof packageId !== 'string'` guard in `studioPackageId` with it. Same
* for `active` / `hidden`, which `filterActiveApps` reads;
* - `name` is optional here on purpose. `resolveHostAppSegment`'s documented
* step 4 exists FOR "the degenerate app carrying neither `_packageId` nor
* `name`", so a required `name` would assert away the one case that branch
* was written to serve.
*
* `unknown` (not `string`) on both members is deliberate for the same reason the
* guards exist: these helpers are handed apps straight out of the metadata
* catalog and must narrow before using either. `spec-symbol-parity.test.ts`
* holds the two-way ratchet on the rename.
*/
type AppRouteLike = { name?: unknown; _packageId?: unknown } & Record<string, unknown>;
export function appRouteSegment(app: AppRouteLike | null | undefined): string | undefined {
if (!app) return undefined;
const seg = (app._packageId as string | undefined) ?? (app.name as string | undefined);
return seg ?? undefined;
}
export function matchAppBySegment<T extends AppRouteLike>(
apps: readonly T[] | null | undefined,
seg: string | null | undefined,
): T | undefined {
if (!apps || seg == null) return undefined;
return apps.find((a) => a?._packageId === seg) ?? apps.find((a) => a?.name === seg);
}
/**
* The ONE segment that addresses `app` among `apps` — the address every
* `/apps/…` link to it should carry.
*
* That is its canonical {@link appRouteSegment} (the package id) whenever the
* segment resolves back to this very app. It does not when a package ships
* more than one app: they share the package id, `matchAppBySegment` answers the
* first, and the name stays the only address that reaches the others — so the
* name is the answer there, never a package id that opens a different app.
*/
export function appAddressSegment<T extends AppRouteLike>(
apps: readonly T[] | null | undefined,
app: T | null | undefined,
): string | undefined {
const canonical = appRouteSegment(app);
if (canonical === undefined || matchAppBySegment(apps, canonical) === app) return canonical;
return typeof app?.name === 'string' ? app.name : canonical;
}
/**
* Where a route that reached its app through the name ALIAS belongs: the same
* pathname with the app segment swapped for {@link appAddressSegment}, or
* `null` when the route already carries it (or names no app here).
*
* The name alias keeps every older name-based URL and bookmark resolving, but
* a page rendered on it kept the alias in every link it built while the
* sidebar built on the package id, so one app answered under two address
* families (objectstack-ai/objectstack#26183). Rewriting the alias to the one
* address is what keeps both: an old URL still opens, and it opens on the
* address every link inside the app agrees on.
*
* Only the app segment changes; the caller carries search and hash across. The
* answer can never loop — it is a segment that resolves back to the same app,
* so the rewritten route answers `null`.
*/
export function canonicalAppPathname(
apps: readonly AppRouteLike[] | null | undefined,
segment: string | null | undefined,
pathname: string,
): string | null {
const app = matchAppBySegment(apps, segment);
const address = appAddressSegment(apps, app);
if (!address || address === segment) return null;
const parts = pathname.split('/');
if (parts[1] !== 'apps' || parts[2] === undefined) return null;
let current: string;
try {
current = decodeURIComponent(parts[2]);
} catch {
return null;
}
if (current !== segment) return null;
parts[2] = encodeURIComponent(address);
return parts.join('/');
}
/**
* The console's own administration app. Only ever a LAST RESORT for the
* resolution below — historically it was hardcoded as the FIRST choice, which
* is the defect objectstack#7231 / objectui#4074 removed.
*/
const SETUP_APP_SEGMENT = 'setup';
/**
* The apps a user can actually open, in metadata order. `active === false`
* deactivates an app; `hidden === true` keeps it out of the launcher (personal
* settings and similar chrome). Neither is a place to send someone.
*
* One definition, because "an app the user can open" has to mean the same thing
* to Home's launcher and to every producer that builds a link into an app.
*
* ⛔ Do NOT add `_unpublished` to this predicate (objectstack#6955 / objectstack-ai/objectstack#4829 A1).
* It is the machine-managed ADR-0045 publish gate, and it is enforced
* SERVER-SIDE — the REST metadata gate withholds unpublished apps from
* non-builders, so any that reach this function belong to a builder entitled to
* open them. Filtering here would hide a builder's own in-progress app from the
* builder (and strand every link this module builds into it) while buying no
* protection the server was not already providing. `hidden` is the opposite
* case: it has no other enforcement point anywhere, so this filter IS its whole
* implementation. Two keys, two layers; this reads exactly one of them. Pinned
* by `__tests__/appRoute.test.ts`.
*/
export function filterActiveApps<T extends AppRouteLike>(apps: readonly T[] | null | undefined): T[] {
if (!apps) return [];
return apps.filter((a) => a?.active !== false && a?.hidden !== true);
}
/**
* Which app should host a framework-owned, **app-independent** page for this
* user — the Approvals Inbox (objectstack#7231), the full inbox
* (`sys_inbox_message`) and the activity list (`sys_activity`) (objectui#4074).
*
* Those pages are mounted for every app (`system/approvals` sits in the host's
* route fragment as BOTH `extraRoutes` and `extraRoutesNoApp`; `sys_*` objects
* are framework-owned and resolve at `/apps/{any app}/{object}`), so the app
* segment in their URL is a statement about WHERE THE USER IS, never about
* where the page lives. Hardcoding `setup` there was a dead end for every
* business user without access to the setup app, and an unannounced app switch
* for everyone else — both measured in a browser on objectui#4074.
*
* Resolution order, each step falling through only when the previous one cannot
* name an app the user can actually open:
* 1. `preferred` — the app they are in, or last had open — RE-CHECKED against
* the live active list, so an app that was deactivated or hidden since is
* not resurrected as a link;
* 2. their first active app — arbitrary but reachable, and on a business
* user's workspace that is a business app rather than `setup`;
* 3. `preferred` unchecked, but ONLY when the list names no app at all. An
* empty list means "not loaded yet" (the pre-catalog render, or a consumer
* outside `MetadataProvider`) at least as often as it means "this user has
* no apps", and demoting a user who is demonstrably rendering inside
* `/apps/{preferred}/…` to `setup` would reintroduce the very defect this
* resolves. A caller with a genuinely empty workspace has no reachable app
* to offer anyway;
* 4. `setup`. What is left is the degenerate app carrying neither
* `_packageId` nor `name` — nothing addressable to build a URL from — so
* the historical target is the least-surprising last resort rather than a
* broken link.
*
* Note step 1 takes ONE `preferred` hint: callers inside the `/apps/:appName/*`
* router pass `currentAppName ?? params.appName` (the shell's published app,
* then the URL's own segment); Home renders outside that router and has only
* `currentAppName`, which is stale by construction — hence the re-check.
*/
export function resolveHostAppSegment(
apps: readonly AppRouteLike[] | null | undefined,
preferred: string | null | undefined,
): string {
const active = filterActiveApps(apps);
const stillActive = appRouteSegment(matchAppBySegment(active, preferred));
if (stillActive) return stillActive;
const firstActive = appRouteSegment(active[0]);
if (firstActive) return firstActive;
if (active.length === 0 && preferred) return preferred;
return SETUP_APP_SEGMENT;
}
/**
* Which app hosts the Approvals Inbox, and an approval request, opened with no
* source app: Home's 我的工作, the bell outside an app, a notification, the
* server's e-mail link (`/system/approvals?request=ID`, forwarded through
* `/apps/setup/system/approvals`). objectstack#35505, ruling option A: such an
* open stays inside an app and no longer lands in Setup by default.
*
* The ruling's order is the object's own app, then the current app, then the
* last used app, then the first app. The first step needs the request's record,
* so the request page asks it (`objectHostAppSegment`, the inbox's deep link);
* this answers the other three, the app the inbox opens in and the request
* falls back to:
* 1. `current` — the app the user is in (`currentAppName`, the route's
* segment) — when it is still one they can open;
* 2. `last` — the app they last had open (`LastAppProvider`'s `ui.app.last`,
* kept per person across sessions), when it still is;
* 3. the first app they can open, then `resolveHostAppSegment`'s own last
* resorts.
* Setup is never skipped: when it is the current or the last app, the request
* stays there (the ruling's agreed default).
*/
export function resolveApprovalsHostSegment(
apps: readonly AppRouteLike[] | null | undefined,
current: string | null | undefined,
last: string | null | undefined,
): string {
const active = filterActiveApps(apps);
const preferred = matchAppBySegment(active, current)
? current
: matchAppBySegment(active, last)
? last
: (current ?? last);
return resolveHostAppSegment(apps, preferred);
}
/**
* Whether a notification's app-relative `action_url` opens the Approvals Inbox
* (`/system/approvals`, with or without `?request=ID`): the link the approvals
* service writes on every request notice. Such a link is hosted by
* {@link resolveApprovalsHostSegment}, not by the generic host.
*/
export function isApprovalsInboxLink(actionUrl: string | null | undefined): boolean {
return /^\/?system\/approvals(?:[?#]|\/?$)/.test((actionUrl ?? '').trim());
}
/**
* App → Studio reverse bridge (ADR-0080). Resolves a running app to its owning
* package's 概览 Overview (`/studio/:packageId/overview`, where every door into
* a package lands, objectstack#38392), or `null` when there is nothing to open:
* - the viewer is not a workspace admin (designing mutates shared package
* metadata, so the entry point is admin-only — mirrors the runtime editors);
* - the app has no owning package (runtime/DB apps), or its container is the
* DB-authored `sys_metadata` pseudo-package, which is not a package the
* Studio design surface can open.
*
* Writability of the target package is NOT checked here — the ADR-0070 D4 gate
* stays the server-side authority, and the Studio surface itself renders
* read-only packages as browse-only.
*/
export function appStudioDesignPath(
app: AppRouteLike | null | undefined,
isWorkspaceAdmin: boolean,
): string | null {
if (!isWorkspaceAdmin) return null;
const packageId = studioPackageId(app);
if (!packageId) return null;
return studioPackageOverviewPath(packageId);
}
/**
* Deep-link from a running app straight to a specific interface's metadata in
* Studio's "All metadata" area — e.g. viewing a dashboard opens
* `/studio/:packageId/metadata/dashboard/<name>`, the item's page there.
* objectstack#36502: the Interfaces pillar it used to open is retired; an
* interface is designed in the running app, and this item page is where it is
* read and, as a fallback, edited as metadata. Same admin/package guards as
* {@link appStudioDesignPath}; returns null when the surface identity is
* incomplete so the caller can fall back to the plain design path.
*/
export function appStudioSurfacePath(
app: AppRouteLike | null | undefined,
isWorkspaceAdmin: boolean,
surface: { type?: string | null; name?: string | null } | null | undefined,
): string | null {
if (!isWorkspaceAdmin) return null;
const packageId = studioPackageId(app);
if (!packageId) return null;
const type = surface?.type;
const name = surface?.name;
if (!type || !name) return null;
// The area's item page for every type, explicitly: not `studioItemPath`,
// which sends a pillar type to its pillar.
return studioMetadataItemAddress(packageId, type, name);
}
/**
* Deep-link from a running app straight to a specific **object's** designer
* in Studio — e.g. viewing the `account` records page opens
* `/studio/:packageId/metadata/object/account`, the object designer
* full-screen on its own address (objectstack#36904). Same admin/package
* guards as {@link appStudioDesignPath}; returns null when the object name is
* missing so the caller can fall back.
*/
export function appStudioObjectPath(
app: AppRouteLike | null | undefined,
isWorkspaceAdmin: boolean,
objectName: string | null | undefined,
): string | null {
if (!isWorkspaceAdmin) return null;
const packageId = studioPackageId(app);
if (!packageId) return null;
if (!objectName) return null;
return studioItemPath(packageId, 'object', objectName);
}
/**
* Route types that correspond to an **interface** item. The console route type
* (`/apps/:pkg/<type>/<name>`) doubles as the metadata type
* (`dashboard` / `page` / `report`).
*/
const INTERFACE_SURFACE_ROUTE_TYPES = new Set(['dashboard', 'page', 'report']);
/**
* Console `/apps/:pkg/<type>` route types that are NOT object records — the
* interface surfaces (handled above) plus the `system` settings area. Any other
* route type IS the object name (see `AppHeader`'s breadcrumb switch: the
* `else if (routeType)` branch treats it as an object), so it deep-links that
* object's designer ({@link appStudioObjectPath}).
*/
const NON_OBJECT_ROUTE_TYPES = new Set([...INTERFACE_SURFACE_ROUTE_TYPES, 'system']);
/**
* App → Studio bridge target for a running-app route (ADR-0080). When the route
* names a specific interface (a dashboard, page, or report — see
* {@link INTERFACE_SURFACE_ROUTE_TYPES}), deep-link straight to THAT item in
* Studio's "All metadata" area via {@link appStudioSurfacePath}. When it names an
* object record page (the route type IS the object name), deep-link that object
* in its designer via {@link appStudioObjectPath}. Otherwise (bare app root,
* a `system` route, or an interface list route with no surface name) fall back
* to the package's 概览 Overview via {@link appStudioDesignPath}. Returns
* null when the bridge should not render (non-admin / no owning package).
* Centralizes the route-type → surface-type mapping so `AppHeader` stays
* declarative and the decision is unit-tested here.
*/
export function appStudioRoutePath(
app: AppRouteLike | null | undefined,
isWorkspaceAdmin: boolean,
route: { type?: string | null; name?: string | null } | null | undefined,
): string | null {
const type = route?.type;
const name = route?.name;
if (type && name && INTERFACE_SURFACE_ROUTE_TYPES.has(type)) {
return appStudioSurfacePath(app, isWorkspaceAdmin, { type, name });
}
if (type && !NON_OBJECT_ROUTE_TYPES.has(type)) {
return appStudioObjectPath(app, isWorkspaceAdmin, type);
}
return appStudioDesignPath(app, isWorkspaceAdmin);
}
/**
* Shared guard: the owning package id usable by the Studio design surface.
* Also the reading of a list's saved view row (objectstack#35933): the row
* carries its served `_packageId`, and the same id names the same package.
*/
export function studioPackageId(app: AppRouteLike | null | undefined): string | null {
const packageId = app?._packageId;
if (typeof packageId !== 'string' || !packageId || packageId === 'sys_metadata') return null;
return packageId;
}
/**
* Where a notification's `action_url` should actually take the user
* (objectui#5179).
*
* ## The contract this reads
*
* `action_url` is **app-relative**, and the producer says so in as many words.
* `service-messaging`'s `actionUrlFor()` takes an explicit `payload.url` when
* one is given, and otherwise synthesizes `/{object}/{id}` from the emit's
* `source` "so the materialization is self-sufficient for navigation (the bell
* no longer has the L2 event's `source_object/source_id` to fall back on —
* ADR-0030 L5)". Its own pins carry that shape: `/showcase_task/t_42`,
* `/opportunities/42`.
*
* So the field names a RECORD, not a console route. Which app hosts it is the
* client's question — the same one {@link resolveHostAppSegment} answers for
* `sys_inbox_message` and `sys_activity`, and for the same reason: notifications
* are app-independent, so the app segment in the URL is a statement about WHERE
* THE USER IS.
*
* Navigating the field verbatim is what objectui#5179 reported. `/showcase_task/t_42`
* matches no route, so the console's catch-all (`<Route path="*" element={<Navigate
* to="/" replace />} />`) forwards it to `/`, and `RootLandingRedirect` resolves
* that to `/apps/<default app>` — the app landing page. Mark-as-read has already
* fired by then, so the user's pointer to the record is not misdirected but gone.
*
* ## Why a resolver rather than a prefix at each call site
*
* Both producers of this navigation — Home's action centre and the top-bar bell
* — had their own answer, and the two disagreed with each other AND with the
* two "see all" drills sitting in the same files. One resolver is what makes
* them incapable of drifting again; it is the same argument objectui#4074 made
* for `resolveHostAppSegment` one screen up.
*
* ## The three shapes, and why each is distinguished
*
* - **external** (`https://…`, or any other scheme) — a producer-supplied
* `payload.url` pointing off-console. It must never be hosted: prefixing
* would build `/apps/crm/https://…`, a route that does not exist. The caller
* decides how to open it, because "new tab" is a UI decision, not a routing one.
* - **already a console route** (`/apps/…`) — an explicit `payload.url` that has
* already named its app. Hosting it twice (`/apps/crm/apps/acme/…`) would
* break the one case that worked before this fix.
* - **app-relative** (everything else) — the synthesized `/{object}/{id}`, and
* the case this exists for. Hosted under `hostAppSegment`.
*
* Returns `null` for an absent or blank url, so callers keep their own fallback
* (Home opens the full inbox; the bell has no link to follow) rather than
* receiving a fabricated target. A protocol-relative `//host/path` counts as
* external — it is a URL to another origin, whatever the current scheme.
*/
export type NotificationTarget =
/** Off-console. Hand to `window.open`, never to the router. */
| { kind: 'external'; url: string }
/** An in-console path, ready for `navigate()`. */
| { kind: 'route'; path: string };
export function resolveNotificationTarget(
actionUrl: string | null | undefined,
hostAppSegment: string,
): NotificationTarget | null {
const url = (actionUrl ?? '').trim();
if (!url) return null;
// Any scheme (`https:`, `mailto:`, …) or a protocol-relative `//host` is off
// this console's router. Deliberately broader than an http(s) test: a
// `mailto:` link prefixed into `/apps/crm/mailto:…` is just as broken.
if (/^[a-z][a-z0-9+.-]*:/i.test(url) || url.startsWith('//')) return { kind: 'external', url };
const rel = url.startsWith('/') ? url : `/${url}`;
// Already addressed to an app — `/apps` alone included, which is the app
// launcher rather than a record, but still this console's own route.
if (rel === '/apps' || rel.startsWith('/apps/')) return { kind: 'route', path: rel };
return { kind: 'route', path: `/apps/${hostAppSegment}${rel}` };
}