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 { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; |
| 2 | import type { |
| 3 | PullRequestSummary, |
| 4 | ThreadPullRequestKey, |
| 5 | ThreadPullRequestLink, |
| 6 | VcsStatusResult, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import { |
| 9 | sourceControlClients, |
| 10 | type ChangeRequestTerminology, |
| 11 | } from "@t3tools/client-runtime/source-control-clients"; |
| 12 | import { changeRequestTermKind } from "../lib/changeRequestTerm"; |
| 13 | |
| 14 | import { |
| 15 | legacyThreadPullRequestKey, |
| 16 | resolveThreadCurrentPullRequestLink, |
| 17 | resolveThreadPullRequestBadge, |
| 18 | threadPullRequestKeyOf, |
| 19 | } from "@t3tools/client-runtime/thread-pull-requests"; |
| 20 | import { msg } from "gt-react-native"; |
| 21 | |
| 22 | import { m, type MessageResolver } from "../i18n/messages"; |
| 23 | |
| 24 | export type ThreadPr = NonNullable<VcsStatusResult["pr"]>; |
| 25 | |
| 26 | export interface ThreadPrPresentation { |
| 27 | readonly number: number; |
| 28 | readonly state: ThreadPr["state"] | null; |
| 29 | readonly kind: "pull-request" | "stack"; |
| 30 | readonly others: number; |
| 31 | readonly isDraft: boolean; |
| 32 | readonly updatedAt: string | null; |
| 33 | readonly url: string; |
| 34 | readonly changeRequest: ChangeRequestTerminology; |
| 35 | readonly label: string; |
| 36 | readonly accessibilityLabel: string; |
| 37 | readonly textClassName: string; |
| 38 | } |
| 39 | |
| 40 | const PR_STATE_TEXT_CLASS: Record<ThreadPr["state"], string> = { |
| 41 | open: "text-adaptive-emerald-600-400", |
| 42 | merged: "text-adaptive-violet-600-400", |
| 43 | closed: "text-adaptive-rose-600-400", |
| 44 | }; |
| 45 | |
| 46 | function prStateKey(state: ThreadPr["state"] | "draft" | null) { |
| 47 | return state ?? "pending"; |
| 48 | } |
| 49 | |
| 50 | /** Render callers pass `useMessages()` so the label follows a language switch. */ |
| 51 | export function presentThreadPr( |
| 52 | pr: ThreadPr, |
| 53 | provider: VcsStatusResult["sourceControlProvider"] | null | undefined, |
| 54 | resolve: MessageResolver = m, |
| 55 | ): ThreadPrPresentation { |
| 56 | const { changeRequest } = sourceControlClients.get(provider?.kind); |
| 57 | const isDraft = pr.state === "open" && pr.isDraft === true; |
| 58 | return { |
| 59 | kind: "pull-request", |
| 60 | others: 0, |
| 61 | number: pr.number, |
| 62 | state: pr.state, |
| 63 | isDraft, |
| 64 | updatedAt: pr.updatedAt ?? null, |
| 65 | url: pr.url, |
| 66 | changeRequest, |
| 67 | label: String(pr.number), |
| 68 | accessibilityLabel: resolve( |
| 69 | msg( |
| 70 | "{term, select, mr {#{number} merge request {state, select, pending {status pending} draft {draft} open {open} merged {merged} other {closed}}} cr {#{number} change request {state, select, pending {status pending} draft {draft} open {open} merged {merged} other {closed}}} other {#{number} pull request {state, select, pending {status pending} draft {draft} open {open} merged {merged} other {closed}}}}", |
| 71 | { |
| 72 | number: String(pr.number), |
| 73 | term: changeRequestTermKind(changeRequest), |
| 74 | state: prStateKey(isDraft ? "draft" : pr.state), |
| 75 | }, |
| 76 | ), |
| 77 | ), |
| 78 | textClassName: isDraft ? "text-foreground-muted" : PR_STATE_TEXT_CLASS[pr.state], |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | export function presentThreadLinkedPullRequests( |
| 83 | links: ReadonlyArray<ThreadPullRequestLink>, |
| 84 | resolve: MessageResolver = m, |
| 85 | ): ThreadPrPresentation | null { |
| 86 | const link = resolveThreadCurrentPullRequestLink(links); |
| 87 | const badge = resolveThreadPullRequestBadge(links); |
| 88 | if (link === null || badge === null) return null; |
| 89 | const { changeRequest } = |
| 90 | sourceControlClients.findByChangeRequestUrl(link.url) ?? sourceControlClients.get(undefined); |
| 91 | const snapshot = link.snapshot; |
| 92 | const linkedCount = badge.kind === "pull-request" && badge.others > 0 ? badge.others + 1 : null; |
| 93 | const isMultiple = badge.kind === "stack" || linkedCount !== null; |
| 94 | const state = isMultiple |
| 95 | ? badge.state === "draft" |
| 96 | ? "open" |
| 97 | : badge.state |
| 98 | : (snapshot?.state ?? null); |
| 99 | const isDraft = isMultiple |
| 100 | ? badge.state === "draft" |
| 101 | : snapshot?.isDraft === true && state === "open"; |
| 102 | const label = |
| 103 | badge.kind === "stack" |
| 104 | ? String(badge.layers) |
| 105 | : linkedCount !== null |
| 106 | ? `+${linkedCount}` |
| 107 | : String(link.number); |
| 108 | return { |
| 109 | kind: badge.kind, |
| 110 | others: badge.kind === "pull-request" ? badge.others : 0, |
| 111 | number: link.number, |
| 112 | state, |
| 113 | isDraft, |
| 114 | updatedAt: snapshot?.updatedAt ?? null, |
| 115 | url: link.url, |
| 116 | changeRequest, |
| 117 | label, |
| 118 | accessibilityLabel: |
| 119 | badge.kind === "stack" |
| 120 | ? resolve( |
| 121 | msg( |
| 122 | "{term, select, mr {{count, plural, one {{count} merge requests in stack, {state, select, pending {status pending} draft {draft} open {open} merged {merged} other {closed}}} other {{count} merge requests in stack, {state, select, pending {status pending} draft {draft} open {open} merged {merged} other {closed}}}}} cr {{count, plural, one {{count} change requests in stack, {state, select, pending {status pending} draft {draft} open {open} merged {merged} other {closed}}} other {{count} change requests in stack, {state, select, pending {status pending} draft {draft} open {open} merged {merged} other {closed}}}}} other {{count, plural, one {{count} pull requests in stack, {state, select, pending {status pending} draft {draft} open {open} merged {merged} other {closed}}} other {{count} pull requests in stack, {state, select, pending {status pending} draft {draft} open {open} merged {merged} other {closed}}}}}}", |
| 123 | { |
| 124 | count: badge.layers, |
| 125 | term: changeRequestTermKind(changeRequest), |
| 126 | state: prStateKey(isDraft ? "draft" : state), |
| 127 | }, |
| 128 | ), |
| 129 | ) |
| 130 | : linkedCount !== null |
| 131 | ? resolve( |
| 132 | msg( |
| 133 | "{term, select, mr {{count, plural, one {{count} linked merge requests, overall {state, select, pending {status pending} draft {draft} open {open} merged {merged} other {closed}}} other {{count} linked merge requests, overall {state, select, pending {status pending} draft {draft} open {open} merged {merged} other {closed}}}}} cr {{count, plural, one {{count} linked change requests, overall {state, select, pending {status pending} draft {draft} open {open} merged {merged} other {closed}}} other {{count} linked change requests, overall {state, select, pending {status pending} draft {draft} open {open} merged {merged} other {closed}}}}} other {{count, plural, one {{count} linked pull requests, overall {state, select, pending {status pending} draft {draft} open {open} merged {merged} other {closed}}} other {{count} linked pull requests, overall {state, select, pending {status pending} draft {draft} open {open} merged {merged} other {closed}}}}}}", |
| 134 | { |
| 135 | count: linkedCount, |
| 136 | term: changeRequestTermKind(changeRequest), |
| 137 | state: prStateKey(badge.state), |
| 138 | }, |
| 139 | ), |
| 140 | ) |
| 141 | : resolve( |
| 142 | msg( |
| 143 | "{term, select, mr {#{number} merge request {state, select, pending {status pending} draft {draft} open {open} merged {merged} other {closed}}} cr {#{number} change request {state, select, pending {status pending} draft {draft} open {open} merged {merged} other {closed}}} other {#{number} pull request {state, select, pending {status pending} draft {draft} open {open} merged {merged} other {closed}}}}", |
| 144 | { |
| 145 | number: String(link.number), |
| 146 | term: changeRequestTermKind(changeRequest), |
| 147 | state: prStateKey(state === null ? null : isDraft ? "draft" : state), |
| 148 | }, |
| 149 | ), |
| 150 | ), |
| 151 | textClassName: state === null || isDraft ? "text-foreground-muted" : PR_STATE_TEXT_CLASS[state], |
| 152 | }; |
| 153 | } |
| 154 | |
| 155 | export function resolveThreadPrSource( |
| 156 | thread: Pick<EnvironmentThreadShell, "pullRequests" | "linkedPullRequest" | "branchPullRequest">, |
| 157 | /** False until the server config loads; the stored references serve until then. */ |
| 158 | supportsSnapshots: boolean, |
| 159 | resolve: MessageResolver = m, |
| 160 | ) { |
| 161 | const linkedPresentation = supportsSnapshots |
| 162 | ? presentThreadLinkedPullRequests(thread.pullRequests, resolve) |
| 163 | : null; |
| 164 | const pullRequestRef = |
| 165 | linkedPresentation !== null |
| 166 | ? null |
| 167 | : ((supportsSnapshots |
| 168 | ? thread.branchPullRequest |
| 169 | : (thread.linkedPullRequest ?? thread.branchPullRequest)) ?? null); |
| 170 | return { linkedPresentation, pullRequestRef }; |
| 171 | } |
| 172 | |
| 173 | export function resolveThreadPrBadgeAction(pr: ThreadPrPresentation, resolve: MessageResolver = m) { |
| 174 | return pr.kind === "stack" || pr.others > 0 |
| 175 | ? { |
| 176 | kind: "list" as const, |
| 177 | accessibilityLabel: resolve(msg("Show {label}", { label: pr.accessibilityLabel })), |
| 178 | } |
| 179 | : { |
| 180 | kind: "open" as const, |
| 181 | accessibilityLabel: resolve(msg("Open {label}", { label: pr.accessibilityLabel })), |
| 182 | }; |
| 183 | } |
| 184 | |
| 185 | /** Records `value` as the newest entry, evicting the least recently answered beyond `limit`. */ |
| 186 | export function rememberBounded<V>( |
| 187 | current: ReadonlyMap<string, V>, |
| 188 | key: string, |
| 189 | value: V, |
| 190 | limit: number, |
| 191 | ): ReadonlyMap<string, V> { |
| 192 | const next = new Map(current); |
| 193 | next.delete(key); |
| 194 | next.set(key, value); |
| 195 | for (const oldest of next.keys()) { |
| 196 | if (next.size <= limit) break; |
| 197 | next.delete(oldest); |
| 198 | } |
| 199 | return next; |
| 200 | } |
| 201 | |
| 202 | /** The newest summary held for `pullRequest`, matched strictly by host, repository and number. */ |
| 203 | export function knownPullRequestSummary( |
| 204 | pullRequest: ThreadPullRequestKey, |
| 205 | threads: Iterable<Pick<EnvironmentThreadShell, "projectId" | "pullRequests">>, |
| 206 | summaries: Iterable<PullRequestSummary>, |
| 207 | ): PullRequestSummary | undefined { |
| 208 | const key = threadPullRequestKeyOf(pullRequest); |
| 209 | let newest: |
| 210 | | { readonly summary: PullRequestSummary; readonly updatedAt: number; readonly readAt: number } |
| 211 | | undefined; |
| 212 | // Checks and reviews change without moving `updatedAt`, so equal versions prefer the later read. |
| 213 | const consider = (summary: PullRequestSummary, readAt = Number.NEGATIVE_INFINITY) => { |
| 214 | const updatedAt = Date.parse(summary.updatedAt); |
| 215 | if ( |
| 216 | newest === undefined || |
| 217 | updatedAt > newest.updatedAt || |
| 218 | (updatedAt === newest.updatedAt && readAt > newest.readAt) |
| 219 | ) { |
| 220 | newest = { summary, updatedAt, readAt }; |
| 221 | } |
| 222 | }; |
| 223 | for (const summary of summaries) { |
| 224 | if (threadPullRequestKeyOf(legacyThreadPullRequestKey(summary)) === key) { |
| 225 | consider(summary, summary.observedAt); |
| 226 | } |
| 227 | } |
| 228 | for (const thread of threads) { |
| 229 | for (const link of thread.pullRequests) { |
| 230 | const snapshot = link.snapshot; |
| 231 | if (snapshot?.updatedAt == null || threadPullRequestKeyOf(link) !== key) continue; |
| 232 | consider( |
| 233 | { |
| 234 | ...snapshot, |
| 235 | updatedAt: snapshot.updatedAt, |
| 236 | provider: ( |
| 237 | sourceControlClients.findByChangeRequestUrl(link.url) ?? |
| 238 | sourceControlClients.get(undefined) |
| 239 | ).kind, |
| 240 | projectId: thread.projectId, |
| 241 | repository: link.repository, |
| 242 | number: link.number, |
| 243 | url: link.url, |
| 244 | }, |
| 245 | Date.parse(snapshot.syncedAt), |
| 246 | ); |
| 247 | } |
| 248 | } |
| 249 | return newest?.summary; |
| 250 | } |