@devries-cameron20 / t3code Mirror · read-only / commits / 5a31f4cf9c
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 20:45 UTC

refactor(web): the pull requests page's scope and grouping hooks live in their own module (#22705)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 21, 2026 at 01:28 UTC 5a31f4cf9c14b69db7a471dafb38c35a4b2db86d
2 files changed +444 -417
apps/web/src/components/pullRequest/usePullRequestListView.ts new
+434
@@ -0,0 +1,434 @@
1 +import { parsePullRequestQuery } from "@t3tools/client-runtime/pull-request-query";
2 +import {
3 + pullRequestHostOf,
4 + type EnvironmentId,
5 + type ProjectId,
6 + type PullRequestInvolvement,
7 + type PullRequestListFilters,
8 + type PullRequestListResult,
9 + type PullRequestListState,
10 + type SourceControlProviderKind,
11 +} from "@t3tools/contracts";
12 +import { useState } from "react";
13 +
14 +import type { PullRequestTabStatusSeed } from "~/components/RightPanelTabs";
15 +import { pullRequestSurfaceId } from "~/rightPanelStore";
16 +import { useAllEnvironmentShellsBootstrapped, useProjects } from "~/state/entities";
17 +import { useEnvironments } from "~/state/environments";
18 +import { useDebouncedValue } from "~/state/queries";
19 +import {
20 + applyPullRequestOverrides,
21 + filterPullRequestsByInvolvement,
22 + findProjectForRepository,
23 + findScopedProject,
24 + groupPullRequestsByInvolvement,
25 + matchesPullRequestFilters,
26 + matchesPullRequestQuery,
27 + partitionPullRequestsWithPriority,
28 + pullRequestDiffStatKey,
29 + pullRequestEntryKey,
30 + pullRequestEntryViewer,
31 + pullRequestEnvironmentSetKey,
32 + pullRequestHostActions,
33 + resolveProjectScope,
34 + resolveQueryEnvironmentIds,
35 + resolveSelectedEnvironmentId,
36 + sortPullRequestGroups,
37 + withDiffStat,
38 + type EnvironmentPullRequestEntry,
39 + type MergedPullRequestList,
40 + type PullRequestDiffStats,
41 + type PullRequestGroup,
42 + type PullRequestListOverride,
43 + type PullRequestPartitionsSnapshot,
44 +} from "./pullRequestList.logic";
45 +import type { PullRequestExpectedHost } from "./PullRequestListFilters";
46 +import type { PullRequestListPreferences, PullRequestListSort } from "./pullRequestListPreferences";
47 +import { assignProjectsToEnvironments } from "./pullRequestProjectAssignment.logic";
48 +import { pullRequestFilterProjects } from "./pullRequestProjectFilter.logic";
49 +
50 +export interface PullRequestsSearch extends PullRequestListPreferences {
51 + readonly environmentId?: EnvironmentId;
52 + readonly projectId?: ProjectId;
53 + readonly host?: string;
54 + readonly repository?: string;
55 + readonly number?: number;
56 + readonly selectedProjectId?: ProjectId;
57 + readonly selectedHost?: string;
58 + readonly selectedEnvironmentId?: EnvironmentId;
59 +}
60 +
61 +const SEARCH_DEBOUNCE_MS = 250;
62 +
63 +export const pullRequestListEntryId = (target: Parameters<typeof pullRequestSurfaceId>[0]) =>
64 + pullRequestSurfaceId({ ...target, repository: target.repository.toLowerCase() });
65 +
66 +/** Which servers and projects the page reads, from the URL and what this client knows. */
67 +export function usePullRequestScope(search: PullRequestsSearch) {
68 + const { environments } = useEnvironments();
69 + const allProjects = useProjects();
70 + const projectsKnown = useAllEnvironmentShellsBootstrapped();
71 + const capableEnvironments = environments
72 + .filter(
73 + (environment) => environment.serverConfig?.environment.capabilities.pullRequests === true,
74 + )
75 + .toSorted((left, right) => left.environmentId.localeCompare(right.environmentId));
76 + const scopedEnvironmentId =
77 + capableEnvironments.find((environment) => environment.environmentId === search.environmentId)
78 + ?.environmentId ?? null;
79 + const knownEnvironmentIds = new Set(environments.map((environment) => environment.environmentId));
80 + const environmentIds = capableEnvironments
81 + .filter(
82 + (environment) =>
83 + scopedEnvironmentId === null || environment.environmentId === scopedEnvironmentId,
84 + )
85 + .map((environment) => environment.environmentId);
86 + const environmentKey = pullRequestEnvironmentSetKey(environmentIds);
87 + const capabilityKnown = environments.some((environment) => environment.serverConfig !== null);
88 + const pullRequestsSupported = environmentIds.length > 0;
89 + const projects = allProjects.filter(
90 + (project) => !project.isScratch && environmentIds.includes(project.environmentId),
91 + );
92 + const environmentLabels = new Map(
93 + environments.map((environment) => [environment.environmentId, environment.label] as const),
94 + );
95 + const scopedProjectId = resolveProjectScope(search.projectId, projects, projectsKnown);
96 + const scopedProject = findScopedProject(projects, scopedEnvironmentId, scopedProjectId);
97 + const scopedProjects = pullRequestFilterProjects(projects, environmentLabels, scopedProject);
98 +
99 + const selectedHost = search.selectedHost ?? search.host;
100 + // A named server still connecting is kept named rather than falling back: falling back while it
101 + // is merely not ready yet would resolve the project against whatever other server has answered,
102 + // which can be the wrong one where two servers share a project id.
103 + const selectedEnvironmentId = resolveSelectedEnvironmentId(
104 + search.selectedEnvironmentId,
105 + knownEnvironmentIds,
106 + scopedEnvironmentId,
107 + );
108 + const projectForRepository =
109 + search.repository === undefined
110 + ? undefined
111 + : findProjectForRepository(projects, {
112 + repository: search.repository,
113 + host: selectedHost,
114 + selectedEnvironmentId,
115 + scopedProjectId,
116 + scopedEnvironmentId,
117 + });
118 + const linkedProjectId = resolveProjectScope(search.selectedProjectId, projects, projectsKnown);
119 + const selectedProject =
120 + linkedProjectId === undefined && search.repository !== undefined
121 + ? projectForRepository
122 + : findScopedProject(projects, selectedEnvironmentId, linkedProjectId ?? scopedProjectId);
123 + const queryEnvironmentIds = resolveQueryEnvironmentIds(
124 + environmentIds,
125 + projects,
126 + scopedProject,
127 + scopedProjectId,
128 + projectsKnown,
129 + );
130 + const environmentQueries = ((): ReadonlyArray<{
131 + readonly environmentId: EnvironmentId;
132 + readonly projectIds?: ReadonlyArray<ProjectId>;
133 + }> => {
134 + const plain = queryEnvironmentIds.map((environmentId) => ({ environmentId }));
135 + if (!projectsKnown || scopedProjectId !== undefined) return plain;
136 + const assignment = assignProjectsToEnvironments(
137 + projects,
138 + queryEnvironmentIds,
139 + queryEnvironmentIds[0],
140 + );
141 + const totals = new Map<EnvironmentId, number>();
142 + for (const project of projects) {
143 + totals.set(project.environmentId, (totals.get(project.environmentId) ?? 0) + 1);
144 + }
145 + return queryEnvironmentIds.flatMap((environmentId) => {
146 + const projectIds = assignment.get(environmentId);
147 + if (projectIds === undefined) return [];
148 + if (projectIds.length === (totals.get(environmentId) ?? 0)) return [{ environmentId }];
149 + return [{ environmentId, projectIds }];
150 + });
151 + })();
152 + const assignmentKey = environmentQueries
153 + .map(({ environmentId, projectIds }) => `${environmentId}#${projectIds?.join("+") ?? "*"}`)
154 + .join("|");
155 + const linkedSelection =
156 + search.repository && search.number && selectedProject
157 + ? {
158 + environmentId: selectedProject.environmentId,
159 + repository: search.repository,
160 + number: search.number,
161 + projectId: selectedProject.id,
162 + ...(selectedHost ? { host: selectedHost } : {}),
163 + }
164 + : null;
165 + const expectedHosts = (() => {
166 + const byHost = new Map<string, PullRequestExpectedHost>();
167 + for (const project of projects) {
168 + const kind = project.repositoryIdentity?.provider as SourceControlProviderKind | undefined;
169 + if (kind === undefined) continue;
170 + const host = pullRequestHostOf(project.repositoryIdentity, kind);
171 + if (!byHost.has(host)) byHost.set(host, { host, kind });
172 + }
173 + return [...byHost.values()];
174 + })();
175 +
176 + return {
177 + capableEnvironments,
178 + scopedEnvironmentId,
179 + environmentKey,
180 + capabilityKnown,
181 + pullRequestsSupported,
182 + projects,
183 + environmentLabels,
184 + scopedProjectId,
185 + scopedProject,
186 + scopedProjects,
187 + selectedProject,
188 + queryEnvironmentIds,
189 + environmentQueries,
190 + assignmentKey,
191 + linkedSelection,
192 + expectedHosts,
193 + projectsKnown,
194 + };
195 +}
196 +
197 +export function usePullRequestQueryFilters(search: PullRequestsSearch, typedQuery: string) {
198 + const sentQuery = useDebouncedValue(typedQuery, SEARCH_DEBOUNCE_MS);
199 + const typedParsed = parsePullRequestQuery(typedQuery);
200 + const sentParsed = parsePullRequestQuery(sentQuery);
201 +
202 + const menuFilters = {
203 + ...(search.draft ? { draft: search.draft } : {}),
204 + ...(search.review ? { review: search.review } : {}),
205 + ...(search.checks ? { checks: search.checks } : {}),
206 + ...(search.author ? { author: search.author } : {}),
207 + ...(search.labels ? { labels: search.labels.map((label) => [label]) } : {}),
208 + };
209 + const menuFiltered = Object.keys(menuFilters).length > 0;
210 + const filters = { ...menuFilters, ...sentParsed.filters };
211 + const hasFilters = Object.keys(filters).length > 0;
212 + const localFilters = { ...menuFilters, ...typedParsed.filters };
213 + const hasLocalFilters = Object.keys(localFilters).length > 0;
214 + return {
215 + sentQuery,
216 + querySettled: typedQuery === sentQuery,
217 + typedParsed,
218 + sentParsed,
219 + menuFilters,
220 + menuFiltered,
221 + filters,
222 + hasFilters,
223 + localFilters,
224 + hasLocalFilters,
225 + };
226 +}
227 +
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 +
333 +/**
334 + * The hosts the list has answered for (kept while a host filter narrows the answer), and the
335 + * groups as shown with measured sizes, optimistic actions, and the reader's sort.
336 + */
337 +export function usePullRequestDisplayGroups({
338 + answered,
339 + host,
340 + groups,
341 + statsByRow,
342 + overrides,
343 + state,
344 + localFilters,
345 + hasLocalFilters,
346 + viewers,
347 + sort,
348 + text,
349 + involvement,
350 +}: {
351 + answered: MergedPullRequestList | null;
352 + host: string | undefined;
353 + groups: ReadonlyArray<PullRequestGroup<EnvironmentPullRequestEntry>>;
354 + statsByRow: PullRequestDiffStats;
355 + overrides: ReadonlyMap<string, PullRequestListOverride>;
356 + state: PullRequestListState;
357 + localFilters: PullRequestListFilters;
358 + hasLocalFilters: boolean;
359 + viewers: PullRequestListResult["viewers"];
360 + sort: PullRequestListSort;
361 + text: string;
362 + involvement: PullRequestInvolvement;
363 +}) {
364 + const [hostsState, setHostsState] = useState<{
365 + readonly source: MergedPullRequestList | null;
366 + readonly host: string | undefined;
367 + readonly hosts: PullRequestListResult["providers"];
368 + }>({ source: null, host: undefined, hosts: [] });
369 + if (answered !== null && (hostsState.source !== answered || hostsState.host !== host)) {
370 + setHostsState({
371 + source: answered,
372 + host,
373 + hosts:
374 + host === undefined || hostsState.hosts.length === 0 ? answered.providers : hostsState.hosts,
375 + });
376 + }
377 + const hosts = hostsState.hosts;
378 + const displayGroups = (() => {
379 + const enriched = groups.map((group) => {
380 + const overridden = applyPullRequestOverrides(
381 + group.entries.map((entry) => withDiffStat(entry, statsByRow)),
382 + overrides,
383 + pullRequestEntryKey,
384 + state,
385 + );
386 + return {
387 + ...group,
388 + entries:
389 + hasLocalFilters && overrides.size > 0
390 + ? overridden.filter(
391 + (entry) =>
392 + !overrides.has(pullRequestEntryKey(entry)) ||
393 + matchesPullRequestFilters(
394 + entry,
395 + localFilters,
396 + pullRequestEntryViewer(entry, viewers),
397 + ),
398 + )
399 + : overridden,
400 + };
401 + });
402 + return sortPullRequestGroups(
403 + enriched,
404 + sort,
405 + text,
406 + (entry) =>
407 + entry.additions + entry.deletions > 0 || statsByRow.has(pullRequestDiffStatKey(entry)),
408 + involvement,
409 + );
410 + })();
411 + const shownCount = displayGroups.reduce((count, group) => count + group.entries.length, 0);
412 + const hostActionsOf = pullRequestHostActions(hosts);
413 + const closeSweepRows = new Map(
414 + displayGroups.flatMap((group) =>
415 + group.entries.map(
416 + (entry) => [pullRequestEntryKey(entry), { entry, groupKey: group.key }] as const,
417 + ),
418 + ),
419 + );
420 + const listedPullRequestsBySurface = new Map(
421 + displayGroups.flatMap((group) =>
422 + group.entries.map((entry) => [pullRequestListEntryId(entry), entry] as const),
423 + ),
424 + );
425 +
426 + return {
427 + hosts,
428 + displayGroups,
429 + shownCount,
430 + hostActionsOf,
431 + closeSweepRows,
432 + listedPullRequestsBySurface,
433 + };
434 +}
apps/web/src/routes/_chat.pull-requests.tsx
+10 -417
@@ -1,7 +1,4 @@
1 -import {
2 - applyPullRequestDraftFilterChange,
3 - parsePullRequestQuery,
4 -} from "@t3tools/client-runtime/pull-request-query";
1 +import { applyPullRequestDraftFilterChange } from "@t3tools/client-runtime/pull-request-query";
2 import { Spinner } from "~/components/ui/spinner";
3 import type { PullRequestSpeedActionResult } from "~/components/pullRequest/PullRequestSpeedActions";
4 import { usePullRequestCloseBatch } from "~/components/pullRequest/usePullRequestActions";
@@ -15,7 +12,6 @@ import type {
12 PullRequestAction,
13 PullRequestInvolvement,
14 PullRequestListCursors,
18 - PullRequestListFilters,
15 PullRequestListInput,
16 PullRequestListResult,
17 PullRequestListState,
@@ -27,27 +23,12 @@ import { ArrowDownUpIcon, LayersIcon, Plug2Icon } from "lucide-react";
23 import { useEffect, useEffectEvent, useRef, useState, type ComponentProps } from "react";
24
25 import {
30 - filterPullRequestsByInvolvement,
31 - findProjectForRepository,
32 - findScopedProject,
26 collectPullRequestListFacets,
34 - groupPullRequestsByInvolvement,
35 - matchesPullRequestFilters,
36 - matchesPullRequestQuery,
27 narrowPullRequestsToFilters,
28 mergePullRequestDiffStats,
39 - partitionPullRequestsWithPriority,
40 - pullRequestDiffStatKey,
29 pullRequestEntryKey,
42 - pullRequestEntryViewer,
30 rankPullRequestMatches,
44 - sortPullRequestGroups,
45 - pullRequestEnvironmentSetKey,
31 readPullRequestListSnapshot,
47 - resolveProjectScope,
48 - resolveQueryEnvironmentIds,
49 - resolveSelectedEnvironmentId,
50 - withDiffStat,
32 writePullRequestListSnapshot,
33 pullRequestStatsRefreshBatches,
34 pullRequestStatsRequestBatches,
@@ -55,28 +36,22 @@ import {
36 type EnvironmentPullRequestEntry,
37 type MergedPullRequestList,
38 type PullRequestDiffStats,
58 - type PullRequestGroup,
39 type PullRequestStatsBatch,
40 type PullRequestStatsPolicy,
41 type PullRequestStatsScope,
42 type PullRequestPartitionsSnapshot,
63 - applyPullRequestOverrides,
43 type PullRequestListOverride,
44 pullRequestOverrideAfterAction,
45 reusePullRequestEntries,
46 settlePullRequestOverrides,
68 - pullRequestHostActions,
47 largestPullRequestReadCount,
48 } from "../components/pullRequest/pullRequestList.logic";
49 import {
50 pullRequestListPreferences,
51 type PullRequestListPreferencePatch,
74 - type PullRequestListPreferences,
52 type PullRequestListSort,
53 writePullRequestListPreferences,
54 } from "../components/pullRequest/pullRequestListPreferences";
78 -import { assignProjectsToEnvironments } from "../components/pullRequest/pullRequestProjectAssignment.logic";
79 -import { pullRequestFilterProjects } from "../components/pullRequest/pullRequestProjectFilter.logic";
55 import { environmentMachineIcon } from "../components/EnvironmentMachineIcon";
56 import { PullRequestDetailPanel } from "../components/pullRequest/PullRequestDetailPanel";
57 import {
@@ -84,7 +59,6 @@ import {
59 PullRequestSearchInput,
60 pullRequestHostLabel,
61 pullRequestProjectKey,
87 - type PullRequestExpectedHost,
62 type PullRequestFilterOption,
63 } from "../components/pullRequest/PullRequestListFilters";
64 import { PullRequestCompactFilterMenu } from "../components/pullRequest/PullRequestCompactFilterMenu";
@@ -95,11 +69,19 @@ import {
69 } from "../components/pullRequest/pullRequestListOptions";
70 import { PullRequestsColumn } from "../components/pullRequest/PullRequestsColumn";
71 import { PullRequestListGroups } from "../components/pullRequest/PullRequestListGroups";
72 +import {
73 + pullRequestGroups,
74 + pullRequestListEntryId,
75 + usePullRequestDisplayGroups,
76 + usePullRequestQueryFilters,
77 + usePullRequestScope,
78 + type PullRequestsSearch,
79 +} from "../components/pullRequest/usePullRequestListView";
80 import { PullRequestListEmptyState } from "../components/pullRequest/PullRequestListEmptyState";
81 import { PullRequestListGhost } from "../components/pullRequest/PullRequestGhosts";
82 import { type PullRequestRowTarget } from "../components/pullRequest/PullRequestRow";
83 import { PullRequestsUnavailableState } from "../components/pullRequest/PullRequestsUnavailableState";
102 -import { RightPanelTabs, type PullRequestTabStatusSeed } from "../components/RightPanelTabs";
84 +import { RightPanelTabs } from "../components/RightPanelTabs";
85 import { isCommandPaletteOpen } from "../commandPaletteBus";
86 import { isElectron } from "../env";
87 import { resolveShortcutCommand, shortcutLabelForCommand } from "../keybindings";
@@ -119,17 +101,13 @@ import { useEscapeToGoBack } from "../hooks/useNavigateBack";
101 import { usePanelAnimationSettings, usePanelPresence } from "../panelAnimations";
102 import {
103 PULL_REQUESTS_PANEL_REF,
122 - pullRequestSurfaceId,
104 selectActiveRightPanelSurface,
105 selectSelectedRightPanelSurface,
106 selectThreadRightPanelState,
107 useRightPanelStore,
108 type PullRequestSurface,
109 } from "../rightPanelStore";
129 -import { useDebouncedValue } from "../state/queries";
130 -import { useAllEnvironmentShellsBootstrapped, useProjects } from "../state/entities";
110 import { useSidebarProjectGroups } from "../hooks/useSidebarProjectGroups";
132 -import { useEnvironments } from "../state/environments";
111 import {
112 pullRequestEnvironment,
113 usePullRequestList,
@@ -154,18 +132,6 @@ function getShortcutContext() {
132 };
133 }
134
157 -export interface PullRequestsSearch extends PullRequestListPreferences {
158 - readonly environmentId?: EnvironmentId;
159 - readonly projectId?: ProjectId;
160 - readonly host?: string;
161 - readonly repository?: string;
162 - readonly number?: number;
163 - readonly selectedProjectId?: ProjectId;
164 - readonly selectedHost?: string;
165 - readonly selectedEnvironmentId?: EnvironmentId;
166 -}
167 -
168 -const SEARCH_DEBOUNCE_MS = 250;
135 /**
136 * One whole page from the host and no more: every provider asks for one row beyond the page as
137 * its "is there more" probe, and GitHub serves a hundred per request — so asking for ninety-nine
@@ -182,9 +148,6 @@ const EMPTY_TERMINAL_LABELS = new Map<string, string>();
148 const EMPTY_PENDING_SURFACES = new Set<string>();
149 const MAX_SEARCH_LABEL_CANDIDATES = 100;
150
185 -const pullRequestListEntryId = (target: Parameters<typeof pullRequestSurfaceId>[0]) =>
186 - pullRequestSurfaceId({ ...target, repository: target.repository.toLowerCase() });
187 -
151 function pullRequestSearchLabels(raw: unknown): Partial<Pick<PullRequestsSearch, "labels">> {
152 const values = (Array.isArray(raw) ? raw : typeof raw === "string" ? [raw] : []).slice(
153 0,
@@ -1655,376 +1618,6 @@ function PullRequestsRouteView() {
1618 );
1619 }
1620
1658 -/** Which servers and projects the page reads, from the URL and what this client knows. */
1659 -function usePullRequestScope(search: PullRequestsSearch) {
1660 - const { environments } = useEnvironments();
1661 - const allProjects = useProjects();
1662 - const projectsKnown = useAllEnvironmentShellsBootstrapped();
1663 - const capableEnvironments = environments
1664 - .filter(
1665 - (environment) => environment.serverConfig?.environment.capabilities.pullRequests === true,
1666 - )
1667 - .toSorted((left, right) => left.environmentId.localeCompare(right.environmentId));
1668 - const scopedEnvironmentId =
1669 - capableEnvironments.find((environment) => environment.environmentId === search.environmentId)
1670 - ?.environmentId ?? null;
1671 - const knownEnvironmentIds = new Set(environments.map((environment) => environment.environmentId));
1672 - const environmentIds = capableEnvironments
1673 - .filter(
1674 - (environment) =>
1675 - scopedEnvironmentId === null || environment.environmentId === scopedEnvironmentId,
1676 - )
1677 - .map((environment) => environment.environmentId);
1678 - const environmentKey = pullRequestEnvironmentSetKey(environmentIds);
1679 - const capabilityKnown = environments.some((environment) => environment.serverConfig !== null);
1680 - const pullRequestsSupported = environmentIds.length > 0;
1681 - const projects = allProjects.filter(
1682 - (project) => !project.isScratch && environmentIds.includes(project.environmentId),
1683 - );
1684 - const environmentLabels = new Map(
1685 - environments.map((environment) => [environment.environmentId, environment.label] as const),
1686 - );
1687 - const scopedProjectId = resolveProjectScope(search.projectId, projects, projectsKnown);
1688 - const scopedProject = findScopedProject(projects, scopedEnvironmentId, scopedProjectId);
1689 - const scopedProjects = pullRequestFilterProjects(projects, environmentLabels, scopedProject);
1690 -
1691 - const selectedHost = search.selectedHost ?? search.host;
1692 - // A named server still connecting is kept named rather than falling back: falling back while it
1693 - // is merely not ready yet would resolve the project against whatever other server has answered,
1694 - // which can be the wrong one where two servers share a project id.
1695 - const selectedEnvironmentId = resolveSelectedEnvironmentId(
1696 - search.selectedEnvironmentId,
1697 - knownEnvironmentIds,
1698 - scopedEnvironmentId,
1699 - );
1700 - const projectForRepository =
1701 - search.repository === undefined
1702 - ? undefined
1703 - : findProjectForRepository(projects, {
1704 - repository: search.repository,
1705 - host: selectedHost,
1706 - selectedEnvironmentId,
1707 - scopedProjectId,
1708 - scopedEnvironmentId,
1709 - });
1710 - const linkedProjectId = resolveProjectScope(search.selectedProjectId, projects, projectsKnown);
1711 - const selectedProject =
1712 - linkedProjectId === undefined && search.repository !== undefined
1713 - ? projectForRepository
1714 - : findScopedProject(projects, selectedEnvironmentId, linkedProjectId ?? scopedProjectId);
1715 - const queryEnvironmentIds = resolveQueryEnvironmentIds(
1716 - environmentIds,
1717 - projects,
1718 - scopedProject,
1719 - scopedProjectId,
1720 - projectsKnown,
1721 - );
1722 - const environmentQueries = ((): ReadonlyArray<{
1723 - readonly environmentId: EnvironmentId;
1724 - readonly projectIds?: ReadonlyArray<ProjectId>;
1725 - }> => {
1726 - const plain = queryEnvironmentIds.map((environmentId) => ({ environmentId }));
1727 - if (!projectsKnown || scopedProjectId !== undefined) return plain;
1728 - const assignment = assignProjectsToEnvironments(
1729 - projects,
1730 - queryEnvironmentIds,
1731 - queryEnvironmentIds[0],
1732 - );
1733 - const totals = new Map<EnvironmentId, number>();
1734 - for (const project of projects) {
1735 - totals.set(project.environmentId, (totals.get(project.environmentId) ?? 0) + 1);
1736 - }
1737 - return queryEnvironmentIds.flatMap((environmentId) => {
1738 - const projectIds = assignment.get(environmentId);
1739 - if (projectIds === undefined) return [];
1740 - if (projectIds.length === (totals.get(environmentId) ?? 0)) return [{ environmentId }];
1741 - return [{ environmentId, projectIds }];
1742 - });
1743 - })();
1744 - const assignmentKey = environmentQueries
1745 - .map(({ environmentId, projectIds }) => `${environmentId}#${projectIds?.join("+") ?? "*"}`)
1746 - .join("|");
1747 - const linkedSelection =
1748 - search.repository && search.number && selectedProject
1749 - ? {
1750 - environmentId: selectedProject.environmentId,
1751 - repository: search.repository,
1752 - number: search.number,
1753 - projectId: selectedProject.id,
1754 - ...(selectedHost ? { host: selectedHost } : {}),
1755 - }
1756 - : null;
1757 - const expectedHosts = (() => {
1758 - const byHost = new Map<string, PullRequestExpectedHost>();
1759 - for (const project of projects) {
1760 - const kind = project.repositoryIdentity?.provider as SourceControlProviderKind | undefined;
1761 - if (kind === undefined) continue;
1762 - const host = pullRequestHostOf(project.repositoryIdentity, kind);
1763 - if (!byHost.has(host)) byHost.set(host, { host, kind });
1764 - }
1765 - return [...byHost.values()];
1766 - })();
1767 -
1768 - return {
1769 - capableEnvironments,
1770 - scopedEnvironmentId,
1771 - environmentKey,
1772 - capabilityKnown,
1773 - pullRequestsSupported,
1774 - projects,
1775 - environmentLabels,
1776 - scopedProjectId,
1777 - scopedProject,
1778 - scopedProjects,
1779 - selectedProject,
1780 - queryEnvironmentIds,
1781 - environmentQueries,
1782 - assignmentKey,
1783 - linkedSelection,
1784 - expectedHosts,
1785 - projectsKnown,
1786 - };
1787 -}
1788 -
1789 -function usePullRequestQueryFilters(search: PullRequestsSearch, typedQuery: string) {
1790 - const sentQuery = useDebouncedValue(typedQuery, SEARCH_DEBOUNCE_MS);
1791 - const typedParsed = parsePullRequestQuery(typedQuery);
1792 - const sentParsed = parsePullRequestQuery(sentQuery);
1793 -
1794 - const menuFilters = {
1795 - ...(search.draft ? { draft: search.draft } : {}),
1796 - ...(search.review ? { review: search.review } : {}),
1797 - ...(search.checks ? { checks: search.checks } : {}),
1798 - ...(search.author ? { author: search.author } : {}),
1799 - ...(search.labels ? { labels: search.labels.map((label) => [label]) } : {}),
1800 - };
1801 - const menuFiltered = Object.keys(menuFilters).length > 0;
1802 - const filters = { ...menuFilters, ...sentParsed.filters };
1803 - const hasFilters = Object.keys(filters).length > 0;
1804 - const localFilters = { ...menuFilters, ...typedParsed.filters };
1805 - const hasLocalFilters = Object.keys(localFilters).length > 0;
1806 - return {
1807 - sentQuery,
1808 - querySettled: typedQuery === sentQuery,
1809 - typedParsed,
1810 - sentParsed,
1811 - menuFilters,
1812 - menuFiltered,
1813 - filters,
1814 - hasFilters,
1815 - localFilters,
1816 - hasLocalFilters,
1817 - };
1818 -}
1819 -
1820 -/** The held rows narrowed to what the reader asked for, in the groups the list shows. */
1821 -function pullRequestGroups({
1822 - known,
1823 - providers,
1824 - viewers,
1825 - involvement,
1826 - localFilters,
1827 - hasLocalFilters,
1828 - text,
1829 - answeredLocally,
1830 - baselineEmpty,
1831 - held,
1832 - authored,
1833 - reviewing,
1834 - partitionsWanted,
1835 -}: {
1836 - known: ReadonlyArray<EnvironmentPullRequestEntry>;
1837 - providers: PullRequestListResult["providers"];
1838 - viewers: PullRequestListResult["viewers"];
1839 - involvement: PullRequestInvolvement;
1840 - localFilters: PullRequestListFilters;
1841 - hasLocalFilters: boolean;
1842 - text: string;
1843 - answeredLocally: boolean;
1844 - baselineEmpty: boolean;
1845 - held: PullRequestPartitionsSnapshot | undefined;
1846 - authored: ReadonlyArray<EnvironmentPullRequestEntry> | undefined;
1847 - reviewing: ReadonlyArray<EnvironmentPullRequestEntry> | undefined;
1848 - partitionsWanted: boolean;
1849 -}) {
1850 - const searchingHosts = new Set(
1851 - providers.flatMap((provider) => (provider.searchesOnHost ? [provider.host] : [])),
1852 - );
1853 -
1854 - const entries = (() => {
1855 - const involvementEntries = filterPullRequestsByInvolvement(known, viewers, involvement);
1856 - // The hosts search more than the row shows — a body, a review, a commit message — so once
1857 - // their answer is in, narrowing it again here would throw away matches the reader asked for.
1858 - // The local pass stands in for the answer that has not arrived yet, and for the hosts that
1859 - // answered without searching at all: Azure DevOps has no text filter, so its rows arrive
1860 - // whole and would otherwise sit under a search that never touched them.
1861 - const narrowedEntries = hasLocalFilters
1862 - ? involvementEntries.filter((entry) =>
1863 - matchesPullRequestFilters(entry, localFilters, pullRequestEntryViewer(entry, viewers)),
1864 - )
1865 - : involvementEntries;
1866 - if (text.length === 0) {
1867 - return narrowedEntries.toSorted((left, right) =>
1868 - right.updatedAt.localeCompare(left.updatedAt),
1869 - );
1870 - }
1871 - return narrowedEntries.filter(
1872 - (entry) =>
1873 - (answeredLocally && searchingHosts.has(entry.host)) || matchesPullRequestQuery(entry, text),
1874 - );
1875 - })();
1876 -
1877 - const listedPullRequestTabStatuses: Record<string, PullRequestTabStatusSeed> = Object.fromEntries(
1878 - entries.map((entry) => [
1879 - pullRequestSurfaceId(entry),
1880 - {
1881 - state: entry.state,
1882 - isDraft: entry.isDraft,
1883 - },
1884 - ]),
1885 - );
1886 -
1887 - const groups = (() => {
1888 - if (involvement !== "all") return [{ key: "others" as const, label: "", entries }];
1889 - if (baselineEmpty) {
1890 - return groupPullRequestsByInvolvement(entries, viewers);
1891 - }
1892 - const narrow = (rows: ReadonlyArray<EnvironmentPullRequestEntry> | undefined) =>
1893 - rows === undefined || !hasLocalFilters
1894 - ? rows
1895 - : rows.filter((entry) =>
1896 - matchesPullRequestFilters(entry, localFilters, pullRequestEntryViewer(entry, viewers)),
1897 - );
1898 - const narrowedAuthored = narrow(partitionsWanted ? (authored ?? held?.authored) : undefined);
1899 - const narrowedReviewing = narrow(partitionsWanted ? (reviewing ?? held?.reviewing) : undefined);
1900 - if (narrowedAuthored === undefined || narrowedReviewing === undefined) {
1901 - return groupPullRequestsByInvolvement(entries, viewers);
1902 - }
1903 - return partitionPullRequestsWithPriority(entries, narrowedAuthored, narrowedReviewing);
1904 - })();
1905 -
1906 - const entriesByStatsKey: ReadonlyMap<string, EnvironmentPullRequestEntry> = new Map(
1907 - groups.flatMap((group) =>
1908 - group.entries.map((entry) => [pullRequestEntryKey(entry), entry] as const),
1909 - ),
1910 - );
1911 - const heldPullRequestsBySurface = new Map(
1912 - groups.flatMap((group) =>
1913 - group.entries.map((entry) => [pullRequestListEntryId(entry), entry] as const),
1914 - ),
1915 - );
1916 - return {
1917 - entries,
1918 - listedPullRequestTabStatuses,
1919 - groups,
1920 - entriesByStatsKey,
1921 - heldPullRequestsBySurface,
1922 - };
1923 -}
1924 -
1925 -/**
1926 - * The hosts the list has answered for (kept while a host filter narrows the answer), and the
1927 - * groups as shown with measured sizes, optimistic actions, and the reader's sort.
1928 - */
1929 -function usePullRequestDisplayGroups({
1930 - answered,
1931 - host,
1932 - groups,
1933 - statsByRow,
1934 - overrides,
1935 - state,
1936 - localFilters,
1937 - hasLocalFilters,
1938 - viewers,
1939 - sort,
1940 - text,
1941 - involvement,
1942 -}: {
1943 - answered: MergedPullRequestList | null;
1944 - host: string | undefined;
1945 - groups: ReadonlyArray<PullRequestGroup<EnvironmentPullRequestEntry>>;
1946 - statsByRow: PullRequestDiffStats;
1947 - overrides: ReadonlyMap<string, PullRequestListOverride>;
1948 - state: PullRequestListState;
1949 - localFilters: PullRequestListFilters;
1950 - hasLocalFilters: boolean;
1951 - viewers: PullRequestListResult["viewers"];
1952 - sort: PullRequestListSort;
1953 - text: string;
1954 - involvement: PullRequestInvolvement;
1955 -}) {
1956 - const [hostsState, setHostsState] = useState<{
1957 - readonly source: MergedPullRequestList | null;
1958 - readonly host: string | undefined;
1959 - readonly hosts: PullRequestListResult["providers"];
1960 - }>({ source: null, host: undefined, hosts: [] });
1961 - if (answered !== null && (hostsState.source !== answered || hostsState.host !== host)) {
1962 - setHostsState({
1963 - source: answered,
1964 - host,
1965 - hosts:
1966 - host === undefined || hostsState.hosts.length === 0 ? answered.providers : hostsState.hosts,
1967 - });
1968 - }
1969 - const hosts = hostsState.hosts;
1970 - const displayGroups = (() => {
1971 - const enriched = groups.map((group) => {
1972 - const overridden = applyPullRequestOverrides(
1973 - group.entries.map((entry) => withDiffStat(entry, statsByRow)),
1974 - overrides,
1975 - pullRequestEntryKey,
1976 - state,
1977 - );
1978 - return {
1979 - ...group,
1980 - entries:
1981 - hasLocalFilters && overrides.size > 0
1982 - ? overridden.filter(
1983 - (entry) =>
1984 - !overrides.has(pullRequestEntryKey(entry)) ||
1985 - matchesPullRequestFilters(
1986 - entry,
1987 - localFilters,
1988 - pullRequestEntryViewer(entry, viewers),
1989 - ),
1990 - )
1991 - : overridden,
1992 - };
1993 - });
1994 - return sortPullRequestGroups(
1995 - enriched,
1996 - sort,
1997 - text,
1998 - (entry) =>
1999 - entry.additions + entry.deletions > 0 || statsByRow.has(pullRequestDiffStatKey(entry)),
2000 - involvement,
2001 - );
2002 - })();
2003 - const shownCount = displayGroups.reduce((count, group) => count + group.entries.length, 0);
2004 - const hostActionsOf = pullRequestHostActions(hosts);
2005 - const closeSweepRows = new Map(
2006 - displayGroups.flatMap((group) =>
2007 - group.entries.map(
2008 - (entry) => [pullRequestEntryKey(entry), { entry, groupKey: group.key }] as const,
2009 - ),
2010 - ),
2011 - );
2012 - const listedPullRequestsBySurface = new Map(
2013 - displayGroups.flatMap((group) =>
2014 - group.entries.map((entry) => [pullRequestListEntryId(entry), entry] as const),
2015 - ),
2016 - );
2017 -
2018 - return {
2019 - hosts,
2020 - displayGroups,
2021 - shownCount,
2022 - hostActionsOf,
2023 - closeSweepRows,
2024 - listedPullRequestsBySurface,
2025 - };
2026 -}
2027 -
1621 function PullRequestFiltersMenuInSidebarOrder(
1622 props: ComponentProps<typeof PullRequestFiltersMenu>,
1623 ) {