@zjazj/web 0.0.5 → 0.0.7
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/index.d.ts +39 -6
- package/dist/index.js +334 -30
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -68,6 +68,14 @@ interface CurrentUser {
|
|
|
68
68
|
roles: string[];
|
|
69
69
|
permissions: string[];
|
|
70
70
|
timeZone?: string;
|
|
71
|
+
/**
|
|
72
|
+
* Set while a platform operator works in this tenant as platform support. `until` is when
|
|
73
|
+
* the session ends, in seconds since 1970. Mirrors the server's `SupportSession`.
|
|
74
|
+
*/
|
|
75
|
+
support?: {
|
|
76
|
+
until: number;
|
|
77
|
+
mode: string;
|
|
78
|
+
};
|
|
71
79
|
isDev?: boolean;
|
|
72
80
|
}
|
|
73
81
|
/** Where the session stands. The shell picks its screen from `state`. */
|
|
@@ -87,11 +95,26 @@ interface SessionStatus {
|
|
|
87
95
|
interface Session {
|
|
88
96
|
/** `dev` for the local dev identity, `platform` for sign-in at the identity provider. */
|
|
89
97
|
readonly kind: string;
|
|
98
|
+
/**
|
|
99
|
+
* Platform support: make a customer's organization the current tenant of this browser,
|
|
100
|
+
* after the control plane let the operator in. The operator's own tenant is remembered
|
|
101
|
+
* as the way back. The page keeps its tenant for its whole life, so the caller opens the
|
|
102
|
+
* app again afterwards. A session without this method cannot enter a tenant.
|
|
103
|
+
*/
|
|
104
|
+
enterTenant?(organizationId: string): Promise<void>;
|
|
105
|
+
/** Platform support: make the operator's own tenant the current one again. */
|
|
106
|
+
leaveTenant?(): Promise<void>;
|
|
90
107
|
getUser(): CurrentUser | null;
|
|
91
108
|
/** The same object until the state changes, so it can back `useSyncExternalStore`. */
|
|
92
109
|
getStatus(): SessionStatus;
|
|
93
|
-
/**
|
|
94
|
-
|
|
110
|
+
/**
|
|
111
|
+
* The bearer token for API calls, refreshed when needed: a minute before it expires, and
|
|
112
|
+
* when a support session reaches its end. `renew` asks for a new one now, which the
|
|
113
|
+
* client does once when a backend answers `token_expired` or `support_ended`.
|
|
114
|
+
*/
|
|
115
|
+
getAccessToken(options?: {
|
|
116
|
+
renew?: boolean;
|
|
117
|
+
}): Promise<string | null>;
|
|
95
118
|
/** Send the browser to the sign-in page. It comes back to the current location. */
|
|
96
119
|
signIn(): Promise<void>;
|
|
97
120
|
signOut(): Promise<void>;
|
|
@@ -304,9 +327,9 @@ declare function ErrorScreen({ error }: {
|
|
|
304
327
|
error: unknown;
|
|
305
328
|
}): react.JSX.Element;
|
|
306
329
|
/**
|
|
307
|
-
* The frame every page renders in: sidebar built from the routes, header with the
|
|
308
|
-
*
|
|
309
|
-
*
|
|
330
|
+
* The frame every page renders in: sidebar built from the routes, header with the tenant
|
|
331
|
+
* switcher and the account menu, and the route content. Use it as the component of the
|
|
332
|
+
* root route. A route with `staticData.layout: 'none'` renders without the frame.
|
|
310
333
|
*/
|
|
311
334
|
declare function AppShell(): react.JSX.Element;
|
|
312
335
|
|
|
@@ -321,7 +344,9 @@ declare function hasPermissions(granted: Iterable<string>, required: readonly st
|
|
|
321
344
|
|
|
322
345
|
/**
|
|
323
346
|
* The current tenant of this browser. One browser profile has one current tenant, and every
|
|
324
|
-
* tab of every app reads the same value.
|
|
347
|
+
* tab of every app reads the same value. The header of every app
|
|
348
|
+
* changes it when a platform operator enters or leaves a tenant as platform support
|
|
349
|
+
* (`Session.enterTenant`, `Session.leaveTenant`).
|
|
325
350
|
*/
|
|
326
351
|
declare const TENANT_STORAGE_KEY = "platform.tenant";
|
|
327
352
|
/** What is stored: the tenant, and the organization its tokens are issued for. */
|
|
@@ -330,6 +355,14 @@ interface StoredTenant {
|
|
|
330
355
|
organizationId: string;
|
|
331
356
|
/** The tenant's UUID, as the organization token carries it. */
|
|
332
357
|
tenantId: string;
|
|
358
|
+
/**
|
|
359
|
+
* Set while a platform operator works in a customer's tenant as platform support: the
|
|
360
|
+
* operator's own tenant, which comes back when the session ends.
|
|
361
|
+
*/
|
|
362
|
+
home?: {
|
|
363
|
+
organizationId: string;
|
|
364
|
+
tenantId: string;
|
|
365
|
+
};
|
|
333
366
|
}
|
|
334
367
|
/** The stored current tenant, or undefined when none is stored or storage is closed. */
|
|
335
368
|
declare function readStoredTenant(): StoredTenant | undefined;
|
package/dist/index.js
CHANGED
|
@@ -3,9 +3,9 @@ import { registerMessages as registerMessages2 } from "@zjazj/i18n";
|
|
|
3
3
|
|
|
4
4
|
// src/locales/en-US.json
|
|
5
5
|
var en_US_default = {
|
|
6
|
-
apps: "Apps",
|
|
7
6
|
account: "Account",
|
|
8
7
|
signOut: "Sign out",
|
|
8
|
+
profile: "Profile",
|
|
9
9
|
theme: "Theme",
|
|
10
10
|
themeLight: "Light",
|
|
11
11
|
themeDark: "Dark",
|
|
@@ -35,14 +35,27 @@ var en_US_default = {
|
|
|
35
35
|
noTenantTitle: "No tenant",
|
|
36
36
|
noTenantBody: "Your account does not belong to a tenant yet. Ask an administrator to invite you.",
|
|
37
37
|
errorTitle: "Something went wrong",
|
|
38
|
-
traceId: "Trace id: {{id}}"
|
|
38
|
+
traceId: "Trace id: {{id}}",
|
|
39
|
+
cancel: "Cancel",
|
|
40
|
+
enterTenant: "Enter a tenant",
|
|
41
|
+
enterTenantBody: "Work in a customer's tenant as platform support, for one hour. Everything you do is recorded to your account. The tenant sees it as done by Platform Support.",
|
|
42
|
+
enterTenantSearch: "Search tenants",
|
|
43
|
+
enterTenantLoading: "Loading tenants\u2026",
|
|
44
|
+
enterTenantNone: "No tenant found.",
|
|
45
|
+
enterTenantConfirm: "Enter",
|
|
46
|
+
supportNotice: "Support in {{tenant}} \xB7 {{minutes}} min left",
|
|
47
|
+
supportExit: "Exit",
|
|
48
|
+
supportEndedTitle: "Support session ended",
|
|
49
|
+
supportEndedBody: "Your time in this tenant is over. Continue in your own tenant.",
|
|
50
|
+
continue: "Continue",
|
|
51
|
+
enterTenantNoSignIn: "No sign-in set up"
|
|
39
52
|
};
|
|
40
53
|
|
|
41
54
|
// src/locales/zh-CN.json
|
|
42
55
|
var zh_CN_default = {
|
|
43
|
-
apps: "\u5E94\u7528",
|
|
44
56
|
account: "\u8D26\u53F7",
|
|
45
57
|
signOut: "\u9000\u51FA\u767B\u5F55",
|
|
58
|
+
profile: "\u4E2A\u4EBA\u8D44\u6599",
|
|
46
59
|
theme: "\u4E3B\u9898",
|
|
47
60
|
themeLight: "\u6D45\u8272",
|
|
48
61
|
themeDark: "\u6DF1\u8272",
|
|
@@ -72,7 +85,20 @@ var zh_CN_default = {
|
|
|
72
85
|
noTenantTitle: "\u6CA1\u6709\u79DF\u6237",
|
|
73
86
|
noTenantBody: "\u4F60\u7684\u8D26\u53F7\u8FD8\u4E0D\u5C5E\u4E8E\u4EFB\u4F55\u79DF\u6237\u3002\u8BF7\u8054\u7CFB\u7BA1\u7406\u5458\u9080\u8BF7\u4F60\u3002",
|
|
74
87
|
errorTitle: "\u51FA\u9519\u4E86",
|
|
75
|
-
traceId: "\u8FFD\u8E2A\u53F7\uFF1A{{id}}"
|
|
88
|
+
traceId: "\u8FFD\u8E2A\u53F7\uFF1A{{id}}",
|
|
89
|
+
cancel: "\u53D6\u6D88",
|
|
90
|
+
enterTenant: "\u8FDB\u5165\u79DF\u6237",
|
|
91
|
+
enterTenantBody: "\u4EE5\u5E73\u53F0\u652F\u6301\u8EAB\u4EFD\u8FDB\u5165\u5BA2\u6237\u7684\u79DF\u6237\uFF0C\u65F6\u957F\u4E00\u5C0F\u65F6\u3002\u4F60\u7684\u6240\u6709\u64CD\u4F5C\u90FD\u8BB0\u5728\u4F60\u81EA\u5DF1\u7684\u8D26\u53F7\u4E0B\uFF0C\u79DF\u6237\u770B\u5230\u7684\u64CD\u4F5C\u4EBA\u662F\u201C\u5E73\u53F0\u652F\u6301\u201D\u3002",
|
|
92
|
+
enterTenantSearch: "\u641C\u7D22\u79DF\u6237",
|
|
93
|
+
enterTenantLoading: "\u6B63\u5728\u52A0\u8F7D\u79DF\u6237\u2026",
|
|
94
|
+
enterTenantNone: "\u6CA1\u6709\u627E\u5230\u79DF\u6237\u3002",
|
|
95
|
+
enterTenantConfirm: "\u8FDB\u5165",
|
|
96
|
+
supportNotice: "\u652F\u6301\u4E2D\uFF1A{{tenant}} \xB7 \u5269\u4F59 {{minutes}} \u5206\u949F",
|
|
97
|
+
supportExit: "\u9000\u51FA",
|
|
98
|
+
supportEndedTitle: "\u652F\u6301\u4F1A\u8BDD\u5DF2\u7ED3\u675F",
|
|
99
|
+
supportEndedBody: "\u4F60\u5728\u8FD9\u4E2A\u79DF\u6237\u7684\u65F6\u95F4\u5DF2\u5230\u3002\u7EE7\u7EED\u4F1A\u56DE\u5230\u4F60\u81EA\u5DF1\u7684\u79DF\u6237\u3002",
|
|
100
|
+
continue: "\u7EE7\u7EED",
|
|
101
|
+
enterTenantNoSignIn: "\u672A\u5F00\u901A\u767B\u5F55"
|
|
76
102
|
};
|
|
77
103
|
|
|
78
104
|
// src/app.tsx
|
|
@@ -122,10 +148,12 @@ function parseStoredTenant(raw) {
|
|
|
122
148
|
try {
|
|
123
149
|
const parsed = JSON.parse(raw);
|
|
124
150
|
if (parsed && typeof parsed === "object") {
|
|
125
|
-
const { organizationId, tenantId } = parsed;
|
|
151
|
+
const { organizationId, tenantId, home } = parsed;
|
|
152
|
+
const back = home && typeof home === "object" ? home : {};
|
|
126
153
|
return {
|
|
127
154
|
...typeof organizationId === "string" && organizationId ? { organizationId } : {},
|
|
128
|
-
...typeof tenantId === "string" && tenantId ? { tenantId } : {}
|
|
155
|
+
...typeof tenantId === "string" && tenantId ? { tenantId } : {},
|
|
156
|
+
...typeof back.organizationId === "string" && back.organizationId && typeof back.tenantId === "string" && back.tenantId ? { home: { organizationId: back.organizationId, tenantId: back.tenantId } } : {}
|
|
129
157
|
};
|
|
130
158
|
}
|
|
131
159
|
} catch {
|
|
@@ -141,10 +169,8 @@ function readStoredTenant() {
|
|
|
141
169
|
}
|
|
142
170
|
}
|
|
143
171
|
function writeStoredTenant(value) {
|
|
144
|
-
const
|
|
145
|
-
if (
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
172
|
+
const stored = (tenant) => tenant ? JSON.stringify([tenant.organizationId, tenant.tenantId, tenant.home ?? null]) : void 0;
|
|
173
|
+
if (stored(readStoredTenant()) === stored(value)) return;
|
|
148
174
|
try {
|
|
149
175
|
globalThis.localStorage?.setItem(TENANT_STORAGE_KEY, JSON.stringify(value));
|
|
150
176
|
} catch {
|
|
@@ -251,6 +277,8 @@ function userFromTokenClaims(claims, profile = {}) {
|
|
|
251
277
|
}
|
|
252
278
|
const roles = strings(claims.organization_roles);
|
|
253
279
|
const timeZone = firstString(claims.tenant_time_zone);
|
|
280
|
+
const { support_until: until, support_mode: mode } = claims;
|
|
281
|
+
const support = typeof until === "number" && Number.isFinite(until) && typeof mode === "string" && mode ? { until, mode } : void 0;
|
|
254
282
|
return {
|
|
255
283
|
id: sub,
|
|
256
284
|
tenantId: tenantId.toLowerCase(),
|
|
@@ -265,7 +293,8 @@ function userFromTokenClaims(claims, profile = {}) {
|
|
|
265
293
|
) ?? sub,
|
|
266
294
|
roles: roles.length > 0 ? roles : strings(claims.roles),
|
|
267
295
|
permissions: strings(claims.permissions),
|
|
268
|
-
...timeZone ? { timeZone } : {}
|
|
296
|
+
...timeZone ? { timeZone } : {},
|
|
297
|
+
...support ? { support } : {}
|
|
269
298
|
};
|
|
270
299
|
}
|
|
271
300
|
function rememberLocation() {
|
|
@@ -324,11 +353,25 @@ function createPlatformSession(options = {}) {
|
|
|
324
353
|
token = { value, expiresAt: typeof claims.exp === "number" ? claims.exp : 0 };
|
|
325
354
|
return next;
|
|
326
355
|
};
|
|
356
|
+
const take = async (identity, organization, home) => {
|
|
357
|
+
try {
|
|
358
|
+
const next = readToken(await identity.getOrganizationToken(organization));
|
|
359
|
+
organizationId = organization;
|
|
360
|
+
writeStoredTenant({ organizationId: organization, tenantId: next.tenantId, home });
|
|
361
|
+
update(next, { state: "ready" });
|
|
362
|
+
return true;
|
|
363
|
+
} catch {
|
|
364
|
+
token = void 0;
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
};
|
|
327
368
|
const adopt = async (identity) => {
|
|
328
369
|
profile = await identity.getIdTokenClaims() ?? {};
|
|
329
370
|
const organizations = strings(profile.organizations);
|
|
330
371
|
const stored = readStoredTenant();
|
|
331
|
-
|
|
372
|
+
if (stored?.home && await take(identity, stored.organizationId, stored.home)) return;
|
|
373
|
+
const own = stored?.home?.organizationId ?? stored?.organizationId;
|
|
374
|
+
const organization = own && organizations.includes(own) ? own : organizations[0];
|
|
332
375
|
if (!organization) {
|
|
333
376
|
fail("no_organization", "The account is not a member of any tenant.");
|
|
334
377
|
return;
|
|
@@ -345,6 +388,13 @@ function createPlatformSession(options = {}) {
|
|
|
345
388
|
writeStoredTenant({ organizationId: organization, tenantId: next.tenantId });
|
|
346
389
|
update(next, { state: "ready" });
|
|
347
390
|
};
|
|
391
|
+
const supportEnded = () => {
|
|
392
|
+
const home = readStoredTenant()?.home;
|
|
393
|
+
if (home) writeStoredTenant(home);
|
|
394
|
+
else clearStoredTenant();
|
|
395
|
+
token = void 0;
|
|
396
|
+
update(null, { state: "error", code: "support_ended" });
|
|
397
|
+
};
|
|
348
398
|
const restore = async () => {
|
|
349
399
|
let identity;
|
|
350
400
|
try {
|
|
@@ -365,16 +415,34 @@ function createPlatformSession(options = {}) {
|
|
|
365
415
|
const started = restore();
|
|
366
416
|
const refresh = () => withTokenLock(async () => {
|
|
367
417
|
const current = token;
|
|
418
|
+
const stillValid = () => current && current.expiresAt > now() / 1e3 ? current : null;
|
|
419
|
+
let value;
|
|
368
420
|
try {
|
|
369
421
|
const identity = await connect();
|
|
370
422
|
await identity.clearAccessToken();
|
|
371
|
-
|
|
423
|
+
value = await identity.getOrganizationToken(organizationId);
|
|
424
|
+
} catch {
|
|
425
|
+
const valid = stillValid();
|
|
426
|
+
if (valid) {
|
|
427
|
+
token = valid;
|
|
428
|
+
return valid.value;
|
|
429
|
+
}
|
|
430
|
+
if (user?.support) supportEnded();
|
|
431
|
+
else end();
|
|
432
|
+
return null;
|
|
433
|
+
}
|
|
434
|
+
try {
|
|
372
435
|
update(readToken(value), { state: "ready" });
|
|
373
436
|
return value;
|
|
374
437
|
} catch {
|
|
375
|
-
if (
|
|
376
|
-
|
|
377
|
-
return
|
|
438
|
+
if (user?.support) {
|
|
439
|
+
supportEnded();
|
|
440
|
+
return null;
|
|
441
|
+
}
|
|
442
|
+
const valid = stillValid();
|
|
443
|
+
if (valid) {
|
|
444
|
+
token = valid;
|
|
445
|
+
return valid.value;
|
|
378
446
|
}
|
|
379
447
|
end();
|
|
380
448
|
return null;
|
|
@@ -412,10 +480,13 @@ function createPlatformSession(options = {}) {
|
|
|
412
480
|
kind: "platform",
|
|
413
481
|
getUser: () => user,
|
|
414
482
|
getStatus: () => status,
|
|
415
|
-
async getAccessToken() {
|
|
483
|
+
async getAccessToken(options2) {
|
|
416
484
|
await started;
|
|
417
485
|
if (!isReady() || !token) return null;
|
|
418
|
-
|
|
486
|
+
const seconds = now() / 1e3;
|
|
487
|
+
const supportOver = user?.support !== void 0 && seconds >= user.support.until;
|
|
488
|
+
const fresh = token.expiresAt - seconds > EXPIRY_LEEWAY_SECONDS;
|
|
489
|
+
if (fresh && !supportOver && !options2?.renew) return token.value;
|
|
419
490
|
refreshing ??= refresh().finally(() => {
|
|
420
491
|
refreshing = void 0;
|
|
421
492
|
});
|
|
@@ -427,6 +498,23 @@ function createPlatformSession(options = {}) {
|
|
|
427
498
|
});
|
|
428
499
|
return signingIn;
|
|
429
500
|
},
|
|
501
|
+
async enterTenant(next) {
|
|
502
|
+
await started;
|
|
503
|
+
const self = user;
|
|
504
|
+
if (!isReady() || !self) throw new Error("Nobody is signed in.");
|
|
505
|
+
const value = await (await connect()).getOrganizationToken(next);
|
|
506
|
+
const { tenantId } = userFromTokenClaims(decodeTokenClaims(value), profile);
|
|
507
|
+
const home = readStoredTenant()?.home ?? {
|
|
508
|
+
organizationId,
|
|
509
|
+
tenantId: self.tenantId
|
|
510
|
+
};
|
|
511
|
+
writeStoredTenant({ organizationId: next, tenantId, home });
|
|
512
|
+
},
|
|
513
|
+
async leaveTenant() {
|
|
514
|
+
const home = readStoredTenant()?.home;
|
|
515
|
+
if (home) writeStoredTenant(home);
|
|
516
|
+
else clearStoredTenant();
|
|
517
|
+
},
|
|
430
518
|
async signOut() {
|
|
431
519
|
channel?.postMessage({ type: "signed-out" });
|
|
432
520
|
clearStoredTenant();
|
|
@@ -548,6 +636,7 @@ var ApiError = class _ApiError extends Error {
|
|
|
548
636
|
});
|
|
549
637
|
}
|
|
550
638
|
};
|
|
639
|
+
var RENEWABLE = /* @__PURE__ */ new Set(["token_expired", "support_ended"]);
|
|
551
640
|
async function platformFetch(input, init) {
|
|
552
641
|
if (isTenantChanged()) {
|
|
553
642
|
throw new ApiError({
|
|
@@ -560,13 +649,26 @@ async function platformFetch(input, init) {
|
|
|
560
649
|
const headers = new Headers(
|
|
561
650
|
init?.headers ?? (input instanceof Request ? input.headers : void 0)
|
|
562
651
|
);
|
|
563
|
-
|
|
652
|
+
const ownToken = !headers.has("Authorization");
|
|
653
|
+
const canRepeat = !(typeof ReadableStream === "function" && init?.body instanceof ReadableStream);
|
|
654
|
+
const again = ownToken && canRepeat && input instanceof Request ? input.clone() : input;
|
|
655
|
+
if (ownToken) {
|
|
564
656
|
const token = await session.getAccessToken();
|
|
565
657
|
if (token) headers.set("Authorization", `Bearer ${token}`);
|
|
566
658
|
}
|
|
567
659
|
const response = await fetch(input, { ...init, headers });
|
|
568
660
|
if (response.ok) return response;
|
|
569
|
-
|
|
661
|
+
const error = await ApiError.fromResponse(response);
|
|
662
|
+
if (ownToken && canRepeat && RENEWABLE.has(error.code)) {
|
|
663
|
+
const renewed = await session.getAccessToken({ renew: true });
|
|
664
|
+
if (renewed) {
|
|
665
|
+
headers.set("Authorization", `Bearer ${renewed}`);
|
|
666
|
+
const retried = await fetch(again, { ...init, headers });
|
|
667
|
+
if (retried.ok) return retried;
|
|
668
|
+
throw await ApiError.fromResponse(retried);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
throw error;
|
|
570
672
|
}
|
|
571
673
|
function createClient(baseUrl = apiBaseUrl()) {
|
|
572
674
|
return hc(baseUrl, { fetch: platformFetch });
|
|
@@ -643,6 +745,18 @@ import {
|
|
|
643
745
|
AvatarFallback,
|
|
644
746
|
Badge,
|
|
645
747
|
Button,
|
|
748
|
+
Command,
|
|
749
|
+
CommandEmpty,
|
|
750
|
+
CommandInput,
|
|
751
|
+
CommandItem,
|
|
752
|
+
CommandList,
|
|
753
|
+
Dialog,
|
|
754
|
+
DialogContent,
|
|
755
|
+
DialogDescription,
|
|
756
|
+
DialogFooter,
|
|
757
|
+
DialogHeader,
|
|
758
|
+
DialogTitle,
|
|
759
|
+
DialogTrigger,
|
|
646
760
|
DropdownMenu,
|
|
647
761
|
DropdownMenuContent,
|
|
648
762
|
DropdownMenuItem,
|
|
@@ -670,8 +784,17 @@ import {
|
|
|
670
784
|
SidebarTrigger,
|
|
671
785
|
useTheme
|
|
672
786
|
} from "@zjazj/ui";
|
|
673
|
-
import {
|
|
674
|
-
|
|
787
|
+
import {
|
|
788
|
+
Building2,
|
|
789
|
+
Check,
|
|
790
|
+
CircleUserRound,
|
|
791
|
+
Languages,
|
|
792
|
+
LayoutGrid,
|
|
793
|
+
LifeBuoy,
|
|
794
|
+
LogOut,
|
|
795
|
+
SunMoon
|
|
796
|
+
} from "lucide-react";
|
|
797
|
+
import { useEffect, useMemo, useState, useSyncExternalStore as useSyncExternalStore2 } from "react";
|
|
675
798
|
|
|
676
799
|
// src/menu.ts
|
|
677
800
|
function requiredPermissions(meta) {
|
|
@@ -744,12 +867,16 @@ function SessionScreen() {
|
|
|
744
867
|
const t = useT("web");
|
|
745
868
|
const session = useSession();
|
|
746
869
|
const status = useSessionStatus();
|
|
870
|
+
const { basePath } = useAppConfig();
|
|
747
871
|
if (status.state === "ended") {
|
|
748
872
|
return /* @__PURE__ */ jsx2(Message, { title: t("signedOutTitle"), body: t("signedOutBody"), children: /* @__PURE__ */ jsx2(Button, { onClick: () => void session.signIn(), children: t("signIn") }) });
|
|
749
873
|
}
|
|
750
874
|
if (status.state === "error" && status.code === "not_configured") {
|
|
751
875
|
return /* @__PURE__ */ jsx2(Message, { title: t("signInRequiredTitle"), body: t("signInRequiredBody") });
|
|
752
876
|
}
|
|
877
|
+
if (status.state === "error" && status.code === "support_ended") {
|
|
878
|
+
return /* @__PURE__ */ jsx2(Message, { title: t("supportEndedTitle"), body: t("supportEndedBody"), children: /* @__PURE__ */ jsx2(Button, { onClick: () => window.location.assign(basePath || "/"), children: t("continue") }) });
|
|
879
|
+
}
|
|
753
880
|
if (status.state === "error" && status.code === "no_organization") {
|
|
754
881
|
return /* @__PURE__ */ jsx2(Message, { title: t("noTenantTitle"), body: t("noTenantBody"), children: /* @__PURE__ */ jsx2(Button, { variant: "outline", onClick: () => void session.signOut(), children: t("signOut") }) });
|
|
755
882
|
}
|
|
@@ -771,13 +898,174 @@ var THEME_MODES = [
|
|
|
771
898
|
{ mode: "system", label: "themeSystem" }
|
|
772
899
|
];
|
|
773
900
|
var LOCALE_LABELS = { "en-US": "localeEnUS", "zh-CN": "localeZhCN" };
|
|
901
|
+
var ENTER_TENANT_PERMISSION = "admin:tenant:enter";
|
|
902
|
+
function messageOf2(error) {
|
|
903
|
+
return error instanceof Error ? error.message : String(error);
|
|
904
|
+
}
|
|
905
|
+
function TenantSwitcher() {
|
|
906
|
+
const user = useUser();
|
|
907
|
+
const session = useSession();
|
|
908
|
+
if (user.support) return /* @__PURE__ */ jsx2(SupportNotice, {});
|
|
909
|
+
const canEnter = Boolean(session.enterTenant) && hasPermissions(user.permissions, [ENTER_TENANT_PERMISSION]);
|
|
910
|
+
return canEnter ? /* @__PURE__ */ jsx2(EnterTenant, {}) : /* @__PURE__ */ jsx2(TenantName, {});
|
|
911
|
+
}
|
|
912
|
+
function TenantName() {
|
|
913
|
+
const t = useT("web");
|
|
914
|
+
const user = useUser();
|
|
915
|
+
return /* @__PURE__ */ jsxs(
|
|
916
|
+
"span",
|
|
917
|
+
{
|
|
918
|
+
className: "text-muted-foreground flex min-w-0 items-center gap-1.5 px-2 text-sm",
|
|
919
|
+
title: t("tenant"),
|
|
920
|
+
children: [
|
|
921
|
+
/* @__PURE__ */ jsx2(Building2, { className: "size-4 shrink-0" }),
|
|
922
|
+
/* @__PURE__ */ jsx2("span", { className: "max-w-40 truncate", children: user.tenantSlug || user.tenantId })
|
|
923
|
+
]
|
|
924
|
+
}
|
|
925
|
+
);
|
|
926
|
+
}
|
|
927
|
+
function EnterTenant() {
|
|
928
|
+
const t = useT("web");
|
|
929
|
+
const user = useUser();
|
|
930
|
+
const session = useSession();
|
|
931
|
+
const { basePath } = useAppConfig();
|
|
932
|
+
const [open, setOpen] = useState(false);
|
|
933
|
+
const [tenants, setTenants] = useState();
|
|
934
|
+
const [chosen, setChosen] = useState();
|
|
935
|
+
const [entering, setEntering] = useState(false);
|
|
936
|
+
const [failure, setFailure] = useState();
|
|
937
|
+
useEffect(() => {
|
|
938
|
+
if (!open) return;
|
|
939
|
+
let stale = false;
|
|
940
|
+
setTenants(void 0);
|
|
941
|
+
setChosen(void 0);
|
|
942
|
+
setFailure(void 0);
|
|
943
|
+
platformFetch(`${apiBaseUrl()}/_platform/tenants?page_size=100`).then((response) => response.json()).then((page) => {
|
|
944
|
+
if (!stale) setTenants(Array.isArray(page.items) ? page.items : []);
|
|
945
|
+
}).catch((error) => {
|
|
946
|
+
if (stale) return;
|
|
947
|
+
setTenants([]);
|
|
948
|
+
setFailure(messageOf2(error));
|
|
949
|
+
});
|
|
950
|
+
return () => {
|
|
951
|
+
stale = true;
|
|
952
|
+
};
|
|
953
|
+
}, [open]);
|
|
954
|
+
const target = tenants?.find((item) => item.tenant_id === chosen && item.organization_id);
|
|
955
|
+
const enter = async () => {
|
|
956
|
+
if (!target || !session.enterTenant) return;
|
|
957
|
+
setEntering(true);
|
|
958
|
+
setFailure(void 0);
|
|
959
|
+
try {
|
|
960
|
+
const response = await platformFetch(`${apiBaseUrl()}/_platform/support`, {
|
|
961
|
+
method: "POST",
|
|
962
|
+
headers: { "Content-Type": "application/json" },
|
|
963
|
+
body: JSON.stringify({ tenant_id: target.tenant_id })
|
|
964
|
+
});
|
|
965
|
+
const entered = await response.json();
|
|
966
|
+
if (typeof entered.organization_id !== "string") {
|
|
967
|
+
throw new Error("The control plane answered without the organization.");
|
|
968
|
+
}
|
|
969
|
+
await session.enterTenant(entered.organization_id);
|
|
970
|
+
window.location.assign(basePath || "/");
|
|
971
|
+
} catch (error) {
|
|
972
|
+
setFailure(messageOf2(error));
|
|
973
|
+
setEntering(false);
|
|
974
|
+
}
|
|
975
|
+
};
|
|
976
|
+
return /* @__PURE__ */ jsxs(Dialog, { open, onOpenChange: setOpen, children: [
|
|
977
|
+
/* @__PURE__ */ jsx2(DialogTrigger, { asChild: true, children: /* @__PURE__ */ jsxs(Button, { variant: "ghost", size: "sm", "aria-label": t("enterTenant"), children: [
|
|
978
|
+
/* @__PURE__ */ jsx2(Building2, {}),
|
|
979
|
+
/* @__PURE__ */ jsx2("span", { className: "max-w-40 truncate", children: user.tenantSlug || user.tenantId })
|
|
980
|
+
] }) }),
|
|
981
|
+
/* @__PURE__ */ jsxs(DialogContent, { className: "sm:max-w-md", children: [
|
|
982
|
+
/* @__PURE__ */ jsxs(DialogHeader, { children: [
|
|
983
|
+
/* @__PURE__ */ jsx2(DialogTitle, { children: t("enterTenant") }),
|
|
984
|
+
/* @__PURE__ */ jsx2(DialogDescription, { children: t("enterTenantBody") })
|
|
985
|
+
] }),
|
|
986
|
+
/* @__PURE__ */ jsxs(Command, { className: "rounded-md border", children: [
|
|
987
|
+
/* @__PURE__ */ jsx2(CommandInput, { placeholder: t("enterTenantSearch") }),
|
|
988
|
+
/* @__PURE__ */ jsxs(CommandList, { children: [
|
|
989
|
+
/* @__PURE__ */ jsx2(CommandEmpty, { children: tenants === void 0 ? t("enterTenantLoading") : t("enterTenantNone") }),
|
|
990
|
+
(tenants ?? []).map((item) => /* @__PURE__ */ jsxs(
|
|
991
|
+
CommandItem,
|
|
992
|
+
{
|
|
993
|
+
value: `${item.name} ${item.slug} ${item.tenant_id}`,
|
|
994
|
+
disabled: !item.organization_id,
|
|
995
|
+
onSelect: () => setChosen(item.tenant_id),
|
|
996
|
+
children: [
|
|
997
|
+
/* @__PURE__ */ jsx2("span", { className: "min-w-0 flex-1 truncate", children: item.name || item.slug }),
|
|
998
|
+
/* @__PURE__ */ jsx2("span", { className: "text-muted-foreground truncate text-xs", children: item.organization_id ? item.slug : t("enterTenantNoSignIn") }),
|
|
999
|
+
item.tenant_id === chosen ? /* @__PURE__ */ jsx2(Check, {}) : null
|
|
1000
|
+
]
|
|
1001
|
+
},
|
|
1002
|
+
item.tenant_id
|
|
1003
|
+
))
|
|
1004
|
+
] })
|
|
1005
|
+
] }),
|
|
1006
|
+
failure ? /* @__PURE__ */ jsx2("p", { className: "text-destructive text-sm", children: failure }) : null,
|
|
1007
|
+
/* @__PURE__ */ jsxs(DialogFooter, { children: [
|
|
1008
|
+
/* @__PURE__ */ jsx2(Button, { variant: "outline", onClick: () => setOpen(false), children: t("cancel") }),
|
|
1009
|
+
/* @__PURE__ */ jsx2(Button, { disabled: !target || entering, onClick: () => void enter(), children: t("enterTenantConfirm") })
|
|
1010
|
+
] })
|
|
1011
|
+
] })
|
|
1012
|
+
] });
|
|
1013
|
+
}
|
|
1014
|
+
function SupportNotice() {
|
|
1015
|
+
const t = useT("web");
|
|
1016
|
+
const user = useUser();
|
|
1017
|
+
const session = useSession();
|
|
1018
|
+
const { basePath } = useAppConfig();
|
|
1019
|
+
const [now, setNow] = useState(() => Date.now());
|
|
1020
|
+
const [leaving, setLeaving] = useState(false);
|
|
1021
|
+
useEffect(() => {
|
|
1022
|
+
const clock = window.setInterval(() => setNow(Date.now()), 3e4);
|
|
1023
|
+
return () => window.clearInterval(clock);
|
|
1024
|
+
}, []);
|
|
1025
|
+
const until = (user.support?.until ?? 0) * 1e3;
|
|
1026
|
+
const minutes = Math.max(0, Math.ceil((until - now) / 6e4));
|
|
1027
|
+
const over = now >= until;
|
|
1028
|
+
useEffect(() => {
|
|
1029
|
+
const wait = until - Date.now() + 1e3;
|
|
1030
|
+
if (wait <= 0) return;
|
|
1031
|
+
const end = window.setTimeout(() => setNow(Date.now()), Math.min(wait, 2147483647));
|
|
1032
|
+
return () => window.clearTimeout(end);
|
|
1033
|
+
}, [until]);
|
|
1034
|
+
useEffect(() => {
|
|
1035
|
+
if (over) void session.getAccessToken();
|
|
1036
|
+
}, [over, session]);
|
|
1037
|
+
const leave = async () => {
|
|
1038
|
+
setLeaving(true);
|
|
1039
|
+
try {
|
|
1040
|
+
await platformFetch(`${apiBaseUrl()}/_platform/support`, { method: "DELETE" });
|
|
1041
|
+
} catch {
|
|
1042
|
+
}
|
|
1043
|
+
await session.leaveTenant?.();
|
|
1044
|
+
window.location.assign(basePath || "/");
|
|
1045
|
+
};
|
|
1046
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex min-w-0 items-center gap-2", children: [
|
|
1047
|
+
/* @__PURE__ */ jsxs(Badge, { variant: "secondary", className: "gap-1.5", children: [
|
|
1048
|
+
/* @__PURE__ */ jsx2(LifeBuoy, {}),
|
|
1049
|
+
/* @__PURE__ */ jsx2("span", { className: "max-w-48 truncate", children: t("supportNotice", { tenant: user.tenantSlug || user.tenantId, minutes }) })
|
|
1050
|
+
] }),
|
|
1051
|
+
/* @__PURE__ */ jsx2(Button, { variant: "outline", size: "sm", disabled: leaving, onClick: () => void leave(), children: t("supportExit") })
|
|
1052
|
+
] });
|
|
1053
|
+
}
|
|
1054
|
+
function profileLink(portalUrl, basePath) {
|
|
1055
|
+
const origin = globalThis.location?.origin ?? "http://localhost";
|
|
1056
|
+
const portal = new URL(portalUrl.replace(/\/*$/, "/"), origin);
|
|
1057
|
+
const href = new URL("profile", portal).href;
|
|
1058
|
+
return { here: portal.origin === origin && portal.pathname === basePath, href };
|
|
1059
|
+
}
|
|
774
1060
|
function AccountMenu() {
|
|
775
1061
|
const t = useT("web");
|
|
776
1062
|
const user = useUser();
|
|
777
1063
|
const session = useSession();
|
|
1064
|
+
const config = useAppConfig();
|
|
778
1065
|
const { theme, setTheme } = useTheme();
|
|
779
1066
|
const [locale, setLocale] = useLocale();
|
|
780
1067
|
const initials = user.username.trim().slice(0, 2).toUpperCase() || "?";
|
|
1068
|
+
const profile = user.isDev ? void 0 : profileLink(config.portalUrl, config.basePath);
|
|
781
1069
|
return /* @__PURE__ */ jsxs(DropdownMenu, { children: [
|
|
782
1070
|
/* @__PURE__ */ jsx2(DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsx2(Button, { variant: "ghost", size: "icon", className: "rounded-full", "aria-label": t("account"), children: /* @__PURE__ */ jsx2(Avatar, { className: "size-8", children: /* @__PURE__ */ jsx2(AvatarFallback, { children: initials }) }) }) }),
|
|
783
1071
|
/* @__PURE__ */ jsxs(DropdownMenuContent, { align: "end", className: "w-56", children: [
|
|
@@ -790,8 +1078,24 @@ function AccountMenu() {
|
|
|
790
1078
|
] })
|
|
791
1079
|
] }),
|
|
792
1080
|
/* @__PURE__ */ jsx2(DropdownMenuSeparator, {}),
|
|
1081
|
+
profile ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1082
|
+
/* @__PURE__ */ jsx2(DropdownMenuItem, { asChild: true, children: profile.here ? /* @__PURE__ */ jsxs(Link, { to: "/profile", children: [
|
|
1083
|
+
/* @__PURE__ */ jsx2(CircleUserRound, {}),
|
|
1084
|
+
t("profile")
|
|
1085
|
+
] }) : (
|
|
1086
|
+
// The portal's own tab, as an app opens in its own (D3).
|
|
1087
|
+
/* @__PURE__ */ jsxs("a", { href: profile.href, target: "platform-portal", children: [
|
|
1088
|
+
/* @__PURE__ */ jsx2(CircleUserRound, {}),
|
|
1089
|
+
t("profile")
|
|
1090
|
+
] })
|
|
1091
|
+
) }),
|
|
1092
|
+
/* @__PURE__ */ jsx2(DropdownMenuSeparator, {})
|
|
1093
|
+
] }) : null,
|
|
793
1094
|
/* @__PURE__ */ jsxs(DropdownMenuSub, { children: [
|
|
794
|
-
/* @__PURE__ */
|
|
1095
|
+
/* @__PURE__ */ jsxs(DropdownMenuSubTrigger, { children: [
|
|
1096
|
+
/* @__PURE__ */ jsx2(SunMoon, {}),
|
|
1097
|
+
t("theme")
|
|
1098
|
+
] }),
|
|
795
1099
|
/* @__PURE__ */ jsx2(DropdownMenuSubContent, { children: /* @__PURE__ */ jsx2(
|
|
796
1100
|
DropdownMenuRadioGroup,
|
|
797
1101
|
{
|
|
@@ -802,7 +1106,10 @@ function AccountMenu() {
|
|
|
802
1106
|
) })
|
|
803
1107
|
] }),
|
|
804
1108
|
/* @__PURE__ */ jsxs(DropdownMenuSub, { children: [
|
|
805
|
-
/* @__PURE__ */
|
|
1109
|
+
/* @__PURE__ */ jsxs(DropdownMenuSubTrigger, { children: [
|
|
1110
|
+
/* @__PURE__ */ jsx2(Languages, {}),
|
|
1111
|
+
t("language")
|
|
1112
|
+
] }),
|
|
806
1113
|
/* @__PURE__ */ jsx2(DropdownMenuSubContent, { children: /* @__PURE__ */ jsx2(
|
|
807
1114
|
DropdownMenuRadioGroup,
|
|
808
1115
|
{
|
|
@@ -881,21 +1188,18 @@ function AppShell() {
|
|
|
881
1188
|
/* @__PURE__ */ jsx2(SidebarContent, { children: /* @__PURE__ */ jsx2(Navigation, { groups, pathname }) }),
|
|
882
1189
|
/* @__PURE__ */ jsx2(SidebarRail, {})
|
|
883
1190
|
] }),
|
|
884
|
-
/* @__PURE__ */ jsxs(SidebarInset, { children: [
|
|
1191
|
+
/* @__PURE__ */ jsxs(SidebarInset, { className: "min-w-0", children: [
|
|
885
1192
|
/* @__PURE__ */ jsxs("header", { className: "bg-background sticky top-0 z-10 flex h-14 shrink-0 items-center gap-2 border-b px-4", children: [
|
|
886
1193
|
/* @__PURE__ */ jsx2(SidebarTrigger, {}),
|
|
887
1194
|
/* @__PURE__ */ jsx2(Separator, { orientation: "vertical", className: "mx-1 h-5" }),
|
|
888
1195
|
/* @__PURE__ */ jsx2("span", { className: "truncate font-medium", children: pageTitle ?? config.title }),
|
|
889
1196
|
/* @__PURE__ */ jsxs("div", { className: "ml-auto flex items-center gap-2", children: [
|
|
890
1197
|
user.isDev ? /* @__PURE__ */ jsx2(Badge, { variant: "outline", children: t("devIdentity") }) : null,
|
|
891
|
-
/* @__PURE__ */ jsx2(
|
|
892
|
-
/* @__PURE__ */ jsx2(LayoutGrid, {}),
|
|
893
|
-
t("apps")
|
|
894
|
-
] }) }),
|
|
1198
|
+
/* @__PURE__ */ jsx2(TenantSwitcher, {}),
|
|
895
1199
|
/* @__PURE__ */ jsx2(AccountMenu, {})
|
|
896
1200
|
] })
|
|
897
1201
|
] }),
|
|
898
|
-
/* @__PURE__ */ jsx2("main", { className: "flex-1 p-4 md:p-6", children: allowed ? /* @__PURE__ */ jsx2(Outlet, {}) : /* @__PURE__ */ jsx2(Forbidden, {}) })
|
|
1202
|
+
/* @__PURE__ */ jsx2("main", { className: "min-w-0 flex-1 p-4 md:p-6", children: allowed ? /* @__PURE__ */ jsx2(Outlet, {}) : /* @__PURE__ */ jsx2(Forbidden, {}) })
|
|
899
1203
|
] }),
|
|
900
1204
|
tenantChanged ? /* @__PURE__ */ jsx2(TenantChanged, {}) : null
|
|
901
1205
|
] });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zjazj/web",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"description": "Browser runtime for platform apps: session, typed API client, layout shell, guards and hooks.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "UNLICENSED",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"@logto/browser": "^3.1.2",
|
|
28
28
|
"hono": "^4.13.17",
|
|
29
29
|
"lucide-react": "^0.545.0",
|
|
30
|
-
"@zjazj/i18n": "^0.0.
|
|
31
|
-
"@zjazj/ui": "^0.0.
|
|
30
|
+
"@zjazj/i18n": "^0.0.7",
|
|
31
|
+
"@zjazj/ui": "^0.0.7"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
34
|
"@tanstack/react-query": "^5.90.0",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"jsdom": "^26.1.0",
|
|
46
46
|
"react": "^19.2.0",
|
|
47
47
|
"react-dom": "^19.2.0",
|
|
48
|
-
"@zjazj/server": "^0.0.
|
|
48
|
+
"@zjazj/server": "^0.0.7"
|
|
49
49
|
},
|
|
50
50
|
"publishConfig": {
|
|
51
51
|
"access": "public",
|