autosolutions-os 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/nav.d.ts ADDED
@@ -0,0 +1,234 @@
1
+ /**
2
+ * The OS menu: the rail every app shows, the 53 tools of the menu map by
3
+ * function, and the one set of routing rules that turns a location into a
4
+ * place in the workspace frame and a click into a destination.
5
+ *
6
+ * Three levels, the same in every AutoSolutions webapp:
7
+ * 1 the rail Hub, Finance, Marketing, Commercial, Operations
8
+ * 2 the menu that function's tools, grouped Reporting / Planning
9
+ * 3 the tab menu the open tool's views, with Back to hub
10
+ *
11
+ * Pure data and pure functions: no React, no Supabase. An app that is not on
12
+ * the core imports this beside the design system's WorkspaceFrame; an app on
13
+ * the core gets it through OsWorkspaceFrame (`autosolutions-os/frame`).
14
+ */
15
+ import { HUB_URL } from "./config.js";
16
+ export { HUB_URL };
17
+ /** A rail or menu row, shaped to drop into the design system's WorkspaceFrame. */
18
+ export type OsNavItem = {
19
+ id: string;
20
+ label: string;
21
+ icon?: string;
22
+ /** Visible but locked; say why in `title`. */
23
+ disabled?: boolean;
24
+ title?: string;
25
+ /** A count at the end of the row, such as unread replies. */
26
+ badge?: string | number;
27
+ };
28
+ export type OsToolStatus = "Live" | "In progress" | "To build" | "No status";
29
+ export type OsTool = {
30
+ id: string;
31
+ label: string;
32
+ icon: string;
33
+ status: OsToolStatus;
34
+ };
35
+ export type OsToolGroup = {
36
+ title: string;
37
+ items: OsTool[];
38
+ };
39
+ export type OsMenuFunction = "finance" | "marketing" | "commercial" | "operations";
40
+ /** The rail: the Hub, then the four business functions. */
41
+ export declare const OS_FUNCTIONS: OsNavItem[];
42
+ /**
43
+ * "Menu map - all 49 items by function", as the design handoff carries it,
44
+ * plus four Live apps the sheet did not have: Shelf simulator (Marketing >
45
+ * Planning), Key accounts (Commercial > Planning), PM Copilot and Candidate
46
+ * hub (Operations > Planning). Owner, source and cadence stay in the sheet.
47
+ * Icons are glyph ids from the design system's NAV_ICONS, which draws them
48
+ * inline; the four new rows borrow ops-inventory-value (a stocked shelf),
49
+ * com-customer-scorecard, hub-tasks and hub-accounts.
50
+ */
51
+ export declare const OS_MENU: Record<OsMenuFunction, OsToolGroup[]>;
52
+ /**
53
+ * Where a Live tool lives. `app` is its catalogue key: in a frame backed by
54
+ * the catalogue (OsWorkspaceFrame), the tool links to that app's address from
55
+ * os_context(), with the company as ?tenant=, only when the person holds the
56
+ * app, and is locked when they do not. `url` is a fixed address, used only by
57
+ * staticLinks(): a frame off the core, with no catalogue to ask. `path` is a
58
+ * route inside the app, query included, joined to either address.
59
+ */
60
+ export type OsToolLink = {
61
+ app?: string;
62
+ path?: string;
63
+ url?: string;
64
+ };
65
+ /**
66
+ * Where the tools go: every Live tool, and the tools whose app is already in
67
+ * the catalogue whatever the map says of them (Influencer Manager, Marketing
68
+ * calendar, Paid media), so every app the app switcher offers is reachable
69
+ * from the menu too. Every link opens in the same tab. Pricing researcher and
70
+ * Brand voice copywriter have no app to open yet, so they stay unlinked
71
+ * ("Live, not linked from the Hub yet"). The Hub itself (smart_comms_hub) is
72
+ * the rail's Hub, not a row.
73
+ */
74
+ export declare const OS_TOOL_LINKS: Record<string, OsToolLink>;
75
+ /** The Hub's own pages, for the rail's Hub, Admin and Settings rows in other apps. */
76
+ export declare const HUB_PATHS: {
77
+ readonly home: "/comms/overview";
78
+ readonly admin: "/admin";
79
+ readonly settings: "/comms/settings";
80
+ };
81
+ /** Why a row is locked. A tool whose status is In progress or To build says so instead. */
82
+ export declare const HINTS: {
83
+ readonly locked: "Ask your company admin to switch this on for you";
84
+ readonly notBuilt: "Not built yet";
85
+ readonly notLinked: "Live, not linked from the Hub yet";
86
+ };
87
+ /** The rail footer: Admin (for someone who manages something), Settings, then Sign out, always last. */
88
+ export declare function osFooter({ admin }?: {
89
+ admin?: boolean;
90
+ }): OsNavItem[];
91
+ /**
92
+ * Tool id to address, for the tools with a fixed `url`: what a frame off the
93
+ * core, which has no catalogue to ask, can link to. No tenant hint.
94
+ */
95
+ export declare function staticLinks(): Record<string, string>;
96
+ /**
97
+ * Tool id to address, and tool id to the reason a row is off, for
98
+ * OsWorkspaceFrame to pass to osFrame. `apps` is os_context().apps: the apps
99
+ * this person holds in the acting company, as the app switcher lists them.
100
+ * For a tool whose OS_TOOL_LINKS entry names an app:
101
+ * - held, with an address: linked to it (plus `path`), with the company as
102
+ * ?tenant=, whatever status the map gives the tool, because the switcher
103
+ * already offers that app;
104
+ * - held, not deployed yet (no address): a Live tool reads "Live, not linked
105
+ * from the Hub yet"; any other keeps the map's own status;
106
+ * - not held: a Live tool is locked ("Ask your company admin to switch this
107
+ * on for you"); any other keeps the map's own status (Not built yet, In
108
+ * progress, To build).
109
+ * A fixed `url` is never consulted here: it is no way round the catalogue.
110
+ * Tools with no app keep osFrame's own rules. Links are final: osFrame adds
111
+ * nothing to them.
112
+ */
113
+ export declare function catalogueLinks(apps: ReadonlyArray<{
114
+ key: string;
115
+ url: string | null;
116
+ }>, tenantSlug?: string | null): {
117
+ links: Record<string, string>;
118
+ locked: Record<string, string>;
119
+ };
120
+ /** A route inside this app. `path` may carry a query, e.g. "/launcher?section=signature". */
121
+ export type OsRoute = {
122
+ /** Where a click goes. */
123
+ path: string;
124
+ /** More path prefixes that select the row, e.g. "/comms" for a tool whose path is "/comms/overview". */
125
+ match?: string[];
126
+ };
127
+ export type OsTab = OsNavItem & OsRoute;
128
+ /** Titled tabs, drawn with the second menu's section headings. */
129
+ export type OsTabGroup = {
130
+ title?: string;
131
+ items: OsTab[];
132
+ };
133
+ /**
134
+ * One of this app's own tools. `id` is an OS_MENU tool id, which places it in
135
+ * the menu and enables it however the map marks it; an id the map does not
136
+ * have needs `fn`, `section`, `label` and `icon` to be placed.
137
+ */
138
+ export type OsAppTool = OsRoute & {
139
+ id: string;
140
+ /**
141
+ * The third menu, flat or in titled groups. Tabs with a `path` navigate;
142
+ * tabs without are page state, read from the frame's state.
143
+ */
144
+ tabs?: OsTab[] | OsTabGroup[];
145
+ /** false for a tool that carries its own tab row in the page: opening it closes the menu. */
146
+ menu?: boolean;
147
+ /** A count at the end of the row, such as unread replies. */
148
+ badge?: string | number;
149
+ fn?: string;
150
+ section?: string;
151
+ label?: string;
152
+ icon?: string;
153
+ };
154
+ /** A row in the Hub function's menu. The Hub passes its own; other apps pass none. */
155
+ export type OsHubItem = OsNavItem & Partial<OsRoute> & {
156
+ href?: string;
157
+ tabs?: OsTab[] | OsTabGroup[];
158
+ menu?: boolean;
159
+ };
160
+ export type OsHubSection = {
161
+ title?: string;
162
+ items: OsHubItem[];
163
+ };
164
+ /** A menu row as osFrame hands it to WorkspaceFrame. */
165
+ export type OsMenuRow = OsNavItem & {
166
+ path?: string;
167
+ match?: string[];
168
+ href?: string;
169
+ tabs?: OsTab[] | OsTabGroup[];
170
+ menu?: boolean;
171
+ };
172
+ export type OsMenuSection = {
173
+ title?: string;
174
+ items: OsMenuRow[];
175
+ };
176
+ /** Where the frame is. `open` omitted means the frame's own rule. */
177
+ export type OsLocation = {
178
+ fn: string;
179
+ item: string | null;
180
+ tab: string | null;
181
+ open?: boolean;
182
+ };
183
+ /** Where a click goes: inside this app (the router navigates) or to another app. */
184
+ export type OsTarget = {
185
+ href: string;
186
+ external: boolean;
187
+ };
188
+ /** The part of WorkspaceFrame's state a destination depends on. */
189
+ export type OsFrameState = {
190
+ fn: {
191
+ id: string;
192
+ };
193
+ item: {
194
+ id: string;
195
+ } | null;
196
+ tab: {
197
+ id: string;
198
+ } | null;
199
+ };
200
+ export type OsFrameOptions = {
201
+ /** This app's catalogue key; "hub" for the Hub itself. */
202
+ appKey: string;
203
+ tools?: OsAppTool[];
204
+ hubItems?: OsHubSection[];
205
+ /** Tool id to address, for tools served by other apps (catalogueLinks or staticLinks). Used as given. */
206
+ links?: Record<string, string>;
207
+ /** Tool id to the reason its row is off (catalogueLinks): wins over the map's status. */
208
+ locked?: Record<string, string>;
209
+ /** Show Admin in the footer. */
210
+ admin?: boolean;
211
+ /** This app's settings route. The rail marks Settings there. Default: the Hub's settings. */
212
+ settingsPath?: string;
213
+ /** This app's landing. In the Hub it is also where Back to hub lands. Default "/", or the Hub's home in the Hub. */
214
+ homePath?: string;
215
+ /** Default HUB_URL. */
216
+ hubUrl?: string;
217
+ /** The acting company's slug, carried to the Hub as ?tenant=. (catalogueLinks carries it to other apps.) */
218
+ tenantSlug?: string | null;
219
+ };
220
+ export type OsFrame = {
221
+ functions: OsNavItem[];
222
+ sections: Record<string, OsMenuSection[]>;
223
+ footerItems: OsNavItem[];
224
+ homeFunction: "hub";
225
+ /** Where a location (pathname plus query) sits in the frame. */
226
+ locate: (location: string) => OsLocation;
227
+ /** Where a click from `location` goes; null when it goes nowhere (opening a menu, or already there). */
228
+ targetFor: (state: OsFrameState, location: string) => OsTarget | null;
229
+ };
230
+ /**
231
+ * The frame for one app: the rail, every function's menu for this person and
232
+ * company, the footer, and the only routing logic the frame needs.
233
+ */
234
+ export declare function osFrame(options: OsFrameOptions): OsFrame;
package/dist/nav.js ADDED
@@ -0,0 +1,433 @@
1
+ /**
2
+ * The OS menu: the rail every app shows, the 53 tools of the menu map by
3
+ * function, and the one set of routing rules that turns a location into a
4
+ * place in the workspace frame and a click into a destination.
5
+ *
6
+ * Three levels, the same in every AutoSolutions webapp:
7
+ * 1 the rail Hub, Finance, Marketing, Commercial, Operations
8
+ * 2 the menu that function's tools, grouped Reporting / Planning
9
+ * 3 the tab menu the open tool's views, with Back to hub
10
+ *
11
+ * Pure data and pure functions: no React, no Supabase. An app that is not on
12
+ * the core imports this beside the design system's WorkspaceFrame; an app on
13
+ * the core gets it through OsWorkspaceFrame (`autosolutions-os/frame`).
14
+ */
15
+ import { HUB_URL } from "./config.js";
16
+ import { withTenantHint } from "./url.js";
17
+ export { HUB_URL };
18
+ /** The rail: the Hub, then the four business functions. */
19
+ export const OS_FUNCTIONS = [
20
+ { id: "hub", label: "Hub", icon: "hub" },
21
+ { id: "finance", label: "Finance", icon: "finance" },
22
+ { id: "marketing", label: "Marketing", icon: "marketing" },
23
+ { id: "commercial", label: "Commercial", icon: "commercial" },
24
+ { id: "operations", label: "Operations", icon: "operations" },
25
+ ];
26
+ /**
27
+ * "Menu map - all 49 items by function", as the design handoff carries it,
28
+ * plus four Live apps the sheet did not have: Shelf simulator (Marketing >
29
+ * Planning), Key accounts (Commercial > Planning), PM Copilot and Candidate
30
+ * hub (Operations > Planning). Owner, source and cadence stay in the sheet.
31
+ * Icons are glyph ids from the design system's NAV_ICONS, which draws them
32
+ * inline; the four new rows borrow ops-inventory-value (a stocked shelf),
33
+ * com-customer-scorecard, hub-tasks and hub-accounts.
34
+ */
35
+ export const OS_MENU = {
36
+ finance: [
37
+ { title: "Reporting", items: [
38
+ { id: "cash-forecast", label: "Cash & forecast", icon: "fin-cash", status: "No status" },
39
+ { id: "pl-vs-budget", label: "P&L vs budget", icon: "fin-pl", status: "No status" },
40
+ { id: "balance-sheet", label: "Balance sheet", icon: "fin-balance", status: "No status" },
41
+ { id: "margin-by-channel", label: "Margin by channel", icon: "fin-margin", status: "No status" },
42
+ { id: "debtors-creditors", label: "Debtors & creditors", icon: "fin-debtors", status: "No status" },
43
+ { id: "board-pack", label: "Board pack", icon: "fin-ratios", status: "No status" },
44
+ ] },
45
+ { title: "Planning", items: [
46
+ { id: "month-end-close", label: "Month-end close", icon: "fin-compliance", status: "No status" },
47
+ { id: "budget-reforecast", label: "Budget & reforecast", icon: "fin-capital", status: "No status" },
48
+ { id: "cost-sheets", label: "Cost sheets", icon: "fin-margin", status: "No status" },
49
+ { id: "approvals-authority", label: "Approvals & authority", icon: "fin-approvals", status: "No status" },
50
+ { id: "compliance-calendar", label: "Compliance calendar", icon: "fin-compliance", status: "No status" },
51
+ ] },
52
+ ],
53
+ marketing: [
54
+ { title: "Performance", items: [
55
+ { id: "live-ecomm-sales", label: "Live eComm sales by store", icon: "mkt-live-ecomm-sales-by-store", status: "Live" },
56
+ { id: "paid-media", label: "Paid media", icon: "mkt-paid-media", status: "No status" },
57
+ { id: "email-sms", label: "Email & SMS", icon: "mkt-email-sms", status: "No status" },
58
+ { id: "retention-ltv", label: "Retention & LTV", icon: "mkt-retention-ltv", status: "No status" },
59
+ { id: "social-influencer", label: "Social & influencer", icon: "mkt-social-influencer", status: "No status" },
60
+ { id: "spend-vs-budget", label: "Spend vs budget", icon: "mkt-spend-vs-budget", status: "No status" },
61
+ ] },
62
+ { title: "Planning", items: [
63
+ { id: "marketing-calendar", label: "Marketing calendar", icon: "mkt-marketing-calendar", status: "No status" },
64
+ { id: "social-content-planner", label: "Social media content planner", icon: "mkt-media-content-planner", status: "No status" },
65
+ { id: "influencer-crm", label: "Influencer CRM", icon: "mkt-influencer-crm", status: "No status" },
66
+ { id: "franks-kitchen", label: "Influencer Manager", icon: "mkt-frank-s-kitchen", status: "In progress" },
67
+ { id: "brand-guidelines", label: "Brand guidelines", icon: "mkt-brand-guidelines", status: "In progress" },
68
+ { id: "npd-validator", label: "NPD validator", icon: "mkt-npd-validator", status: "Live" },
69
+ { id: "pricing-researcher", label: "Pricing researcher", icon: "mkt-pricing-researcher", status: "Live" },
70
+ { id: "shelf-simulator", label: "Shelf simulator", icon: "ops-inventory-value", status: "Live" },
71
+ ] },
72
+ { title: "Creative", items: [
73
+ { id: "image-generator", label: "Artificial Design Studio", icon: "mkt-image-generator", status: "Live" },
74
+ { id: "retail-image-library", label: "Asset Manager", icon: "mkt-retail-ecom-image-library", status: "Live" },
75
+ { id: "brand-voice-copywriter", label: "Brand voice copywriter", icon: "mkt-brand-voice-copywriter", status: "Live" },
76
+ ] },
77
+ ],
78
+ commercial: [
79
+ { title: "Reporting", items: [
80
+ { id: "live-sales-by-channel", label: "Live sales by channel", icon: "com-sales-by-channel", status: "Live" },
81
+ { id: "open-orders", label: "Open orders", icon: "com-open-so-s", status: "Live" },
82
+ { id: "retailer-sell-out", label: "Retailer sell out (POS)", icon: "com-retailer-sell-out-pos", status: "In progress" },
83
+ { id: "new-customer-acquisition", label: "New customer acquisition", icon: "com-customer-funnel", status: "To build" },
84
+ { id: "at-risk-customers", label: "At risk / lapsed customers", icon: "com-at-risk-lapsed-customers", status: "To build" },
85
+ ] },
86
+ { title: "Planning", items: [
87
+ { id: "customer-scorecard", label: "Customer scorecard", icon: "com-customer-scorecard", status: "To build" },
88
+ { id: "pricelist-generator", label: "Pricelist generator", icon: "com-pricelist-generator", status: "To build" },
89
+ { id: "margin-calculator", label: "Margin calculator", icon: "com-margin-calculator", status: "In progress" },
90
+ { id: "trade-marketing-calendar", label: "Trade marketing calendar", icon: "com-trade-marketing-calendar", status: "To build" },
91
+ { id: "line-form-populator", label: "New line form populator", icon: "com-line-form-populator", status: "In progress" },
92
+ { id: "crm", label: "CRM", icon: "com-crm", status: "Live" },
93
+ { id: "key-accounts", label: "Key accounts", icon: "com-customer-scorecard", status: "Live" },
94
+ ] },
95
+ ],
96
+ operations: [
97
+ { title: "Reporting", items: [
98
+ { id: "inventory-health", label: "Inventory health", icon: "ops-inventory-health", status: "Live" },
99
+ { id: "out-of-stocks", label: "Out of stocks", icon: "ops-oos", status: "Live" },
100
+ { id: "forecast-accuracy", label: "Forecast accuracy", icon: "ops-forecast-accuracy", status: "In progress" },
101
+ { id: "open-purchase-orders", label: "Open purchase orders", icon: "ops-open-po-s", status: "Live" },
102
+ { id: "shipment-tracker", label: "Shipment tracker", icon: "ops-shipment-tracker", status: "Live" },
103
+ { id: "customer-otif", label: "Customer OTIF", icon: "ops-customer-otif", status: "To build" },
104
+ { id: "inventory-value", label: "Inventory value by item", icon: "ops-inventory-value", status: "Live" },
105
+ ] },
106
+ { title: "Planning", items: [
107
+ { id: "action-queue", label: "Action queue", icon: "ops-action-queue", status: "Live" },
108
+ { id: "demand-planning", label: "Demand planning", icon: "ops-demand-plan", status: "In progress" },
109
+ { id: "ordering-tool", label: "Ordering tool", icon: "ops-order-planing", status: "Live" },
110
+ { id: "carton-label-generator", label: "Carton label generator", icon: "ops-carton-label-generator", status: "Live" },
111
+ { id: "pm-copilot", label: "PM Copilot", icon: "hub-tasks", status: "Live" },
112
+ { id: "candidate-hub", label: "Candidate hub", icon: "hub-accounts", status: "Live" },
113
+ ] },
114
+ ],
115
+ };
116
+ /* Ops Hub is linked, not federated (CONTRACT.md): its catalogue row decides
117
+ who sees its tools live; the fixed address serves frames off the core. */
118
+ const OPS_HUB = "https://frankhub.lovable.app";
119
+ const opsHub = (path) => ({ app: "ops_hub", url: OPS_HUB, path });
120
+ /**
121
+ * Where the tools go: every Live tool, and the tools whose app is already in
122
+ * the catalogue whatever the map says of them (Influencer Manager, Marketing
123
+ * calendar, Paid media), so every app the app switcher offers is reachable
124
+ * from the menu too. Every link opens in the same tab. Pricing researcher and
125
+ * Brand voice copywriter have no app to open yet, so they stay unlinked
126
+ * ("Live, not linked from the Hub yet"). The Hub itself (smart_comms_hub) is
127
+ * the rail's Hub, not a row.
128
+ */
129
+ export const OS_TOOL_LINKS = {
130
+ "live-ecomm-sales": { app: "alive_reporting" },
131
+ "paid-media": { app: "ad_management" },
132
+ "marketing-calendar": { app: "content_calendar" },
133
+ "franks-kitchen": { app: "frank_kitchen" },
134
+ "npd-validator": { app: "product_idea_validator" },
135
+ "pricing-researcher": {},
136
+ "shelf-simulator": { app: "shelf_simulator" },
137
+ "image-generator": { app: "frank_create" },
138
+ "retail-image-library": { app: "asset_portal" },
139
+ "brand-voice-copywriter": {},
140
+ "live-sales-by-channel": opsHub("/sales-dashboard"),
141
+ "open-orders": opsHub("/customer-orders"),
142
+ "crm": { app: "enxgy_crm", url: "https://e-crm.autosolutions.app" },
143
+ "key-accounts": { app: "key_accounts", url: "https://key-accounts.lovable.app" },
144
+ "inventory-health": opsHub("/inventory-health"),
145
+ "out-of-stocks": opsHub("/inventory-health?view=oos"),
146
+ "open-purchase-orders": opsHub("/po-pipeline"),
147
+ "shipment-tracker": opsHub("/shipment-tracker"),
148
+ "inventory-value": opsHub("/inventory-health?view=valuation"),
149
+ "action-queue": opsHub("/action-queue"),
150
+ "ordering-tool": opsHub("/ordering"),
151
+ "carton-label-generator": { app: "label_maker", url: "https://label-maker-app.lovable.app" },
152
+ "pm-copilot": { app: "pm_copilot", url: "https://pm-copilot.autosolutions.app" },
153
+ "candidate-hub": { app: "candidate_hub" },
154
+ };
155
+ /* Each tool's status in the map, for catalogueLinks. */
156
+ const STATUS = new Map(Object.values(OS_MENU).flatMap((groups) => groups.flatMap((group) => group.items.map((tool) => [tool.id, tool.status]))));
157
+ /** The Hub's own pages, for the rail's Hub, Admin and Settings rows in other apps. */
158
+ export const HUB_PATHS = {
159
+ home: "/comms/overview",
160
+ admin: "/admin",
161
+ settings: "/comms/settings",
162
+ };
163
+ /** Why a row is locked. A tool whose status is In progress or To build says so instead. */
164
+ export const HINTS = {
165
+ locked: "Ask your company admin to switch this on for you",
166
+ notBuilt: "Not built yet",
167
+ notLinked: "Live, not linked from the Hub yet",
168
+ };
169
+ /** The rail footer: Admin (for someone who manages something), Settings, then Sign out, always last. */
170
+ export function osFooter({ admin = false } = {}) {
171
+ return [
172
+ ...(admin ? [{ id: "admin", label: "Admin", icon: "fin-approvals" }] : []),
173
+ { id: "settings", label: "Settings", icon: "settings" },
174
+ { id: "signout", label: "Sign out", icon: "signout" },
175
+ ];
176
+ }
177
+ /**
178
+ * Tool id to address, for the tools with a fixed `url`: what a frame off the
179
+ * core, which has no catalogue to ask, can link to. No tenant hint.
180
+ */
181
+ export function staticLinks() {
182
+ const out = {};
183
+ for (const [tool, link] of Object.entries(OS_TOOL_LINKS))
184
+ if (link.url)
185
+ out[tool] = joinPath(link.url, link.path);
186
+ return out;
187
+ }
188
+ /**
189
+ * Tool id to address, and tool id to the reason a row is off, for
190
+ * OsWorkspaceFrame to pass to osFrame. `apps` is os_context().apps: the apps
191
+ * this person holds in the acting company, as the app switcher lists them.
192
+ * For a tool whose OS_TOOL_LINKS entry names an app:
193
+ * - held, with an address: linked to it (plus `path`), with the company as
194
+ * ?tenant=, whatever status the map gives the tool, because the switcher
195
+ * already offers that app;
196
+ * - held, not deployed yet (no address): a Live tool reads "Live, not linked
197
+ * from the Hub yet"; any other keeps the map's own status;
198
+ * - not held: a Live tool is locked ("Ask your company admin to switch this
199
+ * on for you"); any other keeps the map's own status (Not built yet, In
200
+ * progress, To build).
201
+ * A fixed `url` is never consulted here: it is no way round the catalogue.
202
+ * Tools with no app keep osFrame's own rules. Links are final: osFrame adds
203
+ * nothing to them.
204
+ */
205
+ export function catalogueLinks(apps, tenantSlug = null) {
206
+ const links = {};
207
+ const locked = {};
208
+ for (const [tool, link] of Object.entries(OS_TOOL_LINKS)) {
209
+ if (!link.app)
210
+ continue;
211
+ const app = apps.find((candidate) => candidate.key === link.app);
212
+ if (app?.url)
213
+ links[tool] = withTenantHint(joinPath(app.url, link.path), tenantSlug);
214
+ else if (STATUS.get(tool) !== "Live")
215
+ continue; // the map's own status says it
216
+ else if (app)
217
+ locked[tool] = HINTS.notLinked;
218
+ else
219
+ locked[tool] = HINTS.locked;
220
+ }
221
+ return { links, locked };
222
+ }
223
+ const ORIGIN = "http://os.invalid";
224
+ function joinPath(base, path) {
225
+ if (!path)
226
+ return base;
227
+ try {
228
+ return new URL(path, base.endsWith("/") ? base : `${base}/`).toString();
229
+ }
230
+ catch {
231
+ return base;
232
+ }
233
+ }
234
+ function trimmed(pathname) {
235
+ return pathname.replace(/\/+$/, "") || "/";
236
+ }
237
+ /** How specifically `route` claims `location`: [path length, query params], or null when it does not. */
238
+ function claim(route, location) {
239
+ const wanted = new URL(route, ORIGIN);
240
+ const path = trimmed(wanted.pathname);
241
+ const here = trimmed(location.pathname);
242
+ if (path !== "/" && here !== path && !here.startsWith(`${path}/`))
243
+ return null;
244
+ let params = 0;
245
+ for (const [key, value] of wanted.searchParams) {
246
+ if (location.searchParams.get(key) !== value)
247
+ return null;
248
+ params += 1;
249
+ }
250
+ return [path === "/" ? 0 : path.length, params];
251
+ }
252
+ function better(a, b) {
253
+ if (!a)
254
+ return false;
255
+ if (!b)
256
+ return true;
257
+ return a[0] > b[0] || (a[0] === b[0] && a[1] > b[1]);
258
+ }
259
+ function best(routes, location) {
260
+ let top = null;
261
+ for (const route of routes) {
262
+ if (!route)
263
+ continue;
264
+ const score = claim(route, location);
265
+ if (better(score, top))
266
+ top = score;
267
+ }
268
+ return top;
269
+ }
270
+ /**
271
+ * The frame for one app: the rail, every function's menu for this person and
272
+ * company, the footer, and the only routing logic the frame needs.
273
+ */
274
+ export function osFrame(options) {
275
+ const { appKey, tools = [], hubItems, links = {}, locked = {}, admin = false, settingsPath, hubUrl = HUB_URL, tenantSlug = null, } = options;
276
+ const isHub = appKey === "hub";
277
+ const homePath = options.homePath ?? (isHub ? HUB_PATHS.home : "/");
278
+ const own = new Map(tools.map((tool) => [tool.id, tool]));
279
+ const toHub = (path) => ({ href: withTenantHint(`${hubUrl}${path}`, tenantSlug), external: true });
280
+ const sections = {};
281
+ if (hubItems && hubItems.length)
282
+ sections["hub"] = hubItems.map((section) => ({ ...section, items: [...section.items] }));
283
+ for (const [fn, groups] of Object.entries(OS_MENU)) {
284
+ sections[fn] = groups.map((group) => ({
285
+ title: group.title,
286
+ items: group.items.map((tool) => {
287
+ const mine = own.get(tool.id);
288
+ const base = { id: tool.id, label: tool.label, icon: tool.icon };
289
+ if (mine)
290
+ return rowFor(base, mine);
291
+ /* A link or a reason from the catalogue outranks the map's status:
292
+ the catalogue knows what this person holds. */
293
+ const href = links[tool.id];
294
+ if (href)
295
+ return { ...base, href };
296
+ const reason = locked[tool.id];
297
+ if (reason)
298
+ return { ...base, disabled: true, title: reason };
299
+ if (tool.status !== "Live")
300
+ return { ...base, disabled: true, title: tool.status === "No status" ? HINTS.notBuilt : tool.status };
301
+ return { ...base, disabled: true, title: HINTS.notLinked };
302
+ }),
303
+ }));
304
+ }
305
+ /* This app's tools the menu map does not have, placed where the app says. */
306
+ const mapped = new Set(Object.values(OS_MENU).flatMap((groups) => groups.flatMap((group) => group.items.map((tool) => tool.id))));
307
+ for (const tool of tools) {
308
+ if (mapped.has(tool.id) || !tool.fn)
309
+ continue;
310
+ const list = sections[tool.fn] ?? (sections[tool.fn] = []);
311
+ const title = tool.section;
312
+ let section = list.find((candidate) => candidate.title === title);
313
+ if (!section) {
314
+ section = title === undefined ? { items: [] } : { title, items: [] };
315
+ list.push(section);
316
+ }
317
+ section.items.push(rowFor({ id: tool.id, label: tool.label ?? tool.id, ...(tool.icon ? { icon: tool.icon } : {}) }, tool));
318
+ }
319
+ const footerItems = osFooter({ admin });
320
+ /* Every place a location can land, most specific claim wins. */
321
+ const claims = [];
322
+ if (settingsPath)
323
+ claims.push({ routes: [settingsPath], spot: { fn: "settings", item: null, tab: null } });
324
+ if (isHub)
325
+ claims.push({ routes: [HUB_PATHS.admin], spot: { fn: "admin", item: null, tab: null } });
326
+ for (const [fn, list] of Object.entries(sections)) {
327
+ for (const section of list) {
328
+ for (const row of section.items) {
329
+ if (row.disabled)
330
+ continue;
331
+ claims.push({ routes: [row.path, ...(row.match ?? [])], spot: { fn, item: row.id, tab: null } });
332
+ for (const tab of flatTabs(row.tabs)) {
333
+ claims.push({ routes: [tab.path, ...(tab.match ?? [])], spot: { fn, item: row.id, tab: tab.id } });
334
+ }
335
+ }
336
+ }
337
+ }
338
+ const landingFn = tools.map((tool) => fnOf(sections, tool.id)).find((fn) => fn !== undefined);
339
+ const locate = (location) => {
340
+ let url;
341
+ try {
342
+ url = new URL(location, ORIGIN);
343
+ }
344
+ catch {
345
+ url = new URL("/", ORIGIN);
346
+ }
347
+ let top = null;
348
+ let found;
349
+ for (const candidate of claims) {
350
+ const score = best(candidate.routes, url);
351
+ if (better(score, top)) {
352
+ top = score;
353
+ found = candidate.spot;
354
+ }
355
+ }
356
+ if (found)
357
+ return { ...found };
358
+ /* Nowhere in particular: the Hub's landing, menu closed, or this app's function. */
359
+ if (isHub)
360
+ return { fn: "hub", item: null, tab: null, open: false };
361
+ return { fn: landingFn ?? "hub", item: null, tab: null };
362
+ };
363
+ /* Is `location` already exactly at `path`? */
364
+ const at = (location, path) => {
365
+ try {
366
+ const here = new URL(location, ORIGIN);
367
+ const there = new URL(path, ORIGIN);
368
+ return trimmed(here.pathname) === trimmed(there.pathname) && here.search === there.search;
369
+ }
370
+ catch {
371
+ return false;
372
+ }
373
+ };
374
+ const targetFor = (state, location) => {
375
+ const fn = state.fn.id;
376
+ const here = locate(location);
377
+ const internal = (href, spot) => (here.fn === spot.fn && here.item === spot.item && here.tab === spot.tab ? null : { href, external: false });
378
+ if (fn === "settings") {
379
+ const path = settingsPath ?? (isHub ? HUB_PATHS.settings : undefined);
380
+ if (!path)
381
+ return toHub(HUB_PATHS.settings);
382
+ return at(location, path) ? null : { href: path, external: false };
383
+ }
384
+ if (fn === "admin")
385
+ return isHub ? internal(HUB_PATHS.admin, { fn, item: null, tab: null }) : toHub(HUB_PATHS.admin);
386
+ const row = state.item ? (sections[fn] ?? []).flatMap((section) => section.items).find((r) => r.id === state.item?.id) : undefined;
387
+ if (!row) {
388
+ if (fn !== "hub")
389
+ return null; // opening a function's menu is not a navigation
390
+ if (!isHub)
391
+ return toHub(HUB_PATHS.home);
392
+ /* The Hub: stay anywhere on the landing's own ground, else land on it. */
393
+ const landing = locate(homePath);
394
+ return here.fn === landing.fn && here.item === landing.item ? null : { href: homePath, external: false };
395
+ }
396
+ const tab = state.tab ? flatTabs(row.tabs).find((t) => t.id === state.tab?.id) : undefined;
397
+ if (tab?.path)
398
+ return internal(tab.path, { fn, item: row.id, tab: tab.id });
399
+ if (row.path)
400
+ return internal(row.path, { fn, item: row.id, tab: here.fn === fn && here.item === row.id ? here.tab : null });
401
+ if (row.href)
402
+ return { href: row.href, external: true };
403
+ return null;
404
+ };
405
+ return { functions: OS_FUNCTIONS, sections, footerItems, homeFunction: "hub", locate, targetFor };
406
+ }
407
+ function rowFor(base, tool) {
408
+ const row = { ...base };
409
+ if (tool.path)
410
+ row.path = tool.path;
411
+ if (tool.match)
412
+ row.match = tool.match;
413
+ if (tool.tabs)
414
+ row.tabs = tool.tabs;
415
+ if (tool.menu !== undefined)
416
+ row.menu = tool.menu;
417
+ if (tool.badge !== undefined)
418
+ row.badge = tool.badge;
419
+ return row;
420
+ }
421
+ /** Tabs as one list, whether given flat or in groups. */
422
+ function flatTabs(tabs) {
423
+ if (!tabs)
424
+ return [];
425
+ return tabs.flatMap((tab) => ("items" in tab && Array.isArray(tab.items) ? tab.items : [tab]));
426
+ }
427
+ function fnOf(sections, id) {
428
+ for (const [fn, list] of Object.entries(sections)) {
429
+ if (list.some((section) => section.items.some((row) => row.id === id)))
430
+ return fn;
431
+ }
432
+ return undefined;
433
+ }