autosolutions-os 0.3.2 → 0.3.4
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/CHANGELOG.md +101 -0
- package/README.md +78 -8
- package/dist/contextCache.d.ts +11 -0
- package/dist/contextCache.js +24 -0
- package/dist/coreReader.d.ts +51 -0
- package/dist/coreReader.js +153 -0
- package/dist/coreView.d.ts +56 -0
- package/dist/coreView.js +132 -0
- package/dist/frame.d.ts +2 -15
- package/dist/frame.js +7 -80
- package/dist/nav.d.ts +19 -9
- package/dist/nav.js +35 -10
- package/dist/plate.d.ts +49 -0
- package/dist/plate.js +107 -0
- package/package.json +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,106 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.3.4 - 2026-10-18
|
|
4
|
+
|
|
5
|
+
The Hub's frame in the two apps that keep their own Supabase project (Asset
|
|
6
|
+
Portal and Ops Hub), for a person signed in at the Hub. The final visual review
|
|
7
|
+
found them drawing the frame from fixed addresses, so, unlike the Hub, they
|
|
8
|
+
showed no Admin row for company admins, opened the five held apps with no
|
|
9
|
+
address (Paid media, Marketing calendar, Influencer Manager, NPD validator,
|
|
10
|
+
Shelf simulator) as Hub sample pages instead of the Hub's locked rows, left
|
|
11
|
+
rows for apps the company does not hold open, and drew a static plate with no
|
|
12
|
+
switcher. Apps on the core take this release with a lockfile bump: nothing
|
|
13
|
+
changes for them.
|
|
14
|
+
|
|
15
|
+
### autosolutions-os/core-view
|
|
16
|
+
|
|
17
|
+
- `useCoreFrame({ appKey, email, fallback, companySwitch? })` returns
|
|
18
|
+
`{ source, frameOptions, plate }`: spread `frameOptions` into `osFrame` and
|
|
19
|
+
`plate` into the workspace `TopBar`. With a Hub session for the app's person
|
|
20
|
+
on an autosolutions.app address, they are what OsWorkspaceFrame draws for
|
|
21
|
+
that app: the catalogue's links (with the acting company as `?tenant=`) and
|
|
22
|
+
locks, the Admin row from `my_admin_scope()`, and the acting company's plate
|
|
23
|
+
with its switcher for someone with more than one company. Without one, they
|
|
24
|
+
are the frame from 0.3.3: `staticLinks(fallback.tenantSlug)`, no Admin row,
|
|
25
|
+
the fallback's static plate. `source` is `"core"`, `"checking"` (asking the
|
|
26
|
+
core, as the Hub does while it loads) or `"static"`.
|
|
27
|
+
- The reader under it, `createCoreViewClient({ email })`, borrows the Hub's
|
|
28
|
+
access token from the core's shared cookie at every call, while it is good,
|
|
29
|
+
only for the app's own person (by email), and only for `os_context`,
|
|
30
|
+
`my_admin_scope` and `switch_company_for_app` (`CORE_VIEW_FUNCTIONS`), posted
|
|
31
|
+
to the core's REST endpoint with no cookies. It never signs in, refreshes,
|
|
32
|
+
writes a cookie or storage, or publishes a session, and the token never
|
|
33
|
+
reaches the app. `hubSession(email)` says what it sees: the core user id,
|
|
34
|
+
the email, the expiry and whether the token can be used; never the token.
|
|
35
|
+
- The company switch is the package's own: the core switches, then the app
|
|
36
|
+
reloads when the new company holds it here, goes to its deployment
|
|
37
|
+
elsewhere, or goes to the Hub when the company does not hold the app.
|
|
38
|
+
|
|
39
|
+
### Shared with OsWorkspaceFrame
|
|
40
|
+
|
|
41
|
+
- The company plate and its menu moved from frame.tsx to plate.tsx
|
|
42
|
+
(`useOsPlate`), unchanged, so OsWorkspaceFrame and `useCoreFrame` draw them
|
|
43
|
+
from one copy.
|
|
44
|
+
- The `os_context()` cache follows the Hub's person for `useCoreFrame`
|
|
45
|
+
(`followOsPerson`): a cached answer for someone else is never handed out.
|
|
46
|
+
|
|
47
|
+
### What each app does after taking 0.3.4
|
|
48
|
+
|
|
49
|
+
- Apps on the core: bump `autosolutions-os` to `^0.3.4`. Nothing else.
|
|
50
|
+
- Asset Portal (staff face) and Ops Hub: bump, then build the frame's links,
|
|
51
|
+
Admin row and plate from `useCoreFrame` (README, "On its own project, with
|
|
52
|
+
the Hub's session").
|
|
53
|
+
|
|
54
|
+
## 0.3.3 - 2026-10-18
|
|
55
|
+
|
|
56
|
+
The owner's three calls of 18 October. Every app takes them with a lockfile
|
|
57
|
+
bump: no code changes.
|
|
58
|
+
|
|
59
|
+
### Marketing has an Overview
|
|
60
|
+
|
|
61
|
+
- `OS_MENU.marketing` starts with an Overview row (`marketing-home`), exactly
|
|
62
|
+
like Finance, Commercial and Operations: the same label, glyph and group
|
|
63
|
+
with no title. Cliff's map draws none for Marketing; the owner asked for it
|
|
64
|
+
("be sure it's added everywhere in all apps, exactly the same").
|
|
65
|
+
- Its page in the Hub is `/os/marketing` (`osHubPath("marketing")`), the
|
|
66
|
+
Hub's Marketing hub sample, so the row is marked " (*)" in every app.
|
|
67
|
+
|
|
68
|
+
### Operations' Overview opens Ops Hub
|
|
69
|
+
|
|
70
|
+
- `OS_TOOL_LINKS["operations-home"]` is Ops Hub's home dashboard
|
|
71
|
+
(`{ app: "ops_hub", url: "https://ops-hub.autosolutions.app", path: "/" }`),
|
|
72
|
+
in every app, instead of the Hub's Operations hub sample page. Someone who
|
|
73
|
+
holds Ops Hub goes there, with the company as `?tenant=`; for someone who
|
|
74
|
+
does not, the row is locked with the admin hint, like every Ops Hub row; a
|
|
75
|
+
frame off the core links it from `staticLinks()`. The row loses its " (*)"
|
|
76
|
+
where it opens Ops Hub.
|
|
77
|
+
- In the Hub, the page of a locked row (Operations' Overview, an Ops Hub
|
|
78
|
+
tool's page) now files under its function with no row picked: someone who
|
|
79
|
+
does not hold Ops Hub and opens `/os/operations` sees the Operations menu
|
|
80
|
+
with its Overview locked, not the Hub's landing with the menu closed.
|
|
81
|
+
Before, such a page filed under the function's Overview.
|
|
82
|
+
|
|
83
|
+
### Asset Manager's address
|
|
84
|
+
|
|
85
|
+
- Asset Manager's fixed address is `https://asset-portal.autosolutions.app`
|
|
86
|
+
(was `https://self-asset-manager.lovable.app`), the address the Hub's
|
|
87
|
+
catalogue gives Asset Portal: on autosolutions.app it shares the Hub's
|
|
88
|
+
sign-in.
|
|
89
|
+
|
|
90
|
+
### Counts
|
|
91
|
+
|
|
92
|
+
- The map has 60 rows (56 tools, four Overviews). With every live app's
|
|
93
|
+
address in the catalogue, 18 open an app (Operations' Overview among them)
|
|
94
|
+
and 42 are samples, the same in the Hub and in every app. The menu search
|
|
95
|
+
finds five rows named Overview: the Hub's, Finance's, Marketing's,
|
|
96
|
+
Commercial's and Operations'.
|
|
97
|
+
|
|
98
|
+
### What each app does after taking 0.3.3
|
|
99
|
+
|
|
100
|
+
- Every app: bump `autosolutions-os` to `^0.3.3`.
|
|
101
|
+
- The Hub: draw a page for `/os/marketing` (its Marketing hub sample), as it
|
|
102
|
+
does for the other Overviews.
|
|
103
|
+
|
|
3
104
|
## 0.3.2 - 2026-10-18
|
|
4
105
|
|
|
5
106
|
One app, as the owner asked on 18 October: the top bar, the rail, the menus
|
package/README.md
CHANGED
|
@@ -94,8 +94,8 @@ person signs out in the app.
|
|
|
94
94
|
|
|
95
95
|
`OsWorkspaceFrame` is the frame every app on the core renders around its pages:
|
|
96
96
|
the top bar with the company's mark and switcher, the rail, the OS menu (Cliff's
|
|
97
|
-
menu map, 0.3.0: 56 tools and an Overview first in
|
|
98
|
-
|
|
97
|
+
menu map, 0.3.0: 56 tools and an Overview first in every function, Marketing's
|
|
98
|
+
since 0.3.3, our Shelf simulator, Key accounts, PM Copilot and Candidate hub
|
|
99
99
|
among them), the Hub panel, the footer with Admin for people who manage
|
|
100
100
|
something, and the browser-tab icon. It needs `autosolutions-design-system`
|
|
101
101
|
6.5.0 or later (an optional peer: only this entry point uses it). Take this
|
|
@@ -234,10 +234,15 @@ why).
|
|
|
234
234
|
for it. `initialOpen={false}` starts the menu closed on the first location,
|
|
235
235
|
which a phone no longer needs it for; after that the frame's own rule
|
|
236
236
|
applies. `data-*` and `aria-*` props land on the frame's root.
|
|
237
|
-
- A row whose page is one of the Hub's sample pages (
|
|
238
|
-
every tool with no app to open) has " (*)"
|
|
239
|
-
(0.3.2, `OS_SAMPLE_MARK`): the owner's mark
|
|
240
|
-
that opens an app has none, so a tool loses it
|
|
237
|
+
- A row whose page is one of the Hub's sample pages (Finance's, Marketing's
|
|
238
|
+
and Commercial's Overviews and every tool with no app to open) has " (*)"
|
|
239
|
+
after its name, in every app (0.3.2, `OS_SAMPLE_MARK`): the owner's mark
|
|
240
|
+
for work in progress. A row that opens an app has none, so a tool loses it
|
|
241
|
+
the day it gets one.
|
|
242
|
+
- Operations' Overview is Ops Hub's home dashboard (0.3.3): it opens Ops Hub
|
|
243
|
+
for someone who holds it, and is locked for someone who does not, like
|
|
244
|
+
Ops Hub's tools. In the Hub a locked row's page files under its function,
|
|
245
|
+
with no row picked.
|
|
241
246
|
- Admin appears when `my_admin_scope()` says the person manages something; pass
|
|
242
247
|
`admin` to decide it yourself.
|
|
243
248
|
- Every app shows the Hub panel (0.3.0, `OS_HUB_MENU`, the design's): Smart
|
|
@@ -285,10 +290,13 @@ Supabase: the rail (`OS_FUNCTIONS`), the menu map (`OS_MENU`), the Hub panel
|
|
|
285
290
|
and `searchOsMenu()` (the menu search's data and matching), and `osFrame()`,
|
|
286
291
|
the routing rules the frame runs on. An app on its own
|
|
287
292
|
project (Ops Hub: see "Linked, not federated" in `docs/CONTRACT.md`) pairs it
|
|
288
|
-
with the design system's `WorkspaceFrame`, a static plate and no switcher
|
|
293
|
+
with the design system's `WorkspaceFrame`, a static plate and no switcher; for
|
|
294
|
+
a person signed in at the Hub it draws the Hub's frame instead (0.3.4, below).
|
|
289
295
|
`staticLinks(slug)` are the fixed addresses, the ones the Hub's catalogue
|
|
290
296
|
gives (0.3.2: every app with a live address, Ops Hub's tools on
|
|
291
|
-
ops-hub.autosolutions.app, PM Copilot's console at `/ops
|
|
297
|
+
ops-hub.autosolutions.app, PM Copilot's console at `/ops`; 0.3.3: Operations'
|
|
298
|
+
Overview on Ops Hub's home, Asset Manager on asset-portal.autosolutions.app),
|
|
299
|
+
each with the
|
|
292
300
|
company's slug as `?tenant=`, exactly as the catalogue's links carry it; pass
|
|
293
301
|
the same slug as `tenantSlug` for the rows into the Hub. With no catalogue to
|
|
294
302
|
ask, they are all live, and each app's own sign-in decides. Every other row
|
|
@@ -337,6 +345,68 @@ const menuSearch = useOsMenuSearch({ frame, location: here, onNavigate: (href) =
|
|
|
337
345
|
{menuSearch.results}
|
|
338
346
|
```
|
|
339
347
|
|
|
348
|
+
### On its own project, with the Hub's session (0.3.4)
|
|
349
|
+
|
|
350
|
+
An app on its own Supabase project (Asset Portal, Ops Hub) cannot ask the core
|
|
351
|
+
with its own client. On an autosolutions.app address it can still draw the
|
|
352
|
+
Hub's frame for a person who is signed in at the Hub: `useCoreFrame` from
|
|
353
|
+
`autosolutions-os/core-view` reads the core's own view of that person through
|
|
354
|
+
the Hub's session in the core's shared cookie, and gives the frame what
|
|
355
|
+
OsWorkspaceFrame gives an app on the core: the catalogue's links and locks
|
|
356
|
+
(`catalogueLinks`), the Admin row (`my_admin_scope()`), and the company plate
|
|
357
|
+
with its switcher (`os_context()`, the same plate code as OsWorkspaceFrame's).
|
|
358
|
+
The app keeps its own `osFrame`, `WorkspaceFrame` and `TopBar`, and spreads
|
|
359
|
+
two objects into them:
|
|
360
|
+
|
|
361
|
+
```tsx
|
|
362
|
+
import { TopBar, WorkspaceFrame, companyMarkUrl } from "autosolutions-design-system";
|
|
363
|
+
import { useCoreFrame } from "autosolutions-os/core-view";
|
|
364
|
+
import { osFrame } from "autosolutions-os/nav";
|
|
365
|
+
|
|
366
|
+
const core = useCoreFrame({
|
|
367
|
+
appKey: "asset_portal",
|
|
368
|
+
email: user?.email, // the person signed in to the app
|
|
369
|
+
fallback: { tenantSlug: "frank-body", companyName: "frank body", companySrc: companyMarkUrl("frankbody", "plain") },
|
|
370
|
+
});
|
|
371
|
+
const frame = useMemo(() => osFrame({ appKey: "asset_portal", tools, ...core.frameOptions }), [tools, core.frameOptions]);
|
|
372
|
+
|
|
373
|
+
<WorkspaceFrame topBar={<TopBar variant="workspace" {...menuSearch.topBar} {...core.plate} />} /* the rest as before */ />
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
`core.source` says which frame it is: `"core"` (the core's view), `"checking"`
|
|
377
|
+
(the Hub's session is there and the core is being asked: other apps' rows read
|
|
378
|
+
"Checking your access…" and the plate keeps its place, as in the Hub while it
|
|
379
|
+
loads) or `"static"` (the fallback). The fallback is the frame from before
|
|
380
|
+
0.3.4: `staticLinks(tenantSlug)`, no Admin row, and a static plate. It is drawn
|
|
381
|
+
whenever there is no Hub session for the app's person: off autosolutions.app,
|
|
382
|
+
signed out at the Hub, someone else signed in there (the Hub session counts
|
|
383
|
+
only when its email is `email`), nobody signed in to the app yet, blocked
|
|
384
|
+
cookies, or a core that does not answer. The hook looks at the cookie again
|
|
385
|
+
when the window comes back, when the page is shown again and once a minute.
|
|
386
|
+
|
|
387
|
+
The reader behind it (`createCoreViewClient`; `hubSession(email)` says what it
|
|
388
|
+
sees, without the token) is read-only:
|
|
389
|
+
|
|
390
|
+
- it borrows the Hub's access token from the shared cookie at every call, and
|
|
391
|
+
only while that token is good (more than 30 seconds left). It never signs in,
|
|
392
|
+
never refreshes the token, never writes a cookie or storage, and never
|
|
393
|
+
publishes a session: a refresh would rotate the Hub's refresh token behind
|
|
394
|
+
the Hub's back. A token that has expired since the frame was read keeps the
|
|
395
|
+
frame; on a fresh page it is the fallback until the Hub (or any app on the
|
|
396
|
+
core) refreshes it, and the frame follows when the window comes back;
|
|
397
|
+
- it sends the token to the core's REST endpoint and nowhere else, for
|
|
398
|
+
`os_context`, `my_admin_scope` and `switch_company_for_app` only
|
|
399
|
+
(`CORE_VIEW_FUNCTIONS`); any other name is refused before anything is sent;
|
|
400
|
+
- the company switch is the Hub's (`companySwitch`, default `"os"`): the core
|
|
401
|
+
switches the acting company, then the app reloads when the new company holds
|
|
402
|
+
it at this address, goes to its deployment elsewhere, or goes to the Hub when
|
|
403
|
+
the company does not hold the app. The app's own sign-in, data and access
|
|
404
|
+
rules do not change.
|
|
405
|
+
|
|
406
|
+
On the server there is no cookie to read, so the hook gives the fallback:
|
|
407
|
+
render the frame in the browser only (Asset Portal's staff face does), or
|
|
408
|
+
accept a first paint of the fallback.
|
|
409
|
+
|
|
340
410
|
## On the server
|
|
341
411
|
|
|
342
412
|
The package is plain ES modules, so Node, a test runner and a dev server's SSR
|
package/dist/contextCache.d.ts
CHANGED
|
@@ -47,4 +47,15 @@ export declare function scheduleOsContextRetry(os: unknown): void;
|
|
|
47
47
|
* new answer.
|
|
48
48
|
*/
|
|
49
49
|
export declare function retryOsContext(os: unknown): Promise<OsContext | null>;
|
|
50
|
+
/** Test seam: forget the person followed and the answer, as on a fresh page. */
|
|
51
|
+
export declare function resetOsContextPerson(): void;
|
|
52
|
+
/**
|
|
53
|
+
* Follow the person whose context this is, by their core user id, for a
|
|
54
|
+
* frame that reads the core through a session it does not hold itself
|
|
55
|
+
* (`useCoreFrame`, autosolutions-os/core-view, 0.3.4: the Hub's session, read
|
|
56
|
+
* from the shared cookie). The first call names the person, so a cached
|
|
57
|
+
* answer for anyone else is not handed out; after that a change of person
|
|
58
|
+
* reads again, and null (no Hub session for this person) forgets the answer.
|
|
59
|
+
*/
|
|
60
|
+
export declare function followOsPerson(person: string | null): void;
|
|
50
61
|
export declare function watchOsAuth(auth: unknown): void;
|
package/dist/contextCache.js
CHANGED
|
@@ -193,6 +193,30 @@ function personChanged(person) {
|
|
|
193
193
|
if (lastOs)
|
|
194
194
|
void again(lastOs);
|
|
195
195
|
}
|
|
196
|
+
/** Test seam: forget the person followed and the answer, as on a fresh page. */
|
|
197
|
+
export function resetOsContextPerson() {
|
|
198
|
+
authUser = undefined;
|
|
199
|
+
pendingFor = undefined;
|
|
200
|
+
invalidateOsContext();
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Follow the person whose context this is, by their core user id, for a
|
|
204
|
+
* frame that reads the core through a session it does not hold itself
|
|
205
|
+
* (`useCoreFrame`, autosolutions-os/core-view, 0.3.4: the Hub's session, read
|
|
206
|
+
* from the shared cookie). The first call names the person, so a cached
|
|
207
|
+
* answer for anyone else is not handed out; after that a change of person
|
|
208
|
+
* reads again, and null (no Hub session for this person) forgets the answer.
|
|
209
|
+
*/
|
|
210
|
+
export function followOsPerson(person) {
|
|
211
|
+
if (authUser === undefined) {
|
|
212
|
+
authUser = person;
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
if (person === authUser)
|
|
216
|
+
return;
|
|
217
|
+
authUser = person;
|
|
218
|
+
personChanged(person);
|
|
219
|
+
}
|
|
196
220
|
/**
|
|
197
221
|
* Follow sign-ins and sign-outs through the app's Supabase auth client
|
|
198
222
|
* (`supabase.auth`), live, in this tab too, and for an app whose session is
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** The only core functions the reader calls: the frame's two questions, and the company switch. */
|
|
2
|
+
export declare const CORE_VIEW_FUNCTIONS: readonly ["os_context", "my_admin_scope", "switch_company_for_app"];
|
|
3
|
+
export type CoreViewFunction = (typeof CORE_VIEW_FUNCTIONS)[number];
|
|
4
|
+
/** The Hub's session as the reader sees it. Never the token. */
|
|
5
|
+
export type HubSession = {
|
|
6
|
+
/** The person's core user id. */
|
|
7
|
+
userId: string;
|
|
8
|
+
/** Their email at the core, lower case. */
|
|
9
|
+
email: string;
|
|
10
|
+
/** When the access token in the cookie expires, in seconds since the epoch, as Supabase writes it. */
|
|
11
|
+
expiresAt: number;
|
|
12
|
+
/**
|
|
13
|
+
* The token can be used now (it expires more than 30 seconds from now).
|
|
14
|
+
* When it has expired the person is still signed in at the Hub, and the
|
|
15
|
+
* Hub (or any app on the core) refreshes it the next time it is open.
|
|
16
|
+
*/
|
|
17
|
+
usable: boolean;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* The person signed in at the Hub, when it is the person with this email
|
|
21
|
+
* (the one signed in to the app), as the core's shared cookie on
|
|
22
|
+
* .autosolutions.app says; or null: no email, off autosolutions.app, no
|
|
23
|
+
* shared session, signed out at the Hub, someone else, or cookies blocked.
|
|
24
|
+
*/
|
|
25
|
+
export declare function hubSession(email: string | null | undefined): HubSession | null;
|
|
26
|
+
/** What a call answers: the shape supabase-js's `rpc` resolves to, as the package's frame code reads it. */
|
|
27
|
+
export type CoreViewResult = {
|
|
28
|
+
data: unknown;
|
|
29
|
+
error: {
|
|
30
|
+
message: string;
|
|
31
|
+
} | null;
|
|
32
|
+
};
|
|
33
|
+
/** The read-only core client: `rpc` for the three functions in CORE_VIEW_FUNCTIONS. */
|
|
34
|
+
export type CoreViewClient = {
|
|
35
|
+
rpc: (fn: string, args?: Record<string, unknown>) => Promise<CoreViewResult>;
|
|
36
|
+
};
|
|
37
|
+
export type CoreViewClientOptions = {
|
|
38
|
+
/** The email of the person signed in to the app, read at every call. */
|
|
39
|
+
email: () => string | null | undefined;
|
|
40
|
+
/** For tests: the fetch to use. Default the global one. */
|
|
41
|
+
fetch?: typeof fetch;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* A client for the core that borrows the Hub's session from the shared
|
|
45
|
+
* cookie, read at every call: `rpc(fn, args)` for os_context,
|
|
46
|
+
* my_admin_scope and switch_company_for_app, posted to the core's REST
|
|
47
|
+
* endpoint with the session's access token, as supabase-js would. With no
|
|
48
|
+
* usable Hub session for the app's person, or for any other function, it
|
|
49
|
+
* answers with an error and sends nothing.
|
|
50
|
+
*/
|
|
51
|
+
export declare function createCoreViewClient(options: CoreViewClientOptions): CoreViewClient;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The core's view of a person, for an app on its own Supabase project (0.3.4).
|
|
3
|
+
*
|
|
4
|
+
* Asset Portal and Ops Hub keep their own projects, their own sign-in and
|
|
5
|
+
* their own data (docs/CONTRACT.md, "Federated: own project, one sign-in").
|
|
6
|
+
* On an autosolutions.app address they can still read the core's shared
|
|
7
|
+
* cookie, which holds the Hub's session, and so ask the core the questions
|
|
8
|
+
* the Hub's frame asks: os_context() (the acting company, the person's
|
|
9
|
+
* companies and the apps they hold) and my_admin_scope() (whether they manage
|
|
10
|
+
* anything), and switch the acting company, as the Hub's plate does.
|
|
11
|
+
*
|
|
12
|
+
* This file is that reader, and nothing more:
|
|
13
|
+
*
|
|
14
|
+
* - It never signs in, never refreshes a token, never writes a cookie or
|
|
15
|
+
* storage, and never publishes a session anywhere. It reads the shared
|
|
16
|
+
* cookie on every call and uses the access token in it while that token is
|
|
17
|
+
* good. A refresh would rotate the Hub's refresh token behind the Hub's
|
|
18
|
+
* back; only the Hub and the apps on the core refresh it.
|
|
19
|
+
* - The token goes to the core's REST endpoint and nowhere else, for three
|
|
20
|
+
* functions only (CORE_VIEW_FUNCTIONS). Any other name is refused before
|
|
21
|
+
* anything is sent. The token is never handed to the app.
|
|
22
|
+
* - It answers only for the app's own person: the Hub's session counts only
|
|
23
|
+
* when its email is the email of the person signed in to the app.
|
|
24
|
+
*
|
|
25
|
+
* Off autosolutions.app, with no shared session, after a sign-out at the
|
|
26
|
+
* Hub, or for someone else, there is no Hub session and the reader answers
|
|
27
|
+
* every call with an error, sending nothing. No React, no supabase-js.
|
|
28
|
+
*/
|
|
29
|
+
import { coreProjectRef, corePublishableKey, coreUrl, PARENT_DOMAIN } from "./config.js";
|
|
30
|
+
import { joinChunks, parseCopy, parseSignedOutNote, sharedSessionCookieName, signedOutCookieName, signedOutCopy, } from "./session.js";
|
|
31
|
+
/** The only core functions the reader calls: the frame's two questions, and the company switch. */
|
|
32
|
+
export const CORE_VIEW_FUNCTIONS = ["os_context", "my_admin_scope", "switch_company_for_app"];
|
|
33
|
+
/**
|
|
34
|
+
* A token this close to expiring is not used: supabase-js treats a session as
|
|
35
|
+
* expired 30 seconds early too, and the Hub refreshes well before that.
|
|
36
|
+
*/
|
|
37
|
+
const EXPIRY_MARGIN_MS = 30_000;
|
|
38
|
+
const normalise = (email) => {
|
|
39
|
+
const value = typeof email === "string" ? email.trim().toLowerCase() : "";
|
|
40
|
+
return value.length > 0 ? value : null;
|
|
41
|
+
};
|
|
42
|
+
function onParentDomain() {
|
|
43
|
+
if (typeof window === "undefined" || typeof document === "undefined")
|
|
44
|
+
return false;
|
|
45
|
+
const host = window.location.hostname;
|
|
46
|
+
return host === PARENT_DOMAIN || host.endsWith(`.${PARENT_DOMAIN}`);
|
|
47
|
+
}
|
|
48
|
+
/** The shared session in the cookie, with its token, when it is this person's and not signed out. */
|
|
49
|
+
function readShared(email) {
|
|
50
|
+
const wanted = normalise(email);
|
|
51
|
+
if (wanted === null || !onParentDomain())
|
|
52
|
+
return null;
|
|
53
|
+
try {
|
|
54
|
+
const ref = coreProjectRef();
|
|
55
|
+
const jar = new Map();
|
|
56
|
+
for (const entry of document.cookie ? document.cookie.split("; ") : []) {
|
|
57
|
+
const index = entry.indexOf("=");
|
|
58
|
+
if (index > 0)
|
|
59
|
+
jar.set(entry.slice(0, index), entry.slice(index + 1));
|
|
60
|
+
}
|
|
61
|
+
const raw = joinChunks(sharedSessionCookieName(ref), (name) => jar.get(name));
|
|
62
|
+
const copy = parseCopy(raw);
|
|
63
|
+
if (!raw || !copy || copy.userId === null)
|
|
64
|
+
return null;
|
|
65
|
+
// Signed out at the Hub: the note covers this copy.
|
|
66
|
+
if (signedOutCopy(copy, parseSignedOutNote(jar.get(signedOutCookieName(ref)))))
|
|
67
|
+
return null;
|
|
68
|
+
const value = JSON.parse(raw);
|
|
69
|
+
const token = typeof value.access_token === "string" ? value.access_token : "";
|
|
70
|
+
const theirs = normalise(typeof value.user?.email === "string" ? value.user.email : null);
|
|
71
|
+
if (!token || theirs === null || theirs !== wanted)
|
|
72
|
+
return null;
|
|
73
|
+
return {
|
|
74
|
+
session: {
|
|
75
|
+
userId: copy.userId,
|
|
76
|
+
email: theirs,
|
|
77
|
+
expiresAt: copy.expiresAt,
|
|
78
|
+
usable: copy.expiresAt * 1000 - Date.now() > EXPIRY_MARGIN_MS,
|
|
79
|
+
},
|
|
80
|
+
token,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
// Cookies blocked or unreadable: no Hub session.
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* The person signed in at the Hub, when it is the person with this email
|
|
90
|
+
* (the one signed in to the app), as the core's shared cookie on
|
|
91
|
+
* .autosolutions.app says; or null: no email, off autosolutions.app, no
|
|
92
|
+
* shared session, signed out at the Hub, someone else, or cookies blocked.
|
|
93
|
+
*/
|
|
94
|
+
export function hubSession(email) {
|
|
95
|
+
return readShared(email)?.session ?? null;
|
|
96
|
+
}
|
|
97
|
+
const refused = (message) => ({ data: null, error: { message } });
|
|
98
|
+
/**
|
|
99
|
+
* A client for the core that borrows the Hub's session from the shared
|
|
100
|
+
* cookie, read at every call: `rpc(fn, args)` for os_context,
|
|
101
|
+
* my_admin_scope and switch_company_for_app, posted to the core's REST
|
|
102
|
+
* endpoint with the session's access token, as supabase-js would. With no
|
|
103
|
+
* usable Hub session for the app's person, or for any other function, it
|
|
104
|
+
* answers with an error and sends nothing.
|
|
105
|
+
*/
|
|
106
|
+
export function createCoreViewClient(options) {
|
|
107
|
+
const allowed = new Set(CORE_VIEW_FUNCTIONS);
|
|
108
|
+
return {
|
|
109
|
+
async rpc(fn, args = {}) {
|
|
110
|
+
if (!allowed.has(fn))
|
|
111
|
+
return refused(`${fn}: not one of the core functions a frame reads`);
|
|
112
|
+
const shared = readShared(options.email());
|
|
113
|
+
if (!shared)
|
|
114
|
+
return refused("No Hub session for this person");
|
|
115
|
+
if (!shared.session.usable)
|
|
116
|
+
return refused("The Hub session needs a refresh at the Hub");
|
|
117
|
+
const send = options.fetch ?? (typeof fetch === "function" ? fetch : undefined);
|
|
118
|
+
if (!send)
|
|
119
|
+
return refused("No fetch");
|
|
120
|
+
try {
|
|
121
|
+
const response = await send(`${coreUrl()}/rest/v1/rpc/${fn}`, {
|
|
122
|
+
method: "POST",
|
|
123
|
+
headers: {
|
|
124
|
+
apikey: corePublishableKey(),
|
|
125
|
+
Authorization: `Bearer ${shared.token}`,
|
|
126
|
+
"Content-Type": "application/json",
|
|
127
|
+
Accept: "application/json",
|
|
128
|
+
},
|
|
129
|
+
body: JSON.stringify(args),
|
|
130
|
+
// The token is the only credential: no cookies of any origin go with it.
|
|
131
|
+
credentials: "omit",
|
|
132
|
+
cache: "no-store",
|
|
133
|
+
});
|
|
134
|
+
const text = await response.text();
|
|
135
|
+
let body = null;
|
|
136
|
+
try {
|
|
137
|
+
body = text ? JSON.parse(text) : null;
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
body = null;
|
|
141
|
+
}
|
|
142
|
+
if (!response.ok) {
|
|
143
|
+
const message = body?.message;
|
|
144
|
+
return refused(typeof message === "string" && message ? message : `HTTP ${response.status}`);
|
|
145
|
+
}
|
|
146
|
+
return { data: body, error: null };
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
return refused("The core did not answer");
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type OsCompanySwitch, type OsPlateProps } from "./plate.js";
|
|
2
|
+
export { CORE_VIEW_FUNCTIONS, createCoreViewClient, hubSession, type CoreViewClient, type CoreViewClientOptions, type CoreViewFunction, type CoreViewResult, type HubSession, } from "./coreReader.js";
|
|
3
|
+
export type { OsCompanySwitch, OsPlateProps } from "./plate.js";
|
|
4
|
+
export type CoreFrameOptions = {
|
|
5
|
+
/** This app's catalogue key: "asset_portal", "ops_hub". */
|
|
6
|
+
appKey: string;
|
|
7
|
+
/**
|
|
8
|
+
* The email of the person signed in to the app. The Hub's session counts
|
|
9
|
+
* only when it is the same person; null or undefined (nobody signed in
|
|
10
|
+
* yet) is the fallback frame.
|
|
11
|
+
*/
|
|
12
|
+
email: string | null | undefined;
|
|
13
|
+
/** Today's frame, for when there is no Hub session: the app's own company. */
|
|
14
|
+
fallback: {
|
|
15
|
+
/** The company's slug in the core, for `staticLinks` and the rows into the Hub: "frank-body". */
|
|
16
|
+
tenantSlug: string;
|
|
17
|
+
/** The plate's name, and what it shows when the artwork does not load. */
|
|
18
|
+
companyName?: string;
|
|
19
|
+
/** The plate's artwork. */
|
|
20
|
+
companySrc?: string;
|
|
21
|
+
};
|
|
22
|
+
/** How the plate switches company with a Hub session. Default "os", as in the Hub. */
|
|
23
|
+
companySwitch?: OsCompanySwitch;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Where the frame comes from:
|
|
27
|
+
* - "core": the core's view of the person, through the Hub's session;
|
|
28
|
+
* - "checking": the Hub's session is there and the core is being asked, so
|
|
29
|
+
* other apps' rows read "Checking your access…" and the plate keeps its
|
|
30
|
+
* place, as in the Hub while it loads;
|
|
31
|
+
* - "static": no Hub session for this person, or the core could not be
|
|
32
|
+
* read: the fixed addresses and the app's own company.
|
|
33
|
+
*/
|
|
34
|
+
export type CoreFrameSource = "core" | "checking" | "static";
|
|
35
|
+
/** What `osFrame` takes from the core: spread `frameOptions` into it. */
|
|
36
|
+
export type CoreFrameOptionsForOsFrame = {
|
|
37
|
+
links: Record<string, string>;
|
|
38
|
+
locked: Record<string, string>;
|
|
39
|
+
admin: boolean;
|
|
40
|
+
tenantSlug: string | null;
|
|
41
|
+
};
|
|
42
|
+
/** The design system's TopBar props for the company plate: spread `plate` into `<TopBar>`. */
|
|
43
|
+
export type CorePlateProps = Partial<OsPlateProps>;
|
|
44
|
+
export type CoreFrame = {
|
|
45
|
+
source: CoreFrameSource;
|
|
46
|
+
/** For `osFrame({ appKey, tools, ...frameOptions })`. The same object until something changes. */
|
|
47
|
+
frameOptions: CoreFrameOptionsForOsFrame;
|
|
48
|
+
/** For `<TopBar variant="workspace" {...plate} />`. */
|
|
49
|
+
plate: CorePlateProps;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* The frame's links, Admin row and company plate from the core's own view of
|
|
53
|
+
* the person signed in to the app, when they are signed in at the Hub;
|
|
54
|
+
* otherwise the app's fallback frame. See the top of this file.
|
|
55
|
+
*/
|
|
56
|
+
export declare function useCoreFrame({ appKey, email, fallback, companySwitch }: CoreFrameOptions): CoreFrame;
|
package/dist/coreView.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* autosolutions-os/core-view (0.3.4): the Hub's frame in an app that keeps its
|
|
3
|
+
* own Supabase project, for a person who is signed in at the Hub.
|
|
4
|
+
*
|
|
5
|
+
* Asset Portal and Ops Hub build their frame from `osFrame` and the design
|
|
6
|
+
* system's WorkspaceFrame, because their own client cannot read the core. On
|
|
7
|
+
* an autosolutions.app address the core's shared cookie holds the Hub's
|
|
8
|
+
* session, so `useCoreFrame` reads the core's own view of the person through
|
|
9
|
+
* it (coreReader.ts: os_context(), my_admin_scope() and the company switch,
|
|
10
|
+
* nothing else, never signing in, refreshing or writing anything) and hands
|
|
11
|
+
* back exactly what OsWorkspaceFrame gives an app on the core:
|
|
12
|
+
*
|
|
13
|
+
* - the menu links from the catalogue (`catalogueLinks`): a tool whose app
|
|
14
|
+
* the person holds opens it, with the acting company as ?tenant=; a tool
|
|
15
|
+
* whose app they do not hold is locked; a held app with no address yet
|
|
16
|
+
* opens its page in the Hub;
|
|
17
|
+
* - the Admin row, for someone who manages something;
|
|
18
|
+
* - the company plate from the acting company, and with more than one company
|
|
19
|
+
* its switcher (plate.tsx, the same code as OsWorkspaceFrame's), which
|
|
20
|
+
* switches as the Hub does: a reload here when the new company holds this
|
|
21
|
+
* app at this address, its deployment elsewhere, or the Hub when it does
|
|
22
|
+
* not hold the app.
|
|
23
|
+
*
|
|
24
|
+
* Without a Hub session for the app's person (signed out at the Hub, someone
|
|
25
|
+
* else at the Hub, a partner, cookies blocked, or off autosolutions.app) it
|
|
26
|
+
* hands back today's frame instead: the fixed addresses (`staticLinks`) with
|
|
27
|
+
* the app's own company, no Admin row, and a static plate. The app's own
|
|
28
|
+
* sign-in, data and rules are untouched either way.
|
|
29
|
+
*/
|
|
30
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
31
|
+
import { retryOsContext } from "./context.js";
|
|
32
|
+
import { followOsPerson } from "./contextCache.js";
|
|
33
|
+
import { createCoreViewClient, hubSession } from "./coreReader.js";
|
|
34
|
+
import { myAdminScope } from "./identity.js";
|
|
35
|
+
import { catalogueLinks, staticLinks } from "./nav.js";
|
|
36
|
+
import { useOsPlate } from "./plate.js";
|
|
37
|
+
export { CORE_VIEW_FUNCTIONS, createCoreViewClient, hubSession, } from "./coreReader.js";
|
|
38
|
+
/** How often a page that stays open looks at the shared cookie again, besides focus and coming back into view. */
|
|
39
|
+
const LOOK_EVERY = 60_000;
|
|
40
|
+
const sameHub = (a, b) => (a === b || (a !== null && b !== null && a.userId === b.userId && a.email === b.email && a.usable === b.usable));
|
|
41
|
+
/**
|
|
42
|
+
* The frame's links, Admin row and company plate from the core's own view of
|
|
43
|
+
* the person signed in to the app, when they are signed in at the Hub;
|
|
44
|
+
* otherwise the app's fallback frame. See the top of this file.
|
|
45
|
+
*/
|
|
46
|
+
export function useCoreFrame({ appKey, email, fallback, companySwitch = "os" }) {
|
|
47
|
+
/* One reader per mounted frame, asking with the app's person as they are now. */
|
|
48
|
+
const emailNow = useRef(email);
|
|
49
|
+
emailNow.current = email;
|
|
50
|
+
const [client] = useState(() => createCoreViewClient({ email: () => emailNow.current }));
|
|
51
|
+
/* The Hub's session for this person: read now, and again when the window
|
|
52
|
+
comes back, the page is shown again, once a minute, and when the app's
|
|
53
|
+
person changes. A refreshed token for the same person changes nothing. */
|
|
54
|
+
const [hub, setHub] = useState(() => hubSession(email));
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
const look = () => {
|
|
57
|
+
const next = hubSession(emailNow.current);
|
|
58
|
+
setHub((was) => (sameHub(was, next) ? was : next));
|
|
59
|
+
};
|
|
60
|
+
look();
|
|
61
|
+
if (typeof window === "undefined" || typeof document === "undefined")
|
|
62
|
+
return undefined;
|
|
63
|
+
const wake = () => {
|
|
64
|
+
if (document.visibilityState !== "hidden")
|
|
65
|
+
look();
|
|
66
|
+
};
|
|
67
|
+
document.addEventListener("visibilitychange", wake);
|
|
68
|
+
window.addEventListener("focus", wake);
|
|
69
|
+
window.addEventListener("pageshow", wake);
|
|
70
|
+
const timer = setInterval(look, LOOK_EVERY);
|
|
71
|
+
return () => {
|
|
72
|
+
document.removeEventListener("visibilitychange", wake);
|
|
73
|
+
window.removeEventListener("focus", wake);
|
|
74
|
+
window.removeEventListener("pageshow", wake);
|
|
75
|
+
clearInterval(timer);
|
|
76
|
+
};
|
|
77
|
+
}, [email]);
|
|
78
|
+
/* The answer is this person's: the cache reads again for another person,
|
|
79
|
+
and forgets the answer when there is no Hub session. Declared before the
|
|
80
|
+
plate's own read, so it runs first. */
|
|
81
|
+
const person = hub?.userId ?? null;
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
followOsPerson(person);
|
|
84
|
+
}, [person]);
|
|
85
|
+
/* A token that has become usable (the Hub refreshed it, or the person signed
|
|
86
|
+
in there): ask now rather than on the next timer. */
|
|
87
|
+
const usable = hub?.usable === true;
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
if (usable)
|
|
90
|
+
void retryOsContext(client);
|
|
91
|
+
}, [usable, client]);
|
|
92
|
+
const { context, status, current, plate } = useOsPlate(client, appKey, companySwitch);
|
|
93
|
+
/* The Admin row, as OsWorkspaceFrame decides it: the core's my_admin_scope().
|
|
94
|
+
Asked for each person whose token can be used; a failed answer keeps the
|
|
95
|
+
last one for that person, and nobody's carries over to someone else. */
|
|
96
|
+
const [admin, setAdmin] = useState(null);
|
|
97
|
+
useEffect(() => {
|
|
98
|
+
if (!usable || person === null)
|
|
99
|
+
return undefined;
|
|
100
|
+
let alive = true;
|
|
101
|
+
myAdminScope(client)
|
|
102
|
+
.then((scope) => {
|
|
103
|
+
if (alive)
|
|
104
|
+
setAdmin({ person, manages: scope.isPlatformAdmin || scope.managesTenants.length > 0 });
|
|
105
|
+
})
|
|
106
|
+
.catch(() => {
|
|
107
|
+
/* No Admin row is the safe failure; the admin console checks again anyway. */
|
|
108
|
+
});
|
|
109
|
+
return () => {
|
|
110
|
+
alive = false;
|
|
111
|
+
};
|
|
112
|
+
}, [usable, person, client]);
|
|
113
|
+
const manages = admin !== null && admin.person === person && admin.manages;
|
|
114
|
+
const source = hub !== null && status === "ready" && context !== null
|
|
115
|
+
? "core"
|
|
116
|
+
: usable && status === "loading" ? "checking" : "static";
|
|
117
|
+
const apps = context?.apps;
|
|
118
|
+
const slug = current?.slug ?? null;
|
|
119
|
+
const frameOptions = useMemo(() => {
|
|
120
|
+
if (source === "static") {
|
|
121
|
+
return { links: staticLinks(fallback.tenantSlug), locked: {}, admin: false, tenantSlug: fallback.tenantSlug };
|
|
122
|
+
}
|
|
123
|
+
/* "checking": apps is null, so nothing is linked and other apps' rows read as being checked. */
|
|
124
|
+
const { links, locked } = catalogueLinks(source === "core" ? apps ?? null : null, source === "core" ? slug : null);
|
|
125
|
+
return { links, locked, admin: source === "core" && manages, tenantSlug: source === "core" ? slug : null };
|
|
126
|
+
}, [source, apps, slug, manages, fallback.tenantSlug]);
|
|
127
|
+
const staticPlate = useMemo(() => ({
|
|
128
|
+
...(fallback.companySrc ? { companySrc: fallback.companySrc } : {}),
|
|
129
|
+
...(fallback.companyName ? { companyName: fallback.companyName } : {}),
|
|
130
|
+
}), [fallback.companySrc, fallback.companyName]);
|
|
131
|
+
return { source, frameOptions, plate: source === "static" ? staticPlate : plate };
|
|
132
|
+
}
|
package/dist/frame.d.ts
CHANGED
|
@@ -27,22 +27,9 @@
|
|
|
27
27
|
*/
|
|
28
28
|
import { type ReactNode } from "react";
|
|
29
29
|
import type { WorkspaceState } from "autosolutions-design-system";
|
|
30
|
-
import { type OsTenant } from "./context.js";
|
|
31
30
|
import { type OsAppTool, type OsHubSection } from "./nav.js";
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
* - "os" (default): switch_company_for_app, then reload or land on that
|
|
35
|
-
* company's deployment of this app, like OsCompanySwitcher.
|
|
36
|
-
* - "none": the plate shows the acting company with no chevron, for an app
|
|
37
|
-
* whose company is fixed some other way (an assignment, say).
|
|
38
|
-
* - a function: the app's own in-place switch. The frame calls it with the
|
|
39
|
-
* chosen company, closes the menu and does not reload; when it resolves,
|
|
40
|
-
* the plate and the menu links follow the new company at once, and the
|
|
41
|
-
* frame reads the core again (asking again if that read fails). The function
|
|
42
|
-
* reports its own failures (a toast); a rejection only leaves the plate as
|
|
43
|
-
* it was.
|
|
44
|
-
*/
|
|
45
|
-
export type OsCompanySwitch = "os" | "none" | ((tenant: OsTenant) => Promise<void>);
|
|
31
|
+
import { type OsCompanySwitch } from "./plate.js";
|
|
32
|
+
export type { OsCompanySwitch } from "./plate.js";
|
|
46
33
|
export type OsWorkspaceFrameProps = {
|
|
47
34
|
/** The client's `os` (public schema) connection. */
|
|
48
35
|
os: unknown;
|
package/dist/frame.js
CHANGED
|
@@ -26,23 +26,13 @@ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-run
|
|
|
26
26
|
* the OS menu search (`autosolutions-os/search`), which jumps to any app or
|
|
27
27
|
* page the menu reaches.
|
|
28
28
|
*/
|
|
29
|
-
import { useCallback, useEffect, useId, useMemo,
|
|
30
|
-
import {
|
|
31
|
-
import {
|
|
32
|
-
import { markFor, refreshOsContext, retryOsContext, watchOsAuth } from "./context.js";
|
|
29
|
+
import { useCallback, useEffect, useId, useMemo, useState } from "react";
|
|
30
|
+
import { TopBar, WorkspaceFrame } from "autosolutions-design-system";
|
|
31
|
+
import { retryOsContext, watchOsAuth } from "./context.js";
|
|
33
32
|
import { myAdminScope } from "./identity.js";
|
|
34
33
|
import { catalogueLinks, osFrame } from "./nav.js";
|
|
34
|
+
import { useOsPlate } from "./plate.js";
|
|
35
35
|
import { useMenuSearch } from "./search.js";
|
|
36
|
-
/* A company's mark in the company menu. When its artwork does not load, the
|
|
37
|
-
slot stays, empty (the row already says the company's name), so the names
|
|
38
|
-
stay lined up; never a broken image. */
|
|
39
|
-
function CompanyMenuMark({ src }) {
|
|
40
|
-
const [failed, setFailed] = useState(null);
|
|
41
|
-
if (failed === src)
|
|
42
|
-
return _jsx("span", { className: "as-ws__company-mark", "aria-hidden": "true" });
|
|
43
|
-
return _jsx("img", { className: "as-ws__company-mark", src: src, alt: "", onError: () => { if (failed !== src)
|
|
44
|
-
setFailed(src); } });
|
|
45
|
-
}
|
|
46
36
|
/* This app's own rows and tabs as links to their paths. Rows for other apps
|
|
47
37
|
already carry their address; a locked row is never a link. */
|
|
48
38
|
function linked(sections, hrefFor) {
|
|
@@ -70,21 +60,8 @@ const here = () => (typeof window === "undefined" ? "" : window.location.pathnam
|
|
|
70
60
|
export function OsWorkspaceFrame({ os, auth, appKey, tools, hubItems, location, onNavigate, hrefFor, search, actions, companySwitch = "os", panelFooter, onSignOut, homePath, admin, favicon = true, initialOpen, className, children, ...rest }) {
|
|
71
61
|
/* Only data-* and aria-* reach the frame's root. */
|
|
72
62
|
const attributes = Object.fromEntries(Object.entries(rest).filter(([key]) => /^(data|aria)-/.test(key)));
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
const [ownBusy, setOwnBusy] = useState(false);
|
|
76
|
-
const busy = switching || ownBusy;
|
|
77
|
-
/* An in-place switch (companySwitch as a function) makes the chosen company
|
|
78
|
-
the acting one at once: the plate, the menu's check and the links follow
|
|
79
|
-
it until the core is read again, whether or not that read succeeds, so a
|
|
80
|
-
read that fails never leaves the plate on the company the session has
|
|
81
|
-
left (and a pick of that company is a real switch back). */
|
|
82
|
-
const [acting, setActing] = useState(null);
|
|
83
|
-
const current = acting && acting.over === context ? acting.tenant : read;
|
|
84
|
-
const contextNow = useRef(context);
|
|
85
|
-
contextNow.current = context;
|
|
86
|
-
const live = useRef(true);
|
|
87
|
-
useEffect(() => () => { live.current = false; }, []);
|
|
63
|
+
/* The company plate and its menu (plate.tsx, shared with useCoreFrame). */
|
|
64
|
+
const { context, status, current, plate } = useOsPlate(os, appKey, companySwitch);
|
|
88
65
|
/* The core decides who manages something; the footer only reflects it. */
|
|
89
66
|
const [manages, setManages] = useState(false);
|
|
90
67
|
useEffect(() => {
|
|
@@ -164,59 +141,9 @@ export function OsWorkspaceFrame({ os, auth, appKey, tools, hubItems, location,
|
|
|
164
141
|
const signOut = onSignOut ?? (() => {
|
|
165
142
|
void auth?.signOut().then(() => window.location.reload());
|
|
166
143
|
});
|
|
167
|
-
/* After an in-place switch: read the core again, and again if that fails,
|
|
168
|
-
a few times, while the frame is mounted. */
|
|
169
|
-
const readAgain = (tries) => {
|
|
170
|
-
void refreshOsContext(os).then((value) => {
|
|
171
|
-
if (value || tries <= 1 || !live.current)
|
|
172
|
-
return;
|
|
173
|
-
setTimeout(() => { if (live.current)
|
|
174
|
-
readAgain(tries - 1); }, 1500);
|
|
175
|
-
});
|
|
176
|
-
};
|
|
177
|
-
/* The company menu, offered only when there is somewhere to switch to: a
|
|
178
|
-
menu of radio items, the acting company checked. */
|
|
179
|
-
const pick = (tenant) => {
|
|
180
|
-
if (typeof companySwitch !== "function") {
|
|
181
|
-
void choose(tenant.id, () => setMenuOpen(false));
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
setMenuOpen(false);
|
|
185
|
-
if (!current || busy || tenant.id === current.id)
|
|
186
|
-
return;
|
|
187
|
-
setOwnBusy(true);
|
|
188
|
-
companySwitch(tenant)
|
|
189
|
-
.then(() => {
|
|
190
|
-
if (!live.current)
|
|
191
|
-
return;
|
|
192
|
-
setActing({ tenant, over: contextNow.current });
|
|
193
|
-
readAgain(3);
|
|
194
|
-
})
|
|
195
|
-
.catch(() => {
|
|
196
|
-
/* The app's switch says what went wrong; the plate stays as it was. */
|
|
197
|
-
})
|
|
198
|
-
.finally(() => { if (live.current)
|
|
199
|
-
setOwnBusy(false); });
|
|
200
|
-
};
|
|
201
|
-
const switchable = companySwitch !== "none" && Boolean(current) && tenants.length > 1;
|
|
202
|
-
const companyMenu = switchable && current ? (_jsxs(_Fragment, { children: [_jsx(ActionList, { role: "menu", "aria-label": "Switch company", sections: [{
|
|
203
|
-
title: "Your companies",
|
|
204
|
-
items: tenants.map((tenant) => ({
|
|
205
|
-
content: tenant.name,
|
|
206
|
-
active: tenant.id === current.id,
|
|
207
|
-
checked: tenant.id === current.id,
|
|
208
|
-
disabled: busy,
|
|
209
|
-
prefix: _jsx(CompanyMenuMark, { src: markFor(tenant, "plain") }),
|
|
210
|
-
onAction: () => pick(tenant),
|
|
211
|
-
})),
|
|
212
|
-
}] }), busy && _jsx("div", { className: "as-ws__company-note", role: "status", children: "Switching\u2026" }), error && _jsx("div", { className: "as-ws__company-note as-ws__company-note--error", role: "alert", children: error })] })) : undefined;
|
|
213
144
|
const topBar = (_jsx(TopBar, { variant: "workspace", "data-os-bar": barId, ...(own
|
|
214
145
|
? { searchPlaceholder: search?.placeholder ?? "Search", searchValue: search?.value, onSearchChange: search?.onChange }
|
|
215
|
-
: menuSearch.topBar), actions: actions,
|
|
216
|
-
/* Also what the plate shows when the company has no artwork, or it fails to load. */
|
|
217
|
-
companyName: current?.name,
|
|
218
|
-
/* While the company is read, its place on the bar is kept. */
|
|
219
|
-
companyPending: !current && status === "loading" ? (companySwitch === "none" ? "plate" : true) : undefined, onSwitchCompany: switchable ? () => setMenuOpen((open) => !open) : undefined, companyMenu: companyMenu, companyMenuOpen: menuOpen, onCompanyMenuClose: () => setMenuOpen(false) }));
|
|
146
|
+
: menuSearch.topBar), actions: actions, ...plate }));
|
|
220
147
|
/* The menu search's matches hang under the bar's field, drawn after the
|
|
221
148
|
frame; nothing while they are closed. */
|
|
222
149
|
return (_jsxs(_Fragment, { children: [_jsx(WorkspaceFrame, { className: className, topBar: topBar, functions: frame.functions, sections: sections, footerItems: frame.footerItems, homeFunction: frame.homeFunction, location: { ...frame.locate(location), href: location }, onNavigate: navigate, onSignOut: signOut, favicon: favicon, panelFooter: panelFooter, ...(initialOpen === undefined ? {} : { initialOpen }), ...attributes, children: children }), menuSearch.results] }));
|
package/dist/nav.d.ts
CHANGED
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
* 1 the rail Hub, Finance, Marketing, Commercial, Operations
|
|
9
9
|
* 2 the menu that function's tools, grouped Reporting / Planning
|
|
10
10
|
* (Performance / Planning / Creative in Marketing),
|
|
11
|
-
* an Overview first in
|
|
12
|
-
*
|
|
13
|
-
* Assistants
|
|
11
|
+
* an Overview first in every function (Marketing's
|
|
12
|
+
* since 0.3.3, the owner's call); the Hub's is
|
|
13
|
+
* Smart Comms Hub and Assistants
|
|
14
14
|
* 3 the tool's panel an app's own tool, open: its icon and name, then its
|
|
15
15
|
* views as tabs (design system 6.5.0; no Back to hub)
|
|
16
16
|
*
|
|
@@ -66,7 +66,8 @@ export declare const OS_FUNCTIONS: OsNavItem[];
|
|
|
66
66
|
/**
|
|
67
67
|
* Cliff's menu map (the design's ui_kits/functions dashboards/nav.js and
|
|
68
68
|
* data.js, 16 October 2026): its ids, labels, groups, order, status and
|
|
69
|
-
* views, an Overview at the top of
|
|
69
|
+
* views, an Overview at the top of every function (Cliff draws one for
|
|
70
|
+
* Finance, Commercial and Operations; Marketing's is the owner's, 0.3.3), and
|
|
70
71
|
* the two rows the design keeps from before its merge (Customer scorecard,
|
|
71
72
|
* Clearance list). Then our four Live apps the map does not have, at the end
|
|
72
73
|
* of their groups, so they stay reachable: Shelf simulator (Marketing >
|
|
@@ -125,9 +126,16 @@ export type OsToolLink = {
|
|
|
125
126
|
* Studio, Sales Reporting, Asset Portal and Candidate Hub had none), so a
|
|
126
127
|
* frame off the core sends each row where the Hub's catalogue does once the
|
|
127
128
|
* catalogue has that address (`launch_url`), not to the tool's page in the
|
|
128
|
-
* Hub; and PM Copilot opens its console (`/ops`), from the catalogue too.
|
|
129
|
-
*
|
|
130
|
-
*
|
|
129
|
+
* Hub; and PM Copilot opens its console (`/ops`), from the catalogue too.
|
|
130
|
+
* The five apps whose address the catalogue does not give yet (Paid media,
|
|
131
|
+
* Marketing calendar, Influencer Manager, NPD validator, Shelf simulator)
|
|
132
|
+
* have none: off the core they open their page in the Hub.
|
|
133
|
+
*
|
|
134
|
+
* Since 0.3.3 (the owner's calls of 18 October): Operations' Overview is Ops
|
|
135
|
+
* Hub's home dashboard, so it opens Ops Hub for someone who holds it and is
|
|
136
|
+
* locked for someone who does not, like Ops Hub's tools; and Asset Manager's
|
|
137
|
+
* fixed address is asset-portal.autosolutions.app, which shares the Hub's
|
|
138
|
+
* sign-in (self-asset-manager.lovable.app cannot read it).
|
|
131
139
|
*/
|
|
132
140
|
export declare const OS_TOOL_LINKS: Record<string, OsToolLink>;
|
|
133
141
|
/** The Hub's own pages, for the rail's Hub, Admin and Settings rows in other apps. */
|
|
@@ -153,7 +161,9 @@ export declare const HINTS: {
|
|
|
153
161
|
* The mark after the name of a row whose page is a sample (0.3.2, the
|
|
154
162
|
* owner's "(*)" for work in progress): a row that opens its page in the Hub
|
|
155
163
|
* (`osHubPath`, the design's sample dashboards, mock data) because its tool
|
|
156
|
-
* has no app to open,
|
|
164
|
+
* has no app to open, Finance's, Marketing's and Commercial's Overviews
|
|
165
|
+
* among them (Operations' opens Ops Hub since 0.3.3, and is a sample only
|
|
166
|
+
* where Ops Hub is held with no address yet). osFrame decides it
|
|
157
167
|
* where it decides the link, so a tool loses the mark the day it links to
|
|
158
168
|
* an app. A row that opens an app, this app's own tools, a locked row and
|
|
159
169
|
* the Hub panel (the Hub's real pages) have none. The Hub's sample pages put
|
|
@@ -377,7 +387,7 @@ export type OsMenuEntry = {
|
|
|
377
387
|
/** Unique among the entries: the function, the row, and the tab when it is one. */
|
|
378
388
|
key: string;
|
|
379
389
|
label: string;
|
|
380
|
-
/** Where it sits, for telling the
|
|
390
|
+
/** Where it sits, for telling the five Overviews apart: "Finance", "Hub · Smart Comms Hub", "Key accounts". */
|
|
381
391
|
context: string;
|
|
382
392
|
icon?: string;
|
|
383
393
|
state: OsFrameState;
|
package/dist/nav.js
CHANGED
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
* 1 the rail Hub, Finance, Marketing, Commercial, Operations
|
|
9
9
|
* 2 the menu that function's tools, grouped Reporting / Planning
|
|
10
10
|
* (Performance / Planning / Creative in Marketing),
|
|
11
|
-
* an Overview first in
|
|
12
|
-
*
|
|
13
|
-
* Assistants
|
|
11
|
+
* an Overview first in every function (Marketing's
|
|
12
|
+
* since 0.3.3, the owner's call); the Hub's is
|
|
13
|
+
* Smart Comms Hub and Assistants
|
|
14
14
|
* 3 the tool's panel an app's own tool, open: its icon and name, then its
|
|
15
15
|
* views as tabs (design system 6.5.0; no Back to hub)
|
|
16
16
|
*
|
|
@@ -42,7 +42,8 @@ const overview = (fn) => ({
|
|
|
42
42
|
/**
|
|
43
43
|
* Cliff's menu map (the design's ui_kits/functions dashboards/nav.js and
|
|
44
44
|
* data.js, 16 October 2026): its ids, labels, groups, order, status and
|
|
45
|
-
* views, an Overview at the top of
|
|
45
|
+
* views, an Overview at the top of every function (Cliff draws one for
|
|
46
|
+
* Finance, Commercial and Operations; Marketing's is the owner's, 0.3.3), and
|
|
46
47
|
* the two rows the design keeps from before its merge (Customer scorecard,
|
|
47
48
|
* Clearance list). Then our four Live apps the map does not have, at the end
|
|
48
49
|
* of their groups, so they stay reachable: Shelf simulator (Marketing >
|
|
@@ -76,6 +77,7 @@ export const OS_MENU = {
|
|
|
76
77
|
] },
|
|
77
78
|
],
|
|
78
79
|
marketing: [
|
|
80
|
+
overview("marketing"),
|
|
79
81
|
{ title: "Performance", items: [
|
|
80
82
|
tool("live-ecomm-sales", "Live eComm sales by store", "mkt-live-ecomm-sales-by-store", "Live"),
|
|
81
83
|
tool("paid-media", "Paid media", "mkt-paid-media", "No status"),
|
|
@@ -195,9 +197,16 @@ const opsHub = (path) => ({ app: "ops_hub", url: OPS_HUB, path });
|
|
|
195
197
|
* Studio, Sales Reporting, Asset Portal and Candidate Hub had none), so a
|
|
196
198
|
* frame off the core sends each row where the Hub's catalogue does once the
|
|
197
199
|
* catalogue has that address (`launch_url`), not to the tool's page in the
|
|
198
|
-
* Hub; and PM Copilot opens its console (`/ops`), from the catalogue too.
|
|
199
|
-
*
|
|
200
|
-
*
|
|
200
|
+
* Hub; and PM Copilot opens its console (`/ops`), from the catalogue too.
|
|
201
|
+
* The five apps whose address the catalogue does not give yet (Paid media,
|
|
202
|
+
* Marketing calendar, Influencer Manager, NPD validator, Shelf simulator)
|
|
203
|
+
* have none: off the core they open their page in the Hub.
|
|
204
|
+
*
|
|
205
|
+
* Since 0.3.3 (the owner's calls of 18 October): Operations' Overview is Ops
|
|
206
|
+
* Hub's home dashboard, so it opens Ops Hub for someone who holds it and is
|
|
207
|
+
* locked for someone who does not, like Ops Hub's tools; and Asset Manager's
|
|
208
|
+
* fixed address is asset-portal.autosolutions.app, which shares the Hub's
|
|
209
|
+
* sign-in (self-asset-manager.lovable.app cannot read it).
|
|
201
210
|
*/
|
|
202
211
|
export const OS_TOOL_LINKS = {
|
|
203
212
|
"live-ecomm-sales": { app: "alive_reporting", url: "https://e-sales.autosolutions.app" },
|
|
@@ -208,12 +217,14 @@ export const OS_TOOL_LINKS = {
|
|
|
208
217
|
"pricing-researcher": {},
|
|
209
218
|
"shelf-simulator": { app: "shelf_simulator" },
|
|
210
219
|
"image-generator": { app: "frank_create", url: "https://design-studio.autosolutions.app" },
|
|
211
|
-
"retail-image-library": { app: "asset_portal", url: "https://
|
|
220
|
+
"retail-image-library": { app: "asset_portal", url: "https://asset-portal.autosolutions.app" },
|
|
212
221
|
"brand-voice-copywriter": {},
|
|
213
222
|
"sales": opsHub("/sales-dashboard"),
|
|
214
223
|
"open-orders": opsHub("/customer-orders"),
|
|
215
224
|
"crm": { app: "enxgy_crm", url: "https://e-crm.autosolutions.app" },
|
|
216
225
|
"key-accounts": { app: "key_accounts", url: "https://key-accounts.lovable.app" },
|
|
226
|
+
/* Operations' Overview is Ops Hub's home dashboard (owner, 0.3.3). */
|
|
227
|
+
"operations-home": opsHub("/"),
|
|
217
228
|
"inventory-health": opsHub("/inventory-health"),
|
|
218
229
|
"out-of-stocks": opsHub("/inventory-health?view=oos"),
|
|
219
230
|
"open-purchase-orders": opsHub("/po-pipeline"),
|
|
@@ -248,7 +259,9 @@ export const HINTS = {
|
|
|
248
259
|
* The mark after the name of a row whose page is a sample (0.3.2, the
|
|
249
260
|
* owner's "(*)" for work in progress): a row that opens its page in the Hub
|
|
250
261
|
* (`osHubPath`, the design's sample dashboards, mock data) because its tool
|
|
251
|
-
* has no app to open,
|
|
262
|
+
* has no app to open, Finance's, Marketing's and Commercial's Overviews
|
|
263
|
+
* among them (Operations' opens Ops Hub since 0.3.3, and is a sample only
|
|
264
|
+
* where Ops Hub is held with no address yet). osFrame decides it
|
|
252
265
|
* where it decides the link, so a tool loses the mark the day it links to
|
|
253
266
|
* an app. A row that opens an app, this app's own tools, a locked row and
|
|
254
267
|
* the Hub panel (the Hub's real pages) have none. The Hub's sample pages put
|
|
@@ -443,6 +456,8 @@ export function osFrame(options) {
|
|
|
443
456
|
? { ...base, path, ...(match ? { match } : {}) }
|
|
444
457
|
: { ...base, href: withTenantHint(`${hubUrl}${path}`, tenantSlug) });
|
|
445
458
|
const sections = {};
|
|
459
|
+
/* In the Hub, the pages of locked rows (0.3.3): see the claims below. */
|
|
460
|
+
const lockedPages = [];
|
|
446
461
|
const hubMenu = hubItems ?? OS_HUB_MENU;
|
|
447
462
|
if (hubMenu.length) {
|
|
448
463
|
sections["hub"] = hubItems
|
|
@@ -469,8 +484,11 @@ export function osFrame(options) {
|
|
|
469
484
|
if (href)
|
|
470
485
|
return { ...base, ...(isHub ? { match: [page] } : {}), href };
|
|
471
486
|
const reason = locked[entry.id];
|
|
472
|
-
if (reason)
|
|
487
|
+
if (reason) {
|
|
488
|
+
if (isHub)
|
|
489
|
+
lockedPages.push({ fn, page });
|
|
473
490
|
return { ...base, disabled: true, title: reason };
|
|
491
|
+
}
|
|
474
492
|
/* No app of its own (or one this frame cannot reach: off the core
|
|
475
493
|
with no fixed address, or held and not deployed yet): its page in
|
|
476
494
|
the Hub, a sample, marked so (0.3.2). */
|
|
@@ -513,6 +531,13 @@ export function osFrame(options) {
|
|
|
513
531
|
}
|
|
514
532
|
}
|
|
515
533
|
}
|
|
534
|
+
/* A locked row's page in the Hub files under its function, with no row
|
|
535
|
+
picked (0.3.3): someone who does not hold Ops Hub and opens
|
|
536
|
+
/os/operations (Operations' Overview, Ops Hub's since 0.3.3) sees the
|
|
537
|
+
Operations menu with that row locked, not the Hub's landing. After every
|
|
538
|
+
row's own claim, so a row that claims the same path wins. */
|
|
539
|
+
for (const { fn, page } of lockedPages)
|
|
540
|
+
claims.push({ routes: [page], spot: { fn, item: null, tab: null } });
|
|
516
541
|
const landingFn = tools.map((mine) => fnOf(sections, osToolId(mine.id))).find((fn) => fn !== undefined);
|
|
517
542
|
const locate = (location) => {
|
|
518
543
|
let url;
|
package/dist/plate.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The top bar's company plate and its menu, as every frame on the core draws
|
|
3
|
+
* them: OsWorkspaceFrame, and since 0.3.4 `useCoreFrame`
|
|
4
|
+
* (`autosolutions-os/core-view`), for an app on its own project that reads the
|
|
5
|
+
* core through the Hub's session. One copy, so the two cannot drift: moved
|
|
6
|
+
* here from frame.tsx in 0.3.4, unchanged.
|
|
7
|
+
*
|
|
8
|
+
* The plate shows the acting company's mark (its name, when it has no artwork
|
|
9
|
+
* or the artwork fails to load). With somewhere to switch to, the plate and
|
|
10
|
+
* its chevron are one button that opens a menu of the person's companies, the
|
|
11
|
+
* acting one checked.
|
|
12
|
+
*/
|
|
13
|
+
import { type ReactNode } from "react";
|
|
14
|
+
import { type OsContext, type OsContextStatus, type OsTenant } from "./context.js";
|
|
15
|
+
/**
|
|
16
|
+
* How the plate's chevron switches company.
|
|
17
|
+
* - "os" (default): switch_company_for_app, then reload or land on that
|
|
18
|
+
* company's deployment of this app, like OsCompanySwitcher.
|
|
19
|
+
* - "none": the plate shows the acting company with no chevron, for an app
|
|
20
|
+
* whose company is fixed some other way (an assignment, say).
|
|
21
|
+
* - a function: the app's own in-place switch. The frame calls it with the
|
|
22
|
+
* chosen company, closes the menu and does not reload; when it resolves,
|
|
23
|
+
* the plate and the menu links follow the new company at once, and the
|
|
24
|
+
* frame reads the core again (asking again if that read fails). The function
|
|
25
|
+
* reports its own failures (a toast); a rejection only leaves the plate as
|
|
26
|
+
* it was.
|
|
27
|
+
*/
|
|
28
|
+
export type OsCompanySwitch = "os" | "none" | ((tenant: OsTenant) => Promise<void>);
|
|
29
|
+
/** The design system's TopBar props for the company plate: spread them into `<TopBar>`. */
|
|
30
|
+
export type OsPlateProps = {
|
|
31
|
+
company: string | null;
|
|
32
|
+
companySrc: string | undefined;
|
|
33
|
+
companyName: string | undefined;
|
|
34
|
+
companyPending: true | "plate" | undefined;
|
|
35
|
+
onSwitchCompany: (() => void) | undefined;
|
|
36
|
+
companyMenu: ReactNode;
|
|
37
|
+
companyMenuOpen: boolean;
|
|
38
|
+
onCompanyMenuClose: () => void;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* The acting company (the core's, or the one an in-place switch just made
|
|
42
|
+
* acting), the context it came from, and the plate's TopBar props.
|
|
43
|
+
*/
|
|
44
|
+
export declare function useOsPlate(os: unknown, appKey: string, companySwitch: OsCompanySwitch): {
|
|
45
|
+
context: OsContext | null;
|
|
46
|
+
status: OsContextStatus;
|
|
47
|
+
current: OsTenant | null;
|
|
48
|
+
plate: OsPlateProps;
|
|
49
|
+
};
|
package/dist/plate.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* The top bar's company plate and its menu, as every frame on the core draws
|
|
4
|
+
* them: OsWorkspaceFrame, and since 0.3.4 `useCoreFrame`
|
|
5
|
+
* (`autosolutions-os/core-view`), for an app on its own project that reads the
|
|
6
|
+
* core through the Hub's session. One copy, so the two cannot drift: moved
|
|
7
|
+
* here from frame.tsx in 0.3.4, unchanged.
|
|
8
|
+
*
|
|
9
|
+
* The plate shows the acting company's mark (its name, when it has no artwork
|
|
10
|
+
* or the artwork fails to load). With somewhere to switch to, the plate and
|
|
11
|
+
* its chevron are one button that opens a menu of the person's companies, the
|
|
12
|
+
* acting one checked.
|
|
13
|
+
*/
|
|
14
|
+
import { useEffect, useRef, useState } from "react";
|
|
15
|
+
import { ActionList } from "autosolutions-design-system";
|
|
16
|
+
import { useCompanySwitch } from "./chrome.js";
|
|
17
|
+
import { markFor, refreshOsContext } from "./context.js";
|
|
18
|
+
/* A company's mark in the company menu. When its artwork does not load, the
|
|
19
|
+
slot stays, empty (the row already says the company's name), so the names
|
|
20
|
+
stay lined up; never a broken image. */
|
|
21
|
+
function CompanyMenuMark({ src }) {
|
|
22
|
+
const [failed, setFailed] = useState(null);
|
|
23
|
+
if (failed === src)
|
|
24
|
+
return _jsx("span", { className: "as-ws__company-mark", "aria-hidden": "true" });
|
|
25
|
+
return _jsx("img", { className: "as-ws__company-mark", src: src, alt: "", onError: () => { if (failed !== src)
|
|
26
|
+
setFailed(src); } });
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The acting company (the core's, or the one an in-place switch just made
|
|
30
|
+
* acting), the context it came from, and the plate's TopBar props.
|
|
31
|
+
*/
|
|
32
|
+
export function useOsPlate(os, appKey, companySwitch) {
|
|
33
|
+
const { context, status, current: read, tenants, busy: switching, error, choose } = useCompanySwitch(os, appKey);
|
|
34
|
+
const [menuOpen, setMenuOpen] = useState(false);
|
|
35
|
+
const [ownBusy, setOwnBusy] = useState(false);
|
|
36
|
+
const busy = switching || ownBusy;
|
|
37
|
+
/* An in-place switch (companySwitch as a function) makes the chosen company
|
|
38
|
+
the acting one at once: the plate, the menu's check and the links follow
|
|
39
|
+
it until the core is read again, whether or not that read succeeds, so a
|
|
40
|
+
read that fails never leaves the plate on the company the session has
|
|
41
|
+
left (and a pick of that company is a real switch back). */
|
|
42
|
+
const [acting, setActing] = useState(null);
|
|
43
|
+
const current = acting && acting.over === context ? acting.tenant : read;
|
|
44
|
+
const contextNow = useRef(context);
|
|
45
|
+
contextNow.current = context;
|
|
46
|
+
const live = useRef(true);
|
|
47
|
+
useEffect(() => () => { live.current = false; }, []);
|
|
48
|
+
/* After an in-place switch: read the core again, and again if that fails,
|
|
49
|
+
a few times, while the frame is mounted. */
|
|
50
|
+
const readAgain = (tries) => {
|
|
51
|
+
void refreshOsContext(os).then((value) => {
|
|
52
|
+
if (value || tries <= 1 || !live.current)
|
|
53
|
+
return;
|
|
54
|
+
setTimeout(() => { if (live.current)
|
|
55
|
+
readAgain(tries - 1); }, 1500);
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
/* The company menu, offered only when there is somewhere to switch to: a
|
|
59
|
+
menu of radio items, the acting company checked. */
|
|
60
|
+
const pick = (tenant) => {
|
|
61
|
+
if (typeof companySwitch !== "function") {
|
|
62
|
+
void choose(tenant.id, () => setMenuOpen(false));
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
setMenuOpen(false);
|
|
66
|
+
if (!current || busy || tenant.id === current.id)
|
|
67
|
+
return;
|
|
68
|
+
setOwnBusy(true);
|
|
69
|
+
companySwitch(tenant)
|
|
70
|
+
.then(() => {
|
|
71
|
+
if (!live.current)
|
|
72
|
+
return;
|
|
73
|
+
setActing({ tenant, over: contextNow.current });
|
|
74
|
+
readAgain(3);
|
|
75
|
+
})
|
|
76
|
+
.catch(() => {
|
|
77
|
+
/* The app's switch says what went wrong; the plate stays as it was. */
|
|
78
|
+
})
|
|
79
|
+
.finally(() => { if (live.current)
|
|
80
|
+
setOwnBusy(false); });
|
|
81
|
+
};
|
|
82
|
+
const switchable = companySwitch !== "none" && Boolean(current) && tenants.length > 1;
|
|
83
|
+
const companyMenu = switchable && current ? (_jsxs(_Fragment, { children: [_jsx(ActionList, { role: "menu", "aria-label": "Switch company", sections: [{
|
|
84
|
+
title: "Your companies",
|
|
85
|
+
items: tenants.map((tenant) => ({
|
|
86
|
+
content: tenant.name,
|
|
87
|
+
active: tenant.id === current.id,
|
|
88
|
+
checked: tenant.id === current.id,
|
|
89
|
+
disabled: busy,
|
|
90
|
+
prefix: _jsx(CompanyMenuMark, { src: markFor(tenant, "plain") }),
|
|
91
|
+
onAction: () => pick(tenant),
|
|
92
|
+
})),
|
|
93
|
+
}] }), busy && _jsx("div", { className: "as-ws__company-note", role: "status", children: "Switching\u2026" }), error && _jsx("div", { className: "as-ws__company-note as-ws__company-note--error", role: "alert", children: error })] })) : undefined;
|
|
94
|
+
const plate = {
|
|
95
|
+
company: current?.slug ?? null,
|
|
96
|
+
companySrc: current ? markFor(current, "plain") : undefined,
|
|
97
|
+
/* Also what the plate shows when the company has no artwork, or it fails to load. */
|
|
98
|
+
companyName: current?.name,
|
|
99
|
+
/* While the company is read, its place on the bar is kept. */
|
|
100
|
+
companyPending: !current && status === "loading" ? (companySwitch === "none" ? "plate" : true) : undefined,
|
|
101
|
+
onSwitchCompany: switchable ? () => setMenuOpen((open) => !open) : undefined,
|
|
102
|
+
companyMenu,
|
|
103
|
+
companyMenuOpen: menuOpen,
|
|
104
|
+
onCompanyMenuClose: () => setMenuOpen(false),
|
|
105
|
+
};
|
|
106
|
+
return { context, status, current, plate };
|
|
107
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "autosolutions-os",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"description": "One sign-in, one company switcher, one app grid. What every AutoSolutions app imports instead of copying.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "UNLICENSED",
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"./frame": { "types": "./dist/frame.d.ts", "import": "./dist/frame.js" },
|
|
14
14
|
"./federation": { "types": "./dist/federation.d.ts", "import": "./dist/federation.js" },
|
|
15
15
|
"./search": { "types": "./dist/search.d.ts", "import": "./dist/search.js" },
|
|
16
|
+
"./core-view": { "types": "./dist/coreView.d.ts", "import": "./dist/coreView.js" },
|
|
16
17
|
"./chrome.css": "./dist/chrome.css"
|
|
17
18
|
},
|
|
18
19
|
"files": ["dist", "CHANGELOG.md"],
|