@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
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
type FileTreeEntry,
|
|
6
6
|
} from '@bevel-software/platform-shared';
|
|
7
7
|
import type { PendingEntry } from '../state/workspace.context';
|
|
8
|
+
import { isAccessRulesFile as isAccessRulesPath } from './display-file-name';
|
|
8
9
|
|
|
9
10
|
// RESERVED is not the same as CREATED (see kb-layout.ts): core only seeds
|
|
10
11
|
// KnowledgeBase/, Skills/ and Plugins/, but every reserved name renders as its
|
|
@@ -93,30 +94,58 @@ const READABLE_PAGE = /\.(md|markdown)$/i;
|
|
|
93
94
|
* and any governed folder carries one), so the root-file exclusion below
|
|
94
95
|
* never reaches it. Nobody opens a knowledge base to read who may edit it.
|
|
95
96
|
*/
|
|
96
|
-
const
|
|
97
|
-
const isAccessRulesFile = (entry: FileTreeEntry): boolean => entry.name.toLowerCase() === ACCESS_RULES_FILE;
|
|
97
|
+
const isAccessRulesFile = (entry: FileTreeEntry): boolean => isAccessRulesPath(entry.relativePath);
|
|
98
98
|
|
|
99
99
|
/**
|
|
100
100
|
* Pages worth offering to someone who has nothing open: the documents nearest
|
|
101
101
|
* the top of the knowledge tree, breadth-first, so the opening suggestion is a
|
|
102
102
|
* section heading rather than the fifth file inside the first folder.
|
|
103
103
|
*
|
|
104
|
-
* Scoped to exactly what the explorer browses under "Knowledge"
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
104
|
+
* Scoped to exactly what the explorer browses under "Knowledge" — see
|
|
105
|
+
* `knowledgeFiles` for what that scope is and why.
|
|
106
|
+
*
|
|
107
|
+
* Fewer than `limit` — including none at all — is a legitimate answer for a
|
|
108
|
+
* knowledge base that is still empty; the caller says so rather than padding.
|
|
109
|
+
*/
|
|
110
|
+
export function suggestedPages(
|
|
111
|
+
tree: FileTreeEntry | null,
|
|
112
|
+
kbDirName: string | null,
|
|
113
|
+
limit: number,
|
|
114
|
+
): FileTreeEntry[] {
|
|
115
|
+
return walkKnowledge(tree, kbDirName, (entry) => READABLE_PAGE.test(entry.name), limit);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Every file the explorer browses under "Knowledge", breadth-first — what the
|
|
120
|
+
* toolbar's search palette finds pages by name in. Not only documents: a
|
|
121
|
+
* spreadsheet or a PDF has a row in the explorer, and a search that could not
|
|
122
|
+
* find what the tree beside it shows would read as a broken search. The same
|
|
123
|
+
* exclusions as `suggestedPages` apply, because they are about what the
|
|
124
|
+
* explorer is FOR rather than about file types.
|
|
125
|
+
*
|
|
126
|
+
* Scoped to the CHECKOUT: `KnowledgeBase/` plus any content folder beside it
|
|
127
|
+
* inside `<kbDirName>/`. `Plugins/` is the Skills & Tools app's storage and is
|
|
128
|
+
* not a browsing destination here, and the loose files at the checkout root
|
|
108
129
|
* (`access.md`, `roles.yaml`) are how the deployment is configured, not
|
|
109
130
|
* something to read. A clone that predates the split has no named roots at
|
|
110
131
|
* all, so its whole checkout is the knowledge — including the loose files at
|
|
111
132
|
* its root, which in that layout are pages rather than configuration. A
|
|
112
133
|
* workspace with no checkout in it has nothing to offer.
|
|
113
|
-
*
|
|
114
|
-
* Fewer than `limit` — including none at all — is a legitimate answer for a
|
|
115
|
-
* knowledge base that is still empty; the caller says so rather than padding.
|
|
116
134
|
*/
|
|
117
|
-
export function
|
|
135
|
+
export function knowledgeFiles(tree: FileTreeEntry | null, kbDirName: string | null): FileTreeEntry[] {
|
|
136
|
+
return walkKnowledge(tree, kbDirName, () => true, Infinity);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The walk both of the above share: breadth-first from the knowledge roots,
|
|
141
|
+
* stopping once `limit` files have been accepted. Dot-prefixed entries,
|
|
142
|
+
* `access.md` files and folders named after a reserved root are skipped at
|
|
143
|
+
* every depth before `accept` is asked.
|
|
144
|
+
*/
|
|
145
|
+
function walkKnowledge(
|
|
118
146
|
tree: FileTreeEntry | null,
|
|
119
147
|
kbDirName: string | null,
|
|
148
|
+
accept: (file: FileTreeEntry) => boolean,
|
|
120
149
|
limit: number,
|
|
121
150
|
): FileTreeEntry[] {
|
|
122
151
|
const kbRoot = checkoutRoot(tree, kbDirName);
|
|
@@ -142,7 +171,7 @@ export function suggestedPages(
|
|
|
142
171
|
// Dot-prefixed entries are the repository's own bookkeeping.
|
|
143
172
|
if (entry.name.startsWith('.')) continue;
|
|
144
173
|
if (entry.type === 'file') {
|
|
145
|
-
if (
|
|
174
|
+
if (!isAccessRulesFile(entry) && accept(entry)) pages.push(entry);
|
|
146
175
|
} else if (level === roots || !KB_ROOT_DIRS.has(entry.name)) {
|
|
147
176
|
// Below the roots, the reserved SET decides — not two names: a folder
|
|
148
177
|
// named after any reserved root, at any depth, is the same kind of
|
|
@@ -156,11 +185,17 @@ export function suggestedPages(
|
|
|
156
185
|
return pages.slice(0, limit);
|
|
157
186
|
}
|
|
158
187
|
|
|
159
|
-
|
|
188
|
+
/**
|
|
189
|
+
* The entry at exactly `relativePath` (a file or a folder), or null when the
|
|
190
|
+
* tree has none. It descends only into the folder on the path: an entry's
|
|
191
|
+
* path is its folder's plus its own name, so no other subtree can hold it,
|
|
192
|
+
* and a caller asking on every render does not walk the whole checkout.
|
|
193
|
+
*/
|
|
194
|
+
export function findEntryByPath(tree: FileTreeEntry | null, relativePath: string): FileTreeEntry | null {
|
|
160
195
|
if (!tree) return null;
|
|
161
196
|
if (tree.relativePath === relativePath) return tree;
|
|
162
|
-
|
|
163
|
-
|
|
197
|
+
for (const child of tree.children ?? []) {
|
|
198
|
+
if (relativePath === child.relativePath || relativePath.startsWith(`${child.relativePath}/`)) {
|
|
164
199
|
const found = findEntryByPath(child, relativePath);
|
|
165
200
|
if (found) return found;
|
|
166
201
|
}
|
|
@@ -2,8 +2,10 @@ import {
|
|
|
2
2
|
currentKbLayout,
|
|
3
3
|
isPlatformFile,
|
|
4
4
|
isPlatformRestoreShape,
|
|
5
|
+
isRepositoryOwnFile,
|
|
5
6
|
isRootPlatformFile,
|
|
6
7
|
platformFileRefusal,
|
|
8
|
+
repositoryOwnFileDeleteRefusal,
|
|
7
9
|
type FileTreeEntry,
|
|
8
10
|
} from '@bevel-software/platform-shared';
|
|
9
11
|
import {
|
|
@@ -47,6 +49,19 @@ export function platformFileMoveRefusal(
|
|
|
47
49
|
return isPlatformFile(rel, currentKbLayout()) ? platformFileRefusal(rel) : null;
|
|
48
50
|
}
|
|
49
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Why this row may not be DELETED whoever asks — the same sentence the server
|
|
54
|
+
* refuses with — or null when the caller's write access decides. Only the
|
|
55
|
+
* repository's own files are refused: the root's `access.md` and
|
|
56
|
+
* `roles.yaml`. A nested `access.md` is deleted like any file its caller may
|
|
57
|
+
* write, which hands its folder back to the parent's rules.
|
|
58
|
+
*/
|
|
59
|
+
export function platformFileDeleteRefusal(wsRelativePath: string, kbDirName: string | null): string | null {
|
|
60
|
+
const rel = repoRelative(wsRelativePath, kbDirName);
|
|
61
|
+
if (rel === null) return null;
|
|
62
|
+
return isRepositoryOwnFile(rel, currentKbLayout()) ? repositoryOwnFileDeleteRefusal(rel) : null;
|
|
63
|
+
}
|
|
64
|
+
|
|
50
65
|
/**
|
|
51
66
|
* Why this row may not be DRAGGED, or null when it may — the rename refusal
|
|
52
67
|
* minus the one move that is a repair.
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
import { describe, it, expect } from 'vitest';
|
|
3
|
+
import { readdirSync, readFileSync, statSync } from 'node:fs';
|
|
4
|
+
import { join, dirname, relative, resolve } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { ESLint } from 'eslint';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The browser's built-in `confirm()` does not come back.
|
|
10
|
+
*
|
|
11
|
+
* The browser can switch it off ("prevent this page from creating additional
|
|
12
|
+
* dialogs"), after which every call answers false without showing anything —
|
|
13
|
+
* the branch delete stopped working that way. The rule lives in the repo's
|
|
14
|
+
* `eslint.config.js`; `pnpm lint` is not part of CI, so this suite runs that
|
|
15
|
+
* same rule from that same config over the package's source and apps/web's,
|
|
16
|
+
* and the test job fails on a new use.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const SRC = join(dirname(fileURLToPath(import.meta.url)), '..', '..');
|
|
20
|
+
const REPO_ROOT = resolve(SRC, '..', '..', '..');
|
|
21
|
+
// The rule covers apps/web too (it mounts the app), so the scan does.
|
|
22
|
+
const WEB_SRC = join(REPO_ROOT, 'apps', 'web', 'src');
|
|
23
|
+
const RULES = new Set(['no-restricted-globals', 'no-restricted-properties']);
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* ONE instance for the whole suite. Building one loads the repo's flat config
|
|
27
|
+
* and the typescript parser — seconds on a slow machine — and the first case
|
|
28
|
+
* below lints four fixtures, which built four of them and ran past the
|
|
29
|
+
* default timeout on Windows. The config does not change between cases.
|
|
30
|
+
*/
|
|
31
|
+
let instance: ESLint | null = null;
|
|
32
|
+
function eslint(): ESLint {
|
|
33
|
+
instance ??= new ESLint({
|
|
34
|
+
cwd: REPO_ROOT,
|
|
35
|
+
ruleFilter: ({ ruleId }) => RULES.has(ruleId),
|
|
36
|
+
});
|
|
37
|
+
return instance;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Lint `code` as if it were a file in this package, with the repo's config. */
|
|
41
|
+
async function lintFixture(code: string, dir = SRC): Promise<string[]> {
|
|
42
|
+
const [result] = await eslint().lintText(code, {
|
|
43
|
+
filePath: join(dir, '__lint_fixture__.tsx'),
|
|
44
|
+
});
|
|
45
|
+
return result.messages.map((m) => `${m.ruleId}:${m.line}`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function sourceFiles(dir: string, out: string[] = []): string[] {
|
|
49
|
+
for (const entry of readdirSync(dir)) {
|
|
50
|
+
const full = join(dir, entry);
|
|
51
|
+
if (statSync(full).isDirectory()) sourceFiles(full, out);
|
|
52
|
+
else if (/\.tsx?$/.test(entry)) out.push(full);
|
|
53
|
+
}
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
describe('lint: no built-in confirm', () => {
|
|
58
|
+
it('rejects the global confirm() and window.confirm()', async () => {
|
|
59
|
+
expect(await lintFixture("export const a = confirm('Delete?');\n")).toEqual([
|
|
60
|
+
'no-restricted-globals:1',
|
|
61
|
+
]);
|
|
62
|
+
expect(await lintFixture("export const b = window.confirm('Delete?');\n")).toEqual([
|
|
63
|
+
'no-restricted-properties:1',
|
|
64
|
+
]);
|
|
65
|
+
expect(await lintFixture("export const c = globalThis.confirm('Delete?');\n")).toEqual([
|
|
66
|
+
'no-restricted-properties:1',
|
|
67
|
+
]);
|
|
68
|
+
// apps/web is under the same rule.
|
|
69
|
+
expect(await lintFixture("export const d = confirm('Delete?');\n", WEB_SRC)).toEqual([
|
|
70
|
+
'no-restricted-globals:1',
|
|
71
|
+
]);
|
|
72
|
+
// The first lint of the suite pays for loading the config and the parser.
|
|
73
|
+
}, 60_000);
|
|
74
|
+
|
|
75
|
+
it("allows a local function called confirm (useConfirm()'s result, the file tree's)", async () => {
|
|
76
|
+
const code = [
|
|
77
|
+
"import { useConfirm } from './shared/components';",
|
|
78
|
+
'export function useThing() {',
|
|
79
|
+
' const confirm = useConfirm();',
|
|
80
|
+
" return () => confirm({ title: 'Delete', message: 'Delete?' });",
|
|
81
|
+
'}',
|
|
82
|
+
'',
|
|
83
|
+
].join('\n');
|
|
84
|
+
expect(await lintFixture(code)).toEqual([]);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('finds no built-in confirm anywhere in core-frontend or apps/web', async () => {
|
|
88
|
+
// Only files that mention the word can break the rule; lint just those so
|
|
89
|
+
// the suite stays quick, with the real config.
|
|
90
|
+
const candidates = [...sourceFiles(SRC), ...sourceFiles(WEB_SRC)].filter((f) =>
|
|
91
|
+
/\bconfirm\b/.test(readFileSync(f, 'utf8')),
|
|
92
|
+
);
|
|
93
|
+
expect(candidates.length).toBeGreaterThan(0);
|
|
94
|
+
const results = await eslint().lintFiles(candidates);
|
|
95
|
+
const hits = results.flatMap((r) =>
|
|
96
|
+
r.messages
|
|
97
|
+
.filter((m) => m.ruleId && RULES.has(m.ruleId))
|
|
98
|
+
.map((m) => `${relative(REPO_ROOT, r.filePath)}:${m.line} ${m.message}`),
|
|
99
|
+
);
|
|
100
|
+
expect(hits).toEqual([]);
|
|
101
|
+
}, 120_000);
|
|
102
|
+
});
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { useCallback, useEffect, useId, useRef, useState, type ReactNode } from 'react';
|
|
2
|
+
import { createPortal } from 'react-dom';
|
|
3
|
+
import { Button } from './Button';
|
|
4
|
+
import { Dialog } from './Dialog';
|
|
5
|
+
import { useLatestRef } from './useLatestRef';
|
|
6
|
+
import {
|
|
7
|
+
ConfirmContext,
|
|
8
|
+
type AskFn,
|
|
9
|
+
type ConfirmAnswer,
|
|
10
|
+
type ConfirmRequest,
|
|
11
|
+
} from './confirm-context';
|
|
12
|
+
|
|
13
|
+
interface Pending {
|
|
14
|
+
id: number;
|
|
15
|
+
request: ConfirmRequest;
|
|
16
|
+
resolve(answer: ConfirmAnswer): void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const CANCELLED: ConfirmAnswer = { confirmed: false, dontAskAgain: false };
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Hosts the app's confirmation dialog; mount it once, near the root. Anything
|
|
23
|
+
* below asks through `useConfirm()`.
|
|
24
|
+
*
|
|
25
|
+
* One question shows at a time. A second request waits behind the first
|
|
26
|
+
* rather than stacking two dialogs, so two closes in quick succession are
|
|
27
|
+
* asked in turn.
|
|
28
|
+
*/
|
|
29
|
+
export function ConfirmProvider({ children }: { children: ReactNode }) {
|
|
30
|
+
const [queue, setQueue] = useState<Pending[]>([]);
|
|
31
|
+
const nextId = useRef(0);
|
|
32
|
+
// Mirrors `queue` for `answer` and the unmount sweep below, which must not
|
|
33
|
+
// depend on it. Written in a layout effect: a click on a dialog that has
|
|
34
|
+
// just come up from the queue must already see it at the head.
|
|
35
|
+
const queueRef = useLatestRef(queue);
|
|
36
|
+
|
|
37
|
+
const confirm = useCallback<AskFn>(
|
|
38
|
+
(request, signal) =>
|
|
39
|
+
new Promise<ConfirmAnswer>((resolve) => {
|
|
40
|
+
// The asker is already gone: nobody is there to confirm.
|
|
41
|
+
if (signal.aborted) {
|
|
42
|
+
resolve(CANCELLED);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
nextId.current += 1;
|
|
46
|
+
const id = nextId.current;
|
|
47
|
+
// The asker went away with the question open (or queued): take it
|
|
48
|
+
// down and answer Cancel, so the gone view's action never runs.
|
|
49
|
+
const withdraw = () => {
|
|
50
|
+
resolve(CANCELLED);
|
|
51
|
+
setQueue((q) => q.filter((p) => p.id !== id));
|
|
52
|
+
};
|
|
53
|
+
signal.addEventListener('abort', withdraw, { once: true });
|
|
54
|
+
const settle = (answer: ConfirmAnswer) => {
|
|
55
|
+
signal.removeEventListener('abort', withdraw);
|
|
56
|
+
resolve(answer);
|
|
57
|
+
};
|
|
58
|
+
setQueue((q) => [...q, { id, request, resolve: settle }]);
|
|
59
|
+
}),
|
|
60
|
+
[],
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
// A provider that goes away with questions still open answers them Cancel,
|
|
64
|
+
// so nothing awaiting one hangs forever.
|
|
65
|
+
useEffect(
|
|
66
|
+
() => () => {
|
|
67
|
+
for (const p of queueRef.current) p.resolve(CANCELLED);
|
|
68
|
+
},
|
|
69
|
+
// The ref is stable for the provider's life; listed for the linter.
|
|
70
|
+
[queueRef],
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
const current = queue[0];
|
|
74
|
+
const answer = useCallback((id: number, value: ConfirmAnswer) => {
|
|
75
|
+
const head = queueRef.current[0];
|
|
76
|
+
if (!head || head.id !== id) return;
|
|
77
|
+
head.resolve(value);
|
|
78
|
+
setQueue((q) => (q[0]?.id === id ? q.slice(1) : q));
|
|
79
|
+
}, [queueRef]);
|
|
80
|
+
|
|
81
|
+
return (
|
|
82
|
+
<ConfirmContext.Provider value={confirm}>
|
|
83
|
+
{children}
|
|
84
|
+
{/* Portaled to <body> at the moment it opens, so it lands after any
|
|
85
|
+
portaled panel already there (the branch switcher's list is one)
|
|
86
|
+
and draws above it at the same z-index. */}
|
|
87
|
+
{current &&
|
|
88
|
+
createPortal(
|
|
89
|
+
<ConfirmDialog
|
|
90
|
+
key={current.id}
|
|
91
|
+
request={current.request}
|
|
92
|
+
onAnswer={(value) => answer(current.id, value)}
|
|
93
|
+
/>,
|
|
94
|
+
document.body,
|
|
95
|
+
)}
|
|
96
|
+
</ConfirmContext.Provider>
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function ConfirmDialog({
|
|
101
|
+
request,
|
|
102
|
+
onAnswer,
|
|
103
|
+
}: {
|
|
104
|
+
request: ConfirmRequest;
|
|
105
|
+
onAnswer(answer: ConfirmAnswer): void;
|
|
106
|
+
}) {
|
|
107
|
+
const [dontAskAgain, setDontAskAgain] = useState(false);
|
|
108
|
+
const messageId = useId();
|
|
109
|
+
const cancel = () => onAnswer(CANCELLED);
|
|
110
|
+
return (
|
|
111
|
+
<Dialog
|
|
112
|
+
open
|
|
113
|
+
size="md"
|
|
114
|
+
title={request.title}
|
|
115
|
+
describedBy={messageId}
|
|
116
|
+
// Escape, the scrim and the header's X all count as Cancel.
|
|
117
|
+
onClose={cancel}
|
|
118
|
+
footer={
|
|
119
|
+
<div className="flex w-full flex-wrap items-center justify-end gap-2">
|
|
120
|
+
{request.offerDontAskAgain && (
|
|
121
|
+
<label className="mr-auto flex items-center gap-2 text-detail text-ink-muted">
|
|
122
|
+
<input
|
|
123
|
+
type="checkbox"
|
|
124
|
+
checked={dontAskAgain}
|
|
125
|
+
onChange={(e) => setDontAskAgain(e.target.checked)}
|
|
126
|
+
/>
|
|
127
|
+
Don't ask again
|
|
128
|
+
</label>
|
|
129
|
+
)}
|
|
130
|
+
<Button size="sm" onClick={cancel}>
|
|
131
|
+
{request.cancelLabel ?? 'Cancel'}
|
|
132
|
+
</Button>
|
|
133
|
+
<Button
|
|
134
|
+
size="sm"
|
|
135
|
+
variant={request.destructive ? 'danger' : 'primary'}
|
|
136
|
+
onClick={() =>
|
|
137
|
+
onAnswer({ confirmed: true, dontAskAgain: !!request.offerDontAskAgain && dontAskAgain })
|
|
138
|
+
}
|
|
139
|
+
>
|
|
140
|
+
{request.confirmLabel ?? 'OK'}
|
|
141
|
+
</Button>
|
|
142
|
+
</div>
|
|
143
|
+
}
|
|
144
|
+
>
|
|
145
|
+
{/* The question itself is the dialog's description, so a screen reader
|
|
146
|
+
announces it with the title, as it did the browser's confirm(). */}
|
|
147
|
+
<div id={messageId}>
|
|
148
|
+
{typeof request.message === 'string' ? (
|
|
149
|
+
<p className="text-detail text-ink whitespace-pre-line break-words">{request.message}</p>
|
|
150
|
+
) : (
|
|
151
|
+
request.message
|
|
152
|
+
)}
|
|
153
|
+
</div>
|
|
154
|
+
</Dialog>
|
|
155
|
+
);
|
|
156
|
+
}
|
|
@@ -40,6 +40,11 @@ interface DialogProps {
|
|
|
40
40
|
onClose(): void;
|
|
41
41
|
/** Rendered in the header bar next to the close button. */
|
|
42
42
|
title: ReactNode;
|
|
43
|
+
/**
|
|
44
|
+
* Id of the element that describes the dialog (`aria-describedby`): the
|
|
45
|
+
* question a confirmation asks, so a screen reader reads it with the title.
|
|
46
|
+
*/
|
|
47
|
+
describedBy?: string;
|
|
43
48
|
children: ReactNode;
|
|
44
49
|
/** Pinned footer (e.g. action buttons). Omit for a bodyless dialog. */
|
|
45
50
|
footer?: ReactNode;
|
|
@@ -66,6 +71,7 @@ export function Dialog({
|
|
|
66
71
|
open,
|
|
67
72
|
onClose,
|
|
68
73
|
title,
|
|
74
|
+
describedBy,
|
|
69
75
|
children,
|
|
70
76
|
footer,
|
|
71
77
|
headerActions,
|
|
@@ -173,6 +179,7 @@ export function Dialog({
|
|
|
173
179
|
role="dialog"
|
|
174
180
|
aria-modal="true"
|
|
175
181
|
aria-labelledby={titleId}
|
|
182
|
+
aria-describedby={describedBy}
|
|
176
183
|
tabIndex={-1}
|
|
177
184
|
className={`bg-white border border-line rounded-lg shadow-xl w-full ${SIZE_CLASS[size]} max-h-[90vh] flex flex-col`}
|
|
178
185
|
onClick={(e) => e.stopPropagation()}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { act, fireEvent, render, renderHook, screen, waitFor, within } from '@testing-library/react';
|
|
3
|
+
import { useLayoutEffect, type ReactNode } from 'react';
|
|
4
|
+
import { ConfirmProvider, useConfirm, type ConfirmAnswer } from '..';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The app's own confirmation. It resolves only on the person's answer —
|
|
8
|
+
* Confirm, Cancel, Escape, the scrim or the X — never on anything the browser
|
|
9
|
+
* decides, and it asks one question at a time.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const wrapper = ({ children }: { children: ReactNode }) => <ConfirmProvider>{children}</ConfirmProvider>;
|
|
13
|
+
|
|
14
|
+
afterEach(() => vi.restoreAllMocks());
|
|
15
|
+
|
|
16
|
+
function ask(request: Parameters<ReturnType<typeof useConfirm>>[0] = { title: 'Delete', message: 'Delete it?' }) {
|
|
17
|
+
const { result } = renderHook(() => useConfirm(), { wrapper });
|
|
18
|
+
let answer!: Promise<ConfirmAnswer>;
|
|
19
|
+
act(() => {
|
|
20
|
+
answer = result.current(request);
|
|
21
|
+
});
|
|
22
|
+
return { answer, confirm: result.current };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe('ConfirmProvider', () => {
|
|
26
|
+
it('confirms only on the confirm button', async () => {
|
|
27
|
+
const { answer } = ask({ title: 'Delete', message: 'Delete it?', confirmLabel: 'Delete', destructive: true });
|
|
28
|
+
const dialog = await screen.findByRole('dialog');
|
|
29
|
+
expect(within(dialog).getByText('Delete it?')).toBeInTheDocument();
|
|
30
|
+
fireEvent.click(within(dialog).getByRole('button', { name: 'Delete' }));
|
|
31
|
+
await expect(answer).resolves.toEqual({ confirmed: true, dontAskAgain: false });
|
|
32
|
+
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull());
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it.each([
|
|
36
|
+
['Cancel', () => fireEvent.click(screen.getByRole('button', { name: 'Cancel' }))],
|
|
37
|
+
['the X', () => fireEvent.click(screen.getByRole('button', { name: 'Close' }))],
|
|
38
|
+
['Escape', () => fireEvent.keyDown(document, { key: 'Escape' })],
|
|
39
|
+
[
|
|
40
|
+
'the scrim',
|
|
41
|
+
() => {
|
|
42
|
+
const scrim = screen.getByRole('dialog').parentElement!;
|
|
43
|
+
fireEvent.mouseDown(scrim);
|
|
44
|
+
fireEvent.click(scrim);
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
])('counts %s as Cancel', async (_name, dismiss) => {
|
|
48
|
+
const { answer } = ask();
|
|
49
|
+
await screen.findByRole('dialog');
|
|
50
|
+
dismiss();
|
|
51
|
+
await expect(answer).resolves.toEqual({ confirmed: false, dontAskAgain: false });
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('offers "Don\'t ask again" only when asked to, and reports the tick on confirm', async () => {
|
|
55
|
+
const plain = ask();
|
|
56
|
+
expect(within(await screen.findByRole('dialog')).queryByRole('checkbox')).toBeNull();
|
|
57
|
+
fireEvent.click(screen.getByRole('button', { name: 'OK' }));
|
|
58
|
+
await plain.answer;
|
|
59
|
+
|
|
60
|
+
const { answer } = ask({ title: 'Delete', message: 'Delete it?', offerDontAskAgain: true });
|
|
61
|
+
const dialog = await screen.findByRole('dialog');
|
|
62
|
+
fireEvent.click(within(dialog).getByRole('checkbox', { name: "Don't ask again" }));
|
|
63
|
+
fireEvent.click(within(dialog).getByRole('button', { name: 'OK' }));
|
|
64
|
+
await expect(answer).resolves.toEqual({ confirmed: true, dontAskAgain: true });
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('is not answered by the browser: a suppressed window.confirm is never consulted', async () => {
|
|
68
|
+
const spy = vi.spyOn(window, 'confirm').mockReturnValue(false);
|
|
69
|
+
const { answer } = ask();
|
|
70
|
+
fireEvent.click(within(await screen.findByRole('dialog')).getByRole('button', { name: 'OK' }));
|
|
71
|
+
await expect(answer).resolves.toMatchObject({ confirmed: true });
|
|
72
|
+
expect(spy).not.toHaveBeenCalled();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('withdraws the question as Cancel when the component that asked goes away', async () => {
|
|
76
|
+
// Two askers under one provider; only the first goes away.
|
|
77
|
+
let showAsker = true;
|
|
78
|
+
const asks: { current: ReturnType<typeof useConfirm> | null }[] = [{ current: null }, { current: null }];
|
|
79
|
+
function Asker({ slot }: { slot: number }) {
|
|
80
|
+
asks[slot].current = useConfirm();
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
const tree = () => (
|
|
84
|
+
<ConfirmProvider>
|
|
85
|
+
{showAsker && <Asker slot={0} />}
|
|
86
|
+
<Asker slot={1} />
|
|
87
|
+
</ConfirmProvider>
|
|
88
|
+
);
|
|
89
|
+
const { rerender } = render(tree());
|
|
90
|
+
let gone!: Promise<ConfirmAnswer>;
|
|
91
|
+
let stays!: Promise<ConfirmAnswer>;
|
|
92
|
+
act(() => {
|
|
93
|
+
gone = asks[0].current!({ title: 'Gone', message: 'From the view that leaves?' });
|
|
94
|
+
stays = asks[1].current!({ title: 'Stays', message: 'From the view that stays?' });
|
|
95
|
+
});
|
|
96
|
+
expect(await screen.findByText('From the view that leaves?')).toBeInTheDocument();
|
|
97
|
+
|
|
98
|
+
showAsker = false;
|
|
99
|
+
rerender(tree());
|
|
100
|
+
await expect(gone).resolves.toEqual({ confirmed: false, dontAskAgain: false });
|
|
101
|
+
// The other view's question comes up next and still needs its own answer.
|
|
102
|
+
const dialog = await screen.findByRole('dialog');
|
|
103
|
+
expect(within(dialog).getByText('From the view that stays?')).toBeInTheDocument();
|
|
104
|
+
fireEvent.click(within(dialog).getByRole('button', { name: 'OK' }));
|
|
105
|
+
await expect(stays).resolves.toEqual({ confirmed: true, dontAskAgain: false });
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("withdraws a question asked from the asker's own first layout effect when it goes away", async () => {
|
|
109
|
+
let answer!: Promise<ConfirmAnswer>;
|
|
110
|
+
function AsksOnMount() {
|
|
111
|
+
const confirm = useConfirm();
|
|
112
|
+
useLayoutEffect(() => {
|
|
113
|
+
answer = confirm({ title: 'Early', message: 'Asked on mount?' });
|
|
114
|
+
}, [confirm]);
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
const { rerender } = render(
|
|
118
|
+
<ConfirmProvider>
|
|
119
|
+
<AsksOnMount />
|
|
120
|
+
</ConfirmProvider>,
|
|
121
|
+
);
|
|
122
|
+
expect(await screen.findByText('Asked on mount?')).toBeInTheDocument();
|
|
123
|
+
rerender(<ConfirmProvider>{null}</ConfirmProvider>);
|
|
124
|
+
await expect(answer).resolves.toEqual({ confirmed: false, dontAskAgain: false });
|
|
125
|
+
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull());
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('asks one question at a time, in order', async () => {
|
|
129
|
+
const { result } = renderHook(() => useConfirm(), { wrapper });
|
|
130
|
+
let first!: Promise<ConfirmAnswer>;
|
|
131
|
+
let second!: Promise<ConfirmAnswer>;
|
|
132
|
+
act(() => {
|
|
133
|
+
first = result.current({ title: 'One', message: 'First?' });
|
|
134
|
+
second = result.current({ title: 'Two', message: 'Second?' });
|
|
135
|
+
});
|
|
136
|
+
await screen.findByRole('dialog');
|
|
137
|
+
expect(screen.getAllByRole('dialog')).toHaveLength(1);
|
|
138
|
+
expect(screen.getByText('First?')).toBeInTheDocument();
|
|
139
|
+
fireEvent.click(screen.getByRole('button', { name: 'Cancel' }));
|
|
140
|
+
await expect(first).resolves.toMatchObject({ confirmed: false });
|
|
141
|
+
expect(await screen.findByText('Second?')).toBeInTheDocument();
|
|
142
|
+
fireEvent.click(screen.getByRole('button', { name: 'OK' }));
|
|
143
|
+
await expect(second).resolves.toMatchObject({ confirmed: true });
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('answers a queued question on the first click once it comes up', async () => {
|
|
147
|
+
const { result } = renderHook(() => useConfirm(), { wrapper });
|
|
148
|
+
let first!: Promise<ConfirmAnswer>;
|
|
149
|
+
let second!: Promise<ConfirmAnswer>;
|
|
150
|
+
act(() => {
|
|
151
|
+
first = result.current({ title: 'One', message: 'First?' });
|
|
152
|
+
second = result.current({ title: 'Two', message: 'Second?' });
|
|
153
|
+
});
|
|
154
|
+
fireEvent.click(await screen.findByRole('button', { name: 'OK' }));
|
|
155
|
+
await expect(first).resolves.toMatchObject({ confirmed: true });
|
|
156
|
+
// Clicked in the same tick the second dialog commits: no second click
|
|
157
|
+
// needed, nothing dropped.
|
|
158
|
+
act(() => {
|
|
159
|
+
fireEvent.click(screen.getByRole('button', { name: 'Cancel' }));
|
|
160
|
+
});
|
|
161
|
+
await expect(second).resolves.toMatchObject({ confirmed: false });
|
|
162
|
+
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull());
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('gives the dialog its question as the accessible description', async () => {
|
|
166
|
+
ask({ title: 'Delete branch', message: 'Delete "alice/draft"?' });
|
|
167
|
+
const dialog = await screen.findByRole('dialog', { name: 'Delete branch' });
|
|
168
|
+
expect(dialog).toHaveAccessibleDescription('Delete "alice/draft"?');
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('keeps line breaks in a string question', async () => {
|
|
172
|
+
ask({ title: 'Unsaved', message: 'You have unsaved changes in:\n - a.md\nClose anyway?' });
|
|
173
|
+
const dialog = await screen.findByRole('dialog');
|
|
174
|
+
expect(dialog.textContent).toContain('You have unsaved changes in:\n - a.md\nClose anyway?');
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it('answers open questions Cancel when it goes away', async () => {
|
|
178
|
+
const { result, unmount } = renderHook(() => useConfirm(), { wrapper });
|
|
179
|
+
let answer!: Promise<ConfirmAnswer>;
|
|
180
|
+
act(() => {
|
|
181
|
+
answer = result.current({ title: 'Delete', message: 'Delete it?' });
|
|
182
|
+
});
|
|
183
|
+
await screen.findByRole('dialog');
|
|
184
|
+
unmount();
|
|
185
|
+
await expect(answer).resolves.toEqual({ confirmed: false, dontAskAgain: false });
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('refuses loudly without a provider rather than inventing an answer', async () => {
|
|
189
|
+
const { result } = renderHook(() => useConfirm());
|
|
190
|
+
await expect(result.current({ title: 'x', message: 'y' })).rejects.toThrow(/ConfirmProvider/);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('renders inside the page normally', () => {
|
|
194
|
+
render(<ConfirmProvider><p>page</p></ConfirmProvider>);
|
|
195
|
+
expect(screen.getByText('page')).toBeInTheDocument();
|
|
196
|
+
expect(screen.queryByRole('dialog')).toBeNull();
|
|
197
|
+
});
|
|
198
|
+
});
|