@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,30 @@
|
|
|
1
|
+
import { authFetch } from '../../../lib/api';
|
|
2
|
+
import { handleApiResponse } from '../../git/services/git.api';
|
|
3
|
+
|
|
4
|
+
/** Request shape of `POST /api/workflow/change-requests` (see backend
|
|
5
|
+
* `modules/workflow/workflow.routes.ts`). */
|
|
6
|
+
export interface OpenChangeRequestInput {
|
|
7
|
+
sourceBranch: string;
|
|
8
|
+
targetBranch: string;
|
|
9
|
+
title: string;
|
|
10
|
+
description?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Open a change request directly — no agent in the loop. The backend resolves
|
|
15
|
+
* the source branch's workspace, runs the target→source auto-merge, and
|
|
16
|
+
* creates the CR. This is the core fallback path behind the change-request
|
|
17
|
+
* port; the enterprise registry overrides the port with the agent-driven
|
|
18
|
+
* chat flow (cascade impact analysis + `gh pr create`).
|
|
19
|
+
*/
|
|
20
|
+
export async function openChangeRequest(
|
|
21
|
+
input: OpenChangeRequestInput,
|
|
22
|
+
): Promise<unknown> {
|
|
23
|
+
return handleApiResponse(
|
|
24
|
+
await authFetch('/api/workflow/change-requests', {
|
|
25
|
+
method: 'POST',
|
|
26
|
+
headers: { 'Content-Type': 'application/json' },
|
|
27
|
+
body: JSON.stringify(input),
|
|
28
|
+
}),
|
|
29
|
+
);
|
|
30
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react';
|
|
2
|
+
import type { PullRequestDetail } from '@bevel-software/platform-shared';
|
|
3
|
+
|
|
4
|
+
export interface PrViewerContextValue {
|
|
5
|
+
/** Currently-open PR number, or null when the viewer is closed. */
|
|
6
|
+
openPrNumber: number | null;
|
|
7
|
+
/** Fetched detail for the open PR. Null while loading / when viewer closed. */
|
|
8
|
+
detail: PullRequestDetail | null;
|
|
9
|
+
/** Currently-selected file path in the viewer (`files[].path`). Null = first file. */
|
|
10
|
+
selectedPath: string | null;
|
|
11
|
+
isLoading: boolean;
|
|
12
|
+
lastError: string | null;
|
|
13
|
+
/**
|
|
14
|
+
* True when the open CR failed to load specifically because it no longer
|
|
15
|
+
* exists (backend 404) — as opposed to a transient/network error. Lets the
|
|
16
|
+
* viewer show a calm "no longer available" state instead of an alarming error
|
|
17
|
+
* banner. This is the common outcome of a routine deep-linking a CR whose row
|
|
18
|
+
* is gone (see `RoutineRunDetailsDialog` → `openPr`).
|
|
19
|
+
*/
|
|
20
|
+
notFound: boolean;
|
|
21
|
+
openPr(prNumber: number): void;
|
|
22
|
+
closeViewer(): void;
|
|
23
|
+
selectPath(path: string | null): void;
|
|
24
|
+
/** Force-refresh the detail for the currently-open PR (bypasses backend cache). */
|
|
25
|
+
refresh(): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const PrViewerContext = createContext<PrViewerContextValue | null>(null);
|
|
29
|
+
|
|
30
|
+
export function usePrViewer(): PrViewerContextValue {
|
|
31
|
+
const ctx = useContext(PrViewerContext);
|
|
32
|
+
if (!ctx) throw new Error('usePrViewer must be used within PrViewerContext.Provider');
|
|
33
|
+
return ctx;
|
|
34
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { PrReviewComment } from '@bevel-software/platform-shared';
|
|
2
|
+
|
|
3
|
+
export interface ThreadBucket {
|
|
4
|
+
root: PrReviewComment;
|
|
5
|
+
replies: PrReviewComment[];
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Group a flat comment list into thread buckets: one root (parent missing or
|
|
10
|
+
* unresolvable) plus its replies, oldest first. Replies whose parent has been
|
|
11
|
+
* deleted re-root at themselves so the discussion doesn't vanish. Two-level-
|
|
12
|
+
* plus chains are flattened to root + replies for rendering simplicity.
|
|
13
|
+
*/
|
|
14
|
+
export function bucketizeComments(comments: PrReviewComment[]): ThreadBucket[] {
|
|
15
|
+
const byId = new Map(comments.map((c) => [c.id, c]));
|
|
16
|
+
const roots: ThreadBucket[] = [];
|
|
17
|
+
const rootById = new Map<string, ThreadBucket>();
|
|
18
|
+
|
|
19
|
+
for (const c of comments) {
|
|
20
|
+
if (!c.parentId || !byId.has(c.parentId)) {
|
|
21
|
+
const bucket: ThreadBucket = { root: c, replies: [] };
|
|
22
|
+
roots.push(bucket);
|
|
23
|
+
rootById.set(c.id, bucket);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
for (const c of comments) {
|
|
27
|
+
if (c.parentId && byId.has(c.parentId)) {
|
|
28
|
+
let currentParentId: string | undefined = c.parentId;
|
|
29
|
+
let rootBucket: ThreadBucket | undefined;
|
|
30
|
+
const guard = new Set<string>();
|
|
31
|
+
while (currentParentId && !guard.has(currentParentId)) {
|
|
32
|
+
guard.add(currentParentId);
|
|
33
|
+
const direct = rootById.get(currentParentId);
|
|
34
|
+
if (direct) {
|
|
35
|
+
rootBucket = direct;
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
const parent: PrReviewComment | undefined = byId.get(currentParentId);
|
|
39
|
+
currentParentId = parent?.parentId;
|
|
40
|
+
}
|
|
41
|
+
if (rootBucket) {
|
|
42
|
+
rootBucket.replies.push(c);
|
|
43
|
+
} else {
|
|
44
|
+
// Parent chain went unresolved — re-root this comment. Register it
|
|
45
|
+
// in `rootById` too so later descendants that chase this id as their
|
|
46
|
+
// ancestor still find a bucket to attach to; without this the
|
|
47
|
+
// fallback root is orphaned from any grandchildren.
|
|
48
|
+
const fallback: ThreadBucket = { root: c, replies: [] };
|
|
49
|
+
roots.push(fallback);
|
|
50
|
+
rootById.set(c.id, fallback);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return roots;
|
|
55
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { DiffLine } from '../../workspace/utils/diff';
|
|
2
|
+
|
|
3
|
+
export interface PatchHunk {
|
|
4
|
+
/** 1-based starting line number in the old file. */
|
|
5
|
+
oldStart: number;
|
|
6
|
+
/** 1-based starting line number in the new file. */
|
|
7
|
+
newStart: number;
|
|
8
|
+
header: string; // the raw `@@ -1,3 +1,4 @@ …` line, for tooltip/debug
|
|
9
|
+
lines: DiffLine[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const HUNK_HEADER_RE = /^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Parse a GitHub unified-diff patch into hunks. Handles the standard lead
|
|
16
|
+
* characters: ` ` (same), `+` (added), `-` (removed), `\` (no-newline marker,
|
|
17
|
+
* which we ignore — the adjacent line already captured the content).
|
|
18
|
+
*
|
|
19
|
+
* Returns an empty array for empty/missing patches so callers can uniformly
|
|
20
|
+
* render "no inline diff available" for binary and oversized files.
|
|
21
|
+
*/
|
|
22
|
+
export function parsePatch(patch: string | undefined): PatchHunk[] {
|
|
23
|
+
if (!patch) return [];
|
|
24
|
+
const out: PatchHunk[] = [];
|
|
25
|
+
let current: PatchHunk | null = null;
|
|
26
|
+
|
|
27
|
+
for (const raw of patch.split('\n')) {
|
|
28
|
+
const match = HUNK_HEADER_RE.exec(raw);
|
|
29
|
+
if (match) {
|
|
30
|
+
if (current) out.push(current);
|
|
31
|
+
current = {
|
|
32
|
+
oldStart: parseInt(match[1], 10) || 1,
|
|
33
|
+
newStart: parseInt(match[2], 10) || 1,
|
|
34
|
+
header: raw,
|
|
35
|
+
lines: [],
|
|
36
|
+
};
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (!current) continue; // ignore any preamble before the first hunk
|
|
40
|
+
if (raw.startsWith('\\')) continue; // ""
|
|
41
|
+
|
|
42
|
+
if (raw.startsWith('+')) {
|
|
43
|
+
current.lines.push({ type: 'added', text: raw.slice(1) });
|
|
44
|
+
} else if (raw.startsWith('-')) {
|
|
45
|
+
current.lines.push({ type: 'removed', text: raw.slice(1) });
|
|
46
|
+
} else if (raw.startsWith(' ')) {
|
|
47
|
+
current.lines.push({ type: 'same', text: raw.slice(1) });
|
|
48
|
+
} else if (raw === '') {
|
|
49
|
+
// Empty line in the middle of a hunk — patch format uses an unprefixed
|
|
50
|
+
// empty line as a context line. Preserve it.
|
|
51
|
+
current.lines.push({ type: 'same', text: '' });
|
|
52
|
+
}
|
|
53
|
+
// Anything else is noise (trailing junk) — skip.
|
|
54
|
+
}
|
|
55
|
+
if (current) out.push(current);
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
acceptReviewChange,
|
|
4
|
+
fetchReviewFile,
|
|
5
|
+
fetchReviewSession,
|
|
6
|
+
rejectReviewChange,
|
|
7
|
+
} from '../services/review.api';
|
|
8
|
+
|
|
9
|
+
const fetchMock = vi.fn();
|
|
10
|
+
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
fetchMock.mockReset();
|
|
13
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
14
|
+
localStorage.clear();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
function ok(body: unknown): Response {
|
|
18
|
+
return new Response(JSON.stringify(body), {
|
|
19
|
+
status: 200,
|
|
20
|
+
headers: { 'Content-Type': 'application/json' },
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function err(status: number, body: unknown): Response {
|
|
25
|
+
return new Response(JSON.stringify(body), {
|
|
26
|
+
status,
|
|
27
|
+
headers: { 'Content-Type': 'application/json' },
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe('review.api', () => {
|
|
32
|
+
it('fetchReviewSession GETs the review route without a threadId query', async () => {
|
|
33
|
+
fetchMock.mockResolvedValueOnce(
|
|
34
|
+
ok({
|
|
35
|
+
session: {
|
|
36
|
+
branchName: 'alice/draft-one',
|
|
37
|
+
baselineRef: 'abc123',
|
|
38
|
+
createdAt: '2026-04-20T00:00:00Z',
|
|
39
|
+
changes: [],
|
|
40
|
+
},
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
|
+
const s = await fetchReviewSession('ws-1');
|
|
44
|
+
expect(s?.branchName).toBe('alice/draft-one');
|
|
45
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
46
|
+
const [url, init] = fetchMock.mock.calls[0];
|
|
47
|
+
expect(url).toBe('/api/workspace/ws-1/review');
|
|
48
|
+
expect(init?.method).toBeUndefined();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('fetchReviewSession returns null when the server has no active session', async () => {
|
|
52
|
+
fetchMock.mockResolvedValueOnce(ok({ session: null }));
|
|
53
|
+
const s = await fetchReviewSession('ws-1');
|
|
54
|
+
expect(s).toBeNull();
|
|
55
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('fetchReviewFile encodes path into the query string', async () => {
|
|
59
|
+
fetchMock.mockResolvedValueOnce(
|
|
60
|
+
ok({
|
|
61
|
+
path: 'Knowledge/Foo.md',
|
|
62
|
+
kind: 'modified',
|
|
63
|
+
baseline: 'before',
|
|
64
|
+
current: 'after',
|
|
65
|
+
isBinary: false,
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
const payload = await fetchReviewFile('ws-1', 'Knowledge/Foo.md');
|
|
69
|
+
expect(payload.baseline).toBe('before');
|
|
70
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
71
|
+
const [url] = fetchMock.mock.calls[0];
|
|
72
|
+
expect(url).toBe('/api/workspace/ws-1/review/file?path=Knowledge%2FFoo.md');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('acceptReviewChange POSTs with path in the body for per-file accept', async () => {
|
|
76
|
+
fetchMock.mockResolvedValueOnce(ok({ session: null }));
|
|
77
|
+
await acceptReviewChange('ws-1', 'Knowledge/Foo.md');
|
|
78
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
79
|
+
const [url, init] = fetchMock.mock.calls[0];
|
|
80
|
+
expect(url).toBe('/api/workspace/ws-1/review/accept');
|
|
81
|
+
expect(init?.method).toBe('POST');
|
|
82
|
+
expect(JSON.parse(init?.body as string)).toEqual({ path: 'Knowledge/Foo.md' });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('acceptReviewChange POSTs an empty body for accept-all', async () => {
|
|
86
|
+
fetchMock.mockResolvedValueOnce(ok({ session: null }));
|
|
87
|
+
await acceptReviewChange('ws-1');
|
|
88
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
89
|
+
const [, init] = fetchMock.mock.calls[0];
|
|
90
|
+
expect(JSON.parse(init?.body as string)).toEqual({});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('rejectReviewChange POSTs with path in the body for per-file reject', async () => {
|
|
94
|
+
fetchMock.mockResolvedValueOnce(ok({ session: null }));
|
|
95
|
+
await rejectReviewChange('ws-1', 'Knowledge/Foo.md');
|
|
96
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
97
|
+
const [url, init] = fetchMock.mock.calls[0];
|
|
98
|
+
expect(url).toBe('/api/workspace/ws-1/review/reject');
|
|
99
|
+
expect(JSON.parse(init?.body as string)).toEqual({ path: 'Knowledge/Foo.md' });
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('surfaces non-2xx bodies as ReviewApiError with the server message', async () => {
|
|
103
|
+
fetchMock.mockResolvedValueOnce(
|
|
104
|
+
err(400, { error: 'path "foo.md" is not in the active review' }),
|
|
105
|
+
);
|
|
106
|
+
await expect(fetchReviewFile('ws-1', 'foo.md')).rejects.toMatchObject({
|
|
107
|
+
name: 'ReviewApiError',
|
|
108
|
+
status: 400,
|
|
109
|
+
message: 'path "foo.md" is not in the active review',
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('falls back to HTTP <status> when the server returns a non-JSON body', async () => {
|
|
114
|
+
fetchMock.mockResolvedValueOnce(new Response('<html>nope</html>', { status: 500 }));
|
|
115
|
+
await expect(fetchReviewSession('ws-1')).rejects.toMatchObject({
|
|
116
|
+
name: 'ReviewApiError',
|
|
117
|
+
status: 500,
|
|
118
|
+
message: 'HTTP 500',
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { FileQuestion } from 'lucide-react';
|
|
2
|
+
import type { FileDiffPayload } from '@bevel-software/platform-shared';
|
|
3
|
+
|
|
4
|
+
export function BinaryChangePlaceholder({ payload }: { payload: FileDiffPayload }) {
|
|
5
|
+
return (
|
|
6
|
+
<div className="h-full flex flex-col items-center justify-center gap-2 text-slate-600 text-xs px-6 text-center">
|
|
7
|
+
<FileQuestion size={32} className="text-slate-500" />
|
|
8
|
+
<div className="font-medium text-slate-700">Binary file — preview not available</div>
|
|
9
|
+
<div className="font-mono truncate max-w-full" title={payload.path}>
|
|
10
|
+
{payload.path}
|
|
11
|
+
</div>
|
|
12
|
+
<div>Use Accept or Reject from the file list to decide.</div>
|
|
13
|
+
</div>
|
|
14
|
+
);
|
|
15
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import type { FileDiffPayload } from '@bevel-software/platform-shared';
|
|
3
|
+
import { computeDiff, type DiffLine } from '../../workspace/utils/diff';
|
|
4
|
+
|
|
5
|
+
const MAX_RENDERED_LINES = 5000;
|
|
6
|
+
|
|
7
|
+
export function DiffViewer({ payload }: { payload: FileDiffPayload }) {
|
|
8
|
+
const lines = useMemo<DiffLine[]>(() => {
|
|
9
|
+
if (payload.isBinary) return [];
|
|
10
|
+
const baseline = payload.baseline ?? '';
|
|
11
|
+
const current = payload.current ?? '';
|
|
12
|
+
// For added files, baseline is empty so every current line becomes "added".
|
|
13
|
+
// For deleted files, current is empty so every baseline line becomes "removed".
|
|
14
|
+
return computeDiff(baseline, current);
|
|
15
|
+
}, [payload]);
|
|
16
|
+
|
|
17
|
+
if (lines.length > MAX_RENDERED_LINES) {
|
|
18
|
+
return (
|
|
19
|
+
<div className="h-full flex items-center justify-center text-xs text-slate-600 px-6 text-center">
|
|
20
|
+
Diff is too large to render ({lines.length.toLocaleString()} lines).
|
|
21
|
+
Accept or reject from the file list.
|
|
22
|
+
</div>
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Compute line numbers on each side. Unchanged lines advance both counters;
|
|
27
|
+
// "added" advances only current, "removed" advances only baseline.
|
|
28
|
+
let oldLine = 0;
|
|
29
|
+
let newLine = 0;
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<div className="h-full overflow-auto bg-white font-mono text-[12px] leading-[18px]">
|
|
33
|
+
<table className="w-full border-collapse">
|
|
34
|
+
<tbody>
|
|
35
|
+
{lines.map((l, i) => {
|
|
36
|
+
let marker = ' ';
|
|
37
|
+
let bg = '';
|
|
38
|
+
let textColor = 'text-slate-900';
|
|
39
|
+
let left: number | '' = '';
|
|
40
|
+
let right: number | '' = '';
|
|
41
|
+
if (l.type === 'same') {
|
|
42
|
+
oldLine++;
|
|
43
|
+
newLine++;
|
|
44
|
+
left = oldLine;
|
|
45
|
+
right = newLine;
|
|
46
|
+
} else if (l.type === 'added') {
|
|
47
|
+
newLine++;
|
|
48
|
+
right = newLine;
|
|
49
|
+
marker = '+';
|
|
50
|
+
bg = 'bg-emerald-100';
|
|
51
|
+
textColor = 'text-emerald-700';
|
|
52
|
+
} else {
|
|
53
|
+
oldLine++;
|
|
54
|
+
left = oldLine;
|
|
55
|
+
marker = '−';
|
|
56
|
+
bg = 'bg-red-100';
|
|
57
|
+
textColor = 'text-red-700';
|
|
58
|
+
}
|
|
59
|
+
return (
|
|
60
|
+
<tr key={i} className={bg}>
|
|
61
|
+
<td className="w-12 text-right pr-2 text-slate-500 select-none border-r border-slate-200">
|
|
62
|
+
{left}
|
|
63
|
+
</td>
|
|
64
|
+
<td className="w-12 text-right pr-2 text-slate-500 select-none border-r border-slate-200">
|
|
65
|
+
{right}
|
|
66
|
+
</td>
|
|
67
|
+
<td className={`w-4 text-center ${textColor} select-none`}>{marker}</td>
|
|
68
|
+
<td className={`whitespace-pre-wrap break-words pl-2 pr-3 ${textColor}`}>
|
|
69
|
+
{l.text || '\u00A0'}
|
|
70
|
+
</td>
|
|
71
|
+
</tr>
|
|
72
|
+
);
|
|
73
|
+
})}
|
|
74
|
+
</tbody>
|
|
75
|
+
</table>
|
|
76
|
+
</div>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import Markdown from 'react-markdown';
|
|
3
|
+
import remarkGfm from 'remark-gfm';
|
|
4
|
+
import remarkFrontmatter from 'remark-frontmatter';
|
|
5
|
+
import type { FileDiffPayload } from '@bevel-software/platform-shared';
|
|
6
|
+
import { computeDiff, type DiffLine } from '../../workspace/utils/diff';
|
|
7
|
+
import { parseFrontmatter, labelFor, type FrontmatterData } from '../../workspace/utils/frontmatter';
|
|
8
|
+
|
|
9
|
+
const MAX_RENDERED_LINES = 5000;
|
|
10
|
+
|
|
11
|
+
const MD_COMPONENTS = {
|
|
12
|
+
a: ({ href, children, ...props }: React.ComponentPropsWithoutRef<'a'>) =>
|
|
13
|
+
href && /^https?:\/\//i.test(href)
|
|
14
|
+
? <a href={href} {...props} target="_blank" rel="noopener noreferrer">{children}</a>
|
|
15
|
+
: <a href={href} {...props}>{children}</a>,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Frontmatter-aware diff renderer for markdown files. Splits the diff into
|
|
20
|
+
* frontmatter and body regions; renders the frontmatter as a structured
|
|
21
|
+
* key-by-key red/green panel (rather than raw `key: value` lines), and the
|
|
22
|
+
* body as either a markdown-rendered preview with red/green change blocks or
|
|
23
|
+
* a git-conflict-marker view for side-by-side text inspection.
|
|
24
|
+
*/
|
|
25
|
+
export function MarkdownDiffViewer({ payload }: { payload: FileDiffPayload }) {
|
|
26
|
+
const data = useMemo(() => {
|
|
27
|
+
const baseline = payload.baseline ?? '';
|
|
28
|
+
const current = payload.current ?? '';
|
|
29
|
+
// Cheap pre-check: count newlines before doing the LCS + frontmatter +
|
|
30
|
+
// markdown-rendering work. The diff line count can't exceed baselineLines +
|
|
31
|
+
// currentLines, so if their sum already exceeds the render cap we know the
|
|
32
|
+
// result is "too large" and can skip the heavy computation entirely.
|
|
33
|
+
const rawLineCount = countNewlines(baseline) + countNewlines(current) + 2;
|
|
34
|
+
if (rawLineCount > MAX_RENDERED_LINES) {
|
|
35
|
+
return {
|
|
36
|
+
tooLarge: true as const,
|
|
37
|
+
rawLineCount,
|
|
38
|
+
lines: [] as DiffLine[],
|
|
39
|
+
bodyBlocks: [] as DisplayBlock[],
|
|
40
|
+
frontmatterChanged: false,
|
|
41
|
+
oldFrontmatter: {} as FrontmatterData,
|
|
42
|
+
newFrontmatter: {} as FrontmatterData,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const lines = computeDiff(baseline, current);
|
|
46
|
+
const oldFmEnd = frontmatterLineCount(baseline);
|
|
47
|
+
const newFmEnd = frontmatterLineCount(current);
|
|
48
|
+
const split = splitDiffByFrontmatter(lines, oldFmEnd, newFmEnd);
|
|
49
|
+
return {
|
|
50
|
+
tooLarge: false as const,
|
|
51
|
+
rawLineCount,
|
|
52
|
+
lines,
|
|
53
|
+
bodyBlocks: groupDiffBlocks(split.body),
|
|
54
|
+
frontmatterChanged: split.frontmatter.some((l) => l.type !== 'same'),
|
|
55
|
+
oldFrontmatter: parseFrontmatter(baseline).data,
|
|
56
|
+
newFrontmatter: parseFrontmatter(current).data,
|
|
57
|
+
};
|
|
58
|
+
}, [payload]);
|
|
59
|
+
|
|
60
|
+
if (data.tooLarge || data.lines.length > MAX_RENDERED_LINES) {
|
|
61
|
+
const shown = data.tooLarge ? data.rawLineCount : data.lines.length;
|
|
62
|
+
return (
|
|
63
|
+
<div className="h-full flex items-center justify-center text-xs text-slate-600 px-6 text-center">
|
|
64
|
+
Diff is too large to render ({shown.toLocaleString()} lines).
|
|
65
|
+
Accept or reject from the file list.
|
|
66
|
+
</div>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<div className="h-full overflow-auto bg-white px-4 py-3">
|
|
72
|
+
{data.frontmatterChanged && (
|
|
73
|
+
<FrontmatterDiffPanel
|
|
74
|
+
oldData={data.oldFrontmatter}
|
|
75
|
+
newData={data.newFrontmatter}
|
|
76
|
+
/>
|
|
77
|
+
)}
|
|
78
|
+
<DiffPreview blocks={data.bodyBlocks} />
|
|
79
|
+
</div>
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function countNewlines(s: string): number {
|
|
84
|
+
let count = 0;
|
|
85
|
+
for (let i = 0; i < s.length; i++) {
|
|
86
|
+
if (s.charCodeAt(i) === 10) count++;
|
|
87
|
+
}
|
|
88
|
+
return count;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// ── frontmatter helpers ─────────────────────────────────────────────────
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Count how many lines the frontmatter block occupies (including both `---`
|
|
95
|
+
* delimiters) when `content` is split on '\n'. Returns 0 when there is no
|
|
96
|
+
* frontmatter so callers can skip the frontmatter-split logic.
|
|
97
|
+
*/
|
|
98
|
+
function frontmatterLineCount(content: string): number {
|
|
99
|
+
const lines = content.split('\n').map((l) => (l.endsWith('\r') ? l.slice(0, -1) : l));
|
|
100
|
+
if (lines[0] !== '---') return 0;
|
|
101
|
+
for (let i = 1; i < lines.length; i++) {
|
|
102
|
+
if (lines[i] === '---') return i + 1;
|
|
103
|
+
}
|
|
104
|
+
return 0;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Walk the diff lines, tracking old- and new-line cursors, and split them
|
|
109
|
+
* into the lines that fall inside the frontmatter region and the lines
|
|
110
|
+
* that fall inside the body. `oldFmEnd` / `newFmEnd` are exclusive upper
|
|
111
|
+
* bounds (i.e. the number of frontmatter lines on each side).
|
|
112
|
+
*/
|
|
113
|
+
function splitDiffByFrontmatter(
|
|
114
|
+
lines: DiffLine[],
|
|
115
|
+
oldFmEnd: number,
|
|
116
|
+
newFmEnd: number,
|
|
117
|
+
): { frontmatter: DiffLine[]; body: DiffLine[] } {
|
|
118
|
+
const frontmatter: DiffLine[] = [];
|
|
119
|
+
const body: DiffLine[] = [];
|
|
120
|
+
let oldIdx = 0;
|
|
121
|
+
let newIdx = 0;
|
|
122
|
+
for (const line of lines) {
|
|
123
|
+
const inOldFm = oldIdx < oldFmEnd;
|
|
124
|
+
const inNewFm = newIdx < newFmEnd;
|
|
125
|
+
if (line.type === 'same') {
|
|
126
|
+
if (inOldFm && inNewFm) frontmatter.push(line);
|
|
127
|
+
else body.push(line);
|
|
128
|
+
oldIdx++;
|
|
129
|
+
newIdx++;
|
|
130
|
+
} else if (line.type === 'removed') {
|
|
131
|
+
if (inOldFm) frontmatter.push(line);
|
|
132
|
+
else body.push(line);
|
|
133
|
+
oldIdx++;
|
|
134
|
+
} else {
|
|
135
|
+
if (inNewFm) frontmatter.push(line);
|
|
136
|
+
else body.push(line);
|
|
137
|
+
newIdx++;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return { frontmatter, body };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function FrontmatterDiffPanel({
|
|
144
|
+
oldData,
|
|
145
|
+
newData,
|
|
146
|
+
}: {
|
|
147
|
+
oldData: FrontmatterData;
|
|
148
|
+
newData: FrontmatterData;
|
|
149
|
+
}) {
|
|
150
|
+
const keys = Array.from(new Set([...Object.keys(oldData), ...Object.keys(newData)]));
|
|
151
|
+
const visible = keys.filter((k) => {
|
|
152
|
+
const o = oldData[k];
|
|
153
|
+
const n = newData[k];
|
|
154
|
+
const oEmpty = o === undefined || (Array.isArray(o) ? o.length === 0 : o === '');
|
|
155
|
+
const nEmpty = n === undefined || (Array.isArray(n) ? n.length === 0 : n === '');
|
|
156
|
+
return !(oEmpty && nEmpty);
|
|
157
|
+
});
|
|
158
|
+
if (visible.length === 0) return null;
|
|
159
|
+
|
|
160
|
+
const formatValue = (v: string | string[] | undefined): string => {
|
|
161
|
+
if (v === undefined) return '';
|
|
162
|
+
return Array.isArray(v) ? v.join(', ') : v;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
return (
|
|
166
|
+
<div className="mb-4 rounded-lg border border-slate-300 bg-slate-100/70 divide-y divide-slate-200/60">
|
|
167
|
+
{visible.map((key) => {
|
|
168
|
+
const oldVal = formatValue(oldData[key]);
|
|
169
|
+
const newVal = formatValue(newData[key]);
|
|
170
|
+
const changed = oldVal !== newVal;
|
|
171
|
+
return (
|
|
172
|
+
<div key={key} className="flex items-start gap-3 px-3 py-2 text-xs">
|
|
173
|
+
<span className="shrink-0 text-slate-600 w-28">{labelFor(key)}</span>
|
|
174
|
+
{changed ? (
|
|
175
|
+
<div className="flex-1 space-y-1 break-words">
|
|
176
|
+
{oldVal && (
|
|
177
|
+
<div className="bg-red-100 text-red-700 px-2 py-0.5 rounded-sm">{oldVal}</div>
|
|
178
|
+
)}
|
|
179
|
+
{newVal && (
|
|
180
|
+
<div className="bg-emerald-100 text-emerald-700 px-2 py-0.5 rounded-sm">{newVal}</div>
|
|
181
|
+
)}
|
|
182
|
+
</div>
|
|
183
|
+
) : (
|
|
184
|
+
<span className="text-slate-900 break-words">{oldVal}</span>
|
|
185
|
+
)}
|
|
186
|
+
</div>
|
|
187
|
+
);
|
|
188
|
+
})}
|
|
189
|
+
</div>
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// ── body diff helpers ───────────────────────────────────────────────────
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Group consecutive same/changed runs into display blocks: runs of "same"
|
|
197
|
+
* lines, and "conflict" blocks bundling consecutive removed+added runs
|
|
198
|
+
* together — the same shape git uses for merge conflicts.
|
|
199
|
+
*/
|
|
200
|
+
type DisplayBlock =
|
|
201
|
+
| { type: 'same'; lines: string[] }
|
|
202
|
+
| { type: 'conflict'; removed: string[]; added: string[] };
|
|
203
|
+
|
|
204
|
+
function groupDiffBlocks(lines: DiffLine[]): DisplayBlock[] {
|
|
205
|
+
const result: DisplayBlock[] = [];
|
|
206
|
+
let i = 0;
|
|
207
|
+
while (i < lines.length) {
|
|
208
|
+
if (lines[i].type === 'same') {
|
|
209
|
+
const block: DisplayBlock = { type: 'same', lines: [] };
|
|
210
|
+
while (i < lines.length && lines[i].type === 'same') {
|
|
211
|
+
block.lines.push(lines[i].text);
|
|
212
|
+
i++;
|
|
213
|
+
}
|
|
214
|
+
result.push(block);
|
|
215
|
+
} else {
|
|
216
|
+
const block: DisplayBlock = { type: 'conflict', removed: [], added: [] };
|
|
217
|
+
while (i < lines.length && lines[i].type === 'removed') {
|
|
218
|
+
block.removed.push(lines[i].text);
|
|
219
|
+
i++;
|
|
220
|
+
}
|
|
221
|
+
while (i < lines.length && lines[i].type === 'added') {
|
|
222
|
+
block.added.push(lines[i].text);
|
|
223
|
+
i++;
|
|
224
|
+
}
|
|
225
|
+
result.push(block);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return result;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function DiffPreview({ blocks }: { blocks: DisplayBlock[] }) {
|
|
232
|
+
return (
|
|
233
|
+
<div className="prose prose-sm max-w-none">
|
|
234
|
+
{blocks.map((block, i) => {
|
|
235
|
+
if (block.type === 'same') {
|
|
236
|
+
return (
|
|
237
|
+
<Markdown key={i} remarkPlugins={[remarkGfm, remarkFrontmatter]} components={MD_COMPONENTS}>
|
|
238
|
+
{block.lines.join('\n')}
|
|
239
|
+
</Markdown>
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
return (
|
|
243
|
+
<div key={i} className="my-2">
|
|
244
|
+
{block.removed.length > 0 && (
|
|
245
|
+
<div className="bg-red-50 border-l-2 border-red-700 px-3 py-1 rounded-sm">
|
|
246
|
+
<Markdown remarkPlugins={[remarkGfm, remarkFrontmatter]} components={MD_COMPONENTS}>
|
|
247
|
+
{block.removed.join('\n')}
|
|
248
|
+
</Markdown>
|
|
249
|
+
</div>
|
|
250
|
+
)}
|
|
251
|
+
{block.added.length > 0 && (
|
|
252
|
+
<div className="bg-emerald-50 border-l-2 border-emerald-700 px-3 py-1 rounded-sm mt-1">
|
|
253
|
+
<Markdown remarkPlugins={[remarkGfm, remarkFrontmatter]} components={MD_COMPONENTS}>
|
|
254
|
+
{block.added.join('\n')}
|
|
255
|
+
</Markdown>
|
|
256
|
+
</div>
|
|
257
|
+
)}
|
|
258
|
+
</div>
|
|
259
|
+
);
|
|
260
|
+
})}
|
|
261
|
+
</div>
|
|
262
|
+
);
|
|
263
|
+
}
|