@bevel-software/platform-core-frontend 0.37.0 → 0.39.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 (35) hide show
  1. package/package.json +2 -2
  2. package/src/core/CoreAppShell.tsx +22 -10
  3. package/src/core/__tests__/AppChrome.test.tsx +27 -10
  4. package/src/core/__tests__/ShellRoutes.test.tsx +43 -2
  5. package/src/core/registry.ts +43 -2
  6. package/src/core/shell-paths.ts +30 -0
  7. package/src/lib/api.ts +7 -1
  8. package/src/modules/auth/__tests__/LoginScreen.test.tsx +9 -0
  9. package/src/modules/auth/__tests__/useAuthState.test.ts +92 -0
  10. package/src/modules/auth/components/LoginScreen.tsx +2 -0
  11. package/src/modules/auth/hooks/useAuthState.ts +4 -1
  12. package/src/modules/auth/index.ts +2 -0
  13. package/src/modules/auth/state/accountStorage.ts +32 -0
  14. package/src/modules/auth/state/tokenAccount.ts +53 -0
  15. package/src/modules/layout/index.ts +1 -1
  16. package/src/modules/settings/components/SettingsLayout.tsx +52 -9
  17. package/src/modules/settings/components/SettingsNav.tsx +24 -3
  18. package/src/modules/settings/components/__tests__/SettingsLayout.test.tsx +266 -5
  19. package/src/modules/settings/index.ts +1 -1
  20. package/src/modules/settings/settings-nav-items.tsx +130 -10
  21. package/src/modules/toolbar/commands/__tests__/actions.test.tsx +20 -5
  22. package/src/modules/toolbar/commands/actions.tsx +1 -0
  23. package/src/modules/toolbar/components/ProfileMenu.tsx +13 -1
  24. package/src/modules/toolbar/components/Toolbar.tsx +3 -2
  25. package/src/modules/toolbar/components/__tests__/ProfileMenu.app-sections.test.tsx +87 -0
  26. package/src/modules/toolbar/components/__tests__/ProfileMenu.test.tsx +1 -0
  27. package/src/modules/workspace/components/renderers/HtmlRenderer.tsx +2 -1
  28. package/src/modules/workspace/components/renderers/__tests__/HtmlRenderer.tools.test.tsx +80 -32
  29. package/src/modules/workspace/components/renderers/__tests__/htmlToolBridge.test.ts +78 -8
  30. package/src/modules/workspace/components/renderers/__tests__/htmlViewsGuide.test.ts +10 -3
  31. package/src/modules/workspace/components/renderers/__tests__/pageConsentMemory.test.ts +177 -0
  32. package/src/modules/workspace/components/renderers/__tests__/useHtmlToolBridge.test.tsx +249 -23
  33. package/src/modules/workspace/components/renderers/htmlToolBridge.ts +52 -16
  34. package/src/modules/workspace/components/renderers/pageConsentMemory.ts +75 -0
  35. package/src/modules/workspace/components/renderers/useHtmlToolBridge.tsx +46 -11
@@ -1,6 +1,8 @@
1
- import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, type ReactNode, type RefObject } from 'react';
1
+ import { useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, type ReactNode, type RefObject } from 'react';
2
+ import { AuthContext, accountFetch } from '../../../auth';
2
3
  import { useFileNav } from '../../routing/kb-routes';
3
4
  import { useRendererSurface } from './rendererSurface';
5
+ import { sha256Hex } from '@bevel-software/platform-shared';
4
6
  import { Button, Dialog } from '../../../../shared/components';
5
7
  import {
6
8
  PageConsent,
@@ -11,6 +13,7 @@ import {
11
13
  useOpenPageInPlatform,
12
14
  usePageTools,
13
15
  } from './htmlToolBridge';
16
+ import { isRemembered, remember } from './pageConsentMemory';
14
17
 
15
18
  /**
16
19
  * The card the app shows over a page, outside its frame: the page cannot draw,
@@ -36,6 +39,14 @@ export interface HtmlToolBridgeOptions {
36
39
  * consent across it.
37
40
  */
38
41
  viewKey: unknown;
42
+ /**
43
+ * The page's source as the renderer received it, the HTML its view's
44
+ * document is built from. An Allow is remembered by its SHA-256 digest, so
45
+ * a source whose digest this browser still keeps for the signed-in account
46
+ * asks no more; any other (an edit, an evicted digest, another account, no
47
+ * account above the renderer, a browser that will not store) asks.
48
+ */
49
+ source: string;
39
50
  }
40
51
 
41
52
  export interface HtmlToolBridgeResult {
@@ -66,7 +77,7 @@ function newDocumentId(): string {
66
77
  * passes its frame, its file and its view key, builds its document with
67
78
  * `toolBridge`, and renders `cards` — it holds no bridge logic of its own.
68
79
  */
69
- export function useHtmlToolBridge({ iframeRef, filePath, viewKey }: HtmlToolBridgeOptions): HtmlToolBridgeResult {
80
+ export function useHtmlToolBridge({ iframeRef, filePath, viewKey, source }: HtmlToolBridgeOptions): HtmlToolBridgeResult {
70
81
  const { openFilePage } = useFileNav();
71
82
  const surface = useRendererSurface();
72
83
  // The viewer's tools, for a page on a host that provides them (the file
@@ -75,11 +86,22 @@ export function useHtmlToolBridge({ iframeRef, filePath, viewKey }: HtmlToolBrid
75
86
  const pageTools = usePageTools();
76
87
  const openPageInPlatform = useOpenPageInPlatform();
77
88
  const toolPagePath = surface === null && pageTools ? pageTools.pagePath : null;
78
- const toolsClient = useMemo(() => (toolPagePath ? new PageToolsClient(toolPagePath) : null), [toolPagePath]);
89
+ // The viewer's answer about this page's writes holds for one VIEW of it: a
90
+ // new frame document (a reload, a saved edit, another file) asks again,
91
+ // unless the browser remembers this account's Allow for that document's
92
+ // exact source. Read nullable (not `useAuth`, which throws): no account above
93
+ // the renderer means nothing is kept and every view asks.
94
+ const account = useContext(AuthContext)?.user?.id ?? null;
95
+ // With an account above the renderer, every request the page's calls make
96
+ // (the manual lookup, the call, a refusal's record, an upload) is sent only
97
+ // with that account's token: another tab's sign-in as someone else makes
98
+ // them fail with nothing sent (`accountFetch`).
99
+ const toolsClient = useMemo(
100
+ () => (toolPagePath ? new PageToolsClient(toolPagePath, account === null ? undefined : accountFetch(account)) : null),
101
+ [toolPagePath, account],
102
+ );
79
103
  const [card, setCard] = useState<PageCard | null>(null);
80
104
  const cardRef = useRef<PageCard | null>(null);
81
- // The viewer's answer about this page's writes holds for one VIEW of it: a
82
- // new frame document (a reload, a saved edit, another file) asks again.
83
105
  const consentRef = useRef<PageConsent | null>(null);
84
106
  // Which document this view's is: an identity minted per `viewKey` — for
85
107
  // every view, tools or not, since a page without tools still asks
@@ -97,9 +119,14 @@ export function useHtmlToolBridge({ iframeRef, filePath, viewKey }: HtmlToolBrid
97
119
  return newDocumentId();
98
120
  }, [viewKey]);
99
121
  const documentIdRef = useRef(documentId);
122
+ // What the browser remembers an Allow by: the SHA-256 of the page's exact
123
+ // source, current in the same commit as the document built from it.
124
+ const digest = useMemo(() => sha256Hex(source), [source]);
125
+ const digestRef = useRef(digest);
100
126
  useLayoutEffect(() => {
101
127
  documentIdRef.current = documentId;
102
- }, [documentId]);
128
+ digestRef.current = digest;
129
+ }, [documentId, digest]);
103
130
 
104
131
  /** Show one of the app's cards over the page and resolve with the viewer's answer. A card already up answers this ask too. */
105
132
  const showCard = useCallback((kind: PageCard['kind']): Promise<boolean | null> => {
@@ -189,12 +216,20 @@ export function useHtmlToolBridge({ iframeRef, filePath, viewKey }: HtmlToolBrid
189
216
  });
190
217
  return;
191
218
  }
192
- consentRef.current ??= new PageConsent(() => showCard('write'));
219
+ if (!consentRef.current) {
220
+ // The view's consent remembers by the source this view was built from,
221
+ // for the account viewing it.
222
+ const viewDigest = digestRef.current;
223
+ consentRef.current = new PageConsent(() => showCard('write'), {
224
+ remembered: () => isRemembered(account, viewDigest),
225
+ remember: () => remember(account, viewDigest),
226
+ });
227
+ }
193
228
  void answerToolCall(call, toolsClient, consentRef.current).then(reply);
194
229
  }
195
230
  window.addEventListener('message', onToolMessage);
196
231
  return () => window.removeEventListener('message', onToolMessage);
197
- }, [iframeRef, toolsClient, showCard]);
232
+ }, [iframeRef, toolsClient, showCard, account]);
198
233
 
199
234
  const openInPlatform = useCallback(() => {
200
235
  closeCard(false);
@@ -212,15 +247,15 @@ export function useHtmlToolBridge({ iframeRef, filePath, viewKey }: HtmlToolBrid
212
247
  <Dialog
213
248
  open={card?.kind === 'write'}
214
249
  onClose={() => closeCard(false)}
215
- title={`${pageName(filePath)} wants to write to the knowledge base`}
250
+ title="You're allowing changes using your account. Do you want to allow that?"
216
251
  size="md"
217
252
  footer={
218
253
  <>
219
254
  <Button variant="outline" onClick={() => closeCard(false)}>
220
- Deny
255
+ Don't allow
221
256
  </Button>
222
257
  <Button variant="primary" onClick={() => closeCard(true)}>
223
- Allow while open
258
+ Allow
224
259
  </Button>
225
260
  </>
226
261
  }