fix(web): make review ordering and navigation safe (#2067)
This commit is contained in:
@@ -27,6 +27,8 @@ interface DiffViewerResolverProps {
|
||||
onPreviewMarkdown?: (filePath: string) => void;
|
||||
onRevert?: (filePath: string) => void;
|
||||
enableAcceptReject?: boolean;
|
||||
/** Render and navigate to the active task walkthrough (Pierre diffs only). */
|
||||
enableWalkthroughAnnotations?: boolean;
|
||||
onRevertBlock?: (filePath: string, info: RevertBlockInfo) => void;
|
||||
wordWrap?: boolean;
|
||||
editable?: boolean;
|
||||
@@ -58,6 +60,7 @@ export const DiffViewerResolved = memo(function DiffViewerResolved(props: DiffVi
|
||||
enableComments,
|
||||
baseRef,
|
||||
enableExpansion,
|
||||
enableWalkthroughAnnotations,
|
||||
expandUnchanged,
|
||||
onToggleExpandUnchanged,
|
||||
onPreviewMarkdown,
|
||||
|
||||
@@ -84,6 +84,17 @@ describe("DiffViewer", () => {
|
||||
await waitFor(() => expect(fileDiffProps.length).toBeGreaterThan(renderCount));
|
||||
});
|
||||
|
||||
it("rerenders when walkthrough annotations are disabled", async () => {
|
||||
const { rerender } = render(<DiffViewer data={data} enableWalkthroughAnnotations />);
|
||||
|
||||
await waitFor(() => expect(fileDiffProps.length).toBeGreaterThan(0));
|
||||
const renderCount = fileDiffProps.length;
|
||||
|
||||
rerender(<DiffViewer data={data} enableWalkthroughAnnotations={false} />);
|
||||
|
||||
await waitFor(() => expect(fileDiffProps.length).toBeGreaterThan(renderCount));
|
||||
});
|
||||
|
||||
it("keeps simple hunk separators when full-content reparse rejects expansion", async () => {
|
||||
expansionState.canExpand = false;
|
||||
|
||||
|
||||
@@ -36,6 +36,8 @@ interface DiffViewerProps {
|
||||
onPreviewMarkdown?: (filePath: string) => void;
|
||||
onRevert?: (filePath: string) => void;
|
||||
enableAcceptReject?: boolean;
|
||||
/** Render and navigate to the active task walkthrough inside this diff. */
|
||||
enableWalkthroughAnnotations?: boolean;
|
||||
onRevertBlock?: (filePath: string, info: RevertBlockInfo) => Promise<void> | void;
|
||||
wordWrap?: boolean;
|
||||
/** Enable diff expansion (show expand up/down buttons at hunk separators) */
|
||||
@@ -68,6 +70,7 @@ const SCALAR_PROP_KEYS: (keyof DiffViewerProps)[] = [
|
||||
"onPreviewMarkdown",
|
||||
"onRevert",
|
||||
"enableAcceptReject",
|
||||
"enableWalkthroughAnnotations",
|
||||
"onRevertBlock",
|
||||
"wordWrap",
|
||||
"enableExpansion",
|
||||
@@ -271,6 +274,7 @@ export const DiffViewer = memo(function DiffViewer(props: DiffViewerProps) {
|
||||
onPreviewMarkdown,
|
||||
onRevert,
|
||||
enableAcceptReject = false,
|
||||
enableWalkthroughAnnotations = true,
|
||||
onRevertBlock,
|
||||
wordWrap: wordWrapProp,
|
||||
enableExpansion = false,
|
||||
@@ -293,6 +297,7 @@ export const DiffViewer = memo(function DiffViewer(props: DiffViewerProps) {
|
||||
data,
|
||||
enableComments,
|
||||
enableAcceptReject,
|
||||
enableWalkthroughAnnotations,
|
||||
sessionId,
|
||||
onCommentAdd,
|
||||
onCommentDelete,
|
||||
|
||||
@@ -24,6 +24,8 @@ interface FileDiffViewerProps {
|
||||
onPreviewMarkdown?: (filePath: string) => void;
|
||||
onRevert?: (filePath: string) => void;
|
||||
enableAcceptReject?: boolean;
|
||||
/** Render and navigate to the active task walkthrough inside this diff. */
|
||||
enableWalkthroughAnnotations?: boolean;
|
||||
onRevertBlock?: (filePath: string, info: RevertBlockInfo) => void;
|
||||
wordWrap?: boolean;
|
||||
/** Enable diff expansion (show expand up/down buttons at hunk separators) */
|
||||
@@ -70,6 +72,7 @@ export const FileDiffViewer = memo(function FileDiffViewer({
|
||||
onPreviewMarkdown,
|
||||
onRevert,
|
||||
enableAcceptReject,
|
||||
enableWalkthroughAnnotations,
|
||||
onRevertBlock,
|
||||
wordWrap,
|
||||
enableExpansion,
|
||||
@@ -102,6 +105,7 @@ export const FileDiffViewer = memo(function FileDiffViewer({
|
||||
onPreviewMarkdown={onPreviewMarkdown}
|
||||
onRevert={onRevert}
|
||||
enableAcceptReject={enableAcceptReject}
|
||||
enableWalkthroughAnnotations={enableWalkthroughAnnotations}
|
||||
onRevertBlock={onRevertBlock}
|
||||
wordWrap={wordWrap}
|
||||
enableExpansion={enableExpansion}
|
||||
|
||||
@@ -115,11 +115,13 @@ describe("buildHunkAnnotations", () => {
|
||||
});
|
||||
});
|
||||
|
||||
const WALKTHROUGH_PATH = "apps/web/main.ts";
|
||||
|
||||
describe("buildWalkthroughSelectedLines", () => {
|
||||
it("returns the active walkthrough line range for a matching file", () => {
|
||||
expect(
|
||||
buildWalkthroughSelectedLines(
|
||||
{ path: "apps/web/main.ts", repository_name: "frontend" },
|
||||
{ path: WALKTHROUGH_PATH, repository_name: "frontend" },
|
||||
{ file: "main.ts", repo: "frontend", line: 10, line_end: 12, text: "explain" },
|
||||
),
|
||||
).toEqual({ side: "additions", start: 10, end: 12 });
|
||||
@@ -128,7 +130,7 @@ describe("buildWalkthroughSelectedLines", () => {
|
||||
it("returns null for mismatched repos", () => {
|
||||
expect(
|
||||
buildWalkthroughSelectedLines(
|
||||
{ path: "apps/web/main.ts", repository_name: "backend" },
|
||||
{ path: WALKTHROUGH_PATH, repository_name: "backend" },
|
||||
{ file: "main.ts", repo: "frontend", line: 10, text: "explain" },
|
||||
),
|
||||
).toBeNull();
|
||||
@@ -137,9 +139,19 @@ describe("buildWalkthroughSelectedLines", () => {
|
||||
it("defaults line_end to line when the walkthrough step has no line_end", () => {
|
||||
expect(
|
||||
buildWalkthroughSelectedLines(
|
||||
{ path: "apps/web/main.ts", repository_name: "frontend" },
|
||||
{ path: WALKTHROUGH_PATH, repository_name: "frontend" },
|
||||
{ file: "main.ts", repo: "frontend", line: 15, text: "see this" },
|
||||
),
|
||||
).toEqual({ side: "additions", start: 15, end: 15 });
|
||||
});
|
||||
|
||||
it("returns null when walkthrough annotations are disabled", () => {
|
||||
expect(
|
||||
buildWalkthroughSelectedLines(
|
||||
{ path: WALKTHROUGH_PATH, repository_name: "frontend" },
|
||||
{ file: "main.ts", repo: "frontend", line: 15, text: "see this" },
|
||||
false,
|
||||
),
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -141,6 +141,8 @@ type UseDiffViewerStateOpts = {
|
||||
data: FileDiffData;
|
||||
enableComments: boolean;
|
||||
enableAcceptReject: boolean;
|
||||
/** Render and navigate to the active task walkthrough inside this diff. */
|
||||
enableWalkthroughAnnotations?: boolean;
|
||||
sessionId?: string;
|
||||
onCommentAdd?: (comment: DiffComment) => void;
|
||||
onCommentDelete?: (commentId: string) => void;
|
||||
@@ -164,8 +166,9 @@ type UseDiffViewerStateOpts = {
|
||||
export function buildWalkthroughSelectedLines(
|
||||
file: WalkthroughTargetFile,
|
||||
step: WalkthroughStep | null | undefined,
|
||||
enabled = true,
|
||||
): SelectedLineRange | null {
|
||||
if (!step || !walkthroughStepMatchesFile(file, step)) return null;
|
||||
if (!enabled || !step || !walkthroughStepMatchesFile(file, step)) return null;
|
||||
return {
|
||||
side: "additions",
|
||||
start: step.line,
|
||||
@@ -176,17 +179,18 @@ export function buildWalkthroughSelectedLines(
|
||||
function useWalkthroughSelection(
|
||||
filePath: string,
|
||||
repo: string | undefined,
|
||||
enabled: boolean,
|
||||
): {
|
||||
annotation: DiffLineAnnotation<AnnotationMetadata> | null;
|
||||
selectedLines: SelectedLineRange | null;
|
||||
} {
|
||||
const activeTaskId = useOptionalAppStore((s) => s.tasks.activeTaskId, null);
|
||||
const activeTaskId = useOptionalAppStore((s) => (enabled ? s.tasks.activeTaskId : null), null);
|
||||
const walkthrough = useOptionalAppStore(
|
||||
(s) => (activeTaskId ? s.walkthroughs.byTaskId[activeTaskId] : null),
|
||||
(s) => (enabled && activeTaskId ? s.walkthroughs.byTaskId[activeTaskId] : null),
|
||||
null,
|
||||
);
|
||||
const activeStep = useOptionalAppStore(
|
||||
(s) => (activeTaskId ? (s.walkthroughs.activeStepByTaskId[activeTaskId] ?? 0) : 0),
|
||||
(s) => (enabled && activeTaskId ? (s.walkthroughs.activeStepByTaskId[activeTaskId] ?? 0) : 0),
|
||||
0,
|
||||
);
|
||||
return useMemo(() => {
|
||||
@@ -194,6 +198,7 @@ function useWalkthroughSelection(
|
||||
const selectedLines = buildWalkthroughSelectedLines(
|
||||
{ path: filePath, repository_name: repo },
|
||||
step,
|
||||
enabled,
|
||||
);
|
||||
if (!selectedLines) return { annotation: null, selectedLines: null };
|
||||
return {
|
||||
@@ -204,7 +209,7 @@ function useWalkthroughSelection(
|
||||
},
|
||||
selectedLines,
|
||||
};
|
||||
}, [walkthrough, activeStep, filePath, repo]);
|
||||
}, [walkthrough, activeStep, filePath, repo, enabled]);
|
||||
}
|
||||
|
||||
function useDiffViewerAnnotations({
|
||||
@@ -217,12 +222,14 @@ function useDiffViewerAnnotations({
|
||||
filePath,
|
||||
repo,
|
||||
diff,
|
||||
enableWalkthroughAnnotations,
|
||||
changeLineMapRef,
|
||||
revertInfoRef,
|
||||
}: BuildAnnotationsOpts & {
|
||||
filePath: string;
|
||||
repo?: string;
|
||||
diff?: string;
|
||||
enableWalkthroughAnnotations: boolean;
|
||||
changeLineMapRef: RefObject<Map<string, string>>;
|
||||
revertInfoRef: RefObject<Map<string, RevertBlockInfo>>;
|
||||
}) {
|
||||
@@ -246,7 +253,7 @@ function useDiffViewerAnnotations({
|
||||
],
|
||||
);
|
||||
|
||||
const walkthrough = useWalkthroughSelection(filePath, repo);
|
||||
const walkthrough = useWalkthroughSelection(filePath, repo, enableWalkthroughAnnotations);
|
||||
const reviewFindings = useReviewFindingAnnotations({ filePath, repo, diff });
|
||||
const withWalkthrough = useMemo(() => {
|
||||
const combined = [...annotations, ...reviewFindings.annotations];
|
||||
@@ -415,6 +422,7 @@ export function useDiffViewerState(opts: UseDiffViewerStateOpts) {
|
||||
data,
|
||||
enableComments,
|
||||
enableAcceptReject,
|
||||
enableWalkthroughAnnotations = true,
|
||||
sessionId,
|
||||
onCommentAdd,
|
||||
onCommentDelete,
|
||||
@@ -465,6 +473,7 @@ export function useDiffViewerState(opts: UseDiffViewerStateOpts) {
|
||||
filePath: data.filePath,
|
||||
repo,
|
||||
diff: data.diff,
|
||||
enableWalkthroughAnnotations,
|
||||
changeLineMapRef,
|
||||
revertInfoRef,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user