@bevel-software/platform-core-frontend 0.27.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 (42) hide show
  1. package/package.json +2 -2
  2. package/src/core/ProductName.tsx +4 -4
  3. package/src/core/__tests__/ProductName.test.tsx +46 -0
  4. package/src/core/maker-url.ts +30 -0
  5. package/src/core/registry.ts +27 -0
  6. package/src/lib/__tests__/latest-request.test.ts +48 -0
  7. package/src/lib/latest-request.ts +45 -0
  8. package/src/modules/access/__tests__/ManageAccessDialog.no-sso-invite.test.tsx +222 -0
  9. package/src/modules/access/api.ts +19 -3
  10. package/src/modules/access/components/ManageAccessDialog.tsx +60 -7
  11. package/src/modules/admin/__tests__/AdminRolesPage.fixed-admin.test.tsx +16 -1
  12. package/src/modules/admin/__tests__/UserAccountsPage.test.tsx +284 -39
  13. package/src/modules/admin/components/AdminRolesPage.tsx +13 -14
  14. package/src/modules/admin/components/UserAccountsPage.tsx +91 -107
  15. package/src/modules/admin/services/roles.api.ts +14 -1
  16. package/src/modules/auth/__tests__/LoginScreen.test.tsx +80 -5
  17. package/src/modules/auth/__tests__/sso-strict-probe.test.ts +66 -0
  18. package/src/modules/auth/components/LoginScreen.tsx +100 -75
  19. package/src/modules/auth/services/account.api.ts +31 -2
  20. package/src/modules/auth/services/sso.ts +29 -8
  21. package/src/modules/auth/state/use-sign-in-methods.ts +41 -0
  22. package/src/modules/onboarding/__tests__/InviteButton.test.tsx +214 -0
  23. package/src/modules/onboarding/__tests__/first-page-prompt.test.ts +56 -1
  24. package/src/modules/onboarding/__tests__/get-started-column.test.tsx +538 -81
  25. package/src/modules/onboarding/__tests__/invite-dialog.test.tsx +543 -124
  26. package/src/modules/onboarding/__tests__/invite-emails.test.ts +247 -0
  27. package/src/modules/onboarding/components/GetStartedColumn.tsx +30 -11
  28. package/src/modules/onboarding/components/InviteButton.tsx +119 -15
  29. package/src/modules/onboarding/components/InviteDialog.tsx +392 -75
  30. package/src/modules/onboarding/first-page-prompt.ts +34 -0
  31. package/src/modules/onboarding/invite-emails.ts +171 -33
  32. package/src/modules/onboarding/state/invite-dialog.context.ts +7 -4
  33. package/src/modules/onboarding/state/invite-dialog.tsx +12 -4
  34. package/src/modules/toolbar/commands/actions.tsx +10 -4
  35. package/src/modules/toolbar/commands/useCommandActions.ts +5 -3
  36. package/src/modules/toolbar/components/AppSwitcher.tsx +19 -0
  37. package/src/modules/toolbar/components/__tests__/AppSwitcher.test.tsx +46 -1
  38. package/src/modules/toolbar/components/__tests__/SearchPalette.test.tsx +34 -2
  39. package/src/modules/workspace/hooks/__tests__/useCreatePage.test.ts +137 -0
  40. package/src/modules/workspace/hooks/__tests__/useKnowledgeWriteTarget.test.ts +302 -0
  41. package/src/modules/workspace/hooks/useCreatePage.ts +118 -28
  42. package/src/modules/workspace/hooks/useKnowledgeWriteTarget.ts +238 -0
@@ -0,0 +1,238 @@
1
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2
+ import { currentBranchModel, isProtectedBranch, type FileTreeEntry } from '@bevel-software/platform-shared';
3
+ import { fetchFileAccessBatch } from '../../access/api';
4
+ import { useWorkspace } from '../state/workspace.context';
5
+ import { useMergedWorkspaceTree } from './useMergedWorkspaceTree';
6
+ import { findEntryByPath } from '../utils/fileTree';
7
+ import { useLatestRequest } from '../../../lib/latest-request';
8
+
9
+ /**
10
+ * Where New page writes for the person signed in, on the branch on screen:
11
+ * `folder` is a workspace-relative Knowledge folder, or null — while the
12
+ * check has not answered (`settled` false), and once it has when there is
13
+ * nowhere they may write. Everything that offers New page hides it while
14
+ * `folder` is null.
15
+ */
16
+ export interface KnowledgeWriteTarget {
17
+ folder: string | null;
18
+ settled: boolean;
19
+ /**
20
+ * Whether `folder` was asked for the workspace, branch and page on screen
21
+ * now. Mid-switch the last answer stays up, so nothing flickers, but it may
22
+ * name the page or branch just left: a create asks again first.
23
+ */
24
+ current: boolean;
25
+ /**
26
+ * Ask again now, past the debounce, and resolve with the new answer — what
27
+ * a refused write does, since the refusal says the last answer is stale.
28
+ * The answer is for what is on screen when it resolves: if the page or the
29
+ * branch changed while it was out, it is asked again for the new one.
30
+ */
31
+ recheck(): Promise<string | null>;
32
+ /**
33
+ * The folder for what is on screen NOW: the last answer when it was asked
34
+ * about this page and branch, else a {@link recheck}. What a create writes
35
+ * to — never the folder of a page just left, which `folder` may still show
36
+ * during the debounce after a page change.
37
+ */
38
+ resolve(): Promise<string | null>;
39
+ /**
40
+ * Take New page away on this workspace and branch: a write was refused
41
+ * where the check still says the person may write, so the refusal is not
42
+ * about folders (a protected branch) and no folder here would take it.
43
+ * Lifted by a switch of workspace or branch.
44
+ */
45
+ withdraw(): void;
46
+ }
47
+
48
+ /** The access endpoint answers at most this many paths per call. */
49
+ const ACCESS_BATCH_LIMIT = 500;
50
+
51
+ /**
52
+ * How long a change to the tree or the page on screen waits before it is
53
+ * asked about. The tree is rebuilt on every file event, often several in a
54
+ * row; the first check of all runs at once, so the list never waits on this.
55
+ */
56
+ const WRITE_TARGET_DEBOUNCE_MS = 250;
57
+
58
+ /** Every folder under `entry`, depth first in the tree's own order; dot-folders are not places for pages. */
59
+ function knowledgeFoldersInOrder(entry: FileTreeEntry | null, out: string[] = []): string[] {
60
+ for (const child of entry?.children ?? []) {
61
+ if (child.type !== 'directory' || child.name.startsWith('.')) continue;
62
+ out.push(child.relativePath);
63
+ knowledgeFoldersInOrder(child, out);
64
+ }
65
+ return out;
66
+ }
67
+
68
+ /**
69
+ * The folders a page may go in, in the order requirement 1 picks them: the
70
+ * Knowledge folder, the folder of the page on screen, then every folder in
71
+ * file tree order — each once.
72
+ */
73
+ function candidateFolders(knowledgeRoot: string, openFolder: string | null, folders: readonly string[]): string[] {
74
+ const ordered = [knowledgeRoot, ...(openFolder && openFolder !== knowledgeRoot ? [openFolder] : [])];
75
+ const seen = new Set(ordered);
76
+ for (const folder of folders) {
77
+ if (seen.has(folder)) continue;
78
+ seen.add(folder);
79
+ ordered.push(folder);
80
+ }
81
+ return ordered;
82
+ }
83
+
84
+ /**
85
+ * The folder a page goes in, asked of the batch access endpoint in pages of
86
+ * at most {@link ACCESS_BATCH_LIMIT} paths: the Knowledge folder when the
87
+ * person may `verb` it; else the folder of the page on screen when they may;
88
+ * else the first folder they may, in file tree order. Stops at the first
89
+ * page that answers. Throws when a request fails, which the hook reads as
90
+ * "nowhere" (see below).
91
+ *
92
+ * A protected branch asks `write` (admins get the top, as before). A draft
93
+ * asks `read`: there no write grant is needed and the server's only gate is
94
+ * READ-BEFORE-WRITE, a new page lands only in a folder its author may read.
95
+ * The tree cannot say which folders those are — it shows a folder the person
96
+ * may not read whenever something inside it is shared with them — so every
97
+ * candidate is asked, in one request for all but the largest trees.
98
+ */
99
+ async function findKnowledgeTarget(
100
+ verb: 'write' | 'read',
101
+ workspaceId: string,
102
+ kbDirName: string,
103
+ knowledgeRoot: string,
104
+ openFolder: string | null,
105
+ folders: readonly string[],
106
+ ): Promise<string | null> {
107
+ // The first page carries the two folders that win outright.
108
+ const ordered = candidateFolders(knowledgeRoot, openFolder, folders);
109
+ const toRepo = (path: string) => path.slice(kbDirName.length + 1);
110
+ for (let start = 0; start < ordered.length; start += ACCESS_BATCH_LIMIT) {
111
+ const page = ordered.slice(start, start + ACCESS_BATCH_LIMIT);
112
+ const { results } = await fetchFileAccessBatch(workspaceId, page.map(toRepo), verb);
113
+ const found = page.find((path) => results[toRepo(path)] === true);
114
+ if (found) return found;
115
+ }
116
+ return null;
117
+ }
118
+
119
+ /**
120
+ * {@link KnowledgeWriteTarget} for the workspace on screen. Asked once the
121
+ * file tree is there — before it, only the top of Knowledge could be asked
122
+ * about, and someone who may write only a folder below would see New page
123
+ * arrive late — and again whenever the tree or the page on screen changes:
124
+ * at once for a workspace or branch not yet asked about, debounced after
125
+ * that. That is how a folder an admin shares later brings New page without
126
+ * a reload. `recheck` asks on demand.
127
+ *
128
+ * A protected branch asks who may WRITE. Anywhere else the server lets
129
+ * anyone write where they may read, so a draft asks who may READ, in the
130
+ * same order ({@link findKnowledgeTarget}); a tree with no Knowledge folder
131
+ * in it has nothing they may read there, and is answered without a request.
132
+ *
133
+ * A request that fails settles with no folder: New page is hidden until a
134
+ * later check succeeds, rather than offered where it can only be refused.
135
+ * The last answer stays while a new one is on its way, so nothing that hangs
136
+ * off it flickers on every tree refresh.
137
+ */
138
+ export function useKnowledgeWriteTarget(knowledgeRoot: string | null): KnowledgeWriteTarget {
139
+ const { workspaceId, workspaceBranch, kbDirName, openFilePath } = useWorkspace();
140
+ const { tree } = useMergedWorkspaceTree();
141
+ const [answer, setAnswer] = useState<{ folder: string | null; settled: boolean; askedFor: string | null }>({
142
+ folder: null,
143
+ settled: false,
144
+ askedFor: null,
145
+ });
146
+
147
+ const openFolder =
148
+ openFilePath && knowledgeRoot && openFilePath.startsWith(`${knowledgeRoot}/`)
149
+ ? openFilePath.slice(0, openFilePath.lastIndexOf('/'))
150
+ : null;
151
+ const knowledgeEntry = useMemo(
152
+ () => (knowledgeRoot ? findEntryByPath(tree, knowledgeRoot) : null),
153
+ [tree, knowledgeRoot],
154
+ );
155
+ const folders = useMemo(() => knowledgeFoldersInOrder(knowledgeEntry), [knowledgeEntry]);
156
+ // Not yet known (null) is asked about as protected: the stricter answer.
157
+ const draft = workspaceBranch !== null && !isProtectedBranch(currentBranchModel(), workspaceBranch);
158
+ const knowledgeShown = knowledgeEntry !== null;
159
+ // What an answer was asked about; an answer about anything else is out of date.
160
+ const workspaceKey = `${workspaceId}\n${workspaceBranch}\n${knowledgeRoot}`;
161
+ const askingFor = `${workspaceKey}\n${openFolder}`;
162
+
163
+ // Only the latest question's answer lands: a slow reply to an older tree
164
+ // must not overwrite a newer one. And only the latest answer is RETURNED:
165
+ // a caller that asked before a switch (`recheck` from a create) is handed
166
+ // the answer for what is on screen now, never the folder of a branch or
167
+ // page just left, which it would otherwise write to.
168
+ // (`LatestRequest`, the one protocol for it.)
169
+ // Each answer carries what it was asked about, so a caller can tell an
170
+ // answer for the page on screen from one for a page just left.
171
+ const questions = useLatestRequest<{ folder: string | null; askedFor: string | null }>();
172
+ const ask = useCallback(async (): Promise<{ folder: string | null; askedFor: string | null }> => {
173
+ if (!workspaceId || !kbDirName || !knowledgeRoot) return { folder: null, askedFor: null };
174
+ return questions.run(
175
+ async () => {
176
+ if (draft && !knowledgeShown) return { folder: null, askedFor: askingFor };
177
+ try {
178
+ const folder = await findKnowledgeTarget(draft ? 'read' : 'write', workspaceId, kbDirName, knowledgeRoot, openFolder, folders);
179
+ return { folder, askedFor: askingFor };
180
+ } catch {
181
+ return { folder: null, askedFor: askingFor };
182
+ }
183
+ },
184
+ ({ folder }) => setAnswer({ folder, settled: true, askedFor: askingFor }),
185
+ );
186
+ }, [questions, workspaceId, kbDirName, knowledgeRoot, openFolder, folders, askingFor, draft, knowledgeShown]);
187
+
188
+ const askedWorkspace = useRef<string | null>(null);
189
+ const hasTree = tree !== null;
190
+ useEffect(() => {
191
+ if (!workspaceId || !kbDirName || !knowledgeRoot || !hasTree) return;
192
+ if (askedWorkspace.current !== workspaceKey) {
193
+ askedWorkspace.current = workspaceKey;
194
+ void ask();
195
+ return;
196
+ }
197
+ const timer = setTimeout(() => void ask(), WRITE_TARGET_DEBOUNCE_MS);
198
+ return () => clearTimeout(timer);
199
+ }, [ask, workspaceId, kbDirName, knowledgeRoot, hasTree, workspaceKey]);
200
+
201
+ const [withdrawnFor, setWithdrawnFor] = useState<string | null>(null);
202
+ // A switch lifts a withdrawal for good, so coming back to that branch
203
+ // offers New page again; so does a withdrawal that lands after a switch.
204
+ if (withdrawnFor !== null && withdrawnFor !== workspaceKey) setWithdrawnFor(null);
205
+ const withdrawn = withdrawnFor === workspaceKey;
206
+ const withdraw = useCallback(() => setWithdrawnFor(workspaceKey), [workspaceKey]);
207
+
208
+ // What is on screen, the question to ask about it and the last answer, as
209
+ // of the latest render: a create holds `recheck` / `resolve` across waits,
210
+ // and must ask about the page on screen when it asks, not the one it was
211
+ // started on.
212
+ const screen = useRef({ askingFor, ask, answer, withdrawn });
213
+ useEffect(() => {
214
+ screen.current = { askingFor, ask, answer, withdrawn };
215
+ });
216
+ const recheck = useCallback(async (): Promise<string | null> => {
217
+ for (;;) {
218
+ const { folder, askedFor } = await screen.current.ask();
219
+ // The newest answer is about where the person was, not where they are
220
+ // (they moved inside the debounce): ask about where they are.
221
+ if (askedFor === null || askedFor === screen.current.askingFor) return folder;
222
+ }
223
+ }, []);
224
+ const resolve = useCallback(async (): Promise<string | null> => {
225
+ const { answer: last, askingFor: now, withdrawn: gone } = screen.current;
226
+ if (last.settled && last.askedFor === now) return gone ? null : last.folder;
227
+ return recheck();
228
+ }, [recheck]);
229
+
230
+ return {
231
+ folder: withdrawn ? null : answer.folder,
232
+ settled: answer.settled,
233
+ current: answer.settled && answer.askedFor === askingFor,
234
+ recheck,
235
+ resolve,
236
+ withdraw,
237
+ };
238
+ }