Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestListGroups.logic.ts

124 lines4.2 KB

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 // Host text searches cover fields absent from rows, so do not narrow their matches locally.
61 // Filter text locally while results are pending or when the host has no text search.
62 const narrowedEntries = hasLocalFilters
63 ? involvementEntries.filter((entry) =>
64 matchesPullRequestFilters(entry, localFilters, pullRequestEntryViewer(entry, viewers)),
65 )
66 : involvementEntries;
67 if (text.length === 0) {
68 return narrowedEntries.toSorted((left, right) =>
69 right.updatedAt.localeCompare(left.updatedAt),
70 );
71 }
72 return narrowedEntries.filter(
73 (entry) =>
74 (answeredLocally && searchingHosts.has(entry.host)) || matchesPullRequestQuery(entry, text),
75 );
76 })();
77​
78 const listedPullRequestTabStatuses: Record<string, PullRequestTabStatusSeed> = Object.fromEntries(
79 entries.map((entry) => [
80 pullRequestSurfaceId(entry),
81 {
82 state: entry.state,
83 isDraft: entry.isDraft,
84 },
85 ]),
86 );
87​
88 const groups = (() => {
89 if (involvement !== "all") return [{ key: "others" as const, label: "", entries }];
90 if (baselineEmpty) {
91 return groupPullRequestsByInvolvement(entries, viewers);
92 }
93 const narrow = (rows: ReadonlyArray<EnvironmentPullRequestEntry> | undefined) =>
94 rows === undefined || !hasLocalFilters
95 ? rows
96 : rows.filter((entry) =>
97 matchesPullRequestFilters(entry, localFilters, pullRequestEntryViewer(entry, viewers)),
98 );
99 const narrowedAuthored = narrow(partitionsWanted ? (authored ?? held?.authored) : undefined);
100 const narrowedReviewing = narrow(partitionsWanted ? (reviewing ?? held?.reviewing) : undefined);
101 if (narrowedAuthored === undefined || narrowedReviewing === undefined) {
102 return groupPullRequestsByInvolvement(entries, viewers);
103 }
104 return partitionPullRequestsWithPriority(entries, narrowedAuthored, narrowedReviewing);
105 })();
106​
107 const entriesByStatsKey: ReadonlyMap<string, EnvironmentPullRequestEntry> = new Map(
108 groups.flatMap((group) =>
109 group.entries.map((entry) => [pullRequestEntryKey(entry), entry] as const),
110 ),
111 );
112 const heldPullRequestsBySurface = new Map(
113 groups.flatMap((group) =>
114 group.entries.map((entry) => [pullRequestListEntryId(entry), entry] as const),
115 ),
116 );
117 return {
118 entries,
119 listedPullRequestTabStatuses,
120 groups,
121 entriesByStatsKey,
122 heldPullRequestsBySurface,
123 };