Skip to content

Commit c66b9b1

Browse files
committed
fix(mobile): reset collapsed files when PR comparison changes
1 parent 9519db7 commit c66b9b1

6 files changed

Lines changed: 206 additions & 109 deletions

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { IconButton } from "../../components/ui/IconButton";
3535
import { Button } from "../../components/ui/Button";
3636
import { usePullRequestReviewDraft } from "./usePullRequestReviewDraft";
3737
import { usePullRequestFilesViewed } from "./usePullRequestFilesViewed";
38-
import { usePullRequestComparisonViewedPaths } from "./usePullRequestComparisonViewedPaths";
38+
import { usePullRequestComparisonState } from "./usePullRequestComparisonState";
3939
import { usePullRequestPermissions } from "./usePullRequestPermissions";
4040
import { usePullRequestAction } from "./usePullRequestAction";
4141
import { annotatePullRequestDiff, reviewPosition } from "./pullRequestDiffComments";
@@ -967,7 +967,8 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) {
967967
const parsed = filter.parsed;
968968
const reviewDraft = usePullRequestReviewDraft(route.params);
969969
const viewed = usePullRequestFilesViewed(route.params, historical ? null : detail.data);
970-
const comparisonViewed = usePullRequestComparisonViewedPaths(route.params);
970+
const comparisonState = usePullRequestComparisonState(route.params);
971+
const { collapsedIds, setCollapsedIds } = comparisonState;
971972
const permissions = usePullRequestPermissions(route.params, detail.data);
972973
const scopeKey = pullRequestReviewKey(route.params);
973974
const [diffHead, setDiffHead] = useState({ scopeKey, sha: detail.data?.headSha ?? null });
@@ -1006,7 +1007,7 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) {
10061007
);
10071008
const viewedIds = data.files
10081009
.filter((file) =>
1009-
historical ? comparisonViewed.isViewed(file.path) : viewed.isViewed(file.path),
1010+
historical ? comparisonState.isViewed(file.path) : viewed.isViewed(file.path),
10101011
)
10111012
.map((file) => file.id);
10121013
const [selectedRowId, setSelectedRowId] = useState<string | null>(null);
@@ -1046,7 +1047,6 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) {
10461047
const focusIndex = focusedRows.length
10471048
? data.rows.findIndex((row) => row.id === focusedRows[0]?.id)
10481049
: -1;
1049-
const [collapsedIds, setCollapsedIds] = useState<readonly string[]>(EMPTY_IDS);
10501050
const [visibleFileId, setVisibleFileId] = useState<string | null>(null);
10511051
const nativeView = useRef<NativeReviewDiffViewHandle>(null);
10521052
// Native applies the entry destination before its first viewport. Imperative
@@ -1071,7 +1071,14 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) {
10711071
void nativeView.current.scrollToFile(file.id, false).catch(() => {
10721072
jumpStarted.current = false;
10731073
});
1074-
}, [route.params.path, route.params.fileSelectionId, route.params.focus, data.files, navigation]);
1074+
}, [
1075+
route.params.path,
1076+
route.params.fileSelectionId,
1077+
route.params.focus,
1078+
data.files,
1079+
navigation,
1080+
setCollapsedIds,
1081+
]);
10751082
const appliedFilter = useRef(filter.hideTestFiles);
10761083
useEffect(() => {
10771084
if (appliedFilter.current === filter.hideTestFiles) return;
@@ -1315,7 +1322,7 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) {
13151322
const file = data.files.find((file) => file.id === event.nativeEvent.fileId);
13161323
if (!file || (!historical && !viewed.canToggle)) return;
13171324
const wasViewed = historical
1318-
? comparisonViewed.isViewed(file.path)
1325+
? comparisonState.isViewed(file.path)
13191326
: viewed.isViewed(file.path);
13201327
setCollapsedIds((ids) =>
13211328
wasViewed
@@ -1324,7 +1331,7 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) {
13241331
? ids
13251332
: [...ids, file.id],
13261333
);
1327-
if (historical) comparisonViewed.toggle(file.path);
1334+
if (historical) comparisonState.toggle(file.path);
13281335
else void viewed.toggle(file.path);
13291336
}}
13301337
onToggleComment={(event) => {
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { act } from "react";
2+
import { createRoot } from "react-dom/client";
3+
import { expect, it } from "vite-plus/test";
4+
import type { PullRequestDiffParams } from "./usePullRequestDiff";
5+
import { usePullRequestComparisonState } from "./usePullRequestComparisonState";
6+
7+
const pr: PullRequestDiffParams = {
8+
environmentId: "environment",
9+
projectId: "project",
10+
host: "dev.azure.com",
11+
repository: "repo",
12+
number: 1,
13+
};
14+
15+
function setup() {
16+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
17+
const container = document.createElement("div");
18+
const root = createRoot(container);
19+
function Probe({ params }: { params: PullRequestDiffParams }) {
20+
const comparison = usePullRequestComparisonState(params);
21+
return (
22+
<>
23+
<button type="button" onClick={() => comparison.toggle("notes.txt")}>
24+
{comparison.isViewed("notes.txt") ? "Viewed" : "Unviewed"}
25+
</button>
26+
<button
27+
type="button"
28+
onClick={() =>
29+
comparison.setCollapsedIds((ids) =>
30+
ids.includes("notes.txt")
31+
? ids.filter((id) => id !== "notes.txt")
32+
: [...ids, "notes.txt"],
33+
)
34+
}
35+
>
36+
Toggle file
37+
</button>
38+
{!comparison.collapsedIds.includes("notes.txt") ? <p>File contents</p> : null}
39+
</>
40+
);
41+
}
42+
const render = (params: PullRequestDiffParams) =>
43+
act(() => root.render(<Probe params={params} />));
44+
return {
45+
render,
46+
isViewed: () => container.querySelector("button")?.textContent === "Viewed",
47+
fileVisible: () => container.querySelector("p")?.textContent === "File contents",
48+
toggleFile: () => act(() => container.querySelectorAll("button")[1]?.click()),
49+
toggle: () => act(() => container.querySelector("button")?.click()),
50+
[Symbol.dispose]: () => act(() => root.unmount()),
51+
};
52+
}
53+
54+
it("keeps a viewed path within one comparison but clears it when the comparison changes", () => {
55+
using screen = setup();
56+
const first = { ...pr, iterationRange: { from: 1, to: 2 } };
57+
screen.render(first);
58+
screen.toggle();
59+
screen.render({ ...first });
60+
expect(screen.isViewed()).toBe(true);
61+
62+
screen.render({ ...pr, iterationRange: { from: 2, to: 3 } });
63+
expect(screen.isViewed()).toBe(false);
64+
screen.toggle();
65+
expect(screen.isViewed()).toBe(true);
66+
67+
screen.render(first);
68+
expect(screen.isViewed()).toBe(false);
69+
});
70+
71+
it("keeps historical viewed paths separate from latest and from other pull requests", () => {
72+
using screen = setup();
73+
screen.render({ ...pr, commit: "abc" });
74+
screen.toggle();
75+
screen.render(pr);
76+
expect(screen.isViewed()).toBe(false);
77+
screen.render({ ...pr, commit: "abc", number: 2 });
78+
expect(screen.isViewed()).toBe(false);
79+
});
80+
81+
it("clears a comparison's viewed paths even when the next comparison is not toggled", () => {
82+
using screen = setup();
83+
const first = { ...pr, iterationRange: { from: 1, to: 2 } };
84+
screen.render(first);
85+
screen.toggle();
86+
screen.render({ ...pr, iterationRange: { from: 2, to: 3 } });
87+
expect(screen.isViewed()).toBe(false);
88+
screen.render(first);
89+
expect(screen.isViewed()).toBe(false);
90+
});
91+
92+
it("retains file collapse within a comparison and clears it across A → B → A without toggling B", () => {
93+
using screen = setup();
94+
const first = { ...pr, iterationRange: { from: 1, to: 2 } };
95+
screen.render(first);
96+
screen.toggleFile();
97+
expect(screen.fileVisible()).toBe(false);
98+
screen.render({ ...first });
99+
expect(screen.fileVisible()).toBe(false);
100+
101+
screen.render({ ...pr, iterationRange: { from: 2, to: 3 } });
102+
expect(screen.fileVisible()).toBe(true);
103+
screen.render(first);
104+
expect(screen.fileVisible()).toBe(true);
105+
screen.toggleFile();
106+
expect(screen.fileVisible()).toBe(false);
107+
screen.toggleFile();
108+
expect(screen.fileVisible()).toBe(true);
109+
});
110+
111+
it.each([
112+
{ commit: "def" },
113+
{ commit: undefined },
114+
{ iterationRange: { from: 1, to: 2 } },
115+
{ environmentId: "other-environment" },
116+
{ projectId: "other-project" },
117+
{ host: "other-host" },
118+
{ repository: "other-repo" },
119+
{ number: 2 },
120+
])("clears collapsed files when the comparison identity changes by %j", (change) => {
121+
using screen = setup();
122+
const first = { ...pr, commit: "abc" };
123+
screen.render(first);
124+
screen.toggleFile();
125+
screen.render({ ...first, ...change });
126+
expect(screen.fileVisible()).toBe(true);
127+
screen.render(first);
128+
expect(screen.fileVisible()).toBe(true);
129+
});
130+
131+
it("preserves viewed paths when collapse changes and collapsed files when viewed paths change", () => {
132+
using screen = setup();
133+
screen.render({ ...pr, iterationRange: { from: 1, to: 2 } });
134+
screen.toggleFile();
135+
screen.toggle();
136+
expect(screen.fileVisible()).toBe(false);
137+
expect(screen.isViewed()).toBe(true);
138+
screen.toggleFile();
139+
expect(screen.fileVisible()).toBe(true);
140+
expect(screen.isViewed()).toBe(true);
141+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { useCallback, useState, type SetStateAction } from "react";
2+
import { pullRequestReviewKey } from "./pullRequestReviewDrafts";
3+
import type { PullRequestDiffParams } from "./usePullRequestDiff";
4+
5+
const EMPTY_IDS: readonly string[] = [];
6+
7+
export function usePullRequestComparisonState(params: PullRequestDiffParams) {
8+
const key = JSON.stringify([
9+
pullRequestReviewKey(params),
10+
params.commit ?? null,
11+
params.iterationRange?.from ?? null,
12+
params.iterationRange?.to ?? null,
13+
]);
14+
const [state, setState] = useState<{
15+
key: string;
16+
paths: ReadonlySet<string>;
17+
collapsedIds: readonly string[];
18+
}>(() => ({ key, paths: new Set(), collapsedIds: EMPTY_IDS }));
19+
if (state.key !== key) setState({ key, paths: new Set(), collapsedIds: EMPTY_IDS });
20+
const paths = state.key === key ? state.paths : new Set<string>();
21+
const collapsedIds = state.key === key ? state.collapsedIds : EMPTY_IDS;
22+
const setCollapsedIds = useCallback(
23+
(next: SetStateAction<readonly string[]>) =>
24+
setState((current) => ({
25+
key,
26+
paths: current.key === key ? current.paths : new Set(),
27+
collapsedIds:
28+
typeof next === "function"
29+
? next(current.key === key ? current.collapsedIds : EMPTY_IDS)
30+
: next,
31+
})),
32+
[key],
33+
);
34+
return {
35+
collapsedIds,
36+
setCollapsedIds,
37+
isViewed: (path: string) => paths.has(path),
38+
toggle: (path: string) =>
39+
setState((current) => {
40+
const next = new Set(current.key === key ? current.paths : []);
41+
if (next.has(path)) next.delete(path);
42+
else next.add(path);
43+
return {
44+
key,
45+
paths: next,
46+
collapsedIds: current.key === key ? current.collapsedIds : EMPTY_IDS,
47+
};
48+
}),
49+
};
50+
}

‎apps/mobile/src/features/pull-requests/usePullRequestComparisonViewedPaths.test.tsx‎

Lines changed: 0 additions & 73 deletions
This file was deleted.

‎apps/mobile/src/features/pull-requests/usePullRequestComparisonViewedPaths.ts‎

Lines changed: 0 additions & 28 deletions
This file was deleted.

‎apps/mobile/vite.config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import "vite-plus/test/config";
66
const reactTests = [
77
"src/features/projects/AddProjectScreen.test.ts",
88
"src/features/pull-requests/usePullRequestPreviousVisit.test.tsx",
9-
"src/features/pull-requests/usePullRequestComparisonViewedPaths.test.tsx",
9+
"src/features/pull-requests/usePullRequestComparisonState.test.tsx",
1010
"src/lib/useMediaVideoPlayback.test.tsx",
1111
"src/features/pull-requests/usePullRequestEnvironmentFallback.test.tsx",
1212
"src/lib/filePreview.test.ts",

0 commit comments

Comments
 (0)