|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { detectLocale, t } from './i18n'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Sentinel "package" id for this environment's runtime, DB-authored metadata — |
| 7 | + * items with no code-package binding (`package_id IS NULL`). The metadata |
| 8 | + * list/get API treats `?package=sys_metadata` as exactly that local scope on |
| 9 | + * READ, and a WRITE under it persists `package_id = null` (matching the |
| 10 | + * server's runtime-only provenance, see framework #2252). |
| 11 | + * |
| 12 | + * Why this exists: a self-hosted, metadata-customizable environment is |
| 13 | + * single-tenant — there is no "org" dimension here; the real axis is |
| 14 | + * code-package vs. runtime (DB-authored). Before this scope, the package |
| 15 | + * selector only listed code packages, so metadata authored at runtime |
| 16 | + * (`package_id = null`) was filtered out of every code-package view and became |
| 17 | + * un-navigable (the route redirected to "new"). Surfacing the local scope as a |
| 18 | + * first-class, always-present selector entry makes it discoverable and editable. |
| 19 | + */ |
| 20 | +export const LOCAL_PACKAGE_ID = 'sys_metadata'; |
| 21 | + |
| 22 | +const SYSTEM_SCOPES = new Set(['system', 'cloud']); |
| 23 | + |
| 24 | +/** |
| 25 | + * Build the Studio package-scope options from the raw `package` metadata list. |
| 26 | + * Filters out system/cloud-scoped packages and appends a stable |
| 27 | + * "Local / Custom (this environment)" scope so runtime metadata authored here |
| 28 | + * is always selectable/visible — even when zero items exist yet. |
| 29 | + */ |
| 30 | +export function buildPackageScopeOptions( |
| 31 | + rawList: unknown[] | null | undefined, |
| 32 | +): { id: string; name: string }[] { |
| 33 | + const rows = (rawList ?? []) |
| 34 | + .map((raw) => { |
| 35 | + const item = |
| 36 | + raw && typeof raw === 'object' && 'item' in raw ? (raw as { item: unknown }).item : raw; |
| 37 | + const m = ((item as { manifest?: unknown } | null)?.manifest ?? item ?? {}) as Record< |
| 38 | + string, |
| 39 | + unknown |
| 40 | + >; |
| 41 | + return { |
| 42 | + id: m.id as string, |
| 43 | + scope: m.scope as string, |
| 44 | + name: (m.name as string) || (m.id as string), |
| 45 | + }; |
| 46 | + }) |
| 47 | + .filter((p) => p.id && !SYSTEM_SCOPES.has(p.scope)); |
| 48 | + rows.sort((a, b) => a.name.localeCompare(b.name)); |
| 49 | + const opts = rows.map((p) => ({ id: p.id, name: p.name })); |
| 50 | + // Append (never default) so the existing first-code-package default is |
| 51 | + // preserved; the user opts into the local scope explicitly. |
| 52 | + return [...opts, { id: LOCAL_PACKAGE_ID, name: t('engine.package.local', detectLocale()) }]; |
| 53 | +} |
0 commit comments