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

pullRequestListGroups.logic.ts

123 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 comparePullRequestsByRecency,
11 filterPullRequestsByInvolvement,
12 groupPullRequestsByInvolvement,
13 matchesPullRequestFilters,
14 matchesPullRequestQuery,
15 partitionPullRequestsWithPriority,
16 pullRequestEntryKey,
17 pullRequestEntryViewer,
18 type EnvironmentPullRequestEntry,
19 type PullRequestPartitionsSnapshot,
20} from "./pullRequestList.logic";
21​
22export const pullRequestListEntryId = (target: Parameters<typeof pullRequestSurfaceId>[0]) =>
23 pullRequestSurfaceId({ ...target, repository: target.repository.toLowerCase() });
24​
25/** The held rows narrowed to what the reader asked for, in the groups the list shows. */
26export function pullRequestGroups({
27 known,
28 providers,
29 viewers,
30 involvement,
31 localFilters,
32 hasLocalFilters,
33 text,
34 answeredLocally,
35 baselineEmpty,
36 held,
37 authored,
38 reviewing,
39 partitionsWanted,
40}: {
41 known: ReadonlyArray<EnvironmentPullRequestEntry>;
42 providers: PullRequestListResult["providers"];
43 viewers: PullRequestListResult["viewers"];
44 involvement: PullRequestInvolvement;
45 localFilters: PullRequestListFilters;
46 hasLocalFilters: boolean;
47 text: string;
48 answeredLocally: boolean;
49 baselineEmpty: boolean;
50 held: PullRequestPartitionsSnapshot | undefined;
51 authored: ReadonlyArray<EnvironmentPullRequestEntry> | undefined;
52 reviewing: ReadonlyArray<EnvironmentPullRequestEntry> | undefined;
53 partitionsWanted: boolean;
54}) {
55 const searchingHosts = new Set(
56 providers.flatMap((provider) => (provider.searchesOnHost ? [provider.host] : [])),
57 );
58​
59 const entries = (() => {
60 const involvementEntries = filterPullRequestsByInvolvement(known, viewers, involvement);
61 // Host text searches cover fields absent from rows, so do not narrow their matches locally.
62 // Filter text locally while results are pending or when the host has no text search.
63 const narrowedEntries = hasLocalFilters
64 ? involvementEntries.filter((entry) =>
65 matchesPullRequestFilters(entry, localFilters, pullRequestEntryViewer(entry, viewers)),
66 )
67 : involvementEntries;
68 if (text.length === 0) {
69 return narrowedEntries.toSorted(comparePullRequestsByRecency);
70 }
71 return narrowedEntries.filter(
72 (entry) =>
73 (answeredLocally && searchingHosts.has(entry.host)) || matchesPullRequestQuery(entry, text),
74 );
75 })();
76​
77 const listedPullRequestTabStatuses: Record<string, PullRequestTabStatusSeed> = Object.fromEntries(
78 entries.map((entry) => [
79 pullRequestSurfaceId(entry),
80 {
81 state: entry.state,
82 isDraft: entry.isDraft,
83 },
84 ]),
85 );
86​
87 const groups = (() => {
88 if (involvement !== "all") return [{ key: "others" as const, label: "", entries }];
89 if (baselineEmpty) {
90 return groupPullRequestsByInvolvement(entries, viewers);
91 }
92 const narrow = (rows: ReadonlyArray<EnvironmentPullRequestEntry> | undefined) =>
93 rows === undefined || !hasLocalFilters
94 ? rows
95 : rows.filter((entry) =>
96 matchesPullRequestFilters(entry, localFilters, pullRequestEntryViewer(entry, viewers)),
97 );
98 const narrowedAuthored = narrow(partitionsWanted ? (authored ?? held?.authored) : undefined);
99 const narrowedReviewing = narrow(partitionsWanted ? (reviewing ?? held?.reviewing) : undefined);
100 if (narrowedAuthored === undefined || narrowedReviewing === undefined) {
101 return groupPullRequestsByInvolvement(entries, viewers);
102 }
103 return partitionPullRequestsWithPriority(entries, narrowedAuthored, narrowedReviewing);
104 })();
105​
106 const entriesByStatsKey: ReadonlyMap<string, EnvironmentPullRequestEntry> = new Map(
107 groups.flatMap((group) =>
108 group.entries.map((entry) => [pullRequestEntryKey(entry), entry] as const),
109 ),
110 );
111 const heldPullRequestsBySurface = new Map(
112 groups.flatMap((group) =>
113 group.entries.map((entry) => [pullRequestListEntryId(entry), entry] as const),
114 ),
115 );
116 return {
117 entries,
118 listedPullRequestTabStatuses,
119 groups,
120 entriesByStatsKey,
121 heldPullRequestsBySurface,
122 };