Skip to content

Commit f92fe4d

Browse files
committed
fix(review): preserve chapter totals when filtering files
1 parent 516c190 commit f92fe4d

4 files changed

Lines changed: 107 additions & 26 deletions

File tree

‎apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -979,23 +979,17 @@ export function PullRequestDiffContent({
979979
]);
980980
const chapters = useMemo(
981981
() =>
982-
grouped && visibleParsed.kind === "files"
983-
? reviewFileChapters(visibleParsed.files, (file) => file.path)
982+
grouped && filter.parsed.kind === "files" && visibleParsed.kind === "files"
983+
? reviewFileChapters(filter.parsed.files, (file) => file.path, visibleParsed.files)
984984
: [],
985-
[grouped, visibleParsed],
985+
[grouped, filter.parsed, visibleParsed],
986986
);
987987
const activeChapter = chapters.find((chapter) => chapter.id === chapterId) ?? chapters[0];
988-
const chapterFiles = useMemo(
989-
() =>
990-
activeChapter && result.parsed.kind === "files"
991-
? result.parsed.files.filter((file) => reviewFileGroup(file.path) === activeChapter.id)
992-
: [],
993-
[activeChapter, result.parsed],
994-
);
988+
const chapterFiles = useMemo(() => activeChapter?.files ?? [], [activeChapter]);
995989
const reviewedChapterFiles = chapterFiles.filter((file) => viewed.isViewed(file.path)).length;
996990
const parsed = useMemo(() => {
997991
if (!activeChapter || visibleParsed.kind !== "files") return visibleParsed;
998-
const files = activeChapter.files;
992+
const files = activeChapter.visibleFiles;
999993
return {
1000994
...visibleParsed,
1001995
files,
@@ -1084,7 +1078,7 @@ export function PullRequestDiffContent({
10841078
setLastChapter(activeChapterId);
10851079
if (activeChapter) {
10861080
if (!jumpPath || reviewFileGroup(jumpPath) !== activeChapter.id)
1087-
setJumpPath(activeChapter.files[0]?.path ?? "");
1081+
setJumpPath(activeChapter.visibleFiles[0]?.path ?? "");
10881082
setSelectedRowId(null);
10891083
setRangeEndRowId(null);
10901084
setCollapsedIds(EMPTY_IDS);
@@ -1268,7 +1262,7 @@ export function PullRequestDiffContent({
12681262
const chapter = chapters[position];
12691263
if (!chapter) return;
12701264
setChapterId(chapter.id);
1271-
setJumpPath(chapter.files[0]?.path ?? "");
1265+
setJumpPath(chapter.visibleFiles[0]?.path ?? "");
12721266
setSelectedRowId(null);
12731267
setRangeEndRowId(null);
12741268
jumpStarted.current = false;

‎apps/web/src/components/pullRequest/PullRequestCodeTab.tsx‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -779,8 +779,9 @@ function PullRequestCodeTab({
779779
[annotatedFiles, hideReviewed, commentsOnly, isFileViewed, commentCounts, revealRequest?.path],
780780
);
781781
const chapters = useMemo(
782-
() => (groupByType ? reviewFileChapters(filteredFiles, (file) => file.path) : []),
783-
[filteredFiles, groupByType],
782+
() =>
783+
groupByType ? reviewFileChapters(annotatedFiles, (file) => file.path, filteredFiles) : [],
784+
[annotatedFiles, filteredFiles, groupByType],
784785
);
785786
const chapterPosition = Math.max(
786787
0,
@@ -790,7 +791,7 @@ function PullRequestCodeTab({
790791
const chapterReviewComplete = nextCursor === null && !diffQuery.isPending;
791792
const items = useMemo<CodeViewDiffItem<ReviewAnnotationGroup>[]>(
792793
() =>
793-
(chapter?.files ?? filteredFiles).map(
794+
(chapter?.visibleFiles ?? filteredFiles).map(
794795
({ fileKey, path, fileDiff, annotations, annotationsVersion }) => {
795796
const collapsed = isFileDiffCollapsed(
796797
fileKey,
@@ -1163,12 +1164,18 @@ function PullRequestCodeTab({
11631164
// screen on any tab re-render (a drag-selection, a keystroke in the draft, a review-store
11641165
// update), which is the jank this file is otherwise clean of.
11651166
const refreshDiffQuery = diffQuery.refresh;
1167+
const hasVisibleItems = items.length > 0;
11661168
const renderCodeViewFooter = useCallback(
11671169
() =>
1168-
// Only while something is still owed. A finished diff whose query fails on a later
1169-
// refresh — a reconnect re-runs every one of them — is whole on screen already, and
1170-
// saying otherwise sends the reader looking for files that are all there.
1171-
nextCursor === null ? null : (
1170+
// An empty filtered chapter keeps its navigation and totals. Loading failures belong
1171+
// only to an outstanding cursor, rather than a failed refresh of a finished diff.
1172+
nextCursor === null ? (
1173+
!hasVisibleItems ? (
1174+
<p className="px-4 py-5 text-sm text-muted-foreground">
1175+
No files match the viewing options.
1176+
</p>
1177+
) : null
1178+
) : (
11721179
<div
11731180
ref={setSentinel}
11741181
className="flex items-center justify-center gap-2 py-2 text-xs text-muted-foreground"
@@ -1185,7 +1192,7 @@ function PullRequestCodeTab({
11851192
) : null}
11861193
</div>
11871194
),
1188-
[nextCursor, diffQuery.error, diffQuery.isPending, refreshDiffQuery],
1195+
[nextCursor, hasVisibleItems, diffQuery.error, diffQuery.isPending, refreshDiffQuery],
11891196
);
11901197

11911198
const renderHeaderPrefix = useCallback(
@@ -1860,7 +1867,7 @@ function PullRequestCodeTab({
18601867
return withToolbar(rawSlicesView);
18611868
}
18621869

1863-
if (items.length === 0 && nextCursor === null) {
1870+
if (items.length === 0 && nextCursor === null && chapter === null) {
18641871
return withToolbar(
18651872
<p className="px-4 py-5 text-sm text-muted-foreground">
18661873
{files.length > 0
@@ -1937,7 +1944,7 @@ function PullRequestCodeTab({
19371944
) : null}
19381945
{guideFallback ? (
19391946
<ul className="flex min-w-0 flex-col gap-0.5">
1940-
{members.map((file) => (
1947+
{chapters[index]?.visibleFiles.map((file) => (
19411948
<li key={file.path}>
19421949
<ReviewChapterFileLink
19431950
path={file.path}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { reviewFileChapters } from "./reviewFileClassification.ts";
4+
5+
const getPath = (file: { path: string }) => file.path;
6+
const implementation = Array.from({ length: 30 }, (_, index) => ({
7+
path: `src/file-${index}.ts`,
8+
viewed: index < 15,
9+
}));
10+
11+
describe("review chapter membership and visibility", () => {
12+
it("keeps 15 of 30 reviewed when only the 15 unviewed files are visible", () => {
13+
const visible = implementation.filter((file) => !file.viewed);
14+
const [chapter] = reviewFileChapters(implementation, getPath, visible);
15+
16+
expect(chapter?.files).toHaveLength(30);
17+
expect(chapter?.files.filter((file) => file.viewed)).toHaveLength(15);
18+
expect(chapter?.visibleFiles).toEqual(visible);
19+
expect(reviewFileChapters(implementation, getPath)[0]?.files).toEqual(chapter?.files);
20+
});
21+
22+
it("keeps a fully reviewed chapter reachable when every file in it is hidden", () => {
23+
const files = [
24+
{ path: "src/app.ts", viewed: true },
25+
{ path: "tests/app.test.ts", viewed: true },
26+
{ path: "pnpm-lock.yaml", viewed: false },
27+
];
28+
const chapters = reviewFileChapters(
29+
files,
30+
getPath,
31+
files.filter((file) => !file.viewed),
32+
);
33+
34+
expect(chapters.map((chapter) => chapter.id)).toEqual(["implementation", "tests", "generated"]);
35+
expect(chapters[0]?.files).toEqual([files[0]]);
36+
expect(chapters[0]?.visibleFiles).toEqual([]);
37+
expect(chapters[1]?.files).toEqual([files[1]]);
38+
expect(chapters[1]?.visibleFiles).toEqual([]);
39+
expect(chapters[2]?.visibleFiles).toEqual([files[2]]);
40+
// Chapter-level unviewing still has every path even though the reader has no rows.
41+
expect(chapters.flatMap((chapter) => chapter.files.map(getPath))).toEqual(files.map(getPath));
42+
});
43+
44+
it("keeps the full membership when other viewing options hide unviewed files too", () => {
45+
const [chapter] = reviewFileChapters(implementation, getPath, [implementation[20]!]);
46+
expect(chapter?.files).toHaveLength(30);
47+
expect(chapter?.files.filter((file) => file.viewed)).toHaveLength(15);
48+
expect(chapter?.visibleFiles).toEqual([implementation[20]]);
49+
});
50+
51+
it("preserves the reader's order independently of the complete chapter inventory", () => {
52+
const files = [{ path: "src/z.ts" }, { path: "tests/app.test.ts" }, { path: "src/a.ts" }];
53+
const [chapter] = reviewFileChapters(files, getPath, [files[2]!, files[0]!]);
54+
expect(chapter?.files).toEqual([files[0], files[2]]);
55+
expect(chapter?.visibleFiles).toEqual([files[2], files[0]]);
56+
});
57+
58+
it("uses the whole chapter by default and omits groups with no changed files", () => {
59+
const chapters = reviewFileChapters(implementation, getPath);
60+
expect(chapters.map((chapter) => chapter.id)).toEqual(["implementation"]);
61+
expect(chapters[0]?.visibleFiles).toBe(chapters[0]?.files);
62+
expect(reviewFileChapters([], getPath, [])).toEqual([]);
63+
});
64+
});

‎packages/shared/src/reviewFileClassification.ts‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,29 @@ const REVIEW_FILE_GROUPS = [
77
{ id: "generated", title: "Generated" },
88
] as const;
99

10-
/** Nonempty chapters in the same order on web and native review surfaces. */
11-
export function reviewFileChapters<T>(files: ReadonlyArray<T>, getPath: (file: T) => string) {
10+
/** Chapter membership stays complete while viewing options filter the files shown in its reader. */
11+
export function reviewFileChapters<T>(
12+
files: ReadonlyArray<T>,
13+
getPath: (file: T) => string,
14+
visibleFiles?: ReadonlyArray<T>,
15+
) {
1216
return REVIEW_FILE_GROUPS.flatMap((group) => {
1317
const chapterFiles = files.filter((file) => reviewFileGroup(getPath(file)) === group.id);
14-
return chapterFiles.length > 0 ? [{ ...group, files: chapterFiles }] : [];
18+
return chapterFiles.length > 0
19+
? [
20+
{
21+
...group,
22+
files: chapterFiles,
23+
visibleFiles:
24+
visibleFiles === undefined
25+
? chapterFiles
26+
: visibleFiles.filter((file) => reviewFileGroup(getPath(file)) === group.id),
27+
},
28+
]
29+
: [];
1530
});
1631
}
32+
1733
export function isReviewTestFile(path: string): boolean {
1834
const parts = path.replaceAll("\\", "/").split("/");
1935
const name = parts.at(-1) ?? "";

0 commit comments

Comments
 (0)