Files
kandev/apps/web/components/diff/file-diff-viewer.tsx
T
José Almeida 214a8f5cce fix: scope multi-repo file editor open/save to the right repo (#1382)
* fix: scope multi-repo file editor open/save to the right repo

The editor open/save/sync/delete chain dropped the repository_name in
multi-repo task workspaces, so the backend resolved paths against the
bare task root and failed with "file not found". Thread the repo
subpath through openFile, save, sync, and delete, and store it on
FileEditorState.

* fix: pass repo from diff Edit button to multi-repo file editor

The diff/changes "Edit" button opened files without their repository
scope, so multi-repo files resolved against the bare task root and
failed with "file not found". Thread repository_name through the diff
and review toolbars into onOpenFile.

* fix: preserve repo across tab restore and stale syncs

Address review feedback: persisted file tabs dropped the repo subpath,
so a multi-repo file reopened against the bare task root after a browser
refresh. Persist and restore `repo` on StoredFileTab. Also guard the
workspace sync against a tab being swapped to a different repo mid-fetch,
and widen the remaining onOpenFile type declarations to carry repo.

* fix: thread repo into editor panel fallback fetch

Address review feedback: FileEditorPanel's useFileLoader fallback fetch
(triggered when the open-files map is transiently empty, e.g. after a
session switch) called requestFileContent without the repo subpath,
re-introducing the multi-repo "file not found" failure. Record repo on
the panel params and forward it through useFileLoader. Also assert the
full restored-tab shape in the local-storage round-trip test.

* fix: guard editor fetches against stale session/repo races

Address review feedback: file open and the panel fallback fetch could
apply a late response after the user navigated away. openFile now drops
the result if the active session changed mid-fetch (via fetchFileEditorState),
and the panel loader keys its in-flight guard by session+path+repo so a
changed target re-fetches and stale responses are discarded.
2026-06-15 17:59:05 +01:00

102 lines
3.0 KiB
TypeScript

"use client";
import { memo, useMemo } from "react";
import { DiffViewerResolved as DiffViewer } from "./diff-viewer-resolver";
import { transformGitDiff } from "@/lib/diff";
import type { DiffComment } from "@/lib/diff/types";
import type { RevertBlockInfo } from "./diff-viewer-resolver";
interface FileDiffViewerProps {
filePath: string;
diff: string;
status?: string;
enableComments?: boolean;
sessionId?: string;
onCommentAdd?: (comment: DiffComment) => void;
onCommentDelete?: (commentId: string) => void;
onCommentRun?: (comment: DiffComment) => void;
comments?: DiffComment[];
className?: string;
compact?: boolean;
hideHeader?: boolean;
onOpenFile?: (filePath: string, repo?: string) => void;
onPreviewMarkdown?: (filePath: string) => void;
onRevert?: (filePath: string) => void;
enableAcceptReject?: boolean;
onRevertBlock?: (filePath: string, info: RevertBlockInfo) => void;
wordWrap?: boolean;
/** Enable diff expansion (show expand up/down buttons at hunk separators) */
enableExpansion?: boolean;
/** Base git ref for fetching old content (e.g., "origin/main", "HEAD~1") */
baseRef?: string;
/** Controlled expand-unchanged state */
expandUnchanged?: boolean;
/** Callback when expand-unchanged is toggled (controlled mode) */
onToggleExpandUnchanged?: () => void;
/** Multi-repo subpath for the file (e.g. "kandev"); empty for single-repo. */
repo?: string;
}
/**
* Wrapper around DiffViewer that handles data transformation with memoization.
*
* Use this component when you have raw diff data (filePath, diff, status).
* The transformation to FileDiffData is memoized internally, preventing
* unnecessary re-renders when parent components update.
*
* This is the recommended component to use for rendering git diffs.
*/
export const FileDiffViewer = memo(function FileDiffViewer({
filePath,
diff,
status = "M",
enableComments,
sessionId,
onCommentAdd,
onCommentDelete,
onCommentRun,
comments,
className,
compact,
hideHeader,
onOpenFile,
onPreviewMarkdown,
onRevert,
enableAcceptReject,
onRevertBlock,
wordWrap,
enableExpansion,
baseRef,
expandUnchanged,
onToggleExpandUnchanged,
repo,
}: FileDiffViewerProps) {
const data = useMemo(() => transformGitDiff(filePath, diff, status), [filePath, diff, status]);
return (
<DiffViewer
data={data}
enableComments={enableComments}
sessionId={sessionId}
onCommentAdd={onCommentAdd}
onCommentDelete={onCommentDelete}
onCommentRun={onCommentRun}
comments={comments}
className={className}
compact={compact}
hideHeader={hideHeader}
onOpenFile={onOpenFile}
onPreviewMarkdown={onPreviewMarkdown}
onRevert={onRevert}
enableAcceptReject={enableAcceptReject}
onRevertBlock={onRevertBlock}
wordWrap={wordWrap}
enableExpansion={enableExpansion}
baseRef={baseRef}
expandUnchanged={expandUnchanged}
onToggleExpandUnchanged={onToggleExpandUnchanged}
repo={repo}
/>
);
});