Open History to see the last commit
1import type {
2 PullRequestInvolvement,
3 PullRequestListFilters,
4 PullRequestListResult,
5} from "@t3tools/contracts";
6
7import type { PullRequestTabStatusSeed } from "~/components/RightPanelTabs";
8import { pullRequestSurfaceId } from "~/rightPanelStore";
9import {
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
21export 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. */
25export 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}