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