@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,206 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef } from 'react';
|
|
2
|
+
import { useLocation } from 'react-router-dom';
|
|
3
|
+
import { KNOWLEDGE_BASE_DIR, type FileTreeEntry } from '@bevel-software/platform-shared';
|
|
4
|
+
import { useWorkspace } from '../state/workspace.context';
|
|
5
|
+
import { WorkspaceApiError } from '../services/workspace.api';
|
|
6
|
+
import { branchFromPathname, useFileNav } from '../routing/kb-routes';
|
|
7
|
+
import { useMergedWorkspaceTree } from './useMergedWorkspaceTree';
|
|
8
|
+
import { pathExistsInTree } from '../utils/fileTree';
|
|
9
|
+
import { useKnowledgeWriteTarget } from './useKnowledgeWriteTarget';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* What "New page" writes: a title, so the page is a page from its first save,
|
|
13
|
+
* and a blank line under it for the cursor to land on.
|
|
14
|
+
*/
|
|
15
|
+
export const NEW_PAGE_CONTENT = '# Untitled\n\n';
|
|
16
|
+
|
|
17
|
+
/** `Untitled.md` for the first page, `Untitled N.md` after it. */
|
|
18
|
+
export function untitledPagePath(folder: string, n: number): string {
|
|
19
|
+
return n === 1 ? `${folder}/Untitled.md` : `${folder}/Untitled ${n}.md`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The first number, from `from` on, whose untitled name the tree does not
|
|
24
|
+
* already hold — a second New page makes a second page rather than reusing
|
|
25
|
+
* the first one's name.
|
|
26
|
+
*/
|
|
27
|
+
function freeUntitledNumber(tree: FileTreeEntry | null, folder: string, from = 1): number {
|
|
28
|
+
let n = from;
|
|
29
|
+
while (pathExistsInTree(tree, untitledPagePath(folder, n))) n++;
|
|
30
|
+
return n;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* How many names New page tries before it gives up. The tree it picks from
|
|
35
|
+
* can be a moment behind (a teammate or an agent creating pages too), so a
|
|
36
|
+
* refusal moves on to the next name; a handful covers any real race.
|
|
37
|
+
*/
|
|
38
|
+
const NEW_PAGE_ATTEMPTS = 5;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The exclusive create's "that name is taken": 409, for a file that exists
|
|
42
|
+
* now — or one somebody holds the lock on, which is a page being made there.
|
|
43
|
+
*/
|
|
44
|
+
function isNameTaken(err: unknown): boolean {
|
|
45
|
+
return err instanceof WorkspaceApiError && err.status === 409;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A refusal to write there: 403. Access rules and a protected branch's gate
|
|
50
|
+
* both answer it, so it is only a cue to ask again where the person may
|
|
51
|
+
* write — the fresh answer tells the two apart. Never shown either way.
|
|
52
|
+
*/
|
|
53
|
+
function isRefused(err: unknown): boolean {
|
|
54
|
+
return err instanceof WorkspaceApiError && err.status === 403;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface CreatePage {
|
|
58
|
+
/**
|
|
59
|
+
* The Knowledge folder, or null while there is nowhere safe to create a
|
|
60
|
+
* page: before the workspace has bootstrapped, and while the workspace on
|
|
61
|
+
* screen is not yet the branch the URL names.
|
|
62
|
+
*/
|
|
63
|
+
knowledgeRoot: string | null;
|
|
64
|
+
/**
|
|
65
|
+
* Create `Untitled.md` (or the next free `Untitled N.md`) in `pageFolder`
|
|
66
|
+
* and open it in edit mode. Resolves with the path, or with null when a
|
|
67
|
+
* write was refused and there is nowhere left to try: a fresh check found
|
|
68
|
+
* nowhere the person may write, or the refusal was not about the folder
|
|
69
|
+
* (New page then disappears; there is nothing to say). Rejects, for any
|
|
70
|
+
* failure but a refusal, with an Error whose message is ready to show
|
|
71
|
+
* ("Couldn’t create the page: …").
|
|
72
|
+
*/
|
|
73
|
+
createPage(): Promise<string | null>;
|
|
74
|
+
/**
|
|
75
|
+
* The Knowledge folder New page writes to (`useKnowledgeWriteTarget`): the
|
|
76
|
+
* Knowledge folder itself when the person may write it, else the folder of
|
|
77
|
+
* the page on screen, else the first they may write in file tree order.
|
|
78
|
+
* Null until the first check answers and when there is nowhere —
|
|
79
|
+
* everything that offers New page hides it while this is null. A later
|
|
80
|
+
* check (a tree or page change, a switch) keeps the last folder up until
|
|
81
|
+
* it answers, so nothing flickers; `createPage` asks again before writing
|
|
82
|
+
* when that answer is not for the page and branch on screen.
|
|
83
|
+
*/
|
|
84
|
+
pageFolder: string | null;
|
|
85
|
+
/** Whether that check has answered, so a list can wait for it rather than flash. */
|
|
86
|
+
pageFolderSettled: boolean;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* "New page", shared by everything that offers it — the Get set up list and
|
|
91
|
+
* the command menu — so both pick names the same way and neither can ever
|
|
92
|
+
* overwrite a page. It writes where the person may write (`pageFolder`),
|
|
93
|
+
* and a refused write asks again rather than showing who may write there.
|
|
94
|
+
*
|
|
95
|
+
* The create is EXCLUSIVE (`ifAbsent`): the name comes from the tree on
|
|
96
|
+
* screen, which may not yet show a page someone else just made, and a plain
|
|
97
|
+
* write would replace that page with an empty one. The page opens through
|
|
98
|
+
* `openWorkspacePath(path, { edit: true })`, which the viewer honours the way
|
|
99
|
+
* an Edit click is honoured — lock, fresh read and all.
|
|
100
|
+
*
|
|
101
|
+
* The caller owns how it says "busy" and where it shows a failure: the
|
|
102
|
+
* column keeps it on its step, the menu reopens with it.
|
|
103
|
+
*/
|
|
104
|
+
export function useCreatePage(): CreatePage {
|
|
105
|
+
const { kbDirName, createFile, workspaceBranch } = useWorkspace();
|
|
106
|
+
const { tree } = useMergedWorkspaceTree();
|
|
107
|
+
const { openWorkspacePath } = useFileNav();
|
|
108
|
+
const { pathname } = useLocation();
|
|
109
|
+
// Where the page would be WRITTEN against where it would OPEN. `createFile`
|
|
110
|
+
// writes to the workspace on screen (`workspaceBranch`), while the page
|
|
111
|
+
// opens on the branch the URL names; mid-switch those differ, the URL
|
|
112
|
+
// already naming the destination. A page made in that gap lands on the
|
|
113
|
+
// branch the person just left, and the viewer then looks for it on the new
|
|
114
|
+
// one and finds nothing. So there is no folder to create in until the two
|
|
115
|
+
// agree. No branch in the URL (the Library) means the workspace on screen
|
|
116
|
+
// is the destination, as `openWorkspacePath` reads it.
|
|
117
|
+
const destination = branchFromPathname(pathname) ?? workspaceBranch;
|
|
118
|
+
const ready = kbDirName !== null && workspaceBranch !== null && destination === workspaceBranch;
|
|
119
|
+
const knowledgeRoot = ready ? `${kbDirName}/${KNOWLEDGE_BASE_DIR}` : null;
|
|
120
|
+
|
|
121
|
+
// Where the person may write is asked of the workspace on screen whatever
|
|
122
|
+
// the URL says, so the list and the menu know it mid-switch too; only the
|
|
123
|
+
// create waits for the two to agree.
|
|
124
|
+
const {
|
|
125
|
+
folder: pageFolder,
|
|
126
|
+
settled: pageFolderSettled,
|
|
127
|
+
recheck,
|
|
128
|
+
resolve,
|
|
129
|
+
withdraw,
|
|
130
|
+
} = useKnowledgeWriteTarget(kbDirName !== null ? `${kbDirName}/${KNOWLEDGE_BASE_DIR}` : null);
|
|
131
|
+
|
|
132
|
+
// What a create was started on. A switch of branch while it waits (for a
|
|
133
|
+
// recheck, for a refused attempt) would otherwise let it write to the
|
|
134
|
+
// workspace it captured and open the page back on the branch just left;
|
|
135
|
+
// after every wait it checks it is still where it started, and stops if
|
|
136
|
+
// not. The page it may have made by then stays where it was made.
|
|
137
|
+
const liveKey = `${workspaceBranch}\n${destination}`;
|
|
138
|
+
const live = useRef(liveKey);
|
|
139
|
+
useEffect(() => {
|
|
140
|
+
live.current = liveKey;
|
|
141
|
+
}, [liveKey]);
|
|
142
|
+
|
|
143
|
+
const createPage = useCallback(async (): Promise<string | null> => {
|
|
144
|
+
if (!knowledgeRoot || !pageFolderSettled) {
|
|
145
|
+
throw new Error('Couldn’t create the page: the workspace is still loading.');
|
|
146
|
+
}
|
|
147
|
+
const startedOn = liveKey;
|
|
148
|
+
const moved = (): boolean => live.current !== startedOn;
|
|
149
|
+
// The folder for the page and branch on screen now (`resolve`): the last
|
|
150
|
+
// answer when it is about them, else asked again — never a folder shown
|
|
151
|
+
// for a page just left during the check's debounce.
|
|
152
|
+
let folder = await resolve();
|
|
153
|
+
if (moved()) return null;
|
|
154
|
+
// Folders a write was refused in, this create: each refusal asks again,
|
|
155
|
+
// and a fresh answer naming one of these says the refusal is not about
|
|
156
|
+
// the folder.
|
|
157
|
+
// Every folder this loop writes comes from `resolve` or a refusal's
|
|
158
|
+
// `recheck`, both answered for the page and branch on screen when they
|
|
159
|
+
// return (see `useKnowledgeWriteTarget`).
|
|
160
|
+
const refused = new Set<string>();
|
|
161
|
+
while (folder) {
|
|
162
|
+
let n = freeUntitledNumber(tree, folder);
|
|
163
|
+
let path = untitledPagePath(folder, n);
|
|
164
|
+
for (let attempt = 1; ; attempt++) {
|
|
165
|
+
if (moved()) return null;
|
|
166
|
+
try {
|
|
167
|
+
await createFile(path, NEW_PAGE_CONTENT, { ifAbsent: true });
|
|
168
|
+
// Made, but the person has moved on: it is not opened over them.
|
|
169
|
+
if (moved()) return path;
|
|
170
|
+
openWorkspacePath(path, { edit: true });
|
|
171
|
+
return path;
|
|
172
|
+
} catch (err) {
|
|
173
|
+
if (isNameTaken(err) && attempt < NEW_PAGE_ATTEMPTS) {
|
|
174
|
+
n = freeUntitledNumber(tree, folder, n + 1);
|
|
175
|
+
path = untitledPagePath(folder, n);
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
// A refusal is never shown: who may write where is nobody's business
|
|
179
|
+
// here. Access changed since the check, so ask again and move on to
|
|
180
|
+
// the next folder they may write — or, with none, New page is gone.
|
|
181
|
+
if (isRefused(err)) {
|
|
182
|
+
refused.add(folder);
|
|
183
|
+
const next = await recheck();
|
|
184
|
+
if (moved()) return null;
|
|
185
|
+
if (next && !refused.has(next)) {
|
|
186
|
+
folder = next;
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
// The check still says they may write where they were refused:
|
|
190
|
+
// the branch, not the folder, said no, and no folder here would
|
|
191
|
+
// take the page. New page goes rather than fail on every click.
|
|
192
|
+
if (next) withdraw();
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
// And should any other failure name who may write, that goes too.
|
|
196
|
+
const msg = (err instanceof Error ? err.message : String(err)).replace(/\s*Eligible:[\s\S]*$/, '');
|
|
197
|
+
throw new Error(`Couldn’t create the page: ${msg}`, { cause: err });
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
// Nowhere to write: the fresh answer has hidden New page, which says it.
|
|
202
|
+
return null;
|
|
203
|
+
}, [knowledgeRoot, liveKey, pageFolderSettled, resolve, recheck, withdraw, tree, createFile, openWorkspacePath]);
|
|
204
|
+
|
|
205
|
+
return { knowledgeRoot, createPage, pageFolder, pageFolderSettled };
|
|
206
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { useCallback } from 'react';
|
|
1
|
+
import { useCallback, useContext } from 'react';
|
|
2
2
|
import { resolveKbHref } from '../routing/kb-routes';
|
|
3
3
|
import { rawFileUrl } from '../services/workspace.api';
|
|
4
|
-
import {
|
|
4
|
+
import { WorkspaceContext } from '../state/workspace.context';
|
|
5
|
+
import { useRendererSurface } from '../components/renderers/rendererSurface';
|
|
5
6
|
import { useImageRevision } from './useImageRevision';
|
|
6
7
|
import type { KbImageResolver } from '../components/renderers/kbMarkdownPipeline';
|
|
7
8
|
|
|
@@ -12,7 +13,7 @@ import type { KbImageResolver } from '../components/renderers/kbMarkdownPipeline
|
|
|
12
13
|
* not ours to serve.
|
|
13
14
|
*
|
|
14
15
|
* There were three copies of this — the markdown renderer, the review panel
|
|
15
|
-
* and the skill page — each spelling out resolve → kind-check →
|
|
16
|
+
* and the skill page — each spelling out resolve → kind-check → the raw URL
|
|
16
17
|
* with its own workspace id. That is the same drift-shape this PR removed for
|
|
17
18
|
* the LINK handlers (one decoded its href, one did not) and it was already
|
|
18
19
|
* paying out: the repair-heuristic bug below had to be fixed in one place, not
|
|
@@ -23,6 +24,11 @@ import type { KbImageResolver } from '../components/renderers/kbMarkdownPipeline
|
|
|
23
24
|
* branch's tree no matter which branch is checked out. `basePath` is the file
|
|
24
25
|
* the image sits in; null means "no file yet" and every src resolves to null.
|
|
25
26
|
*
|
|
27
|
+
* On a {@link RendererSurface} — the embed — the bytes come from that
|
|
28
|
+
* surface's own route instead, because there is no session to send; the
|
|
29
|
+
* grammar above is unchanged. `workspaceId` is then not consulted at all: a
|
|
30
|
+
* surface shows one page of one branch, and the token is what names it.
|
|
31
|
+
*
|
|
26
32
|
* The image revision is folded into the URL as `&v=` so a teammate replacing
|
|
27
33
|
* a picture under the same name reaches an open tab. `useImageRevision`
|
|
28
34
|
* subscribes to `workspaceId` — including one the session is not focused on,
|
|
@@ -33,11 +39,20 @@ export function useWorkspaceImageResolver(
|
|
|
33
39
|
workspaceId: string | null,
|
|
34
40
|
basePath: string | null,
|
|
35
41
|
): KbImageResolver {
|
|
36
|
-
const
|
|
42
|
+
const surface = useRendererSurface();
|
|
43
|
+
// Read optionally: a surface knows its own `kbDirName` and stands up no
|
|
44
|
+
// workspace provider.
|
|
45
|
+
const workspaceKbDirName = useContext(WorkspaceContext)?.kbDirName ?? null;
|
|
46
|
+
const kbDirName = surface?.kbDirName ?? workspaceKbDirName;
|
|
37
47
|
const revision = useImageRevision(workspaceId);
|
|
38
48
|
return useCallback<KbImageResolver>(
|
|
39
49
|
(src) => {
|
|
40
|
-
|
|
50
|
+
// In the app, the workspace this resolver was POINTED AT — which is not
|
|
51
|
+
// always the one on screen (the skill page renders the default branch's
|
|
52
|
+
// tree from a suggestion branch), so it must stay the resolver's own
|
|
53
|
+
// argument and never be re-derived from a renderer context.
|
|
54
|
+
if (!surface && !workspaceId) return null;
|
|
55
|
+
if (basePath === null || kbDirName === null) return null;
|
|
41
56
|
// `repairMangledPath: false` — the junk-segment repair is for LINK
|
|
42
57
|
// destinations a model may have mangled. An image src is a path the
|
|
43
58
|
// author wrote, and the repair rewrites any path with a later segment
|
|
@@ -46,10 +61,12 @@ export function useWorkspaceImageResolver(
|
|
|
46
61
|
const target = resolveKbHref(src, { basePath, kbDirName, repairMangledPath: false });
|
|
47
62
|
if (target?.kind !== 'workspace') return null;
|
|
48
63
|
return {
|
|
49
|
-
src:
|
|
64
|
+
src: surface
|
|
65
|
+
? surface.rawUrl(target.path, { version: revision })
|
|
66
|
+
: rawFileUrl(workspaceId!, target.path, { version: revision }),
|
|
50
67
|
path: target.path,
|
|
51
68
|
};
|
|
52
69
|
},
|
|
53
|
-
[workspaceId, basePath, kbDirName, revision],
|
|
70
|
+
[surface, workspaceId, basePath, kbDirName, revision],
|
|
54
71
|
);
|
|
55
72
|
}
|
|
@@ -43,6 +43,8 @@ import { contentChanged } from '../utils/diff';
|
|
|
43
43
|
import { isUploadNoise, walkEntries, type DroppedItem } from '../utils/readDroppedEntries';
|
|
44
44
|
import { tabsKey, type PersistedTabState } from '../utils/tab-persistence';
|
|
45
45
|
import { traceFiles } from '../utils/file-trace';
|
|
46
|
+
import { displayFileName } from '../utils/display-file-name';
|
|
47
|
+
import { useConfirm } from '../../../shared/components';
|
|
46
48
|
|
|
47
49
|
const PERSIST_DEBOUNCE_MS = 200;
|
|
48
50
|
// Bounded concurrency cap for upload requests. The server serializes git
|
|
@@ -274,6 +276,11 @@ export function useWorkspaceState(): UseWorkspaceStateReturn {
|
|
|
274
276
|
* workspaceId it was started with against this before touching state.
|
|
275
277
|
*/
|
|
276
278
|
const workspaceIdRef = useRef<string | null>(workspaceId);
|
|
279
|
+
// Unsaved-work questions are asked in the app's own dialog and awaited.
|
|
280
|
+
const confirm = useConfirm();
|
|
281
|
+
// Paths whose close is waiting on that question: a second close of the same
|
|
282
|
+
// tab while it is asked must not queue the same question twice.
|
|
283
|
+
const askingClosePathsRef = useRef(new Set<string>());
|
|
277
284
|
useEffect(() => { openTabsRef.current = openTabs; }, [openTabs]);
|
|
278
285
|
useEffect(() => { activeTabPathRef.current = activeTabPath; }, [activeTabPath]);
|
|
279
286
|
useEffect(() => { fileTreeRef.current = fileTree; }, [fileTree]);
|
|
@@ -508,11 +515,31 @@ export function useWorkspaceState(): UseWorkspaceStateReturn {
|
|
|
508
515
|
|
|
509
516
|
const closeTab = useCallback(async (
|
|
510
517
|
tab: OpenTab,
|
|
511
|
-
options?: { skipConfirm?: boolean },
|
|
518
|
+
options?: { skipConfirm?: boolean; workspaceId?: string | null },
|
|
512
519
|
): Promise<{ closed: boolean; newActivePath: string | null }> => {
|
|
520
|
+
// Pin the workspace the close was decided in. The question waits on the
|
|
521
|
+
// person; if the workspace moves to another branch meanwhile, a tab with
|
|
522
|
+
// the same path there is not the one they agreed to close.
|
|
523
|
+
const closeWorkspaceId =
|
|
524
|
+
options?.workspaceId !== undefined ? options.workspaceId : workspaceIdRef.current;
|
|
525
|
+
const notClosed = () => ({ closed: false, newActivePath: activeTabPathRef.current });
|
|
526
|
+
if (workspaceIdRef.current !== closeWorkspaceId) return notClosed();
|
|
513
527
|
if (tab.isDirty && !options?.skipConfirm) {
|
|
514
|
-
|
|
515
|
-
if (
|
|
528
|
+
// The name the tab shows, so this prompt and the bulk one agree.
|
|
529
|
+
if (askingClosePathsRef.current.has(tab.path)) return notClosed();
|
|
530
|
+
askingClosePathsRef.current.add(tab.path);
|
|
531
|
+
let confirmed: boolean;
|
|
532
|
+
try {
|
|
533
|
+
({ confirmed } = await confirm({
|
|
534
|
+
title: 'Unsaved changes',
|
|
535
|
+
message: UNSAVED_TAB_WARNING(displayFileName(tab.path, kbDirName)),
|
|
536
|
+
confirmLabel: 'Close anyway',
|
|
537
|
+
destructive: true,
|
|
538
|
+
}));
|
|
539
|
+
} finally {
|
|
540
|
+
askingClosePathsRef.current.delete(tab.path);
|
|
541
|
+
}
|
|
542
|
+
if (!confirmed || workspaceIdRef.current !== closeWorkspaceId) return notClosed();
|
|
516
543
|
}
|
|
517
544
|
const tabs = openTabsRef.current;
|
|
518
545
|
const idx = tabs.findIndex((t) => t.path === tab.path);
|
|
@@ -527,7 +554,7 @@ export function useWorkspaceState(): UseWorkspaceStateReturn {
|
|
|
527
554
|
setActiveTabPath(newActivePath);
|
|
528
555
|
}
|
|
529
556
|
return { closed: true, newActivePath };
|
|
530
|
-
}, []);
|
|
557
|
+
}, [kbDirName, confirm]);
|
|
531
558
|
|
|
532
559
|
const closeAllTabs = useCallback(() => {
|
|
533
560
|
setOpenTabs([]);
|
|
@@ -694,9 +721,9 @@ export function useWorkspaceState(): UseWorkspaceStateReturn {
|
|
|
694
721
|
|
|
695
722
|
// ── Working-tree mutations ────────────────────────────────────────────────
|
|
696
723
|
|
|
697
|
-
const createFile = useCallback(async (relativePath: string, content?: string) => {
|
|
724
|
+
const createFile = useCallback(async (relativePath: string, content?: string, options?: { ifAbsent?: boolean }) => {
|
|
698
725
|
if (!workspaceId) return;
|
|
699
|
-
await writeFile(workspaceId, relativePath, content ?? '');
|
|
726
|
+
await writeFile(workspaceId, relativePath, content ?? '', options);
|
|
700
727
|
await refreshFileTree();
|
|
701
728
|
bumpFs();
|
|
702
729
|
}, [workspaceId, refreshFileTree, bumpFs]);
|
|
@@ -1232,8 +1259,15 @@ export function useWorkspaceState(): UseWorkspaceStateReturn {
|
|
|
1232
1259
|
);
|
|
1233
1260
|
const dirty = toClose.filter((t) => t.isDirty);
|
|
1234
1261
|
if (dirty.length > 0) {
|
|
1235
|
-
const confirmed =
|
|
1262
|
+
const { confirmed } = await confirm({
|
|
1263
|
+
title: 'Unsaved changes',
|
|
1264
|
+
message: UNSAVED_TABS_BULK_WARNING(dirty.map((t) => displayFileName(t.path, kbDirName))),
|
|
1265
|
+
confirmLabel: 'Close anyway',
|
|
1266
|
+
destructive: true,
|
|
1267
|
+
});
|
|
1236
1268
|
if (!confirmed) return false;
|
|
1269
|
+
// The question waited on the person; the workspace may have moved on.
|
|
1270
|
+
if (!isCurrent()) return false;
|
|
1237
1271
|
}
|
|
1238
1272
|
// Optimistic tree removal. A folder delete on the server takes 2-3s
|
|
1239
1273
|
// (N per-file commits batched into one push) — without optimism the
|
|
@@ -1276,7 +1310,7 @@ export function useWorkspaceState(): UseWorkspaceStateReturn {
|
|
|
1276
1310
|
// server state. The backend's end-of-batch `fs-tree-changed` SSE
|
|
1277
1311
|
// event triggers a refresh anyway as belt-and-suspenders. No second
|
|
1278
1312
|
// `bumpFs()` either — the optimistic bump above already counted.
|
|
1279
|
-
}, [workspaceId, refreshFileTree, bumpFs]);
|
|
1313
|
+
}, [workspaceId, kbDirName, refreshFileTree, bumpFs, confirm]);
|
|
1280
1314
|
|
|
1281
1315
|
const reloadTabFromDisk = useCallback(async (relativePath: string) => {
|
|
1282
1316
|
if (!workspaceId) return;
|
|
@@ -111,6 +111,16 @@ describe('useFileNav.openFile', () => {
|
|
|
111
111
|
);
|
|
112
112
|
});
|
|
113
113
|
|
|
114
|
+
it('asks the viewer for the editor through router state, never through the URL', () => {
|
|
115
|
+
navigateMock.mockClear();
|
|
116
|
+
const { result } = renderNav('alice/draft');
|
|
117
|
+
result.current.openWorkspacePath('knowledge-base/KnowledgeBase/Untitled.md', { edit: true });
|
|
118
|
+
expect(navigateMock).toHaveBeenCalledWith(
|
|
119
|
+
'/workspace/alice%2Fdraft/knowledge-base/KnowledgeBase/Untitled.md',
|
|
120
|
+
{ state: { startEditing: true, startEditingPath: 'knowledge-base/KnowledgeBase/Untitled.md' } },
|
|
121
|
+
);
|
|
122
|
+
});
|
|
123
|
+
|
|
114
124
|
it('opens a tree path verbatim even when a folder inside it is named like the kbDirName', () => {
|
|
115
125
|
navigateMock.mockClear();
|
|
116
126
|
const { result } = renderNav('alice/draft');
|