@bevel-software/platform-core-frontend 0.26.0 → 0.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/core/CoreAppShell.tsx +87 -25
- package/src/core/ProductName.tsx +49 -0
- package/src/core/__tests__/ProductName.test.tsx +46 -0
- package/src/core/__tests__/ShellRoutes.test.tsx +22 -0
- package/src/core/__tests__/core-contributions.test.ts +16 -0
- package/src/core/core-contributions.ts +17 -0
- package/src/core/maker-url.ts +30 -0
- package/src/core/registry.ts +78 -1
- package/src/index.css +23 -5
- package/src/index.ts +5 -0
- package/src/lib/__tests__/latest-request.test.ts +48 -0
- package/src/lib/latest-request.ts +45 -0
- package/src/modules/access/__tests__/ManageAccessDialog.no-sso-invite.test.tsx +222 -0
- package/src/modules/access/__tests__/ManageAccessDialog.proposal.test.tsx +1 -1
- package/src/modules/access/api.ts +19 -3
- package/src/modules/access/components/ManageAccessDialog.tsx +61 -8
- package/src/modules/admin/__tests__/AdminRolesPage.fixed-admin.test.tsx +16 -1
- package/src/modules/admin/__tests__/RolesCorruptedBanner.test.tsx +70 -0
- package/src/modules/admin/__tests__/UserAccountsPage.test.tsx +284 -39
- package/src/modules/admin/components/AdminRolesPage.tsx +13 -14
- package/src/modules/admin/components/RolesCorruptedBanner.tsx +9 -4
- package/src/modules/admin/components/UserAccountsPage.tsx +91 -107
- package/src/modules/admin/services/roles.api.ts +14 -1
- package/src/modules/auth/__tests__/LoginScreen.test.tsx +80 -5
- package/src/modules/auth/__tests__/sso-strict-probe.test.ts +66 -0
- package/src/modules/auth/components/LoginScreen.tsx +101 -73
- package/src/modules/auth/services/account.api.ts +59 -3
- package/src/modules/auth/services/sso.ts +30 -10
- package/src/modules/auth/state/use-sign-in-methods.ts +41 -0
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.sync-banner.test.tsx +176 -0
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.unreadable.test.tsx +68 -0
- package/src/modules/change-requests/__tests__/ChangeRequestLink.test.tsx +182 -0
- package/src/modules/change-requests/__tests__/FileChangeBoxes.test.tsx +1 -1
- package/src/modules/change-requests/__tests__/readFileOnBranch.test.ts +79 -0
- package/src/modules/change-requests/components/ChangeBox.tsx +6 -1
- package/src/modules/change-requests/components/ChangeRequestDialog.tsx +45 -15
- package/src/modules/change-requests/components/ChangeRequestLink.tsx +151 -0
- package/src/modules/change-requests/components/CrFileTree.tsx +2 -1
- package/src/modules/change-requests/components/FileChangeBoxes.tsx +4 -2
- package/src/modules/change-requests/hooks/useApplyChangeRequest.ts +6 -3
- package/src/modules/change-requests/services/change-requests.api.ts +15 -2
- package/src/modules/change-requests/utils/readFailure.ts +22 -0
- package/src/modules/embed/__tests__/EmbedLinkPage.test.tsx +74 -0
- package/src/modules/embed/__tests__/EmbedView.test.tsx +473 -0
- package/src/modules/embed/__tests__/embed-config.test.ts +128 -0
- package/src/modules/embed/__tests__/embed-host.test.ts +111 -0
- package/src/modules/embed/__tests__/mcp-app-view.test.ts +295 -0
- package/src/modules/embed/__tests__/mount.test.tsx +157 -0
- package/src/modules/embed/components/EmbedLinkPage.tsx +109 -0
- package/src/modules/embed/components/EmbedView.tsx +545 -0
- package/src/modules/embed/embed-config.ts +132 -0
- package/src/modules/embed/embed-host.ts +120 -0
- package/src/modules/embed/mount.tsx +80 -0
- package/src/modules/embed/services/embed-token.ts +22 -0
- package/src/modules/embed/services/embed.api.ts +158 -0
- package/src/modules/git/__tests__/BranchSwitcher.test.tsx +202 -4
- package/src/modules/git/__tests__/GitSyncFailedBanner.test.tsx +59 -1
- package/src/modules/git/__tests__/PullNeededBanner.test.tsx +39 -22
- package/src/modules/git/__tests__/useAutoPullUpdates.test.tsx +7 -2
- package/src/modules/git/branch-delete-menu-item.tsx +24 -0
- package/src/modules/git/components/BranchSwitcher.tsx +29 -5
- package/src/modules/git/components/GitSyncFailedBanner.tsx +53 -15
- package/src/modules/git/components/HistoryVersionPreview.tsx +5 -3
- package/src/modules/git/components/PullNeededBanner.tsx +15 -49
- package/src/modules/git/hooks/useAutoPullUpdates.ts +9 -5
- package/src/modules/git/index.ts +15 -0
- package/src/modules/git/services/__tests__/error-messages.test.ts +67 -0
- package/src/modules/git/services/__tests__/pull-failure.test.ts +36 -0
- package/src/modules/git/services/error-messages.ts +58 -1
- package/src/modules/git/services/pull-failure.ts +68 -0
- package/src/modules/git/state/branch-delete-confirm.ts +46 -0
- package/src/modules/layout/__tests__/HeaderAlignment.test.tsx +1 -0
- package/src/modules/layout/__tests__/breakpoints.test.ts +7 -1
- package/src/modules/layout/breakpoints.ts +8 -0
- package/src/modules/library/__tests__/PluginExtras.test.tsx +9 -9
- package/src/modules/library/__tests__/PluginPage.test.tsx +8 -1
- package/src/modules/library/__tests__/SkillsTree.test.tsx +13 -4
- package/src/modules/library/components/LibraryPage.tsx +22 -0
- package/src/modules/library/components/PluginExtras.tsx +23 -6
- package/src/modules/library/components/PluginPage.tsx +6 -1
- package/src/modules/library/routes/LibraryRoutes.tsx +8 -7
- package/src/modules/library/services/plugins.api.ts +4 -1
- package/src/modules/library/state/plugins-revision.ts +28 -0
- package/src/modules/onboarding/__tests__/InviteButton.test.tsx +214 -0
- package/src/modules/onboarding/__tests__/agent-connection-api.test.ts +55 -0
- package/src/modules/onboarding/__tests__/creator-welcome.test.tsx +87 -82
- package/src/modules/onboarding/__tests__/first-page-prompt.test.ts +164 -0
- package/src/modules/onboarding/__tests__/get-started-column.test.tsx +1458 -0
- package/src/modules/onboarding/__tests__/invite-dialog.test.tsx +811 -0
- package/src/modules/onboarding/__tests__/invite-emails.test.ts +247 -0
- package/src/modules/onboarding/__tests__/onboarding.test.tsx +226 -193
- package/src/modules/onboarding/__tests__/starter-pack-card.test.tsx +136 -0
- package/src/modules/onboarding/components/CreatorWelcomePage.tsx +7 -11
- package/src/modules/onboarding/components/EmailChipsInput.tsx +138 -0
- package/src/modules/onboarding/components/GetStartedColumn.tsx +670 -0
- package/src/modules/onboarding/components/InviteButton.tsx +139 -0
- package/src/modules/onboarding/components/InviteDialog.tsx +741 -0
- package/src/modules/onboarding/components/RootLanding.tsx +8 -24
- package/src/modules/onboarding/components/StarterPackCard.tsx +98 -0
- package/src/modules/onboarding/components/WelcomePage.tsx +65 -154
- package/src/modules/onboarding/first-page-prompt.ts +123 -0
- package/src/modules/onboarding/invite-emails.ts +313 -0
- package/src/modules/onboarding/services/agent-connection.api.ts +53 -0
- package/src/modules/onboarding/services/starter-packs.api.ts +83 -0
- package/src/modules/onboarding/state/agent-connection.ts +167 -0
- package/src/modules/onboarding/state/invite-dialog.context.ts +35 -0
- package/src/modules/onboarding/state/invite-dialog.tsx +39 -0
- package/src/modules/onboarding/state/onboarding.ts +133 -59
- package/src/modules/onboarding/state/starter-packs.ts +169 -0
- package/src/modules/settings/settings-nav-items.tsx +8 -3
- package/src/modules/setup/__tests__/setup.first-run-storage.test.tsx +389 -0
- package/src/modules/setup/__tests__/setup.github.test.tsx +5 -5
- package/src/modules/setup/__tests__/setup.kept-draft.test.tsx +1 -1
- package/src/modules/setup/__tests__/setup.repository-change.test.tsx +1 -1
- package/src/modules/setup/__tests__/setup.repository.test.tsx +21 -21
- package/src/modules/setup/__tests__/setup.test.tsx +56 -15
- package/src/modules/setup/__tests__/useConnectionProbe.test.tsx +123 -0
- package/src/modules/setup/components/FirstRunStorage.tsx +560 -0
- package/src/modules/setup/components/SetupGate.tsx +46 -1
- package/src/modules/setup/components/SetupScreen.tsx +81 -84
- package/src/modules/setup/hooks/useConnectionProbe.ts +93 -0
- package/src/modules/setup/services/setup.api.ts +3 -3
- package/src/modules/setup/utils/suggested-branch.ts +27 -0
- package/src/modules/toolbar/commands/__tests__/actions.test.tsx +210 -0
- package/src/modules/toolbar/commands/actions.tsx +330 -0
- package/src/modules/toolbar/commands/command-menu.ts +44 -0
- package/src/modules/toolbar/commands/useCommandActions.ts +71 -0
- package/src/modules/toolbar/commands/useCommandShortcuts.ts +121 -0
- package/src/modules/toolbar/components/AppSwitcher.tsx +125 -22
- package/src/modules/toolbar/components/ExternalAgentAccessPage.tsx +1 -1
- package/src/modules/toolbar/components/ProfileMenu.tsx +9 -4
- package/src/modules/toolbar/components/SearchPalette.tsx +540 -0
- package/src/modules/toolbar/components/Toolbar.tsx +10 -0
- package/src/modules/toolbar/components/__tests__/AppSwitcher.test.tsx +230 -52
- package/src/modules/toolbar/components/__tests__/ProfileMenu.test.tsx +64 -0
- package/src/modules/toolbar/components/__tests__/SearchPalette.test.tsx +864 -0
- package/src/modules/toolbar/components/__tests__/Toolbar.test.tsx +57 -2
- package/src/modules/toolbar/search/__tests__/rank.test.ts +93 -0
- package/src/modules/toolbar/search/__tests__/sources.test.ts +131 -0
- package/src/modules/toolbar/search/rank.ts +111 -0
- package/src/modules/toolbar/search/sources.ts +169 -0
- package/src/modules/workflow/hooks/useFileLock.ts +10 -7
- package/src/modules/workflow/services/__tests__/lock.api.test.ts +41 -0
- package/src/modules/workflow/services/lock.api.ts +17 -2
- package/src/modules/workspace/__tests__/useWorkspaceState.test.ts +49 -1
- package/src/modules/workspace/components/EditorTabs.tsx +39 -15
- package/src/modules/workspace/components/FileExplorer.tsx +39 -6
- package/src/modules/workspace/components/FilePaneCard.tsx +6 -2
- package/src/modules/workspace/components/FileRoute.tsx +12 -7
- package/src/modules/workspace/components/FileViewer.tsx +271 -74
- package/src/modules/workspace/components/KbPageHeader.tsx +13 -6
- package/src/modules/workspace/components/__tests__/EditorTabs.test.tsx +88 -21
- package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +92 -4
- package/src/modules/workspace/components/__tests__/FileRoute.test.tsx +23 -3
- package/src/modules/workspace/components/__tests__/FileViewer.test.tsx +330 -6
- package/src/modules/workspace/components/__tests__/KbPageHeader.test.tsx +17 -2
- package/src/modules/workspace/components/renderers/DocxRenderer.tsx +11 -16
- package/src/modules/workspace/components/renderers/DownloadFileButton.tsx +6 -0
- package/src/modules/workspace/components/renderers/EmailRenderer.tsx +23 -21
- package/src/modules/workspace/components/renderers/ImageRenderer.tsx +13 -29
- package/src/modules/workspace/components/renderers/KbMarkdownView.tsx +9 -2
- package/src/modules/workspace/components/renderers/MarkdownRenderer.tsx +8 -0
- package/src/modules/workspace/components/renderers/PdfRenderer.tsx +14 -19
- package/src/modules/workspace/components/renderers/PptxRenderer.tsx +11 -17
- package/src/modules/workspace/components/renderers/ToolRenderer.tsx +35 -7
- package/src/modules/workspace/components/renderers/XlsxRenderer.tsx +11 -17
- package/src/modules/workspace/components/renderers/__tests__/EmailRenderer.links.test.tsx +73 -0
- package/src/modules/workspace/components/renderers/__tests__/ToolRenderer.scaffold.test.tsx +64 -0
- package/src/modules/workspace/components/renderers/__tests__/ToolRenderer.surface.test.tsx +82 -0
- package/src/modules/workspace/components/renderers/__tests__/bridgeHarness.ts +99 -0
- package/src/modules/workspace/components/renderers/__tests__/htmlSandbox.test.ts +129 -35
- package/src/modules/workspace/components/renderers/__tests__/htmlViewsGuide.test.ts +149 -0
- package/src/modules/workspace/components/renderers/__tests__/kbMarkdownLinkPolicy.test.tsx +100 -0
- package/src/modules/workspace/components/renderers/htmlSandbox.ts +76 -20
- package/src/modules/workspace/components/renderers/index.ts +26 -0
- package/src/modules/workspace/components/renderers/kbMarkdownPipeline.tsx +51 -1
- package/src/modules/workspace/components/renderers/rendererRawRead.ts +53 -0
- package/src/modules/workspace/components/renderers/rendererSurface.ts +109 -0
- package/src/modules/workspace/components/renderers/rendererWorkspace.ts +8 -2
- package/src/modules/workspace/hooks/__tests__/useCreatePage.test.ts +137 -0
- package/src/modules/workspace/hooks/__tests__/useKnowledgeWriteTarget.test.ts +302 -0
- package/src/modules/workspace/hooks/__tests__/useWorkspaceState.test.tsx +126 -21
- package/src/modules/workspace/hooks/useCreatePage.ts +206 -0
- package/src/modules/workspace/hooks/useKnowledgeWriteTarget.ts +238 -0
- package/src/modules/workspace/hooks/useWorkspaceImageResolver.ts +24 -7
- package/src/modules/workspace/hooks/useWorkspaceState.ts +42 -8
- package/src/modules/workspace/routing/__tests__/kb-routes.test.tsx +10 -0
- package/src/modules/workspace/routing/__tests__/md-links-parity.test.tsx +113 -0
- package/src/modules/workspace/routing/kb-routes.ts +66 -12
- package/src/modules/workspace/state/editable-page.ts +42 -0
- package/src/modules/workspace/state/workspace.context.ts +13 -4
- package/src/modules/workspace/utils/__tests__/display-file-name.test.ts +60 -0
- package/src/modules/workspace/utils/__tests__/fileTree.test.ts +75 -0
- package/src/modules/workspace/utils/__tests__/treeConfirm.test.ts +16 -0
- package/src/modules/workspace/utils/display-file-name.ts +102 -0
- package/src/modules/workspace/utils/fileTree.ts +49 -14
- package/src/modules/workspace/utils/treeConfirm.ts +15 -0
- package/src/shared/__tests__/no-builtin-confirm.test.ts +102 -0
- package/src/shared/components/ConfirmProvider.tsx +156 -0
- package/src/shared/components/Dialog.tsx +7 -0
- package/src/shared/components/__tests__/ConfirmProvider.test.tsx +198 -0
- package/src/shared/components/confirm-context.ts +94 -0
- package/src/shared/components/index.ts +3 -0
- package/src/modules/onboarding/components/WelcomeRoute.tsx +0 -57
- package/src/modules/onboarding/welcome-state.ts +0 -36
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { EMAIL_RE } from '../../lib/email';
|
|
2
|
+
|
|
3
|
+
/** Commas, semicolons and any whitespace separate; lower-cased, empty parts dropped. */
|
|
4
|
+
function tokens(text: string): string[] {
|
|
5
|
+
return text
|
|
6
|
+
.split(/[\s,;]+/)
|
|
7
|
+
.map((part) => part.trim().toLowerCase())
|
|
8
|
+
.filter(Boolean);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Split typed or pasted text into addresses. Commas, semicolons and any
|
|
13
|
+
* whitespace separate; a `Name <address>` entry — what a mail client puts on
|
|
14
|
+
* the clipboard when you copy a recipient list — contributes its address.
|
|
15
|
+
* Lower-cased, because the server treats addresses that way and a chip for
|
|
16
|
+
* `Juan@` beside one for `juan@` would be one person invited twice.
|
|
17
|
+
*
|
|
18
|
+
* A display name can hold the very separators the list uses: Outlook copies
|
|
19
|
+
* `Doe, Jane <jane@example.com>`, others quote it (`"Doe, Jane" <…>`). So a
|
|
20
|
+
* quoted name goes first, and the text before an angle-bracketed address is
|
|
21
|
+
* read as its name — only the parts of it that are addresses themselves (a
|
|
22
|
+
* bare one earlier in the list) survive. Text after the last bracketed entry
|
|
23
|
+
* names nothing, so it splits as typed, a mistyped address included, and
|
|
24
|
+
* shows as the invalid chip it is.
|
|
25
|
+
*/
|
|
26
|
+
export function splitEmails(text: string): string[] {
|
|
27
|
+
const unquoted = text.replace(/"[^"]*"/g, ' ');
|
|
28
|
+
const out: string[] = [];
|
|
29
|
+
let rest = 0;
|
|
30
|
+
for (const entry of unquoted.matchAll(/<([^<>]*)>/g)) {
|
|
31
|
+
out.push(...tokens(unquoted.slice(rest, entry.index)).filter((part) => part.includes('@')));
|
|
32
|
+
out.push(...tokens(entry[1] ?? ''));
|
|
33
|
+
rest = entry.index + entry[0].length;
|
|
34
|
+
}
|
|
35
|
+
out.push(...tokens(unquoted.slice(rest)));
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function isValidEmail(value: string): boolean {
|
|
40
|
+
return EMAIL_RE.test(value);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type InviteRole = 'member' | 'admin';
|
|
44
|
+
|
|
45
|
+
/** The Admin role's canonical name in `roles.yaml` (the backend's `ADMIN_CANONICAL`). */
|
|
46
|
+
const ADMIN_ROLE = 'admin';
|
|
47
|
+
|
|
48
|
+
/** What became of one address. */
|
|
49
|
+
export type InviteOutcome =
|
|
50
|
+
/**
|
|
51
|
+
* `passwordSet`: the account was created with the starting password.
|
|
52
|
+
* `notified`: how the deployment told the person of the invite (`'email'`
|
|
53
|
+
* from the cloud's mail, say), as the server's listeners answered; absent
|
|
54
|
+
* when nobody did and the admin hands over the link themselves.
|
|
55
|
+
*/
|
|
56
|
+
| { email: string; status: 'created'; role: InviteRole; roleError?: string; passwordSet?: boolean; notified?: string[] }
|
|
57
|
+
/**
|
|
58
|
+
* Already had an account. Invited as Admin, they are made one too:
|
|
59
|
+
* `promoted` when this call did it, `alreadyAdmin` when they were one.
|
|
60
|
+
* `deactivated`: the account is switched off and cannot sign in until an
|
|
61
|
+
* admin switches it back on. Inviting does not do that — switching on asks
|
|
62
|
+
* the deployment for a seat, and is the User accounts page's decision —
|
|
63
|
+
* and leaves it entirely unchanged: no password, and no Admin role either.
|
|
64
|
+
*
|
|
65
|
+
* With a starting password: `passwordSet` when the account had none and
|
|
66
|
+
* now has it, `hasOwnPassword` when it already had one (never replaced),
|
|
67
|
+
* `passwordError` when setting it failed — the account is then left
|
|
68
|
+
* exactly as it was, Admin promotion included.
|
|
69
|
+
*/
|
|
70
|
+
| {
|
|
71
|
+
email: string;
|
|
72
|
+
status: 'existing';
|
|
73
|
+
promoted?: boolean;
|
|
74
|
+
alreadyAdmin?: boolean;
|
|
75
|
+
roleError?: string;
|
|
76
|
+
deactivated?: boolean;
|
|
77
|
+
passwordSet?: boolean;
|
|
78
|
+
hasOwnPassword?: boolean;
|
|
79
|
+
passwordError?: string;
|
|
80
|
+
}
|
|
81
|
+
/** The deployment's admission rules had no place for the account (a seat limit, say). */
|
|
82
|
+
| { email: string; status: 'no-seat'; message: string }
|
|
83
|
+
| { email: string; status: 'error'; message: string };
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* What a send came to: one outcome per address, or — when a starting
|
|
87
|
+
* password was given and the account list could not be read — nothing sent
|
|
88
|
+
* at all (see {@link sendInvites}).
|
|
89
|
+
*/
|
|
90
|
+
export type InviteResult =
|
|
91
|
+
| { status: 'sent'; outcomes: InviteOutcome[] }
|
|
92
|
+
| { status: 'accounts-unreadable' };
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The address now has an account it can sign in to — new, or already there
|
|
96
|
+
* and switched on. One whose starting password could not be set still can
|
|
97
|
+
* when the deployment has single sign-on (`sso`).
|
|
98
|
+
*/
|
|
99
|
+
export function isInvited(outcome: InviteOutcome, sso = false): boolean {
|
|
100
|
+
return (
|
|
101
|
+
outcome.status === 'created' ||
|
|
102
|
+
(outcome.status === 'existing' && !outcome.deactivated && (!outcome.passwordError || sso))
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The account now signs in with the starting password the admin typed. */
|
|
107
|
+
export function gotPassword(outcome: InviteOutcome): boolean {
|
|
108
|
+
return (outcome.status === 'created' || outcome.status === 'existing') && Boolean(outcome.passwordSet);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The deployment emailed this person the invite itself, so the admin need not send them the link. */
|
|
112
|
+
export function wasEmailed(outcome: InviteOutcome): boolean {
|
|
113
|
+
return outcome.status === 'created' && (outcome.notified?.includes('email') ?? false);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** One row of the account list, as much of it as inviting needs. */
|
|
117
|
+
interface ListedAccount {
|
|
118
|
+
email: string;
|
|
119
|
+
deactivatedAt?: string | null;
|
|
120
|
+
hasPassword?: boolean;
|
|
121
|
+
/** The deployment admin signs in with the environment password, hash or not. */
|
|
122
|
+
isEnvAdmin?: boolean;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The server refused a promotion because the account is switched off (`addMember`'s `ifActive`). */
|
|
126
|
+
function isSwitchedOff(err: unknown): boolean {
|
|
127
|
+
return (err as { kind?: unknown } | null)?.kind === 'deactivated';
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Invite each address (`POST /api/admin/accounts`): an account with the
|
|
132
|
+
* starting password when the admin gave one, and otherwise with none — then
|
|
133
|
+
* the person signs in with single sign-on, or not at all until someone sets
|
|
134
|
+
* a password, which is why the dialog asks for one on a deployment without
|
|
135
|
+
* single sign-on.
|
|
136
|
+
*
|
|
137
|
+
* One at a time, not in parallel. Each create asks the deployment's admission
|
|
138
|
+
* rules for a place, and a host that sells seats answers from a count —
|
|
139
|
+
* concurrent asks would race that count, and the order the admin typed is the
|
|
140
|
+
* order the seats should go in.
|
|
141
|
+
*
|
|
142
|
+
* The create endpoint is an upsert, so it cannot say "this person was already
|
|
143
|
+
* here". The account list read first can, and an address already on it is
|
|
144
|
+
* not created again: re-creating is not free (it asks for a seat again, and
|
|
145
|
+
* a full plan refuses somebody who already HAS one). The list holds
|
|
146
|
+
* switched-off accounts too, and those are reported as such rather than as
|
|
147
|
+
* able to sign in, and never written.
|
|
148
|
+
*
|
|
149
|
+
* A starting password is sent with `keepExistingPassword`: the server gives
|
|
150
|
+
* it only to a new account or a switched-on one with no password, testing
|
|
151
|
+
* and writing in one statement, and says whether it did. So someone's own
|
|
152
|
+
* password is never replaced, even one set after the list was read. The list
|
|
153
|
+
* still decides what each row says, and an account it shows with a password
|
|
154
|
+
* (or the deployment admin, who signs in with the environment's) is not
|
|
155
|
+
* written at all. A list that cannot be read stops a send with a password
|
|
156
|
+
* before any write: the rows could not say who already had an account.
|
|
157
|
+
* Without a password an unreadable list just means every address is treated
|
|
158
|
+
* as new, as before.
|
|
159
|
+
*
|
|
160
|
+
* Only a refusal the server marks as an admission refusal
|
|
161
|
+
* (`kind: 'admission'`) is "no seat left". Any other is an error in the
|
|
162
|
+
* server's own words — the route's admin check answers 403 as well, for an
|
|
163
|
+
* admin verdict gone stale in another tab.
|
|
164
|
+
*
|
|
165
|
+
* "Admin" is a role membership, not an account property, so it is a second
|
|
166
|
+
* write after the account's. Somebody who already had an account is made an
|
|
167
|
+
* Admin too when invited as one: the admin asked for that person to be an
|
|
168
|
+
* Admin, and that they could already sign in does not change the request.
|
|
169
|
+
* A switched-off account is the exception: it is left unchanged, so it is
|
|
170
|
+
* not made an Admin either. The list says who was switched off when it was
|
|
171
|
+
* read; the promotion itself is sent `ifActive`, so the server refuses one
|
|
172
|
+
* switched off since, at the write, and the row says "switched off".
|
|
173
|
+
* The roster is read first so an existing Admin (including one the server
|
|
174
|
+
* configuration fixes) is reported as such rather than written again. A
|
|
175
|
+
* refused promotion leaves the account standing as it was and says so on
|
|
176
|
+
* the row.
|
|
177
|
+
*
|
|
178
|
+
* The password goes to the server and nowhere else: no outcome carries it.
|
|
179
|
+
*/
|
|
180
|
+
export async function sendInvites(
|
|
181
|
+
emails: string[],
|
|
182
|
+
role: InviteRole,
|
|
183
|
+
api: {
|
|
184
|
+
listAccounts(): Promise<ListedAccount[]>;
|
|
185
|
+
createAccount(
|
|
186
|
+
email: string,
|
|
187
|
+
name: string,
|
|
188
|
+
password?: string,
|
|
189
|
+
options?: { keepExistingPassword?: boolean },
|
|
190
|
+
): Promise<{ passwordSet?: boolean; deactivated?: boolean; notified?: string[] } | void>;
|
|
191
|
+
addMember(canonical: string, email: string, options?: { ifActive?: boolean }): Promise<unknown>;
|
|
192
|
+
fetchRoles(): Promise<{ canonical: string; members: string[]; fixedMembers?: string[] }[]>;
|
|
193
|
+
},
|
|
194
|
+
options: { password?: string } = {},
|
|
195
|
+
): Promise<InviteResult> {
|
|
196
|
+
const password = options.password || undefined;
|
|
197
|
+
/** Every address with an account. Unreadable means none known. */
|
|
198
|
+
let existing: Map<string, ListedAccount> | null = null;
|
|
199
|
+
try {
|
|
200
|
+
existing = new Map((await api.listAccounts()).map((a) => [a.email.toLowerCase(), a]));
|
|
201
|
+
} catch {
|
|
202
|
+
existing = null;
|
|
203
|
+
}
|
|
204
|
+
if (!existing && password) return { status: 'accounts-unreadable' };
|
|
205
|
+
/** Who is an Admin already; asked only when someone is to be made one. Unreadable means unknown. */
|
|
206
|
+
let admins: Set<string> | null = null;
|
|
207
|
+
if (role === 'admin') {
|
|
208
|
+
try {
|
|
209
|
+
const entry = (await api.fetchRoles()).find((r) => r.canonical === ADMIN_ROLE);
|
|
210
|
+
admins = new Set([...(entry?.members ?? []), ...(entry?.fixedMembers ?? [])].map((e) => e.toLowerCase()));
|
|
211
|
+
} catch {
|
|
212
|
+
admins = null;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
const outcomes: InviteOutcome[] = [];
|
|
216
|
+
for (const email of emails) {
|
|
217
|
+
const account = existing?.get(email.toLowerCase());
|
|
218
|
+
if (account) {
|
|
219
|
+
let off: { deactivated?: true } = account.deactivatedAt ? { deactivated: true } : {};
|
|
220
|
+
const ownPassword = Boolean(account.hasPassword || account.isEnvAdmin);
|
|
221
|
+
let passwordFlags: { passwordSet?: boolean; hasOwnPassword?: boolean } = {};
|
|
222
|
+
if (password && !account.deactivatedAt) {
|
|
223
|
+
if (ownPassword) {
|
|
224
|
+
passwordFlags = { hasOwnPassword: true };
|
|
225
|
+
} else {
|
|
226
|
+
try {
|
|
227
|
+
const reply = await api.createAccount(email, '', password, { keepExistingPassword: true });
|
|
228
|
+
// Not set: since the list was read it was switched off, or got a password of its own.
|
|
229
|
+
if (reply?.passwordSet !== false) passwordFlags = { passwordSet: true };
|
|
230
|
+
else if (reply.deactivated) off = { deactivated: true };
|
|
231
|
+
else passwordFlags = { hasOwnPassword: true };
|
|
232
|
+
} catch (err) {
|
|
233
|
+
const passwordError = err instanceof Error ? err.message : 'Could not set the password';
|
|
234
|
+
outcomes.push({ email, status: 'existing', passwordError });
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
} else if (ownPassword && !account.deactivatedAt) {
|
|
239
|
+
passwordFlags = { hasOwnPassword: true };
|
|
240
|
+
}
|
|
241
|
+
const base = { email, status: 'existing' as const, ...off, ...passwordFlags };
|
|
242
|
+
if (role !== 'admin' || off.deactivated) {
|
|
243
|
+
outcomes.push(base);
|
|
244
|
+
} else if (admins?.has(email.toLowerCase())) {
|
|
245
|
+
outcomes.push({ ...base, alreadyAdmin: true });
|
|
246
|
+
} else {
|
|
247
|
+
try {
|
|
248
|
+
await api.addMember(ADMIN_ROLE, email, { ifActive: true });
|
|
249
|
+
outcomes.push({ ...base, promoted: true });
|
|
250
|
+
} catch (err) {
|
|
251
|
+
if (isSwitchedOff(err)) {
|
|
252
|
+
outcomes.push({ ...base, deactivated: true });
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
const roleError = err instanceof Error ? err.message : 'Could not make them an admin';
|
|
256
|
+
outcomes.push({ ...base, roleError });
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
let created: { passwordSet?: boolean; notified?: string[] } = {};
|
|
262
|
+
/**
|
|
263
|
+
* Not on the list, but there by the time of the write — with its own
|
|
264
|
+
* password, or switched off — and left as it was.
|
|
265
|
+
*/
|
|
266
|
+
let appeared: { hasOwnPassword: true } | { deactivated: true } | null = null;
|
|
267
|
+
/** How the deployment told the person, when its listeners did (a new account only). */
|
|
268
|
+
const told = (reply: { notified?: string[] } | void): { notified?: string[] } =>
|
|
269
|
+
reply?.notified && reply.notified.length > 0 ? { notified: reply.notified } : {};
|
|
270
|
+
try {
|
|
271
|
+
if (password) {
|
|
272
|
+
const reply = await api.createAccount(email, '', password, { keepExistingPassword: true });
|
|
273
|
+
if (reply?.passwordSet === false) appeared = reply.deactivated ? { deactivated: true } : { hasOwnPassword: true };
|
|
274
|
+
else created = { passwordSet: true, ...told(reply) };
|
|
275
|
+
} else {
|
|
276
|
+
created = told(await api.createAccount(email, ''));
|
|
277
|
+
}
|
|
278
|
+
} catch (err) {
|
|
279
|
+
const message = err instanceof Error ? err.message : 'Could not create account';
|
|
280
|
+
const kind = (err as { kind?: unknown } | null)?.kind;
|
|
281
|
+
outcomes.push(
|
|
282
|
+
kind === 'admission' ? { email, status: 'no-seat', message } : { email, status: 'error', message },
|
|
283
|
+
);
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
if (role !== 'admin' || (appeared && 'deactivated' in appeared)) {
|
|
287
|
+
outcomes.push(
|
|
288
|
+
appeared ? { email, status: 'existing', ...appeared } : { email, status: 'created', role, ...created },
|
|
289
|
+
);
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
try {
|
|
293
|
+
await api.addMember(ADMIN_ROLE, email, { ifActive: true });
|
|
294
|
+
outcomes.push(
|
|
295
|
+
appeared
|
|
296
|
+
? { email, status: 'existing', ...appeared, promoted: true }
|
|
297
|
+
: { email, status: 'created', role: 'admin', ...created },
|
|
298
|
+
);
|
|
299
|
+
} catch (err) {
|
|
300
|
+
if (isSwitchedOff(err)) {
|
|
301
|
+
outcomes.push({ email, status: 'existing', deactivated: true });
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
const roleError = err instanceof Error ? err.message : 'Could not make them an admin';
|
|
305
|
+
outcomes.push(
|
|
306
|
+
appeared
|
|
307
|
+
? { email, status: 'existing', ...appeared, roleError }
|
|
308
|
+
: { email, status: 'created', role: 'member', roleError, ...created },
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return { status: 'sent', outcomes };
|
|
313
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { authFetch } from '../../../lib/api';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Whether one of the signed-in person's agents has reached the platform —
|
|
5
|
+
* `GET /api/onboarding/agent-connection`. MIRRORS the backend's
|
|
6
|
+
* `AgentConnectionResponse` (`modules/audit/agent-connection.routes.ts`).
|
|
7
|
+
*
|
|
8
|
+
* "Connected" means an agent made an authenticated call with one of the
|
|
9
|
+
* person's live connections or keys, which every client does the moment it
|
|
10
|
+
* connects (it lists the tools) — not that it has used a tool yet.
|
|
11
|
+
*/
|
|
12
|
+
export interface AgentConnection {
|
|
13
|
+
connected: boolean;
|
|
14
|
+
/** The agent's most recent call, ISO 8601. Present once connected. */
|
|
15
|
+
at?: string;
|
|
16
|
+
/** The agent's registered client name ("Claude"), or the connection key's label. */
|
|
17
|
+
client?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Which of the two `client` is: `agent` for an OAuth connection's
|
|
20
|
+
* registered name, `key` for a connection key's free-text label.
|
|
21
|
+
*/
|
|
22
|
+
kind?: 'agent' | 'key';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function optionalString(value: unknown): value is string | undefined {
|
|
26
|
+
return value === undefined || typeof value === 'string';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Throws on a refusal or a malformed body; callers read any failure as "not
|
|
31
|
+
* yet". Checked field by field rather than cast: a body that is not the
|
|
32
|
+
* documented shape is a failure, not a quiet "not connected" — and a
|
|
33
|
+
* "connected" with a stray `client` or `kind` must not steer the first-page
|
|
34
|
+
* step's links.
|
|
35
|
+
*/
|
|
36
|
+
export async function fetchAgentConnection(): Promise<AgentConnection> {
|
|
37
|
+
const res = await authFetch('/api/onboarding/agent-connection');
|
|
38
|
+
if (!res.ok) throw new Error(`agent-connection: ${res.status}`);
|
|
39
|
+
const body: unknown = await res.json();
|
|
40
|
+
if (typeof body !== 'object' || body === null || typeof (body as { connected?: unknown }).connected !== 'boolean') {
|
|
41
|
+
throw new Error('agent-connection: malformed body');
|
|
42
|
+
}
|
|
43
|
+
const { connected, at, client, kind } = body as Record<string, unknown>;
|
|
44
|
+
if (connected === false) return { connected: false };
|
|
45
|
+
if (
|
|
46
|
+
!optionalString(at) ||
|
|
47
|
+
!optionalString(client) ||
|
|
48
|
+
(kind !== undefined && kind !== 'agent' && kind !== 'key')
|
|
49
|
+
) {
|
|
50
|
+
throw new Error('agent-connection: malformed body');
|
|
51
|
+
}
|
|
52
|
+
return { connected: true, at, client, kind };
|
|
53
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { authFetch } from '../../../lib/api';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The starter-pack question — `GET /api/onboarding/starter-packs` and
|
|
5
|
+
* `POST /api/onboarding/starter-pack`. MIRRORS the backend's
|
|
6
|
+
* `StarterPacksAnswer` / `StarterPackApplied`
|
|
7
|
+
* (`modules/onboarding/starter-pack.service.ts`).
|
|
8
|
+
*/
|
|
9
|
+
export interface StarterPackSummary {
|
|
10
|
+
id: string;
|
|
11
|
+
/** The chip's label. */
|
|
12
|
+
name: string;
|
|
13
|
+
description: string;
|
|
14
|
+
order: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface ChosenStarterPack {
|
|
18
|
+
id: string;
|
|
19
|
+
name: string;
|
|
20
|
+
/** The team's own "write your first page" request. */
|
|
21
|
+
firstPagePrompt: string;
|
|
22
|
+
/** The pack's pages still exactly as it wrote them (workspace-relative): placeholders, not pages. */
|
|
23
|
+
starterPages: string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface StarterPacksAnswer {
|
|
27
|
+
/** Ask this person: an admin, nobody has answered, the knowledge base is still new. */
|
|
28
|
+
offered: boolean;
|
|
29
|
+
/** A pack's id, `none` for a skip, or null while unanswered. */
|
|
30
|
+
chosen: string | null;
|
|
31
|
+
packs: StarterPackSummary[];
|
|
32
|
+
chosenPack: ChosenStarterPack | null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface StarterPackApplied {
|
|
36
|
+
id: string;
|
|
37
|
+
name: string | null;
|
|
38
|
+
pages: number;
|
|
39
|
+
skills: number;
|
|
40
|
+
/** "Added 4 pages and 4 skills for Engineering." — empty for a skip. */
|
|
41
|
+
summary: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The id that answers "Skip, I'll start from scratch". */
|
|
45
|
+
export const SKIP_STARTER_PACK = 'none';
|
|
46
|
+
|
|
47
|
+
/** A refusal, with the server's own sentence. */
|
|
48
|
+
export class StarterPackApiError extends Error {
|
|
49
|
+
readonly status: number;
|
|
50
|
+
constructor(message: string, status: number) {
|
|
51
|
+
super(message);
|
|
52
|
+
this.name = 'StarterPackApiError';
|
|
53
|
+
this.status = status;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function errorOf(res: Response, fallback: string): Promise<StarterPackApiError> {
|
|
58
|
+
const body = (await res.json().catch(() => null)) as { error?: unknown } | null;
|
|
59
|
+
return new StarterPackApiError(typeof body?.error === 'string' ? body.error : fallback, res.status);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export async function fetchStarterPacks(): Promise<StarterPacksAnswer> {
|
|
63
|
+
const res = await authFetch('/api/onboarding/starter-packs');
|
|
64
|
+
if (!res.ok) throw await errorOf(res, `starter-packs: ${res.status}`);
|
|
65
|
+
const body = (await res.json()) as Partial<StarterPacksAnswer>;
|
|
66
|
+
return {
|
|
67
|
+
offered: body.offered === true,
|
|
68
|
+
chosen: typeof body.chosen === 'string' ? body.chosen : null,
|
|
69
|
+
packs: Array.isArray(body.packs) ? body.packs : [],
|
|
70
|
+
chosenPack: body.chosenPack ?? null,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Choose `id` (or {@link SKIP_STARTER_PACK}). Throws {@link StarterPackApiError} with the server's reason. */
|
|
75
|
+
export async function chooseStarterPack(id: string): Promise<StarterPackApplied> {
|
|
76
|
+
const res = await authFetch('/api/onboarding/starter-pack', {
|
|
77
|
+
method: 'POST',
|
|
78
|
+
headers: { 'Content-Type': 'application/json' },
|
|
79
|
+
body: JSON.stringify({ id }),
|
|
80
|
+
});
|
|
81
|
+
if (!res.ok) throw await errorOf(res, 'Couldn’t add the starter pages. Try again.');
|
|
82
|
+
return (await res.json()) as StarterPackApplied;
|
|
83
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { useContext, useEffect, useState, useSyncExternalStore } from 'react';
|
|
2
|
+
import { AuthContext } from '../../auth/state/auth.context';
|
|
3
|
+
import { useEventBus } from '../../workflow/state/event-bus.context';
|
|
4
|
+
import { fetchAgentConnection, type AgentConnection } from '../services/agent-connection.api';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* "Is your agent connected yet?", shared by the connect-your-agent page and
|
|
8
|
+
* the Get set up list.
|
|
9
|
+
*
|
|
10
|
+
* The server SAYS when it happens: an agent's first authenticated request
|
|
11
|
+
* stamps its connection or key, and the stamp paths emit `agent-connected`
|
|
12
|
+
* for the owner over the event stream every tab holds. So this hook listens,
|
|
13
|
+
* and asks the server only on arrival and when the person comes back to the
|
|
14
|
+
* tab — for an agent that connected before this tab existed, or while its
|
|
15
|
+
* stream was down. Never on a timer.
|
|
16
|
+
*
|
|
17
|
+
* A "yes" is remembered for the session, PER SIGNED-IN USER — the record
|
|
18
|
+
* hangs off the auth context's user object, which the session holds for as
|
|
19
|
+
* long as that person is signed in — and every mounted reader hears it: the
|
|
20
|
+
* page that saw the agent arrive hands the answer to the list it navigates
|
|
21
|
+
* back to, instead of the list showing "not yet" until its own request
|
|
22
|
+
* comes back. A "not yet" is never remembered — it is exactly the answer
|
|
23
|
+
* expected to change. A fresh session starts from nothing.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The shortest gap between two asks prompted by coming back to the tab:
|
|
28
|
+
* switching windows a dozen times a minute is not a dozen questions.
|
|
29
|
+
*/
|
|
30
|
+
export const AGENT_RECHECK_MS = 30_000;
|
|
31
|
+
|
|
32
|
+
interface Known {
|
|
33
|
+
connection: AgentConnection | null;
|
|
34
|
+
version: number;
|
|
35
|
+
listeners: Set<() => void>;
|
|
36
|
+
subscribe(listener: () => void): () => void;
|
|
37
|
+
snapshot(): number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const known = new WeakMap<object, Known>();
|
|
41
|
+
|
|
42
|
+
function knownFor(user: object): Known {
|
|
43
|
+
const found = known.get(user);
|
|
44
|
+
if (found) return found;
|
|
45
|
+
const record: Known = {
|
|
46
|
+
connection: null,
|
|
47
|
+
version: 0,
|
|
48
|
+
listeners: new Set(),
|
|
49
|
+
subscribe(listener) {
|
|
50
|
+
record.listeners.add(listener);
|
|
51
|
+
return () => record.listeners.delete(listener);
|
|
52
|
+
},
|
|
53
|
+
snapshot: () => record.version,
|
|
54
|
+
};
|
|
55
|
+
known.set(user, record);
|
|
56
|
+
return record;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function rememberConnected(record: Known, connection: AgentConnection): void {
|
|
60
|
+
record.connection = connection;
|
|
61
|
+
record.version++;
|
|
62
|
+
record.listeners.forEach((l) => l());
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const nobody = {
|
|
66
|
+
subscribe: () => () => {},
|
|
67
|
+
snapshot: () => 0,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export interface AgentConnectionState extends AgentConnection {
|
|
71
|
+
/** The first answer is in (a failed request counts as "not yet"). */
|
|
72
|
+
settled: boolean;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const UNKNOWN: AgentConnectionState = { connected: false, settled: false };
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The signed-in person's agent connection.
|
|
79
|
+
*
|
|
80
|
+
* Told by the server's `agent-connected` event the moment an agent of theirs
|
|
81
|
+
* makes its first call. Asked once on mount, and again when the person comes
|
|
82
|
+
* back to the tab — at most once per {@link AGENT_RECHECK_MS} — so an agent
|
|
83
|
+
* connected before this tab opened, or while its event stream was down, still
|
|
84
|
+
* counts. A failed ask reads as "not yet".
|
|
85
|
+
*
|
|
86
|
+
* Nothing is asked or listened for while `enabled` is false, or signed out.
|
|
87
|
+
*/
|
|
88
|
+
export function useAgentConnection({ enabled = true }: { enabled?: boolean } = {}): AgentConnectionState {
|
|
89
|
+
const auth = useContext(AuthContext);
|
|
90
|
+
const user = auth?.user ?? null;
|
|
91
|
+
const userId = user?.id ?? null;
|
|
92
|
+
const record = user ? knownFor(user) : null;
|
|
93
|
+
const bus = useEventBus();
|
|
94
|
+
useSyncExternalStore(record?.subscribe ?? nobody.subscribe, record?.snapshot ?? nobody.snapshot, nobody.snapshot);
|
|
95
|
+
// Keyed by the record, so a tab that switches accounts never shows one
|
|
96
|
+
// person's answer to the next.
|
|
97
|
+
const [answer, setAnswer] = useState<{ record: Known; state: AgentConnectionState } | null>(null);
|
|
98
|
+
|
|
99
|
+
// The server's word, the moment it happens. The bus is already filtered to
|
|
100
|
+
// this account's sessions; the check here covers a tab that switched
|
|
101
|
+
// accounts while the subscription stood. An event older than what is
|
|
102
|
+
// remembered (a replay on reconnect, a key's first use landing after a
|
|
103
|
+
// newer connection's) changes nothing: the newest use names the client the
|
|
104
|
+
// first-page prompt should address.
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
if (!enabled || !record || !userId || !bus) return;
|
|
107
|
+
return bus.subscribe('agent-connected', (event) => {
|
|
108
|
+
if (event.forUserId !== userId) return;
|
|
109
|
+
const current = record.connection;
|
|
110
|
+
if (current?.at && event.at <= current.at) return;
|
|
111
|
+
rememberConnected(record, { connected: true, at: event.at, client: event.client, kind: event.agentKind });
|
|
112
|
+
});
|
|
113
|
+
}, [enabled, record, userId, bus]);
|
|
114
|
+
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
// Same gate as the listener above: no account to ask about, no ask.
|
|
117
|
+
if (!enabled || !record || !userId || record.connection) return;
|
|
118
|
+
let cancelled = false;
|
|
119
|
+
let inFlight = false;
|
|
120
|
+
let lastAsked = 0;
|
|
121
|
+
|
|
122
|
+
const check = async () => {
|
|
123
|
+
// Another reader may have heard the yes first; it is remembered for everyone.
|
|
124
|
+
if (cancelled || inFlight || record.connection) return;
|
|
125
|
+
inFlight = true;
|
|
126
|
+
lastAsked = Date.now();
|
|
127
|
+
let result: AgentConnection;
|
|
128
|
+
try {
|
|
129
|
+
result = await fetchAgentConnection();
|
|
130
|
+
} catch {
|
|
131
|
+
// A refusal or a blip reads as "not yet" on screen; the event, or
|
|
132
|
+
// the next return to the tab, says otherwise.
|
|
133
|
+
result = { connected: false };
|
|
134
|
+
}
|
|
135
|
+
inFlight = false;
|
|
136
|
+
if (cancelled) return;
|
|
137
|
+
if (result.connected) {
|
|
138
|
+
rememberConnected(record, result);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
setAnswer({ record, state: { connected: false, settled: true } });
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The person is back: the tab was shown, or the window focused. Asked
|
|
146
|
+
* again if it has not been lately — an agent that connected while the
|
|
147
|
+
* stream was down, or in a window this tab never heard from.
|
|
148
|
+
*/
|
|
149
|
+
const onReturn = () => {
|
|
150
|
+
if (document.hidden || inFlight || record.connection) return;
|
|
151
|
+
if (Date.now() - lastAsked >= AGENT_RECHECK_MS) void check();
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
void check();
|
|
155
|
+
document.addEventListener('visibilitychange', onReturn);
|
|
156
|
+
window.addEventListener('focus', onReturn);
|
|
157
|
+
return () => {
|
|
158
|
+
cancelled = true;
|
|
159
|
+
document.removeEventListener('visibilitychange', onReturn);
|
|
160
|
+
window.removeEventListener('focus', onReturn);
|
|
161
|
+
};
|
|
162
|
+
}, [enabled, record, userId]);
|
|
163
|
+
|
|
164
|
+
if (!record) return UNKNOWN;
|
|
165
|
+
if (record.connection) return { ...record.connection, connected: true, settled: true };
|
|
166
|
+
return answer?.record === record ? answer.state : UNKNOWN;
|
|
167
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The invite dialog's channel, split out from the provider that renders it so
|
|
5
|
+
* the provider module exports only a component (Fast Refresh; the same split
|
|
6
|
+
* as `library/state/toast.context.ts` / `toast.tsx`). The provider lives in
|
|
7
|
+
* `./invite-dialog.tsx`.
|
|
8
|
+
*
|
|
9
|
+
* What the invite entry points share: a way to open THE invite dialog, and a
|
|
10
|
+
* counter that moves every time it invited somebody.
|
|
11
|
+
*
|
|
12
|
+
* Several places open it — the toolbar's Invite button, the "Get set up"
|
|
13
|
+
* column, User accounts' "Invite new users" row and Manage access's Invite
|
|
14
|
+
* on a person with no account — and they must open the same dialog rather
|
|
15
|
+
* than one each, or the column's "Invite your team" would tick from one and
|
|
16
|
+
* not the other. The
|
|
17
|
+
* counter is how the column hears about it: it re-reads the account list
|
|
18
|
+
* when `invitedRevision` moves instead of polling for a change it caused.
|
|
19
|
+
*/
|
|
20
|
+
export interface InviteDialogController {
|
|
21
|
+
/** `emails` fills in the address field (Manage access's Invite). */
|
|
22
|
+
open(options?: { emails?: string[] }): void;
|
|
23
|
+
invitedRevision: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const InviteDialogContext = createContext<InviteDialogController | null>(null);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The shared controller, or null outside a provider. Null rather than a throw
|
|
30
|
+
* because the toolbar is rendered bare in its own tests, and an entry point
|
|
31
|
+
* with nothing to open should simply not be offered.
|
|
32
|
+
*/
|
|
33
|
+
export function useInviteDialog(): InviteDialogController | null {
|
|
34
|
+
return useContext(InviteDialogContext);
|
|
35
|
+
}
|