Skip to content

Commit c5ebd2c

Browse files
refactor(web): the pull request list's grouping helper lives in a logic module
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0290927 commit c5ebd2c

3 files changed

Lines changed: 129 additions & 117 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,12 @@ import { INVOLVEMENT_TABS, SORT_OPTIONS, STATE_TABS } from "./pullRequestListOpt
6565
import { PullRequestsColumn } from "./PullRequestsColumn";
6666
import { PullRequestListGroups } from "./PullRequestListGroups";
6767
import {
68-
pullRequestGroups,
69-
pullRequestListEntryId,
7068
usePullRequestDisplayGroups,
7169
usePullRequestQueryFilters,
7270
usePullRequestScope,
7371
type PullRequestsSearch,
7472
} from "./usePullRequestListView";
73+
import { pullRequestGroups, pullRequestListEntryId } from "./pullRequestListGroups.logic";
7574
import { PullRequestListEmptyState } from "./PullRequestListEmptyState";
7675
import { PullRequestListGhost } from "./PullRequestGhosts";
7776
import { type PullRequestRowTarget } from "./PullRequestRow";
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import type {
2+
PullRequestInvolvement,
3+
PullRequestListFilters,
4+
PullRequestListResult,
5+
} from "@t3tools/contracts";
6+
7+
import type { PullRequestTabStatusSeed } from "~/components/RightPanelTabs";
8+
import { pullRequestSurfaceId } from "~/rightPanelStore";
9+
import {
10+
filterPullRequestsByInvolvement,
11+
groupPullRequestsByInvolvement,
12+
matchesPullRequestFilters,
13+
matchesPullRequestQuery,
14+
partitionPullRequestsWithPriority,
15+
pullRequestEntryKey,
16+
pullRequestEntryViewer,
17+
type EnvironmentPullRequestEntry,
18+
type PullRequestPartitionsSnapshot,
19+
} from "./pullRequestList.logic";
20+
21+
export const pullRequestListEntryId = (target: Parameters<typeof pullRequestSurfaceId>[0]) =>
22+
pullRequestSurfaceId({ ...target, repository: target.repository.toLowerCase() });
23+
24+
/** The held rows narrowed to what the reader asked for, in the groups the list shows. */
25+
export function pullRequestGroups({
26+
known,
27+
providers,
28+
viewers,
29+
involvement,
30+
localFilters,
31+
hasLocalFilters,
32+
text,
33+
answeredLocally,
34+
baselineEmpty,
35+
held,
36+
authored,
37+
reviewing,
38+
partitionsWanted,
39+
}: {
40+
known: ReadonlyArray<EnvironmentPullRequestEntry>;
41+
providers: PullRequestListResult["providers"];
42+
viewers: PullRequestListResult["viewers"];
43+
involvement: PullRequestInvolvement;
44+
localFilters: PullRequestListFilters;
45+
hasLocalFilters: boolean;
46+
text: string;
47+
answeredLocally: boolean;
48+
baselineEmpty: boolean;
49+
held: PullRequestPartitionsSnapshot | undefined;
50+
authored: ReadonlyArray<EnvironmentPullRequestEntry> | undefined;
51+
reviewing: ReadonlyArray<EnvironmentPullRequestEntry> | undefined;
52+
partitionsWanted: boolean;
53+
}) {
54+
const searchingHosts = new Set(
55+
providers.flatMap((provider) => (provider.searchesOnHost ? [provider.host] : [])),
56+
);
57+
58+
const entries = (() => {
59+
const involvementEntries = filterPullRequestsByInvolvement(known, viewers, involvement);
60+
// The hosts search more than the row shows — a body, a review, a commit message — so once
61+
// their answer is in, narrowing it again here would throw away matches the reader asked for.
62+
// The local pass stands in for the answer that has not arrived yet, and for the hosts that
63+
// answered without searching at all: Azure DevOps has no text filter, so its rows arrive
64+
// whole and would otherwise sit under a search that never touched them.
65+
const narrowedEntries = hasLocalFilters
66+
? involvementEntries.filter((entry) =>
67+
matchesPullRequestFilters(entry, localFilters, pullRequestEntryViewer(entry, viewers)),
68+
)
69+
: involvementEntries;
70+
if (text.length === 0) {
71+
return narrowedEntries.toSorted((left, right) =>
72+
right.updatedAt.localeCompare(left.updatedAt),
73+
);
74+
}
75+
return narrowedEntries.filter(
76+
(entry) =>
77+
(answeredLocally && searchingHosts.has(entry.host)) || matchesPullRequestQuery(entry, text),
78+
);
79+
})();
80+
81+
const listedPullRequestTabStatuses: Record<string, PullRequestTabStatusSeed> = Object.fromEntries(
82+
entries.map((entry) => [
83+
pullRequestSurfaceId(entry),
84+
{
85+
state: entry.state,
86+
isDraft: entry.isDraft,
87+
},
88+
]),
89+
);
90+
91+
const groups = (() => {
92+
if (involvement !== "all") return [{ key: "others" as const, label: "", entries }];
93+
if (baselineEmpty) {
94+
return groupPullRequestsByInvolvement(entries, viewers);
95+
}
96+
const narrow = (rows: ReadonlyArray<EnvironmentPullRequestEntry> | undefined) =>
97+
rows === undefined || !hasLocalFilters
98+
? rows
99+
: rows.filter((entry) =>
100+
matchesPullRequestFilters(entry, localFilters, pullRequestEntryViewer(entry, viewers)),
101+
);
102+
const narrowedAuthored = narrow(partitionsWanted ? (authored ?? held?.authored) : undefined);
103+
const narrowedReviewing = narrow(partitionsWanted ? (reviewing ?? held?.reviewing) : undefined);
104+
if (narrowedAuthored === undefined || narrowedReviewing === undefined) {
105+
return groupPullRequestsByInvolvement(entries, viewers);
106+
}
107+
return partitionPullRequestsWithPriority(entries, narrowedAuthored, narrowedReviewing);
108+
})();
109+
110+
const entriesByStatsKey: ReadonlyMap<string, EnvironmentPullRequestEntry> = new Map(
111+
groups.flatMap((group) =>
112+
group.entries.map((entry) => [pullRequestEntryKey(entry), entry] as const),
113+
),
114+
);
115+
const heldPullRequestsBySurface = new Map(
116+
groups.flatMap((group) =>
117+
group.entries.map((entry) => [pullRequestListEntryId(entry), entry] as const),
118+
),
119+
);
120+
return {
121+
entries,
122+
listedPullRequestTabStatuses,
123+
groups,
124+
entriesByStatsKey,
125+
heldPullRequestsBySurface,
126+
};
127+
}

‎apps/web/src/components/pullRequest/usePullRequestListView.ts‎

Lines changed: 1 addition & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,14 @@ import {
1111
} from "@t3tools/contracts";
1212
import { useState } from "react";
1313

14-
import type { PullRequestTabStatusSeed } from "~/components/RightPanelTabs";
15-
import { pullRequestSurfaceId } from "~/rightPanelStore";
1614
import { useAllEnvironmentShellsBootstrapped, useProjects } from "~/state/entities";
1715
import { useEnvironments } from "~/state/environments";
1816
import { useDebouncedValue } from "~/state/queries";
1917
import {
2018
applyPullRequestOverrides,
21-
filterPullRequestsByInvolvement,
2219
findProjectForRepository,
2320
findScopedProject,
24-
groupPullRequestsByInvolvement,
2521
matchesPullRequestFilters,
26-
matchesPullRequestQuery,
27-
partitionPullRequestsWithPriority,
2822
pullRequestDiffStatKey,
2923
pullRequestEntryKey,
3024
pullRequestEntryViewer,
@@ -40,12 +34,12 @@ import {
4034
type PullRequestDiffStats,
4135
type PullRequestGroup,
4236
type PullRequestListOverride,
43-
type PullRequestPartitionsSnapshot,
4437
} from "./pullRequestList.logic";
4538
import type { PullRequestExpectedHost } from "./PullRequestListFilters";
4639
import type { PullRequestListPreferences, PullRequestListSort } from "./pullRequestListPreferences";
4740
import { assignProjectsToEnvironments } from "./pullRequestProjectAssignment.logic";
4841
import { pullRequestFilterProjects } from "./pullRequestProjectFilter.logic";
42+
import { pullRequestListEntryId } from "./pullRequestListGroups.logic";
4943

5044
export interface PullRequestsSearch extends PullRequestListPreferences {
5145
readonly environmentId?: EnvironmentId;
@@ -60,9 +54,6 @@ export interface PullRequestsSearch extends PullRequestListPreferences {
6054

6155
const SEARCH_DEBOUNCE_MS = 250;
6256

63-
export const pullRequestListEntryId = (target: Parameters<typeof pullRequestSurfaceId>[0]) =>
64-
pullRequestSurfaceId({ ...target, repository: target.repository.toLowerCase() });
65-
6657
/** Which servers and projects the page reads, from the URL and what this client knows. */
6758
export function usePullRequestScope(search: PullRequestsSearch) {
6859
const { environments } = useEnvironments();
@@ -225,111 +216,6 @@ export function usePullRequestQueryFilters(search: PullRequestsSearch, typedQuer
225216
};
226217
}
227218

228-
/** The held rows narrowed to what the reader asked for, in the groups the list shows. */
229-
export function pullRequestGroups({
230-
known,
231-
providers,
232-
viewers,
233-
involvement,
234-
localFilters,
235-
hasLocalFilters,
236-
text,
237-
answeredLocally,
238-
baselineEmpty,
239-
held,
240-
authored,
241-
reviewing,
242-
partitionsWanted,
243-
}: {
244-
known: ReadonlyArray<EnvironmentPullRequestEntry>;
245-
providers: PullRequestListResult["providers"];
246-
viewers: PullRequestListResult["viewers"];
247-
involvement: PullRequestInvolvement;
248-
localFilters: PullRequestListFilters;
249-
hasLocalFilters: boolean;
250-
text: string;
251-
answeredLocally: boolean;
252-
baselineEmpty: boolean;
253-
held: PullRequestPartitionsSnapshot | undefined;
254-
authored: ReadonlyArray<EnvironmentPullRequestEntry> | undefined;
255-
reviewing: ReadonlyArray<EnvironmentPullRequestEntry> | undefined;
256-
partitionsWanted: boolean;
257-
}) {
258-
const searchingHosts = new Set(
259-
providers.flatMap((provider) => (provider.searchesOnHost ? [provider.host] : [])),
260-
);
261-
262-
const entries = (() => {
263-
const involvementEntries = filterPullRequestsByInvolvement(known, viewers, involvement);
264-
// The hosts search more than the row shows — a body, a review, a commit message — so once
265-
// their answer is in, narrowing it again here would throw away matches the reader asked for.
266-
// The local pass stands in for the answer that has not arrived yet, and for the hosts that
267-
// answered without searching at all: Azure DevOps has no text filter, so its rows arrive
268-
// whole and would otherwise sit under a search that never touched them.
269-
const narrowedEntries = hasLocalFilters
270-
? involvementEntries.filter((entry) =>
271-
matchesPullRequestFilters(entry, localFilters, pullRequestEntryViewer(entry, viewers)),
272-
)
273-
: involvementEntries;
274-
if (text.length === 0) {
275-
return narrowedEntries.toSorted((left, right) =>
276-
right.updatedAt.localeCompare(left.updatedAt),
277-
);
278-
}
279-
return narrowedEntries.filter(
280-
(entry) =>
281-
(answeredLocally && searchingHosts.has(entry.host)) || matchesPullRequestQuery(entry, text),
282-
);
283-
})();
284-
285-
const listedPullRequestTabStatuses: Record<string, PullRequestTabStatusSeed> = Object.fromEntries(
286-
entries.map((entry) => [
287-
pullRequestSurfaceId(entry),
288-
{
289-
state: entry.state,
290-
isDraft: entry.isDraft,
291-
},
292-
]),
293-
);
294-
295-
const groups = (() => {
296-
if (involvement !== "all") return [{ key: "others" as const, label: "", entries }];
297-
if (baselineEmpty) {
298-
return groupPullRequestsByInvolvement(entries, viewers);
299-
}
300-
const narrow = (rows: ReadonlyArray<EnvironmentPullRequestEntry> | undefined) =>
301-
rows === undefined || !hasLocalFilters
302-
? rows
303-
: rows.filter((entry) =>
304-
matchesPullRequestFilters(entry, localFilters, pullRequestEntryViewer(entry, viewers)),
305-
);
306-
const narrowedAuthored = narrow(partitionsWanted ? (authored ?? held?.authored) : undefined);
307-
const narrowedReviewing = narrow(partitionsWanted ? (reviewing ?? held?.reviewing) : undefined);
308-
if (narrowedAuthored === undefined || narrowedReviewing === undefined) {
309-
return groupPullRequestsByInvolvement(entries, viewers);
310-
}
311-
return partitionPullRequestsWithPriority(entries, narrowedAuthored, narrowedReviewing);
312-
})();
313-
314-
const entriesByStatsKey: ReadonlyMap<string, EnvironmentPullRequestEntry> = new Map(
315-
groups.flatMap((group) =>
316-
group.entries.map((entry) => [pullRequestEntryKey(entry), entry] as const),
317-
),
318-
);
319-
const heldPullRequestsBySurface = new Map(
320-
groups.flatMap((group) =>
321-
group.entries.map((entry) => [pullRequestListEntryId(entry), entry] as const),
322-
),
323-
);
324-
return {
325-
entries,
326-
listedPullRequestTabStatuses,
327-
groups,
328-
entriesByStatsKey,
329-
heldPullRequestsBySurface,
330-
};
331-
}
332-
333219
/**
334220
* The hosts the list has answered for (kept while a host filter narrows the answer), and the
335221
* groups as shown with measured sizes, optimistic actions, and the reader's sort.

0 commit comments

Comments
 (0)