@bevel-software/platform-core-frontend 0.1.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/LICENSE +202 -0
- package/package.json +70 -0
- package/src/core/CoreAppShell.tsx +444 -0
- package/src/core/__tests__/ShellRoutes.test.tsx +95 -0
- package/src/core/events.ts +29 -0
- package/src/core/registry.ts +336 -0
- package/src/css-side-effect-imports.d.ts +7 -0
- package/src/html-extensions.ts +12 -0
- package/src/index.css +49 -0
- package/src/index.ts +23 -0
- package/src/lib/api.ts +57 -0
- package/src/lib/utils.ts +138 -0
- package/src/modules/access/__tests__/ManageAccessDialog.test.tsx +130 -0
- package/src/modules/access/__tests__/useFileAccess.test.tsx +253 -0
- package/src/modules/access/api.ts +205 -0
- package/src/modules/access/components/AccessRestrictedBanner.tsx +31 -0
- package/src/modules/access/components/ManageAccessDialog.tsx +1232 -0
- package/src/modules/access/components/NodeOwnersBanner.tsx +30 -0
- package/src/modules/access/hooks/useFileAccess.ts +151 -0
- package/src/modules/admin/components/AdminRolesPage.tsx +943 -0
- package/src/modules/admin/components/RolesCorruptedBanner.tsx +74 -0
- package/src/modules/admin/services/admin.api.ts +92 -0
- package/src/modules/admin/services/roles.api.ts +136 -0
- package/src/modules/admin/state/admin.context.tsx +223 -0
- package/src/modules/auth/components/LoginScreen.tsx +135 -0
- package/src/modules/auth/hooks/useAuthState.ts +59 -0
- package/src/modules/auth/services/auth.api.ts +24 -0
- package/src/modules/auth/services/microsoft-oauth.ts +57 -0
- package/src/modules/auth/state/auth.context.ts +18 -0
- package/src/modules/git/__tests__/BranchSwitcher.test.tsx +347 -0
- package/src/modules/git/__tests__/FileHistoryPanel.test.tsx +201 -0
- package/src/modules/git/__tests__/ProtectedBranchBanner.test.tsx +82 -0
- package/src/modules/git/__tests__/PullNeededBanner.test.tsx +441 -0
- package/src/modules/git/__tests__/git.api.test.ts +113 -0
- package/src/modules/git/__tests__/pr.api.test.ts +44 -0
- package/src/modules/git/__tests__/useAutoPullUpdates.test.tsx +212 -0
- package/src/modules/git/components/BranchSwitcher.tsx +527 -0
- package/src/modules/git/components/DiffView.tsx +30 -0
- package/src/modules/git/components/FileComparisonPanel.tsx +377 -0
- package/src/modules/git/components/FileHistoryPanel.tsx +320 -0
- package/src/modules/git/components/ProtectedBranchBanner.tsx +42 -0
- package/src/modules/git/components/PullNeededBanner.tsx +204 -0
- package/src/modules/git/components/PullRequestsForMe.tsx +244 -0
- package/src/modules/git/components/UnifiedDiffView.tsx +37 -0
- package/src/modules/git/hooks/__tests__/useGitState.deleteBranch.test.tsx +99 -0
- package/src/modules/git/hooks/useAutoPullUpdates.ts +121 -0
- package/src/modules/git/hooks/useGitState.ts +335 -0
- package/src/modules/git/services/__tests__/error-messages.test.ts +134 -0
- package/src/modules/git/services/error-messages.ts +257 -0
- package/src/modules/git/services/git.api.ts +163 -0
- package/src/modules/git/services/pr-prompts.ts +159 -0
- package/src/modules/git/services/pr.api.ts +39 -0
- package/src/modules/git/state/auto-update.context.ts +21 -0
- package/src/modules/git/state/git.context.ts +82 -0
- package/src/modules/layout/components/AppLayout.tsx +40 -0
- package/src/modules/layout/components/DemoBanner.tsx +57 -0
- package/src/modules/layout/components/MaintenanceOverlay.tsx +104 -0
- package/src/modules/layout/components/MobileChatLayout.tsx +153 -0
- package/src/modules/layout/components/ResizableThreePaneLayout.tsx +203 -0
- package/src/modules/layout/components/SlideOverlay.tsx +224 -0
- package/src/modules/layout/components/__tests__/MobileChatLayout.test.tsx +77 -0
- package/src/modules/layout/components/__tests__/ResizableThreePaneLayout.test.tsx +57 -0
- package/src/modules/layout/hooks/useMediaQuery.ts +23 -0
- package/src/modules/layout/state/__tests__/layout.context.test.tsx +39 -0
- package/src/modules/layout/state/layout.context.ts +46 -0
- package/src/modules/library/__tests__/DetailDialog.test.tsx +200 -0
- package/src/modules/library/__tests__/diff.test.ts +62 -0
- package/src/modules/library/__tests__/loadout.test.tsx +72 -0
- package/src/modules/library/__tests__/status.test.ts +117 -0
- package/src/modules/library/components/ChangeRequestDock.tsx +47 -0
- package/src/modules/library/components/CompareView.tsx +357 -0
- package/src/modules/library/components/DetailDialog.tsx +566 -0
- package/src/modules/library/components/GearIcon.tsx +12 -0
- package/src/modules/library/components/LibraryCard.tsx +132 -0
- package/src/modules/library/components/LibraryPage.tsx +254 -0
- package/src/modules/library/components/LoadoutSidebar.tsx +156 -0
- package/src/modules/library/components/StatusGem.tsx +21 -0
- package/src/modules/library/components/SuggestChange.tsx +171 -0
- package/src/modules/library/components/particles.ts +50 -0
- package/src/modules/library/hooks/useLibraryData.ts +119 -0
- package/src/modules/library/hooks/useSkillDetail.ts +74 -0
- package/src/modules/library/library.css +320 -0
- package/src/modules/library/services/library.api.ts +164 -0
- package/src/modules/library/state/loadout.tsx +129 -0
- package/src/modules/library/state/toast.tsx +42 -0
- package/src/modules/library/utils/diff.ts +102 -0
- package/src/modules/library/utils/status.ts +116 -0
- package/src/modules/pr/__tests__/PrApprovalBadge.test.tsx +93 -0
- package/src/modules/pr/__tests__/PrCancelButton.test.tsx +158 -0
- package/src/modules/pr/__tests__/PrCancelConfirmDialog.test.tsx +44 -0
- package/src/modules/pr/__tests__/PrMergeButton.test.tsx +360 -0
- package/src/modules/pr/__tests__/bucketize-comments.test.ts +85 -0
- package/src/modules/pr/__tests__/parsePatch.test.ts +104 -0
- package/src/modules/pr/__tests__/usePrViewerState.test.ts +72 -0
- package/src/modules/pr/components/OpenChangeRequestDialog.tsx +193 -0
- package/src/modules/pr/components/PrApprovalBadge.tsx +80 -0
- package/src/modules/pr/components/PrCancelButton.tsx +99 -0
- package/src/modules/pr/components/PrCancelConfirmDialog.tsx +119 -0
- package/src/modules/pr/components/PrCommentComposer.tsx +92 -0
- package/src/modules/pr/components/PrCommentThread.tsx +168 -0
- package/src/modules/pr/components/PrCommentsPanel.tsx +143 -0
- package/src/modules/pr/components/PrDiffPane.tsx +425 -0
- package/src/modules/pr/components/PrFileRow.tsx +164 -0
- package/src/modules/pr/components/PrFilesList.tsx +53 -0
- package/src/modules/pr/components/PrHeader.tsx +45 -0
- package/src/modules/pr/components/PrHeaderOverflowMenu.tsx +58 -0
- package/src/modules/pr/components/PrMergeButton.tsx +268 -0
- package/src/modules/pr/components/PrMergeConfirmDialog.tsx +112 -0
- package/src/modules/pr/components/PrRefreshFromTargetButton.tsx +110 -0
- package/src/modules/pr/components/PrViewer.tsx +364 -0
- package/src/modules/pr/hooks/usePrViewerState.ts +150 -0
- package/src/modules/pr/services/pr-approvals.api.ts +31 -0
- package/src/modules/pr/services/pr-cancel.api.ts +19 -0
- package/src/modules/pr/services/pr-comments.api.ts +45 -0
- package/src/modules/pr/services/pr-detail.api.ts +17 -0
- package/src/modules/pr/services/pr-merge.api.ts +53 -0
- package/src/modules/pr/services/pr-open.api.ts +30 -0
- package/src/modules/pr/state/pr-viewer.context.ts +34 -0
- package/src/modules/pr/utils/bucketize-comments.ts +55 -0
- package/src/modules/pr/utils/parsePatch.ts +57 -0
- package/src/modules/review/__tests__/review.api.test.ts +121 -0
- package/src/modules/review/components/BinaryChangePlaceholder.tsx +15 -0
- package/src/modules/review/components/DiffViewer.tsx +78 -0
- package/src/modules/review/components/MarkdownDiffViewer.tsx +263 -0
- package/src/modules/review/components/ReviewFileRow.tsx +114 -0
- package/src/modules/review/components/ReviewPanel.tsx +260 -0
- package/src/modules/review/components/ReviewPanelSurface.tsx +52 -0
- package/src/modules/review/components/__tests__/ReviewPanel.test.tsx +106 -0
- package/src/modules/review/hooks/__tests__/useReviewState.test.ts +88 -0
- package/src/modules/review/hooks/useReviewState.ts +256 -0
- package/src/modules/review/services/review.api.ts +72 -0
- package/src/modules/review/state/ReviewProvider.tsx +28 -0
- package/src/modules/review/state/review.context.ts +29 -0
- package/src/modules/secrets-vault/components/ConnectToolsPage.tsx +488 -0
- package/src/modules/secrets-vault/components/SecretsPage.tsx +276 -0
- package/src/modules/secrets-vault/components/ToolSecretsPanel.tsx +362 -0
- package/src/modules/secrets-vault/components/__tests__/SecretsPage.test.tsx +51 -0
- package/src/modules/secrets-vault/services/connect.api.ts +113 -0
- package/src/modules/secrets-vault/services/secrets.api.ts +82 -0
- package/src/modules/secrets-vault/services/tool-secrets.api.ts +116 -0
- package/src/modules/toolbar/components/AdminMenu.tsx +250 -0
- package/src/modules/toolbar/components/AppSwitcher.tsx +108 -0
- package/src/modules/toolbar/components/ExternalAgentAccessPage.tsx +548 -0
- package/src/modules/toolbar/components/Toolbar.tsx +123 -0
- package/src/modules/toolbar/components/__tests__/AppSwitcher.test.tsx +122 -0
- package/src/modules/toolbar/components/__tests__/Toolbar.test.tsx +353 -0
- package/src/modules/toolbar/services/external-api-keys.api.ts +65 -0
- package/src/modules/tools/ToolsExplorerPage.tsx +380 -0
- package/src/modules/workflow/hooks/useEventBusSubscription.ts +24 -0
- package/src/modules/workflow/hooks/useFileLock.ts +430 -0
- package/src/modules/workflow/services/lock.api.ts +139 -0
- package/src/modules/workflow/state/EventBusFocusBinder.tsx +25 -0
- package/src/modules/workflow/state/EventBusProvider.tsx +232 -0
- package/src/modules/workflow/state/event-bus.context.ts +75 -0
- package/src/modules/workspace/__tests__/testFixtures.ts +54 -0
- package/src/modules/workspace/__tests__/useWorkspaceState.test.ts +91 -0
- package/src/modules/workspace/components/EditorTabs.tsx +384 -0
- package/src/modules/workspace/components/FileExplorer.tsx +1068 -0
- package/src/modules/workspace/components/FileRoute.tsx +392 -0
- package/src/modules/workspace/components/FileViewer.tsx +906 -0
- package/src/modules/workspace/components/__tests__/EditorTabs.test.tsx +241 -0
- package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +666 -0
- package/src/modules/workspace/components/__tests__/FileRoute.test.tsx +330 -0
- package/src/modules/workspace/components/__tests__/FileViewer.test.tsx +492 -0
- package/src/modules/workspace/components/renderers/CsvRenderer.tsx +174 -0
- package/src/modules/workspace/components/renderers/DocxRenderer.tsx +87 -0
- package/src/modules/workspace/components/renderers/HtmlRenderer.tsx +231 -0
- package/src/modules/workspace/components/renderers/ImageRenderer.tsx +50 -0
- package/src/modules/workspace/components/renderers/KbMarkdownView.tsx +287 -0
- package/src/modules/workspace/components/renderers/MarkdownRenderer.tsx +252 -0
- package/src/modules/workspace/components/renderers/MermaidDiagram.tsx +268 -0
- package/src/modules/workspace/components/renderers/PdfRenderer.tsx +81 -0
- package/src/modules/workspace/components/renderers/TextRenderer.tsx +86 -0
- package/src/modules/workspace/components/renderers/ToolForm.tsx +404 -0
- package/src/modules/workspace/components/renderers/ToolRenderer.tsx +417 -0
- package/src/modules/workspace/components/renderers/XlsxRenderer.tsx +157 -0
- package/src/modules/workspace/components/renderers/__tests__/CsvRenderer.test.ts +57 -0
- package/src/modules/workspace/components/renderers/__tests__/KbMarkdownView.test.tsx +95 -0
- package/src/modules/workspace/components/renderers/__tests__/MarkdownRenderer.test.tsx +328 -0
- package/src/modules/workspace/components/renderers/__tests__/PdfRenderer.test.tsx +167 -0
- package/src/modules/workspace/components/renderers/__tests__/htmlSandbox.test.ts +318 -0
- package/src/modules/workspace/components/renderers/csvUtils.ts +76 -0
- package/src/modules/workspace/components/renderers/htmlSandbox.ts +332 -0
- package/src/modules/workspace/components/renderers/index.ts +39 -0
- package/src/modules/workspace/components/renderers/types.ts +27 -0
- package/src/modules/workspace/hooks/__tests__/useWorkspaceState.test.tsx +298 -0
- package/src/modules/workspace/hooks/useWorkspaceState.ts +1315 -0
- package/src/modules/workspace/routing/__tests__/kb-routes.test.tsx +99 -0
- package/src/modules/workspace/routing/kb-routes.ts +266 -0
- package/src/modules/workspace/services/__tests__/workspace.api.test.ts +72 -0
- package/src/modules/workspace/services/workspace.api.ts +185 -0
- package/src/modules/workspace/state/workspace.context.ts +235 -0
- package/src/modules/workspace/utils/__tests__/frontmatter.test.ts +107 -0
- package/src/modules/workspace/utils/diff.ts +53 -0
- package/src/modules/workspace/utils/fileTree.ts +179 -0
- package/src/modules/workspace/utils/frontmatter.ts +80 -0
- package/src/modules/workspace/utils/readDroppedEntries.ts +119 -0
- package/src/modules/workspace/utils/tab-persistence.ts +23 -0
- package/src/shared/components/Dialog.tsx +198 -0
- package/src/shared/components/PageShell.tsx +51 -0
- package/src/shared/components/useModalLayer.ts +35 -0
- package/src/shared/markdown/Markdown.tsx +62 -0
- package/src/test-setup.ts +59 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { AlertTriangle } from 'lucide-react';
|
|
2
|
+
import { isProtectedBranch, protectedBranchDisplayName, DEFAULT_BRANCH } from '@bevel-software/platform-shared';
|
|
3
|
+
import { useGit } from '../state/git.context';
|
|
4
|
+
|
|
5
|
+
export function ProtectedBranchBanner() {
|
|
6
|
+
const git = useGit();
|
|
7
|
+
const branch = git.status?.branch;
|
|
8
|
+
if (git.availability !== 'ready' || !isProtectedBranch(branch)) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const displayName = protectedBranchDisplayName(branch) ?? branch;
|
|
13
|
+
// On the default branch, drafts naturally land back on it, so the call-to-
|
|
14
|
+
// action is generic. On any other protected branch, point users at the
|
|
15
|
+
// default branch as the usual propose target (e.g. Current is updated
|
|
16
|
+
// separately by leadership rolling the Target company state in).
|
|
17
|
+
const defaultDisplayName = protectedBranchDisplayName(DEFAULT_BRANCH) ?? DEFAULT_BRANCH;
|
|
18
|
+
const callToAction =
|
|
19
|
+
branch === DEFAULT_BRANCH
|
|
20
|
+
? 'most users propose updates by starting a shared draft'
|
|
21
|
+
: `most users propose updates by starting a shared draft against the ${defaultDisplayName}`;
|
|
22
|
+
|
|
23
|
+
// The banner is now informational only: write access is governed by
|
|
24
|
+
// roles.yaml + access.md, not by which branch is checked out. Users who
|
|
25
|
+
// hold the right role (Admin) may edit directly; others will be refused
|
|
26
|
+
// by the backend with a per-path AccessDenied message at commit time.
|
|
27
|
+
return (
|
|
28
|
+
<div
|
|
29
|
+
role="status"
|
|
30
|
+
className="flex items-center gap-2 px-3 py-1.5 bg-amber-50 border-b border-amber-200 text-xs text-amber-800 shrink-0"
|
|
31
|
+
>
|
|
32
|
+
<AlertTriangle size={13} className="shrink-0" />
|
|
33
|
+
<span className="flex-1">
|
|
34
|
+
You are viewing the <span className="font-semibold">{displayName}</span> (
|
|
35
|
+
<span className="font-mono font-semibold">{branch}</span>) — {callToAction}.
|
|
36
|
+
</span>
|
|
37
|
+
</div>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Re-exported for call sites that already imported from this component. */
|
|
42
|
+
export { isProtectedBranch as isProtectedBranchName } from '@bevel-software/platform-shared';
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
import { GitMerge } from 'lucide-react';
|
|
3
|
+
import { isProtectedBranch } from '@bevel-software/platform-shared';
|
|
4
|
+
import { useGit } from '../state/git.context';
|
|
5
|
+
import { useAutoUpdate } from '../state/auto-update.context';
|
|
6
|
+
import { useCrCreationPort } from '../../../core/registry';
|
|
7
|
+
import { PR_STALE_EVENT } from '../../../core/events';
|
|
8
|
+
import { useWorkspace } from '../../workspace/state/workspace.context';
|
|
9
|
+
import { sanitizeErrorText } from '../services/error-messages';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Classify a pull failure into a short, user-safe phrase. Returned strings
|
|
13
|
+
* flow through the change-request port's `resolvePullIssue` (the enterprise
|
|
14
|
+
* registry splices them into the chat composer, where the user sees them), so
|
|
15
|
+
* the labels must not leak git vocabulary — no "merge conflict",
|
|
16
|
+
* "uncommitted", "working tree", "stash", "HEAD". The agent's own prompt
|
|
17
|
+
* knows the underlying mechanics; the user gets a workspace-level description
|
|
18
|
+
* of what happened.
|
|
19
|
+
*/
|
|
20
|
+
function classifyPullFailure(error: unknown): string {
|
|
21
|
+
const sanitized = sanitizeErrorText(error).toLowerCase();
|
|
22
|
+
if (/\b(conflict|conflicts|merge conflict|would be overwritten)\b/.test(sanitized)) {
|
|
23
|
+
return 'two versions of the same file need to be reconciled';
|
|
24
|
+
}
|
|
25
|
+
if (/\b(uncommitted|local changes|working tree|dirty|stash)\b/.test(sanitized)) {
|
|
26
|
+
return 'there are local changes that need to be sorted out first';
|
|
27
|
+
}
|
|
28
|
+
if (
|
|
29
|
+
/\b(network|auth|credential|permission denied|unauthorized|forbidden|timeout|timed out|could not resolve host|failed to connect|401|403)\b/.test(
|
|
30
|
+
sanitized,
|
|
31
|
+
)
|
|
32
|
+
) {
|
|
33
|
+
return 'a connection or permission problem';
|
|
34
|
+
}
|
|
35
|
+
return 'something unexpected went wrong';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Shown when the current branch is behind origin. On protected branches
|
|
40
|
+
* (current-company-state / target-company-state) the API guards block local
|
|
41
|
+
* commits, so when the working tree is clean a `git pull --rebase` is a
|
|
42
|
+
* fast-forward — that's the path we optimise for. If the rebase is refused
|
|
43
|
+
* (dirty tree from a mid-flow edit, or any non-FF situation a power user
|
|
44
|
+
* created via the CLI), we surface the error inline AND hand the failure to
|
|
45
|
+
* the change-request port so the agent can investigate. On feature branches
|
|
46
|
+
* the user may have overlapping local work, so we skip the direct attempt
|
|
47
|
+
* and hand off straight away. The banner auto-hides once
|
|
48
|
+
* `unmergedFromUpstream` flips back to false.
|
|
49
|
+
*/
|
|
50
|
+
export function PullNeededBanner() {
|
|
51
|
+
const git = useGit();
|
|
52
|
+
const autoUpdate = useAutoUpdate();
|
|
53
|
+
const workspace = useWorkspace();
|
|
54
|
+
const crPort = useCrCreationPort();
|
|
55
|
+
const [pending, setPending] = useState(false);
|
|
56
|
+
const [error, setError] = useState<string | null>(null);
|
|
57
|
+
|
|
58
|
+
if (git.availability !== 'ready') return null;
|
|
59
|
+
const status = git.status;
|
|
60
|
+
if (!status?.unmergedFromUpstream) return null;
|
|
61
|
+
|
|
62
|
+
// Captured for the handlers below — TS control-flow narrowing on `status`
|
|
63
|
+
// doesn't survive into nested function declarations.
|
|
64
|
+
const branchName = status.branch;
|
|
65
|
+
const onProtectedBranch = isProtectedBranch(status.branch);
|
|
66
|
+
const hasUnsavedEditorChanges = !!workspace.hasUnsavedFileChanges;
|
|
67
|
+
// Under save=share the working tree is never dirty and there are no
|
|
68
|
+
// mid-merge conflicts to gate on — the only thing that can still block
|
|
69
|
+
// the auto-pull is in-memory tab edits the user hasn't saved yet.
|
|
70
|
+
const autoEligible = onProtectedBranch && !hasUnsavedEditorChanges;
|
|
71
|
+
const autoFailed = autoUpdate.status === 'failed' && autoUpdate.branch === status.branch;
|
|
72
|
+
|
|
73
|
+
if (autoEligible && !autoFailed) return null;
|
|
74
|
+
|
|
75
|
+
async function pullDirectly() {
|
|
76
|
+
setError(null);
|
|
77
|
+
setPending(true);
|
|
78
|
+
try {
|
|
79
|
+
// Pull is the only operation whose failure should hand off to the
|
|
80
|
+
// agent — that's the user-visible action they clicked Retry on, and
|
|
81
|
+
// a pull failure is the case where the agent has a real recovery
|
|
82
|
+
// (resolve conflicts, switch branches, etc.) to attempt. Wrap it
|
|
83
|
+
// alone so a transient post-pull refresh hiccup can't masquerade
|
|
84
|
+
// as a pull failure and seed a misleading "I tried to bring in
|
|
85
|
+
// teammate updates but it didn't go through" prompt.
|
|
86
|
+
try {
|
|
87
|
+
await git.pull();
|
|
88
|
+
} catch (err) {
|
|
89
|
+
const reason =
|
|
90
|
+
err instanceof Error
|
|
91
|
+
? sanitizeErrorText(err) || 'Could not get updates.'
|
|
92
|
+
: 'Could not get updates.';
|
|
93
|
+
setError(reason);
|
|
94
|
+
const seedReason = classifyPullFailure(err);
|
|
95
|
+
// Hand off through the change-request port so the user always has a
|
|
96
|
+
// path forward — the enterprise registry seeds the agent chat, where
|
|
97
|
+
// the common rebase-refused case (dirty working tree from a mid-flow
|
|
98
|
+
// agent edit) is exactly the kind of thing the agent can unblock.
|
|
99
|
+
crPort?.resolvePullIssue?.({
|
|
100
|
+
workspaceId: workspace.workspaceId,
|
|
101
|
+
branch: branchName,
|
|
102
|
+
kind: 'pull-failed',
|
|
103
|
+
reason: seedReason,
|
|
104
|
+
});
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Post-pull reconciliation. The working tree on disk just changed,
|
|
109
|
+
// so refresh + bump so the explorer + open tabs don't show stale
|
|
110
|
+
// bytes, and notify the PR list that its data may have moved.
|
|
111
|
+
// Failures here do NOT roll back the pull or seed the agent — the
|
|
112
|
+
// remote state already changed and a transient refresh hiccup is
|
|
113
|
+
// a UI staleness issue, not a "couldn't update" failure.
|
|
114
|
+
try {
|
|
115
|
+
await workspace.refreshFileTree();
|
|
116
|
+
// Invalidate cached tab content so every open tab re-reads from disk
|
|
117
|
+
// on next activation; the active tab refetches eagerly. If a tab's
|
|
118
|
+
// path vanished underneath the pull, the eager refetch's 404 handler
|
|
119
|
+
// drops that tab automatically.
|
|
120
|
+
workspace.bumpFsRevision();
|
|
121
|
+
// Tell the PR list its data may have moved (new requests from
|
|
122
|
+
// teammates, or merges they completed). Same event chat-end /
|
|
123
|
+
// merge / share fire.
|
|
124
|
+
window.dispatchEvent(new CustomEvent(PR_STALE_EVENT));
|
|
125
|
+
} catch (err) {
|
|
126
|
+
const reason =
|
|
127
|
+
err instanceof Error
|
|
128
|
+
? sanitizeErrorText(err) || 'Could not refresh after update.'
|
|
129
|
+
: 'Could not refresh after update.';
|
|
130
|
+
setError(reason);
|
|
131
|
+
console.debug('[pull] post-pull refresh failed:', reason);
|
|
132
|
+
}
|
|
133
|
+
} finally {
|
|
134
|
+
setPending(false);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function askAgentToMerge() {
|
|
139
|
+
crPort?.resolvePullIssue?.({
|
|
140
|
+
workspaceId: workspace.workspaceId,
|
|
141
|
+
branch: branchName,
|
|
142
|
+
kind: 'behind',
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const showRetry = onProtectedBranch && autoFailed;
|
|
147
|
+
// The assistant hand-off needs a registered pull-issue resolver (the
|
|
148
|
+
// enterprise chat port); without one there's no agent to ask, so the
|
|
149
|
+
// action hides and the banner stays informational.
|
|
150
|
+
const showAssistantAction = !onProtectedBranch && !!crPort?.resolvePullIssue;
|
|
151
|
+
const showAction = showRetry || showAssistantAction;
|
|
152
|
+
const handleClick = showRetry ? pullDirectly : askAgentToMerge;
|
|
153
|
+
const buttonLabel = pending ? 'Retrying…' : showRetry ? 'Retry' : 'Ask assistant';
|
|
154
|
+
const message = autoFailed
|
|
155
|
+
? 'Couldn’t update automatically'
|
|
156
|
+
: showAssistantAction
|
|
157
|
+
? 'Your draft is missing teammate updates'
|
|
158
|
+
: 'Updates are waiting';
|
|
159
|
+
// After save=share, the only thing that can keep auto-pull from running on
|
|
160
|
+
// a protected branch is in-memory tab edits. The previous fallback referred
|
|
161
|
+
// to "discard your local changes" — a dirty-tree / conflicted-paths exit
|
|
162
|
+
// that's now structurally impossible. We render that guidance only when
|
|
163
|
+
// there are unsaved tab edits, and otherwise leave it null.
|
|
164
|
+
const guidance = autoFailed
|
|
165
|
+
? autoUpdate.reason
|
|
166
|
+
: showAssistantAction
|
|
167
|
+
? null
|
|
168
|
+
: hasUnsavedEditorChanges
|
|
169
|
+
? 'Finish or save your open file before updating.'
|
|
170
|
+
: null;
|
|
171
|
+
|
|
172
|
+
return (
|
|
173
|
+
<div
|
|
174
|
+
role="status"
|
|
175
|
+
className="px-3 py-1.5 bg-sky-50 border-b border-sky-200 text-xs text-sky-800 shrink-0"
|
|
176
|
+
>
|
|
177
|
+
<div className="flex items-center gap-2">
|
|
178
|
+
<GitMerge size={13} className="shrink-0" />
|
|
179
|
+
<span className="flex-1">
|
|
180
|
+
{message} (<span className="font-mono font-semibold">{status.branch}</span>).
|
|
181
|
+
</span>
|
|
182
|
+
{showAction && (
|
|
183
|
+
<button
|
|
184
|
+
onClick={handleClick}
|
|
185
|
+
disabled={pending}
|
|
186
|
+
className="px-2 py-0.5 rounded bg-sky-600 hover:bg-sky-700 disabled:opacity-50 disabled:cursor-not-allowed text-white font-medium"
|
|
187
|
+
>
|
|
188
|
+
{buttonLabel}
|
|
189
|
+
</button>
|
|
190
|
+
)}
|
|
191
|
+
</div>
|
|
192
|
+
{guidance && (
|
|
193
|
+
<div className="mt-1 ml-5 text-sky-700">
|
|
194
|
+
{guidance}
|
|
195
|
+
</div>
|
|
196
|
+
)}
|
|
197
|
+
{error && (
|
|
198
|
+
<div role="alert" className="mt-1 ml-5 text-rose-700">
|
|
199
|
+
{error}
|
|
200
|
+
</div>
|
|
201
|
+
)}
|
|
202
|
+
</div>
|
|
203
|
+
);
|
|
204
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { ChevronDown, ChevronRight, GitPullRequest, Check, X, Clock } from 'lucide-react';
|
|
3
|
+
import type { PullRequestSummary } from '@bevel-software/platform-shared';
|
|
4
|
+
import { listPullRequestsForMe } from '../services/pr.api';
|
|
5
|
+
import { friendlyGitError } from '../services/error-messages';
|
|
6
|
+
import { useGit } from '../state/git.context';
|
|
7
|
+
import { usePrViewer } from '../../pr/state/pr-viewer.context';
|
|
8
|
+
import { PR_STALE_EVENT } from '../../../core/events';
|
|
9
|
+
|
|
10
|
+
const POLL_INTERVAL_MS = 60_000;
|
|
11
|
+
|
|
12
|
+
export function PullRequestsForMe() {
|
|
13
|
+
const git = useGit();
|
|
14
|
+
const [prs, setPrs] = useState<PullRequestSummary[]>([]);
|
|
15
|
+
const [loading, setLoading] = useState(true);
|
|
16
|
+
const [error, setError] = useState<string | null>(null);
|
|
17
|
+
const [expanded, setExpanded] = useState(true);
|
|
18
|
+
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
19
|
+
const mountedRef = useRef(false);
|
|
20
|
+
// Snapshot of branches from the previous PR list so we can detect which
|
|
21
|
+
// PRs vanished (merged / closed on GitHub) between polls and offer their
|
|
22
|
+
// orphaned local branches to the auto-prune path.
|
|
23
|
+
const prevBranchesRef = useRef<Set<string>>(new Set());
|
|
24
|
+
|
|
25
|
+
const available = git.availability === 'ready';
|
|
26
|
+
const deleteBranch = git.deleteBranch;
|
|
27
|
+
const refreshBranches = git.refreshBranches;
|
|
28
|
+
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
mountedRef.current = true;
|
|
31
|
+
return () => {
|
|
32
|
+
mountedRef.current = false;
|
|
33
|
+
if (timerRef.current) clearTimeout(timerRef.current);
|
|
34
|
+
};
|
|
35
|
+
}, []);
|
|
36
|
+
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
if (!available) return;
|
|
39
|
+
|
|
40
|
+
let cancelled = false;
|
|
41
|
+
|
|
42
|
+
async function fetchOnce(opts: { fresh?: boolean } = {}) {
|
|
43
|
+
try {
|
|
44
|
+
const data = await listPullRequestsForMe(opts);
|
|
45
|
+
if (cancelled || !mountedRef.current) return;
|
|
46
|
+
|
|
47
|
+
// Detect PRs whose branch just vanished from our list — that's the
|
|
48
|
+
// "PR merged + remote branch deleted" case. Ask the backend to prune
|
|
49
|
+
// the local counterpart iff origin agrees it's gone (the backend's
|
|
50
|
+
// `onlyIfNoRemote` guard rejects if the branch is still on origin).
|
|
51
|
+
const nextBranches = new Set(data.map((p) => p.branch));
|
|
52
|
+
const vanished: string[] = [];
|
|
53
|
+
for (const name of prevBranchesRef.current) {
|
|
54
|
+
if (!nextBranches.has(name)) vanished.push(name);
|
|
55
|
+
}
|
|
56
|
+
prevBranchesRef.current = nextBranches;
|
|
57
|
+
|
|
58
|
+
if (vanished.length > 0) {
|
|
59
|
+
// Settle each prune independently — one branch still lingering on
|
|
60
|
+
// origin shouldn't block the others. We ignore errors: the common
|
|
61
|
+
// 4xx here is "still exists on origin", which just means "don't
|
|
62
|
+
// prune yet".
|
|
63
|
+
const results = await Promise.allSettled(
|
|
64
|
+
vanished.map((name) => deleteBranch(name, { onlyIfNoRemote: true })),
|
|
65
|
+
);
|
|
66
|
+
const anyPruned = results.some((r) => r.status === 'fulfilled');
|
|
67
|
+
if (anyPruned) {
|
|
68
|
+
// deleteBranch already calls refreshBranches per deletion, but
|
|
69
|
+
// that races with this effect. Explicit refresh here keeps the
|
|
70
|
+
// switcher list in sync with the pruned state.
|
|
71
|
+
refreshBranches().catch(() => {});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
setPrs(data);
|
|
76
|
+
setError(null);
|
|
77
|
+
} catch (err) {
|
|
78
|
+
if (cancelled || !mountedRef.current) return;
|
|
79
|
+
setError(friendlyGitError(err));
|
|
80
|
+
} finally {
|
|
81
|
+
if (!cancelled && mountedRef.current) setLoading(false);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function schedule() {
|
|
86
|
+
if (timerRef.current) clearTimeout(timerRef.current);
|
|
87
|
+
if (document.hidden) return;
|
|
88
|
+
timerRef.current = setTimeout(async () => {
|
|
89
|
+
await fetchOnce();
|
|
90
|
+
if (!cancelled && mountedRef.current) schedule();
|
|
91
|
+
}, POLL_INTERVAL_MS);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function handleVisibility() {
|
|
95
|
+
if (document.hidden) {
|
|
96
|
+
if (timerRef.current) clearTimeout(timerRef.current);
|
|
97
|
+
} else {
|
|
98
|
+
// fetchOnce swallows its own errors, but guard the chain anyway so a stray
|
|
99
|
+
// synchronous throw doesn't bubble out as an unhandled rejection.
|
|
100
|
+
fetchOnce()
|
|
101
|
+
.then(() => {
|
|
102
|
+
if (!cancelled && mountedRef.current) schedule();
|
|
103
|
+
})
|
|
104
|
+
.catch((e) => {
|
|
105
|
+
console.error('[PullRequestsForMe] poll failed', e);
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Immediate refresh whenever something in the app may have mutated the
|
|
111
|
+
// PR list (share dialog opened one, agent turn finished a create/delete
|
|
112
|
+
// via `gh`, etc.). Saves the user from waiting up to POLL_INTERVAL_MS
|
|
113
|
+
// for the next background refresh.
|
|
114
|
+
function handlePrStale() {
|
|
115
|
+
// fresh=true skips the server cache — the event fires because the UI has
|
|
116
|
+
// a specific reason to think the list just changed (PR create via gh or
|
|
117
|
+
// the share dialog, agent turn end), and a stale 30s cache would defeat
|
|
118
|
+
// the point of asking for an immediate refresh.
|
|
119
|
+
fetchOnce({ fresh: true }).catch((e) => {
|
|
120
|
+
console.error('[PullRequestsForMe] refresh-on-stale failed', e);
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
fetchOnce()
|
|
125
|
+
.then(() => {
|
|
126
|
+
if (!cancelled && mountedRef.current) schedule();
|
|
127
|
+
})
|
|
128
|
+
.catch((e) => {
|
|
129
|
+
console.error('[PullRequestsForMe] initial load failed', e);
|
|
130
|
+
});
|
|
131
|
+
document.addEventListener('visibilitychange', handleVisibility);
|
|
132
|
+
window.addEventListener(PR_STALE_EVENT, handlePrStale);
|
|
133
|
+
|
|
134
|
+
return () => {
|
|
135
|
+
cancelled = true;
|
|
136
|
+
document.removeEventListener('visibilitychange', handleVisibility);
|
|
137
|
+
window.removeEventListener(PR_STALE_EVENT, handlePrStale);
|
|
138
|
+
if (timerRef.current) clearTimeout(timerRef.current);
|
|
139
|
+
};
|
|
140
|
+
}, [available, deleteBranch, refreshBranches]);
|
|
141
|
+
|
|
142
|
+
if (!available) return null;
|
|
143
|
+
|
|
144
|
+
return (
|
|
145
|
+
<div className="border-t border-slate-200 shrink-0 max-h-60 flex flex-col">
|
|
146
|
+
<button
|
|
147
|
+
onClick={() => setExpanded((v) => !v)}
|
|
148
|
+
className="flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium text-slate-600 hover:text-slate-900 hover:bg-white/60"
|
|
149
|
+
>
|
|
150
|
+
{expanded ? <ChevronDown size={12} /> : <ChevronRight size={12} />}
|
|
151
|
+
<GitPullRequest size={12} />
|
|
152
|
+
<span className="flex-1 text-left uppercase tracking-wide">Change requests for you</span>
|
|
153
|
+
{!loading && prs.length > 0 && (
|
|
154
|
+
<span className="text-[10px] bg-slate-100 text-slate-700 rounded px-1.5 py-0.5">
|
|
155
|
+
{prs.length}
|
|
156
|
+
</span>
|
|
157
|
+
)}
|
|
158
|
+
</button>
|
|
159
|
+
|
|
160
|
+
{expanded && (
|
|
161
|
+
<div className="flex-1 overflow-y-auto">
|
|
162
|
+
{loading && (
|
|
163
|
+
<div className="px-3 py-2 text-xs text-slate-600">Loading…</div>
|
|
164
|
+
)}
|
|
165
|
+
{!loading && error && (
|
|
166
|
+
<div className="px-3 py-2 text-xs text-red-600">{error}</div>
|
|
167
|
+
)}
|
|
168
|
+
{!loading && !error && prs.length === 0 && (
|
|
169
|
+
<div className="px-3 py-2 text-xs text-slate-600">Nothing waiting for your review.</div>
|
|
170
|
+
)}
|
|
171
|
+
{!loading && !error && prs.map((pr) => (
|
|
172
|
+
<PrRow key={pr.number} pr={pr} />
|
|
173
|
+
))}
|
|
174
|
+
</div>
|
|
175
|
+
)}
|
|
176
|
+
</div>
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function PrRow({ pr }: { pr: PullRequestSummary }) {
|
|
181
|
+
const touched = pr.touchedNodePaths.length;
|
|
182
|
+
const { openPr } = usePrViewer();
|
|
183
|
+
// The summary endpoint doesn't carry app-level approval state (that only
|
|
184
|
+
// comes from the detail endpoint, which is per-PR). We show the GitHub
|
|
185
|
+
// review badge as-is until the user opens the PR and sees the real
|
|
186
|
+
// Bevel-side progress. The header bar inside the viewer is authoritative.
|
|
187
|
+
return (
|
|
188
|
+
<div
|
|
189
|
+
role="button"
|
|
190
|
+
tabIndex={0}
|
|
191
|
+
onClick={() => openPr(pr.number)}
|
|
192
|
+
onKeyDown={(e) => {
|
|
193
|
+
if (e.key === 'Enter' || e.key === ' ') {
|
|
194
|
+
e.preventDefault();
|
|
195
|
+
openPr(pr.number);
|
|
196
|
+
}
|
|
197
|
+
}}
|
|
198
|
+
className="block px-3 py-1.5 hover:bg-white/60 border-b border-slate-200 last:border-b-0 group cursor-pointer"
|
|
199
|
+
title={pr.title}
|
|
200
|
+
>
|
|
201
|
+
<div className="flex items-center gap-1.5 text-xs text-slate-900">
|
|
202
|
+
<span className="text-slate-600 font-mono shrink-0">#{pr.number}</span>
|
|
203
|
+
<span className="truncate flex-1">{pr.title}</span>
|
|
204
|
+
</div>
|
|
205
|
+
<div className="flex items-center gap-2 mt-0.5 text-[10px] text-slate-600">
|
|
206
|
+
<span className="truncate">{pr.appAuthor?.name ?? pr.author.login}</span>
|
|
207
|
+
<ReviewBadge review={pr.review} />
|
|
208
|
+
{touched > 0 && (
|
|
209
|
+
<span className="shrink-0">
|
|
210
|
+
{touched} file{touched === 1 ? '' : 's'}
|
|
211
|
+
</span>
|
|
212
|
+
)}
|
|
213
|
+
</div>
|
|
214
|
+
</div>
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function ReviewBadge({ review }: { review: PullRequestSummary['review'] }) {
|
|
219
|
+
if (review.changesRequested > 0) {
|
|
220
|
+
return (
|
|
221
|
+
<span className="flex items-center gap-0.5 text-red-600 shrink-0">
|
|
222
|
+
<X size={10} />
|
|
223
|
+
{review.changesRequested}
|
|
224
|
+
</span>
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
if (review.approvals > 0) {
|
|
228
|
+
return (
|
|
229
|
+
<span className="flex items-center gap-0.5 text-emerald-600 shrink-0">
|
|
230
|
+
<Check size={10} />
|
|
231
|
+
{review.approvals}
|
|
232
|
+
</span>
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
if (review.pendingLogins.length > 0) {
|
|
236
|
+
return (
|
|
237
|
+
<span className="flex items-center gap-0.5 text-amber-600 shrink-0">
|
|
238
|
+
<Clock size={10} />
|
|
239
|
+
{review.pendingLogins.length}
|
|
240
|
+
</span>
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
interface Props {
|
|
2
|
+
diff: string;
|
|
3
|
+
/** Copy shown when `diff` is empty / whitespace-only. */
|
|
4
|
+
emptyMessage?: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Render a unified-diff string with the same colour vocabulary used across
|
|
9
|
+
* every git surface in the app — sky-blue hunk headers, emerald additions,
|
|
10
|
+
* red removals, muted index/file headers. Lives in `git/` because both the
|
|
11
|
+
* file-history panel and the cross-branch comparison panel render diffs.
|
|
12
|
+
*/
|
|
13
|
+
export function UnifiedDiffView({ diff, emptyMessage = 'No changes.' }: Props) {
|
|
14
|
+
if (!diff.trim()) {
|
|
15
|
+
return (
|
|
16
|
+
<div className="px-3 py-3 text-xs text-slate-600">{emptyMessage}</div>
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
const lines = diff.split('\n');
|
|
20
|
+
return (
|
|
21
|
+
<pre className="px-3 py-2 text-xs font-mono leading-5 whitespace-pre">
|
|
22
|
+
{lines.map((line, i) => {
|
|
23
|
+
let cls = 'text-slate-700';
|
|
24
|
+
if (line.startsWith('+++') || line.startsWith('---')) cls = 'text-slate-600';
|
|
25
|
+
else if (line.startsWith('@@')) cls = 'text-sky-600';
|
|
26
|
+
else if (line.startsWith('+')) cls = 'text-emerald-700 bg-emerald-50';
|
|
27
|
+
else if (line.startsWith('-')) cls = 'text-red-700 bg-red-50';
|
|
28
|
+
else if (line.startsWith('diff ') || line.startsWith('index ')) cls = 'text-slate-600';
|
|
29
|
+
return (
|
|
30
|
+
<div key={i} className={cls}>
|
|
31
|
+
{line || ' '}
|
|
32
|
+
</div>
|
|
33
|
+
);
|
|
34
|
+
})}
|
|
35
|
+
</pre>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { renderHook, act, waitFor } from '@testing-library/react';
|
|
3
|
+
import type { BranchInfo } from '@bevel-software/platform-shared';
|
|
4
|
+
import { useGitState } from '../useGitState';
|
|
5
|
+
import {
|
|
6
|
+
deleteBranch as apiDeleteBranch,
|
|
7
|
+
fetchBranches,
|
|
8
|
+
fetchStatus,
|
|
9
|
+
} from '../../services/git.api';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* `useGitState` — the optimistic delete. Deleting a branch hides the row
|
|
13
|
+
* the instant the user confirms (before `git push --delete origin <name>`
|
|
14
|
+
* resolves over the wire) so the picker feels instant; a failure
|
|
15
|
+
* reconciles to server truth and rethrows so the caller surfaces the
|
|
16
|
+
* error in the picker's banner.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
vi.mock('../../services/git.api', () => ({
|
|
20
|
+
fetchStatus: vi.fn(),
|
|
21
|
+
fetchBranches: vi.fn(),
|
|
22
|
+
createBranch: vi.fn(),
|
|
23
|
+
deleteBranch: vi.fn(),
|
|
24
|
+
pull: vi.fn(),
|
|
25
|
+
fetchForkBase: vi.fn(),
|
|
26
|
+
revert: vi.fn(),
|
|
27
|
+
fetchFileHistory: vi.fn(),
|
|
28
|
+
fetchFileDiff: vi.fn(),
|
|
29
|
+
fetchFileComparison: vi.fn(),
|
|
30
|
+
}));
|
|
31
|
+
|
|
32
|
+
const mockFetchBranches = vi.mocked(fetchBranches);
|
|
33
|
+
const mockFetchStatus = vi.mocked(fetchStatus);
|
|
34
|
+
const mockDeleteBranch = vi.mocked(apiDeleteBranch);
|
|
35
|
+
|
|
36
|
+
function mkBranch(name: string): BranchInfo {
|
|
37
|
+
return { name, isProtected: false, ahead: 0, behind: 0, hasRemote: true };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe('useGitState — optimistic deleteBranch', () => {
|
|
41
|
+
beforeEach(() => {
|
|
42
|
+
vi.clearAllMocks();
|
|
43
|
+
// Status fetch isn't what these tests care about; resolve it so the
|
|
44
|
+
// hook's initial poll doesn't reject and pollute `lastError`.
|
|
45
|
+
mockFetchStatus.mockResolvedValue({
|
|
46
|
+
branch: 'alice/elsewhere',
|
|
47
|
+
hasUpstream: true,
|
|
48
|
+
unmergedFromUpstream: false,
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('hides the row immediately, before the delete API resolves', async () => {
|
|
53
|
+
mockFetchBranches.mockResolvedValue([mkBranch('alice/draft'), mkBranch('bob/draft')]);
|
|
54
|
+
const { result } = renderHook(() => useGitState('ws-1'));
|
|
55
|
+
await waitFor(() => expect(result.current.branches).toHaveLength(2));
|
|
56
|
+
|
|
57
|
+
// Hold the delete's network promise open so we can assert mid-flight.
|
|
58
|
+
let resolveApi: () => void = () => {};
|
|
59
|
+
mockDeleteBranch.mockImplementation(() => new Promise((res) => { resolveApi = res; }));
|
|
60
|
+
|
|
61
|
+
act(() => { void result.current.deleteBranch('bob/draft'); });
|
|
62
|
+
|
|
63
|
+
// Optimistic: bob/draft is gone from the exposed list even though
|
|
64
|
+
// `apiDeleteBranch` has not yet resolved.
|
|
65
|
+
expect(result.current.branches.map((b) => b.name)).toEqual(['alice/draft']);
|
|
66
|
+
|
|
67
|
+
// Resolve the delete API. fetchBranches is called by the post-success
|
|
68
|
+
// refresh — return the server's new truth (bob/draft gone).
|
|
69
|
+
mockFetchBranches.mockResolvedValue([mkBranch('alice/draft')]);
|
|
70
|
+
await act(async () => { resolveApi(); });
|
|
71
|
+
|
|
72
|
+
// Final state matches the optimistic decision — no flicker.
|
|
73
|
+
await waitFor(() =>
|
|
74
|
+
expect(result.current.branches.map((b) => b.name)).toEqual(['alice/draft']),
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('on failure, restores the row and rethrows the error', async () => {
|
|
79
|
+
mockFetchBranches.mockResolvedValue([mkBranch('alice/draft'), mkBranch('bob/draft')]);
|
|
80
|
+
const { result } = renderHook(() => useGitState('ws-1'));
|
|
81
|
+
await waitFor(() => expect(result.current.branches).toHaveLength(2));
|
|
82
|
+
|
|
83
|
+
mockDeleteBranch.mockRejectedValue(new Error('push failed'));
|
|
84
|
+
// The reconciling refresh after failure refetches — the delete didn't
|
|
85
|
+
// commit, so bob/draft is still there server-side.
|
|
86
|
+
mockFetchBranches.mockResolvedValue([mkBranch('alice/draft'), mkBranch('bob/draft')]);
|
|
87
|
+
|
|
88
|
+
await expect(
|
|
89
|
+
act(async () => { await result.current.deleteBranch('bob/draft'); }),
|
|
90
|
+
).rejects.toThrow('push failed');
|
|
91
|
+
|
|
92
|
+
// Row is back — pendingDeletes was cleared in the `finally` and
|
|
93
|
+
// rawBranches still contains bob/draft per the failed-state refresh.
|
|
94
|
+
expect(result.current.branches.map((b) => b.name).sort()).toEqual([
|
|
95
|
+
'alice/draft',
|
|
96
|
+
'bob/draft',
|
|
97
|
+
]);
|
|
98
|
+
});
|
|
99
|
+
});
|