@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.
Files changed (206) hide show
  1. package/package.json +2 -2
  2. package/src/core/CoreAppShell.tsx +87 -25
  3. package/src/core/ProductName.tsx +49 -0
  4. package/src/core/__tests__/ProductName.test.tsx +46 -0
  5. package/src/core/__tests__/ShellRoutes.test.tsx +22 -0
  6. package/src/core/__tests__/core-contributions.test.ts +16 -0
  7. package/src/core/core-contributions.ts +17 -0
  8. package/src/core/maker-url.ts +30 -0
  9. package/src/core/registry.ts +78 -1
  10. package/src/index.css +23 -5
  11. package/src/index.ts +5 -0
  12. package/src/lib/__tests__/latest-request.test.ts +48 -0
  13. package/src/lib/latest-request.ts +45 -0
  14. package/src/modules/access/__tests__/ManageAccessDialog.no-sso-invite.test.tsx +222 -0
  15. package/src/modules/access/__tests__/ManageAccessDialog.proposal.test.tsx +1 -1
  16. package/src/modules/access/api.ts +19 -3
  17. package/src/modules/access/components/ManageAccessDialog.tsx +61 -8
  18. package/src/modules/admin/__tests__/AdminRolesPage.fixed-admin.test.tsx +16 -1
  19. package/src/modules/admin/__tests__/RolesCorruptedBanner.test.tsx +70 -0
  20. package/src/modules/admin/__tests__/UserAccountsPage.test.tsx +284 -39
  21. package/src/modules/admin/components/AdminRolesPage.tsx +13 -14
  22. package/src/modules/admin/components/RolesCorruptedBanner.tsx +9 -4
  23. package/src/modules/admin/components/UserAccountsPage.tsx +91 -107
  24. package/src/modules/admin/services/roles.api.ts +14 -1
  25. package/src/modules/auth/__tests__/LoginScreen.test.tsx +80 -5
  26. package/src/modules/auth/__tests__/sso-strict-probe.test.ts +66 -0
  27. package/src/modules/auth/components/LoginScreen.tsx +101 -73
  28. package/src/modules/auth/services/account.api.ts +59 -3
  29. package/src/modules/auth/services/sso.ts +30 -10
  30. package/src/modules/auth/state/use-sign-in-methods.ts +41 -0
  31. package/src/modules/change-requests/__tests__/ChangeRequestDialog.sync-banner.test.tsx +176 -0
  32. package/src/modules/change-requests/__tests__/ChangeRequestDialog.unreadable.test.tsx +68 -0
  33. package/src/modules/change-requests/__tests__/ChangeRequestLink.test.tsx +182 -0
  34. package/src/modules/change-requests/__tests__/FileChangeBoxes.test.tsx +1 -1
  35. package/src/modules/change-requests/__tests__/readFileOnBranch.test.ts +79 -0
  36. package/src/modules/change-requests/components/ChangeBox.tsx +6 -1
  37. package/src/modules/change-requests/components/ChangeRequestDialog.tsx +45 -15
  38. package/src/modules/change-requests/components/ChangeRequestLink.tsx +151 -0
  39. package/src/modules/change-requests/components/CrFileTree.tsx +2 -1
  40. package/src/modules/change-requests/components/FileChangeBoxes.tsx +4 -2
  41. package/src/modules/change-requests/hooks/useApplyChangeRequest.ts +6 -3
  42. package/src/modules/change-requests/services/change-requests.api.ts +15 -2
  43. package/src/modules/change-requests/utils/readFailure.ts +22 -0
  44. package/src/modules/embed/__tests__/EmbedLinkPage.test.tsx +74 -0
  45. package/src/modules/embed/__tests__/EmbedView.test.tsx +473 -0
  46. package/src/modules/embed/__tests__/embed-config.test.ts +128 -0
  47. package/src/modules/embed/__tests__/embed-host.test.ts +111 -0
  48. package/src/modules/embed/__tests__/mcp-app-view.test.ts +295 -0
  49. package/src/modules/embed/__tests__/mount.test.tsx +157 -0
  50. package/src/modules/embed/components/EmbedLinkPage.tsx +109 -0
  51. package/src/modules/embed/components/EmbedView.tsx +545 -0
  52. package/src/modules/embed/embed-config.ts +132 -0
  53. package/src/modules/embed/embed-host.ts +120 -0
  54. package/src/modules/embed/mount.tsx +80 -0
  55. package/src/modules/embed/services/embed-token.ts +22 -0
  56. package/src/modules/embed/services/embed.api.ts +158 -0
  57. package/src/modules/git/__tests__/BranchSwitcher.test.tsx +202 -4
  58. package/src/modules/git/__tests__/GitSyncFailedBanner.test.tsx +59 -1
  59. package/src/modules/git/__tests__/PullNeededBanner.test.tsx +39 -22
  60. package/src/modules/git/__tests__/useAutoPullUpdates.test.tsx +7 -2
  61. package/src/modules/git/branch-delete-menu-item.tsx +24 -0
  62. package/src/modules/git/components/BranchSwitcher.tsx +29 -5
  63. package/src/modules/git/components/GitSyncFailedBanner.tsx +53 -15
  64. package/src/modules/git/components/HistoryVersionPreview.tsx +5 -3
  65. package/src/modules/git/components/PullNeededBanner.tsx +15 -49
  66. package/src/modules/git/hooks/useAutoPullUpdates.ts +9 -5
  67. package/src/modules/git/index.ts +15 -0
  68. package/src/modules/git/services/__tests__/error-messages.test.ts +67 -0
  69. package/src/modules/git/services/__tests__/pull-failure.test.ts +36 -0
  70. package/src/modules/git/services/error-messages.ts +58 -1
  71. package/src/modules/git/services/pull-failure.ts +68 -0
  72. package/src/modules/git/state/branch-delete-confirm.ts +46 -0
  73. package/src/modules/layout/__tests__/HeaderAlignment.test.tsx +1 -0
  74. package/src/modules/layout/__tests__/breakpoints.test.ts +7 -1
  75. package/src/modules/layout/breakpoints.ts +8 -0
  76. package/src/modules/library/__tests__/PluginExtras.test.tsx +9 -9
  77. package/src/modules/library/__tests__/PluginPage.test.tsx +8 -1
  78. package/src/modules/library/__tests__/SkillsTree.test.tsx +13 -4
  79. package/src/modules/library/components/LibraryPage.tsx +22 -0
  80. package/src/modules/library/components/PluginExtras.tsx +23 -6
  81. package/src/modules/library/components/PluginPage.tsx +6 -1
  82. package/src/modules/library/routes/LibraryRoutes.tsx +8 -7
  83. package/src/modules/library/services/plugins.api.ts +4 -1
  84. package/src/modules/library/state/plugins-revision.ts +28 -0
  85. package/src/modules/onboarding/__tests__/InviteButton.test.tsx +214 -0
  86. package/src/modules/onboarding/__tests__/agent-connection-api.test.ts +55 -0
  87. package/src/modules/onboarding/__tests__/creator-welcome.test.tsx +87 -82
  88. package/src/modules/onboarding/__tests__/first-page-prompt.test.ts +164 -0
  89. package/src/modules/onboarding/__tests__/get-started-column.test.tsx +1458 -0
  90. package/src/modules/onboarding/__tests__/invite-dialog.test.tsx +811 -0
  91. package/src/modules/onboarding/__tests__/invite-emails.test.ts +247 -0
  92. package/src/modules/onboarding/__tests__/onboarding.test.tsx +226 -193
  93. package/src/modules/onboarding/__tests__/starter-pack-card.test.tsx +136 -0
  94. package/src/modules/onboarding/components/CreatorWelcomePage.tsx +7 -11
  95. package/src/modules/onboarding/components/EmailChipsInput.tsx +138 -0
  96. package/src/modules/onboarding/components/GetStartedColumn.tsx +670 -0
  97. package/src/modules/onboarding/components/InviteButton.tsx +139 -0
  98. package/src/modules/onboarding/components/InviteDialog.tsx +741 -0
  99. package/src/modules/onboarding/components/RootLanding.tsx +8 -24
  100. package/src/modules/onboarding/components/StarterPackCard.tsx +98 -0
  101. package/src/modules/onboarding/components/WelcomePage.tsx +65 -154
  102. package/src/modules/onboarding/first-page-prompt.ts +123 -0
  103. package/src/modules/onboarding/invite-emails.ts +313 -0
  104. package/src/modules/onboarding/services/agent-connection.api.ts +53 -0
  105. package/src/modules/onboarding/services/starter-packs.api.ts +83 -0
  106. package/src/modules/onboarding/state/agent-connection.ts +167 -0
  107. package/src/modules/onboarding/state/invite-dialog.context.ts +35 -0
  108. package/src/modules/onboarding/state/invite-dialog.tsx +39 -0
  109. package/src/modules/onboarding/state/onboarding.ts +133 -59
  110. package/src/modules/onboarding/state/starter-packs.ts +169 -0
  111. package/src/modules/settings/settings-nav-items.tsx +8 -3
  112. package/src/modules/setup/__tests__/setup.first-run-storage.test.tsx +389 -0
  113. package/src/modules/setup/__tests__/setup.github.test.tsx +5 -5
  114. package/src/modules/setup/__tests__/setup.kept-draft.test.tsx +1 -1
  115. package/src/modules/setup/__tests__/setup.repository-change.test.tsx +1 -1
  116. package/src/modules/setup/__tests__/setup.repository.test.tsx +21 -21
  117. package/src/modules/setup/__tests__/setup.test.tsx +56 -15
  118. package/src/modules/setup/__tests__/useConnectionProbe.test.tsx +123 -0
  119. package/src/modules/setup/components/FirstRunStorage.tsx +560 -0
  120. package/src/modules/setup/components/SetupGate.tsx +46 -1
  121. package/src/modules/setup/components/SetupScreen.tsx +81 -84
  122. package/src/modules/setup/hooks/useConnectionProbe.ts +93 -0
  123. package/src/modules/setup/services/setup.api.ts +3 -3
  124. package/src/modules/setup/utils/suggested-branch.ts +27 -0
  125. package/src/modules/toolbar/commands/__tests__/actions.test.tsx +210 -0
  126. package/src/modules/toolbar/commands/actions.tsx +330 -0
  127. package/src/modules/toolbar/commands/command-menu.ts +44 -0
  128. package/src/modules/toolbar/commands/useCommandActions.ts +71 -0
  129. package/src/modules/toolbar/commands/useCommandShortcuts.ts +121 -0
  130. package/src/modules/toolbar/components/AppSwitcher.tsx +125 -22
  131. package/src/modules/toolbar/components/ExternalAgentAccessPage.tsx +1 -1
  132. package/src/modules/toolbar/components/ProfileMenu.tsx +9 -4
  133. package/src/modules/toolbar/components/SearchPalette.tsx +540 -0
  134. package/src/modules/toolbar/components/Toolbar.tsx +10 -0
  135. package/src/modules/toolbar/components/__tests__/AppSwitcher.test.tsx +230 -52
  136. package/src/modules/toolbar/components/__tests__/ProfileMenu.test.tsx +64 -0
  137. package/src/modules/toolbar/components/__tests__/SearchPalette.test.tsx +864 -0
  138. package/src/modules/toolbar/components/__tests__/Toolbar.test.tsx +57 -2
  139. package/src/modules/toolbar/search/__tests__/rank.test.ts +93 -0
  140. package/src/modules/toolbar/search/__tests__/sources.test.ts +131 -0
  141. package/src/modules/toolbar/search/rank.ts +111 -0
  142. package/src/modules/toolbar/search/sources.ts +169 -0
  143. package/src/modules/workflow/hooks/useFileLock.ts +10 -7
  144. package/src/modules/workflow/services/__tests__/lock.api.test.ts +41 -0
  145. package/src/modules/workflow/services/lock.api.ts +17 -2
  146. package/src/modules/workspace/__tests__/useWorkspaceState.test.ts +49 -1
  147. package/src/modules/workspace/components/EditorTabs.tsx +39 -15
  148. package/src/modules/workspace/components/FileExplorer.tsx +39 -6
  149. package/src/modules/workspace/components/FilePaneCard.tsx +6 -2
  150. package/src/modules/workspace/components/FileRoute.tsx +12 -7
  151. package/src/modules/workspace/components/FileViewer.tsx +271 -74
  152. package/src/modules/workspace/components/KbPageHeader.tsx +13 -6
  153. package/src/modules/workspace/components/__tests__/EditorTabs.test.tsx +88 -21
  154. package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +92 -4
  155. package/src/modules/workspace/components/__tests__/FileRoute.test.tsx +23 -3
  156. package/src/modules/workspace/components/__tests__/FileViewer.test.tsx +330 -6
  157. package/src/modules/workspace/components/__tests__/KbPageHeader.test.tsx +17 -2
  158. package/src/modules/workspace/components/renderers/DocxRenderer.tsx +11 -16
  159. package/src/modules/workspace/components/renderers/DownloadFileButton.tsx +6 -0
  160. package/src/modules/workspace/components/renderers/EmailRenderer.tsx +23 -21
  161. package/src/modules/workspace/components/renderers/ImageRenderer.tsx +13 -29
  162. package/src/modules/workspace/components/renderers/KbMarkdownView.tsx +9 -2
  163. package/src/modules/workspace/components/renderers/MarkdownRenderer.tsx +8 -0
  164. package/src/modules/workspace/components/renderers/PdfRenderer.tsx +14 -19
  165. package/src/modules/workspace/components/renderers/PptxRenderer.tsx +11 -17
  166. package/src/modules/workspace/components/renderers/ToolRenderer.tsx +35 -7
  167. package/src/modules/workspace/components/renderers/XlsxRenderer.tsx +11 -17
  168. package/src/modules/workspace/components/renderers/__tests__/EmailRenderer.links.test.tsx +73 -0
  169. package/src/modules/workspace/components/renderers/__tests__/ToolRenderer.scaffold.test.tsx +64 -0
  170. package/src/modules/workspace/components/renderers/__tests__/ToolRenderer.surface.test.tsx +82 -0
  171. package/src/modules/workspace/components/renderers/__tests__/bridgeHarness.ts +99 -0
  172. package/src/modules/workspace/components/renderers/__tests__/htmlSandbox.test.ts +129 -35
  173. package/src/modules/workspace/components/renderers/__tests__/htmlViewsGuide.test.ts +149 -0
  174. package/src/modules/workspace/components/renderers/__tests__/kbMarkdownLinkPolicy.test.tsx +100 -0
  175. package/src/modules/workspace/components/renderers/htmlSandbox.ts +76 -20
  176. package/src/modules/workspace/components/renderers/index.ts +26 -0
  177. package/src/modules/workspace/components/renderers/kbMarkdownPipeline.tsx +51 -1
  178. package/src/modules/workspace/components/renderers/rendererRawRead.ts +53 -0
  179. package/src/modules/workspace/components/renderers/rendererSurface.ts +109 -0
  180. package/src/modules/workspace/components/renderers/rendererWorkspace.ts +8 -2
  181. package/src/modules/workspace/hooks/__tests__/useCreatePage.test.ts +137 -0
  182. package/src/modules/workspace/hooks/__tests__/useKnowledgeWriteTarget.test.ts +302 -0
  183. package/src/modules/workspace/hooks/__tests__/useWorkspaceState.test.tsx +126 -21
  184. package/src/modules/workspace/hooks/useCreatePage.ts +206 -0
  185. package/src/modules/workspace/hooks/useKnowledgeWriteTarget.ts +238 -0
  186. package/src/modules/workspace/hooks/useWorkspaceImageResolver.ts +24 -7
  187. package/src/modules/workspace/hooks/useWorkspaceState.ts +42 -8
  188. package/src/modules/workspace/routing/__tests__/kb-routes.test.tsx +10 -0
  189. package/src/modules/workspace/routing/__tests__/md-links-parity.test.tsx +113 -0
  190. package/src/modules/workspace/routing/kb-routes.ts +66 -12
  191. package/src/modules/workspace/state/editable-page.ts +42 -0
  192. package/src/modules/workspace/state/workspace.context.ts +13 -4
  193. package/src/modules/workspace/utils/__tests__/display-file-name.test.ts +60 -0
  194. package/src/modules/workspace/utils/__tests__/fileTree.test.ts +75 -0
  195. package/src/modules/workspace/utils/__tests__/treeConfirm.test.ts +16 -0
  196. package/src/modules/workspace/utils/display-file-name.ts +102 -0
  197. package/src/modules/workspace/utils/fileTree.ts +49 -14
  198. package/src/modules/workspace/utils/treeConfirm.ts +15 -0
  199. package/src/shared/__tests__/no-builtin-confirm.test.ts +102 -0
  200. package/src/shared/components/ConfirmProvider.tsx +156 -0
  201. package/src/shared/components/Dialog.tsx +7 -0
  202. package/src/shared/components/__tests__/ConfirmProvider.test.tsx +198 -0
  203. package/src/shared/components/confirm-context.ts +94 -0
  204. package/src/shared/components/index.ts +3 -0
  205. package/src/modules/onboarding/components/WelcomeRoute.tsx +0 -57
  206. package/src/modules/onboarding/welcome-state.ts +0 -36
@@ -0,0 +1,157 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
+ import { fireEvent, screen, waitFor } from '@testing-library/react';
3
+ import type { AppRegistry } from '../../../core/registry';
4
+ import type { EmbedFileView } from '../services/embed.api';
5
+ import { embedApiBase, resetEmbedConfig } from '../embed-config';
6
+
7
+ /** The HTTP surface, stubbed — the mount is about wiring, not about the API. */
8
+ const api = vi.hoisted(() => {
9
+ class FakeEmbedApiError extends Error {
10
+ readonly status: number;
11
+ constructor(status: number, message: string) {
12
+ super(message);
13
+ this.name = 'EmbedApiError';
14
+ this.status = status;
15
+ }
16
+ }
17
+ return {
18
+ EmbedApiError: FakeEmbedApiError,
19
+ loadEmbed: vi.fn(),
20
+ lockEmbed: vi.fn(),
21
+ heartbeatEmbed: vi.fn(),
22
+ cancelEmbed: vi.fn(),
23
+ saveEmbed: vi.fn(),
24
+ proposeEmbed: vi.fn(),
25
+ embedRawUrl: vi.fn((token: string, path?: string) => `/api/embed/raw?token=${token}${path ? `&path=${path}` : ''}`),
26
+ };
27
+ });
28
+ vi.mock('../services/embed.api', () => api);
29
+
30
+ import { mountEmbed } from '../mount';
31
+
32
+ const ORIGIN = 'https://hexis.example';
33
+
34
+ const VIEW: EmbedFileView = {
35
+ nodeName: 'Thing',
36
+ repoRelative: 'Data/Thing.md',
37
+ workspacePath: 'knowledge-base/Data/Thing.md',
38
+ kbDirName: 'knowledge-base',
39
+ branch: 'main',
40
+ appUrl: `${ORIGIN}/workspace/main/knowledge-base/Data/Thing.md`,
41
+ content: '# Thing\n\nWhat it is.\n',
42
+ contentIsText: true,
43
+ linked: true,
44
+ canRead: true,
45
+ canWrite: false,
46
+ linkUrl: `${ORIGIN}/embed/link?token=tok`,
47
+ };
48
+
49
+ const EMPTY_REGISTRY = {
50
+ apps: [],
51
+ panes: [],
52
+ topLevelRoutes: [],
53
+ viewerRoutes: [],
54
+ renderers: [],
55
+ banners: [],
56
+ fileViewerPanels: [],
57
+ adminMenuItems: [],
58
+ explorerItems: [],
59
+ folderMenuItems: [],
60
+ settingsNavItems: [],
61
+ } as unknown as AppRegistry;
62
+
63
+ let el: HTMLElement;
64
+ let handle: { unmount(): void } | null = null;
65
+
66
+ beforeEach(() => {
67
+ for (const fn of Object.values(api)) (fn as { mockReset?: () => void }).mockReset?.();
68
+ api.loadEmbed.mockResolvedValue(VIEW);
69
+ el = document.createElement('div');
70
+ document.body.appendChild(el);
71
+ });
72
+
73
+ afterEach(() => {
74
+ handle?.unmount();
75
+ handle = null;
76
+ el.remove();
77
+ resetEmbedConfig();
78
+ });
79
+
80
+ /**
81
+ * What the MCP App view calls after loading the deployment's embed bundle
82
+ * into a chat host's sandbox: the same embed the `/embed` route renders,
83
+ * mounted outside the app, with the token handed in rather than read from a
84
+ * page URL the sandbox does not have.
85
+ */
86
+ describe('mountEmbed', () => {
87
+ it('renders the page for the token it was handed, with the app renderer for the type', async () => {
88
+ handle = mountEmbed(el, { registry: EMPTY_REGISTRY, baseUrl: ORIGIN, token: 'tok' });
89
+ expect(await screen.findByRole('heading', { name: 'Thing' })).toBeTruthy();
90
+ expect(api.loadEmbed).toHaveBeenCalledWith('tok');
91
+ // A reader who may not write is offered a proposal, as on the file page.
92
+ expect(screen.getByRole('button', { name: 'Propose changes' })).toBeTruthy();
93
+ });
94
+
95
+ it('mounts a renderer the deployment registered, so the chat shows what the file page shows', async () => {
96
+ const registry = {
97
+ ...EMPTY_REGISTRY,
98
+ renderers: [{ extensions: ['.md'], Component: () => <div data-testid="deployment-md">the deployment renderer</div> }],
99
+ } as unknown as AppRegistry;
100
+ handle = mountEmbed(el, { registry, baseUrl: ORIGIN, token: 'tok' });
101
+ expect(await screen.findByTestId('deployment-md')).toBeTruthy();
102
+ });
103
+
104
+ /**
105
+ * The runtime the embed reads is per document, so one mount owns it at a
106
+ * time: a second mount takes the first down, and the one that owns the
107
+ * runtime hands it back on unmount — a replaced handle does not reset what
108
+ * its successor configured.
109
+ */
110
+ it('holds one embed per document, and hands the runtime back on unmount', async () => {
111
+ const first = mountEmbed(el, { registry: EMPTY_REGISTRY, baseUrl: ORIGIN, token: 'tok' });
112
+ await screen.findByRole('heading', { name: 'Thing' });
113
+ const second = document.createElement('div');
114
+ document.body.appendChild(second);
115
+ handle = mountEmbed(second, { registry: EMPTY_REGISTRY, baseUrl: 'https://other.example', token: 'tok2' });
116
+ await screen.findByRole('heading', { name: 'Thing' });
117
+ expect(el.textContent).toBe('');
118
+ expect(embedApiBase()).toBe('https://other.example');
119
+ expect(api.loadEmbed).toHaveBeenLastCalledWith('tok2');
120
+ // The replaced handle is inert.
121
+ first.unmount();
122
+ expect(embedApiBase()).toBe('https://other.example');
123
+ handle.unmount();
124
+ handle = null;
125
+ expect(embedApiBase()).toBe('');
126
+ second.remove();
127
+ });
128
+
129
+ /**
130
+ * A view taken down mid-edit — replaced by a second mount, or unmounted
131
+ * outright — lets go of its edit lock on the way out, rather than leaving
132
+ * the file shut to other writers until the lock's TTL.
133
+ */
134
+ it('releases an edit lock the view holds when it is taken down', async () => {
135
+ api.loadEmbed.mockResolvedValue({ ...VIEW, canWrite: true });
136
+ api.lockEmbed.mockResolvedValue({ acquired: true });
137
+ api.cancelEmbed.mockResolvedValue(undefined);
138
+ const first = mountEmbed(el, { registry: EMPTY_REGISTRY, baseUrl: ORIGIN, token: 'tok' });
139
+ fireEvent.click(await screen.findByRole('button', { name: 'Edit' }));
140
+ await screen.findByRole('button', { name: 'Save' });
141
+ expect(api.cancelEmbed).not.toHaveBeenCalled();
142
+ const second = document.createElement('div');
143
+ document.body.appendChild(second);
144
+ handle = mountEmbed(second, { registry: EMPTY_REGISTRY, baseUrl: ORIGIN, token: 'tok2' });
145
+ await waitFor(() => expect(api.cancelEmbed).toHaveBeenCalledWith('tok'));
146
+ expect(first).toBeDefined();
147
+ second.remove();
148
+ });
149
+
150
+ it('unmounts cleanly', async () => {
151
+ handle = mountEmbed(el, { registry: EMPTY_REGISTRY, baseUrl: ORIGIN, token: 'tok' });
152
+ await screen.findByRole('heading', { name: 'Thing' });
153
+ handle.unmount();
154
+ handle = null;
155
+ expect(el.textContent).toBe('');
156
+ });
157
+ });
@@ -0,0 +1,109 @@
1
+ import { useCallback, useState, type ReactNode } from 'react';
2
+ import { Button } from '../../../shared/components';
3
+ import { AuthContext } from '../../auth/state/auth.context';
4
+ import { useAuthState } from '../../auth/hooks/useAuthState';
5
+ import { LoginScreen } from '../../auth/components/LoginScreen';
6
+ import { getToken } from '../../../lib/api';
7
+ import { linkEmbedAccount } from '../services/embed.api';
8
+ import { outsideAccountOf } from '../services/embed-token';
9
+
10
+ function tokenFromUrl(): string {
11
+ return new URLSearchParams(window.location.search).get('token') ?? '';
12
+ }
13
+
14
+ function Centered({ children }: { children: ReactNode }) {
15
+ return (
16
+ <div className="flex h-full items-center justify-center bg-white px-6 text-center text-sm text-ink">
17
+ <div className="max-w-sm">{children}</div>
18
+ </div>
19
+ );
20
+ }
21
+
22
+ /**
23
+ * The account-link page, opened in a new tab when an embed viewer whose
24
+ * outside account is not yet linked clicks Edit. It signs them in the ordinary
25
+ * way and then — once they CONFIRM it, naming both accounts — links the
26
+ * account the embed token carries to the user they just authenticated as.
27
+ *
28
+ * The confirmation is not a courtesy. A link to this page can be crafted by
29
+ * anybody holding a token for their own outside account; followed silently by
30
+ * a signed-in victim, it would bind the attacker's account to the victim's,
31
+ * and hand the attacker the victim's knowledge-base access from then on.
32
+ * Refusing to be framed does nothing against a top-level link, so nothing is
33
+ * linked until the person who is signed in says so.
34
+ *
35
+ * THIS PAGE IS NEVER FRAMED, and that is the whole reason it is a page of its
36
+ * own rather than a step inside the embed. It acts under the signed-in
37
+ * session, so a page that could frame it could link a foreign account to
38
+ * whoever happened to be signed in — and from then on read and write the
39
+ * knowledge base as them. The server sends `frame-ancestors 'none'` and
40
+ * `X-Frame-Options: DENY` for exactly this route; the embed page, which acts
41
+ * under a token alone, sends no framing restriction at all.
42
+ */
43
+ export function EmbedLinkPage() {
44
+ const auth = useAuthState();
45
+ const token = tokenFromUrl();
46
+ const [status, setStatus] = useState<'idle' | 'linking' | 'done' | 'declined' | 'error'>('idle');
47
+ const [error, setError] = useState<string | null>(null);
48
+ const outside = outsideAccountOf(token);
49
+
50
+ const onLink = useCallback(() => {
51
+ setStatus('linking');
52
+ linkEmbedAccount(token, getToken())
53
+ .then(() => setStatus('done'))
54
+ .catch((err: unknown) => {
55
+ setError(err instanceof Error ? err.message : 'Linking failed.');
56
+ setStatus('error');
57
+ });
58
+ }, [token]);
59
+
60
+ return (
61
+ <AuthContext.Provider value={auth}>
62
+ {auth.isLoading ? (
63
+ <Centered>Loading…</Centered>
64
+ ) : !auth.user ? (
65
+ <LoginScreen />
66
+ ) : !token ? (
67
+ <Centered>This link has no token. Open it again from the page in your chat.</Centered>
68
+ ) : status === 'done' ? (
69
+ <Centered>
70
+ <p className="mb-1 text-base font-semibold text-ink">Account linked</p>
71
+ <p>
72
+ {/* Not "click Edit": whether the page offers Edit or Propose
73
+ changes depends on this person's access, which this page
74
+ does not know. */}
75
+ Your account is connected. Close this tab and reload the page you came from.
76
+ </p>
77
+ </Centered>
78
+ ) : status === 'error' ? (
79
+ <Centered>
80
+ <p role="alert" className="text-danger">
81
+ Could not link your account: {error}
82
+ </p>
83
+ </Centered>
84
+ ) : status === 'declined' ? (
85
+ <Centered>Nothing was linked. You can close this tab.</Centered>
86
+ ) : status === 'linking' ? (
87
+ <Centered>Linking your account…</Centered>
88
+ ) : (
89
+ <Centered>
90
+ <p className="mb-1 text-base font-semibold text-ink">Link your Atlassian account?</p>
91
+ <p className="mb-3">
92
+ The Atlassian account{' '}
93
+ {outside ? <code className="break-all">{outside}</code> : 'in this link'} will read and edit
94
+ this knowledge base as <strong>{auth.user.email}</strong>. Only continue if you opened this
95
+ link yourself, from a page in Jira or Confluence.
96
+ </p>
97
+ <span className="flex justify-center gap-2">
98
+ <Button variant="quiet" size="sm" onClick={() => setStatus('declined')}>
99
+ Cancel
100
+ </Button>
101
+ <Button variant="primary" size="sm" onClick={onLink}>
102
+ Link accounts
103
+ </Button>
104
+ </span>
105
+ </Centered>
106
+ )}
107
+ </AuthContext.Provider>
108
+ );
109
+ }