Skip to content

Commit 5c6d86b

Browse files
committed
fix(desktop): align review identity and Channels click coverage (#6429)
Retain the selected review only while a pull-request fetch is in flight, clear it when the completed list omits that ID, and cover the Channels tab journey that opens the latest matching conversation in place. Signed-off-by: Thomas Petersen <[email protected]>
1 parent bd540d0 commit 5c6d86b

6 files changed

Lines changed: 217 additions & 10 deletions

File tree

‎desktop/src/features/projects/lib/projectReviewDisplay.test.mjs‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import assert from "node:assert/strict";
22
import test from "node:test";
33

44
import {
5+
currentPullRequestForSelection,
56
projectReviewFilesChangedBody,
67
retainLatestByKey,
78
reviewDiffWorkspaceBranch,
9+
shouldReplaceRetainedPullRequest,
810
} from "./projectReviewDisplay.ts";
911

1012
test("retainLatestByKey keeps the previous value when shouldReplace is false", () => {
@@ -35,6 +37,89 @@ test("retainLatestByKey takes a new key immediately", () => {
3537
assert.deepEqual(retained, { files: [] });
3638
});
3739

40+
test("an explicit selected review does not fall back to another identity", () => {
41+
const selected = { id: "pr-a" };
42+
const branchReview = { id: "pr-branch" };
43+
44+
assert.equal(
45+
currentPullRequestForSelection({
46+
fallback: branchReview,
47+
pullRequests: [selected, branchReview],
48+
selectedPullRequestId: "pr-a",
49+
}),
50+
selected,
51+
);
52+
assert.equal(
53+
currentPullRequestForSelection({
54+
fallback: branchReview,
55+
pullRequests: [branchReview],
56+
selectedPullRequestId: "pr-a",
57+
}),
58+
null,
59+
);
60+
assert.equal(
61+
currentPullRequestForSelection({
62+
fallback: branchReview,
63+
pullRequests: [branchReview],
64+
selectedPullRequestId: null,
65+
}),
66+
branchReview,
67+
);
68+
});
69+
70+
test("retained review identity stays aligned with the diff-query identity across fetch phases", () => {
71+
const reviewA = { id: "pr-a" };
72+
const renderedCache = { current: { key: "repo:pr-a", value: reviewA } };
73+
const diffQueryCache = { current: { key: "repo:pr-a", value: reviewA } };
74+
75+
const renderedDuringFetch = retainLatestByKey(
76+
renderedCache,
77+
"repo:pr-a",
78+
currentPullRequestForSelection({
79+
fallback: { id: "pr-branch" },
80+
pullRequests: [],
81+
selectedPullRequestId: "pr-a",
82+
}),
83+
(next) => shouldReplaceRetainedPullRequest(next, true),
84+
);
85+
const diffDuringFetch = retainLatestByKey(
86+
diffQueryCache,
87+
"repo:pr-a",
88+
currentPullRequestForSelection({
89+
fallback: { id: "pr-branch" },
90+
pullRequests: [],
91+
selectedPullRequestId: "pr-a",
92+
}),
93+
(next) => shouldReplaceRetainedPullRequest(next, true),
94+
);
95+
assert.equal(renderedDuringFetch, reviewA);
96+
assert.equal(diffDuringFetch, reviewA);
97+
assert.equal(renderedDuringFetch.id, diffDuringFetch.id);
98+
99+
const renderedAfterComplete = retainLatestByKey(
100+
renderedCache,
101+
"repo:pr-a",
102+
currentPullRequestForSelection({
103+
fallback: { id: "pr-branch" },
104+
pullRequests: [],
105+
selectedPullRequestId: "pr-a",
106+
}),
107+
(next) => shouldReplaceRetainedPullRequest(next, false),
108+
);
109+
const diffAfterComplete = retainLatestByKey(
110+
diffQueryCache,
111+
"repo:pr-a",
112+
currentPullRequestForSelection({
113+
fallback: { id: "pr-branch" },
114+
pullRequests: [],
115+
selectedPullRequestId: "pr-a",
116+
}),
117+
(next) => shouldReplaceRetainedPullRequest(next, false),
118+
);
119+
assert.equal(renderedAfterComplete, null);
120+
assert.equal(diffAfterComplete, null);
121+
});
122+
38123
test("review files stay mounted when a populated diff races an unavailable snapshot", () => {
39124
assert.equal(
40125
projectReviewFilesChangedBody({

‎desktop/src/features/projects/lib/projectReviewDisplay.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,40 @@ export function retainLatestByKey<T>(
2525
return cache.current.value;
2626
}
2727

28+
/**
29+
* Keep a selected review through transient empty refetches. Once the fetch
30+
* is idle, accept the completed result — including null — so the rendered
31+
* identity can match the diff-query identity.
32+
*/
33+
export function shouldReplaceRetainedPullRequest(
34+
next: unknown,
35+
isFetching: boolean,
36+
): boolean {
37+
return Boolean(next) || !isFetching;
38+
}
39+
40+
/**
41+
* Resolve the current review for a selection. An explicit ID that is missing
42+
* from the list is `null` so a completed refetch can clear it; pass
43+
* `fallback` only when no ID is selected (branch auto-select).
44+
*/
45+
export function currentPullRequestForSelection<T extends { id: string }>({
46+
fallback = null,
47+
pullRequests,
48+
selectedPullRequestId,
49+
}: {
50+
fallback?: T | null;
51+
pullRequests: readonly T[] | undefined;
52+
selectedPullRequestId: string | null;
53+
}): T | null {
54+
if (selectedPullRequestId) {
55+
return (
56+
pullRequests?.find((item) => item.id === selectedPullRequestId) ?? null
57+
);
58+
}
59+
return fallback;
60+
}
61+
2862
/**
2963
* Which body to render under a review's Files changed section.
3064
* A populated diff must keep the files panel mounted even when the repository

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,7 @@ export function ProjectDetailScreen(props: ProjectDetailScreenProps) {
910910
repoDiffLoading={displayedRepoDiffLoading}
911911
pullRequests={pullRequestsQuery.data ?? []}
912912
pullRequestsError={pullRequestsQuery.error}
913+
pullRequestsFetching={pullRequestsQuery.isFetching}
913914
pullRequestsLoading={pullRequestsQuery.isLoading}
914915
repoContributors={repoContributors}
915916
repoHost={repoRemote.host}

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ import {
2727
import { repositoryDiscussionQuery } from "@/features/projects/lib/discussionChannels";
2828
import type { ProjectRepoHost } from "@/features/projects/lib/projectRepoHost";
2929
import {
30+
currentPullRequestForSelection,
3031
projectReviewFilesChangedBody,
3132
retainLatestByKey,
33+
shouldReplaceRetainedPullRequest,
3234
} from "@/features/projects/lib/projectReviewDisplay";
3335
import { projectRepoUnavailableReason } from "@/features/projects/lib/projectRepoAvailability";
3436
import type { UserProfileLookup } from "@/features/profile/lib/identity";
@@ -115,6 +117,7 @@ export function WorkspaceTabs({
115117
sharedHeaderBackdrop,
116118
pullRequests,
117119
pullRequestsError,
120+
pullRequestsFetching,
118121
pullRequestsLoading,
119122
onSelectedCommitHashChange,
120123
onFilesContextChange,
@@ -161,6 +164,8 @@ export function WorkspaceTabs({
161164
sharedHeaderBackdrop?: boolean;
162165
pullRequests: ProjectPullRequest[];
163166
pullRequestsError: unknown;
167+
/** True while a pull-request list fetch (including refetch) is in flight. */
168+
pullRequestsFetching: boolean;
164169
pullRequestsLoading: boolean;
165170
onSelectedCommitHashChange: (hash: string | null) => void;
166171
onFilesContextChange?: (context: {
@@ -240,10 +245,10 @@ export function WorkspaceTabs({
240245
retryPending={sourceControls?.fetchPending}
241246
/>
242247
) : null;
243-
const currentSelectedPullRequest =
244-
pullRequests.find(
245-
(pullRequest) => pullRequest.id === selectedPullRequestId,
246-
) ?? null;
248+
const currentSelectedPullRequest = currentPullRequestForSelection({
249+
pullRequests,
250+
selectedPullRequestId,
251+
});
247252
const selectedPullRequestCacheKey = `${project.id}:${selectedPullRequestId ?? "none"}`;
248253
const selectedPullRequestCache = React.useRef({
249254
key: selectedPullRequestCacheKey,
@@ -253,7 +258,7 @@ export function WorkspaceTabs({
253258
selectedPullRequestCache,
254259
selectedPullRequestCacheKey,
255260
currentSelectedPullRequest,
256-
(next) => Boolean(next),
261+
(next) => shouldReplaceRetainedPullRequest(next, pullRequestsFetching),
257262
);
258263
const filesChangedBody = projectReviewFilesChangedBody({
259264
hasPopulatedDiff: (repoDiff?.files.length ?? 0) > 0,

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import {
1111
useProjectRepoSnapshotQuery,
1212
} from "@/features/projects/hooks";
1313
import {
14+
currentPullRequestForSelection,
1415
retainLatestByKey,
1516
reviewDiffWorkspaceBranch,
17+
shouldReplaceRetainedPullRequest,
1618
} from "@/features/projects/lib/projectReviewDisplay";
1719
import { normalizeRepositoryUrl } from "@/features/projects/lib/projectsViewHelpers";
1820
import { useProjectCommitDiffQuery } from "@/features/projects/useProjectCommitDiff";
@@ -55,16 +57,18 @@ export function useRetainedPullRequestSelection({
5557
selectedBranchPullRequestCache,
5658
selectedBranchPullRequestKey,
5759
currentBranchPullRequest,
58-
(next) => Boolean(next) || !isFetching,
60+
(next) => shouldReplaceRetainedPullRequest(next, isFetching),
5961
);
6062
const openBranchPullRequest =
6163
selectedBranchPullRequest?.status === "Open" ||
6264
selectedBranchPullRequest?.status === "Draft"
6365
? selectedBranchPullRequest
6466
: null;
65-
const currentRepoPullRequest =
66-
pullRequests?.find((item) => item.id === selectedPullRequestId) ??
67-
selectedBranchPullRequest;
67+
const currentRepoPullRequest = currentPullRequestForSelection({
68+
fallback: selectedBranchPullRequest,
69+
pullRequests,
70+
selectedPullRequestId,
71+
});
6872
const activeRepoPullRequestKey = [
6973
repository?.id ?? "none",
7074
selectedPullRequestId ?? `branch:${activeBranch}`,
@@ -77,7 +81,7 @@ export function useRetainedPullRequestSelection({
7781
activeRepoPullRequestCache,
7882
activeRepoPullRequestKey,
7983
currentRepoPullRequest,
80-
(next) => Boolean(next) || !isFetching,
84+
(next) => shouldReplaceRetainedPullRequest(next, isFetching),
8185
);
8286
return {
8387
activeRepoPullRequest,

‎desktop/tests/e2e/project-pr-review.spec.ts‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ async function enableProjectsFeature(page: import("@playwright/test").Page) {
3333
});
3434
}
3535

36+
async function waitForMockLiveSubscription(
37+
page: import("@playwright/test").Page,
38+
channelName: string,
39+
) {
40+
await expect
41+
.poll(() =>
42+
page.evaluate(
43+
(name) =>
44+
window.__BUZZ_E2E_HAS_MOCK_LIVE_SUBSCRIPTION__?.({
45+
channelName: name,
46+
}) ?? false,
47+
channelName,
48+
),
49+
)
50+
.toBe(true);
51+
}
52+
3653
async function openBuzzProject(page: import("@playwright/test").Page) {
3754
await page.goto("/", { waitUntil: "domcontentloaded" });
3855
await page.getByTestId("open-projects-view").click();
@@ -1455,6 +1472,67 @@ test("project detail lists follow overview header geometry", async ({
14551472
);
14561473
});
14571474

1475+
test("channels tab opens the latest matching conversation without leaving the project", async ({
1476+
page,
1477+
}) => {
1478+
await enableProjectsFeature(page);
1479+
await installMockBridge(page);
1480+
await page.goto("/", { waitUntil: "domcontentloaded" });
1481+
await page.getByTestId("channel-general").click();
1482+
await waitForMockLiveSubscription(page, "general");
1483+
1484+
const olderContent = "Older matching conversation for channels tab";
1485+
const latestContent = "Latest matching conversation for channels tab";
1486+
await page.evaluate(
1487+
({ author, latestContent, olderContent, repoToken }) => {
1488+
const now = Math.floor(Date.now() / 1_000);
1489+
window.__BUZZ_E2E_EMIT_MOCK_MESSAGE__?.({
1490+
channelName: "general",
1491+
content: `${olderContent} ${repoToken}`,
1492+
createdAt: now - 1,
1493+
kind: 9,
1494+
pubkey: author,
1495+
});
1496+
window.__BUZZ_E2E_EMIT_MOCK_MESSAGE__?.({
1497+
channelName: "general",
1498+
content: `${latestContent} ${repoToken}`,
1499+
createdAt: now,
1500+
kind: 9,
1501+
pubkey: author,
1502+
});
1503+
},
1504+
{
1505+
author: TEST_IDENTITIES.alice.pubkey,
1506+
latestContent,
1507+
olderContent,
1508+
repoToken: `${DEFAULT_MOCK_PUBKEY} buzz`,
1509+
},
1510+
);
1511+
1512+
await page.getByTestId("open-projects-view").click();
1513+
await page.getByTestId("projects-section-projects").click();
1514+
const projectEntry = page
1515+
.locator(
1516+
'[data-testid="project-card-buzz"], [data-testid="project-row-buzz"]',
1517+
)
1518+
.first();
1519+
await expect(projectEntry).toBeVisible({ timeout: 10_000 });
1520+
await projectEntry.click();
1521+
await page.getByRole("tab", { name: "Channels", exact: true }).click();
1522+
const channelRow = page.getByTestId("project-channel-row").first();
1523+
await expect(channelRow).toBeVisible({ timeout: 10_000 });
1524+
await channelRow.click();
1525+
1526+
const panel = page.getByTestId("project-conversation-panel");
1527+
await expect(panel).toBeVisible();
1528+
await expect(panel).toContainText(latestContent);
1529+
await expect(panel).not.toContainText(olderContent);
1530+
await expect(page.getByTestId("project-detail-scroll")).toBeVisible();
1531+
await expect(
1532+
page.getByRole("tab", { name: "Channels", exact: true }),
1533+
).toHaveAttribute("aria-selected", "true");
1534+
});
1535+
14581536
test("overview list titles share a row with controls and task groups align", async ({
14591537
page,
14601538
}) => {

0 commit comments

Comments
 (0)