Skip to content

Commit bb565f5

Browse files
committed
fix(desktop): retain one selected review across Projects chrome (#6429)
Resolve the selected review once during pull-request refetch so the workspace, diff query, breadcrumb, context card, and agent payload stay on the same identity until a completed result omits it. Signed-off-by: Thomas Petersen <[email protected]>
1 parent 5c6d86b commit bb565f5

4 files changed

Lines changed: 180 additions & 39 deletions

File tree

‎desktop/src/features/projects/ui/ProjectDetailScreen.tsx‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ export function ProjectDetailScreen(props: ProjectDetailScreenProps) {
188188
activeRepoPullRequest,
189189
openBranchPullRequest,
190190
selectedBranchPullRequest,
191+
selectedPullRequest,
191192
} = useRetainedPullRequestSelection({
192193
activeBranch,
193194
isFetching: pullRequestsQuery.isFetching,
@@ -688,9 +689,6 @@ export function ProjectDetailScreen(props: ProjectDetailScreenProps) {
688689
const displayedRepositoryContributors =
689690
displayedRepositorySnapshot?.contributors ?? repoContributors;
690691
const displayedRepositoryFiles = displayedRepositorySnapshot?.files ?? [];
691-
const selectedPullRequest =
692-
pullRequestsQuery.data?.find((item) => item.id === selectedPullRequestId) ??
693-
null;
694692
const selectedIssue =
695693
issuesQuery.data?.find((item) => item.id === selectedIssueId) ?? null;
696694
const displayedSnapshotCommits =
@@ -910,13 +908,13 @@ export function ProjectDetailScreen(props: ProjectDetailScreenProps) {
910908
repoDiffLoading={displayedRepoDiffLoading}
911909
pullRequests={pullRequestsQuery.data ?? []}
912910
pullRequestsError={pullRequestsQuery.error}
913-
pullRequestsFetching={pullRequestsQuery.isFetching}
914911
pullRequestsLoading={pullRequestsQuery.isLoading}
915912
repoContributors={repoContributors}
916913
repoHost={repoRemote.host}
917914
repoSource={repoSource}
918915
selectedCommitHash={selectedCommitHash}
919916
selectedIssueId={selectedIssueId}
917+
selectedPullRequest={selectedPullRequest}
920918
selectedPullRequestId={selectedPullRequestId}
921919
sharedHeaderBackdrop={sharedHeaderBackdrop}
922920
snapshot={displayedRepoSnapshot}

‎desktop/src/features/projects/ui/ProjectWorkspaceTabs.tsx‎

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ import {
2626
} from "@/features/projects/lib/projectContributorMatching";
2727
import { repositoryDiscussionQuery } from "@/features/projects/lib/discussionChannels";
2828
import type { ProjectRepoHost } from "@/features/projects/lib/projectRepoHost";
29-
import {
30-
currentPullRequestForSelection,
31-
projectReviewFilesChangedBody,
32-
retainLatestByKey,
33-
shouldReplaceRetainedPullRequest,
34-
} from "@/features/projects/lib/projectReviewDisplay";
29+
import { projectReviewFilesChangedBody } from "@/features/projects/lib/projectReviewDisplay";
3530
import { projectRepoUnavailableReason } from "@/features/projects/lib/projectRepoAvailability";
3631
import type { UserProfileLookup } from "@/features/profile/lib/identity";
3732
import { normalizePubkey } from "@/shared/lib/pubkey";
@@ -113,11 +108,11 @@ export function WorkspaceTabs({
113108
repoDiffLoading,
114109
selectedCommitHash,
115110
selectedIssueId,
111+
selectedPullRequest,
116112
selectedPullRequestId,
117113
sharedHeaderBackdrop,
118114
pullRequests,
119115
pullRequestsError,
120-
pullRequestsFetching,
121116
pullRequestsLoading,
122117
onSelectedCommitHashChange,
123118
onFilesContextChange,
@@ -160,12 +155,11 @@ export function WorkspaceTabs({
160155
repoDiffLoading: boolean;
161156
selectedCommitHash: string | null;
162157
selectedIssueId: string | null;
158+
selectedPullRequest: ProjectPullRequest | null;
163159
selectedPullRequestId: string | null;
164160
sharedHeaderBackdrop?: boolean;
165161
pullRequests: ProjectPullRequest[];
166162
pullRequestsError: unknown;
167-
/** True while a pull-request list fetch (including refetch) is in flight. */
168-
pullRequestsFetching: boolean;
169163
pullRequestsLoading: boolean;
170164
onSelectedCommitHashChange: (hash: string | null) => void;
171165
onFilesContextChange?: (context: {
@@ -245,21 +239,6 @@ export function WorkspaceTabs({
245239
retryPending={sourceControls?.fetchPending}
246240
/>
247241
) : null;
248-
const currentSelectedPullRequest = currentPullRequestForSelection({
249-
pullRequests,
250-
selectedPullRequestId,
251-
});
252-
const selectedPullRequestCacheKey = `${project.id}:${selectedPullRequestId ?? "none"}`;
253-
const selectedPullRequestCache = React.useRef({
254-
key: selectedPullRequestCacheKey,
255-
value: currentSelectedPullRequest,
256-
});
257-
const selectedPullRequest = retainLatestByKey(
258-
selectedPullRequestCache,
259-
selectedPullRequestCacheKey,
260-
currentSelectedPullRequest,
261-
(next) => shouldReplaceRetainedPullRequest(next, pullRequestsFetching),
262-
);
263242
const filesChangedBody = projectReviewFilesChangedBody({
264243
hasPopulatedDiff: (repoDiff?.files.length ?? 0) > 0,
265244
hasSelectedPullRequest: Boolean(selectedPullRequest),
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import assert from "node:assert/strict";
2+
import { after, afterEach, before, test } from "node:test";
3+
4+
import { JSDOM } from "jsdom";
5+
6+
import { buildProjectDetailAgentContext } from "../lib/projectDetailAgentContext.ts";
7+
import { projectDetailSelectionItem } from "../lib/projectDetailSelectionItem.ts";
8+
import { reviewDiffWorkspaceBranch } from "../lib/projectReviewDisplay.ts";
9+
import { buildProjectDetailCrumbs } from "./useProjectDetailCrumbs.ts";
10+
11+
const OWNER = "a".repeat(64);
12+
const REVIEW_A_ID = "b".repeat(64);
13+
14+
const repository = {
15+
id: `${OWNER}:buzz`,
16+
cloneUrls: ["https://example.com/buzz.git"],
17+
defaultBranch: "main",
18+
channelId: "trusted-repository-channel",
19+
name: "buzz",
20+
repoAddress: `30617:${OWNER}:buzz`,
21+
};
22+
23+
const reviewA = {
24+
id: REVIEW_A_ID,
25+
title: "Ship the retained review",
26+
author: "alice",
27+
status: "Open",
28+
branchName: "feature-a",
29+
targetBranch: "main",
30+
cloneUrls: repository.cloneUrls,
31+
repoAddress: repository.repoAddress,
32+
channelId: "forged-origin-channel",
33+
};
34+
35+
const noop = () => {};
36+
37+
function productionConsumers({ activeRepoPullRequest, selectedPullRequest }) {
38+
const crumbs = buildProjectDetailCrumbs({
39+
activeTab: "prs",
40+
commit: null,
41+
issue: null,
42+
pullRequest: selectedPullRequest,
43+
setRequestedTab: noop,
44+
setSelectedCommitHash: noop,
45+
setSelectedIssueId: noop,
46+
setSelectedPullRequestId: noop,
47+
setTabsResetKey: noop,
48+
});
49+
const contextItem = projectDetailSelectionItem({
50+
projectChannelId: "trusted-project-channel",
51+
projectId: "project-id",
52+
pullRequest: selectedPullRequest,
53+
repository,
54+
});
55+
const agent = buildProjectDetailAgentContext({
56+
activeTab: "prs",
57+
branch: "feature-a",
58+
project: { name: "buzz" },
59+
repository: {
60+
name: repository.name,
61+
repoAddress: repository.repoAddress,
62+
},
63+
source: "remote",
64+
workItems: [null, null, selectedPullRequest],
65+
});
66+
return {
67+
agentReviewId: agent.workItem?.id ?? null,
68+
crumbTitle: crumbs.activeWorkItemCrumb?.title ?? null,
69+
contextId: contextItem?.id ?? null,
70+
diffQueryId: activeRepoPullRequest?.id ?? null,
71+
diffWorkspaceBranch: reviewDiffWorkspaceBranch({
72+
activeBranch: "feature-a",
73+
defaultBranch: repository.defaultBranch,
74+
pullRequest: activeRepoPullRequest,
75+
}),
76+
};
77+
}
78+
79+
const dom = new JSDOM("<!doctype html><html><body></body></html>", {
80+
url: "http://localhost",
81+
});
82+
83+
before(() => {
84+
Object.assign(globalThis, {
85+
document: dom.window.document,
86+
HTMLElement: dom.window.HTMLElement,
87+
IS_REACT_ACT_ENVIRONMENT: true,
88+
window: dom.window,
89+
});
90+
});
91+
92+
afterEach(async () => {
93+
const { cleanup } = await import("@testing-library/react");
94+
cleanup();
95+
});
96+
97+
after(() => dom.window.close());
98+
99+
let hookModule;
100+
before(async () => {
101+
hookModule = await import("./useRetainedProjectGitViews.ts");
102+
});
103+
104+
async function renderSelection(initialProps) {
105+
const { renderHook } = await import("@testing-library/react");
106+
return renderHook(
107+
(props) => hookModule.useRetainedPullRequestSelection(props),
108+
{ initialProps },
109+
);
110+
}
111+
112+
test("selected review chrome and diff query stay aligned across fetch phases", async () => {
113+
const populated = {
114+
activeBranch: "feature-a",
115+
isFetching: false,
116+
pullRequests: [reviewA],
117+
repository,
118+
selectedPullRequestId: REVIEW_A_ID,
119+
};
120+
const { rerender, result } = await renderSelection(populated);
121+
122+
const populatedConsumers = productionConsumers(result.current);
123+
assert.equal(result.current.selectedPullRequest, reviewA);
124+
assert.equal(result.current.activeRepoPullRequest, reviewA);
125+
assert.deepEqual(populatedConsumers, {
126+
agentReviewId: REVIEW_A_ID,
127+
crumbTitle: "Ship the retained review",
128+
contextId: `review:${REVIEW_A_ID}`,
129+
diffQueryId: REVIEW_A_ID,
130+
diffWorkspaceBranch: "main",
131+
});
132+
133+
rerender({
134+
...populated,
135+
isFetching: true,
136+
pullRequests: [],
137+
});
138+
const fetchingConsumers = productionConsumers(result.current);
139+
assert.equal(result.current.selectedPullRequest, reviewA);
140+
assert.equal(
141+
result.current.selectedPullRequest,
142+
result.current.activeRepoPullRequest,
143+
);
144+
assert.deepEqual(fetchingConsumers, populatedConsumers);
145+
146+
rerender({
147+
...populated,
148+
isFetching: false,
149+
pullRequests: [],
150+
});
151+
const completedConsumers = productionConsumers(result.current);
152+
assert.equal(result.current.selectedPullRequest, null);
153+
assert.equal(result.current.activeRepoPullRequest, null);
154+
assert.deepEqual(completedConsumers, {
155+
agentReviewId: null,
156+
crumbTitle: null,
157+
contextId: null,
158+
diffQueryId: null,
159+
diffWorkspaceBranch: "feature-a",
160+
});
161+
});

‎desktop/src/features/projects/ui/useRetainedProjectGitViews.ts‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,29 +64,32 @@ export function useRetainedPullRequestSelection({
6464
selectedBranchPullRequest?.status === "Draft"
6565
? selectedBranchPullRequest
6666
: null;
67-
const currentRepoPullRequest = currentPullRequestForSelection({
68-
fallback: selectedBranchPullRequest,
67+
const currentSelectedPullRequest = currentPullRequestForSelection({
6968
pullRequests,
7069
selectedPullRequestId,
7170
});
72-
const activeRepoPullRequestKey = [
71+
const selectedPullRequestKey = [
7372
repository?.id ?? "none",
74-
selectedPullRequestId ?? `branch:${activeBranch}`,
73+
selectedPullRequestId ?? "none",
7574
].join(":");
76-
const activeRepoPullRequestCache = React.useRef({
77-
key: activeRepoPullRequestKey,
78-
value: currentRepoPullRequest,
75+
const selectedPullRequestCache = React.useRef({
76+
key: selectedPullRequestKey,
77+
value: currentSelectedPullRequest,
7978
});
80-
const activeRepoPullRequest = retainLatestByKey(
81-
activeRepoPullRequestCache,
82-
activeRepoPullRequestKey,
83-
currentRepoPullRequest,
79+
const selectedPullRequest = retainLatestByKey(
80+
selectedPullRequestCache,
81+
selectedPullRequestKey,
82+
currentSelectedPullRequest,
8483
(next) => shouldReplaceRetainedPullRequest(next, isFetching),
8584
);
85+
const activeRepoPullRequest = selectedPullRequestId
86+
? selectedPullRequest
87+
: selectedBranchPullRequest;
8688
return {
8789
activeRepoPullRequest,
8890
openBranchPullRequest,
8991
selectedBranchPullRequest,
92+
selectedPullRequest,
9093
};
9194
}
9295

0 commit comments

Comments
 (0)