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 { orderDiffFilesForReview } from "@t3tools/client-runtime/diff-file-order"; |
| 2 | import type { ChangeRequestTerminology } from "@t3tools/client-runtime/source-control-clients"; |
| 3 | import { changeRequestTermKind } from "../../lib/changeRequestTerm"; |
| 4 | import type { PullRequestDiffResult } from "@t3tools/contracts"; |
| 5 | import { msg } from "gt-react-native"; |
| 6 | |
| 7 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 8 | import { |
| 9 | buildReviewParsedDiff, |
| 10 | type ReviewParsedDiff, |
| 11 | type ReviewRenderableFile, |
| 12 | } from "../review/reviewModel"; |
| 13 | |
| 14 | export interface PullRequestDiffPage { |
| 15 | readonly cursor: string | null; |
| 16 | readonly result: PullRequestDiffResult; |
| 17 | } |
| 18 | |
| 19 | interface CachedPullRequestDiffPages { |
| 20 | readonly pages: readonly PullRequestDiffPage[]; |
| 21 | readonly cache: WeakMap<PullRequestDiffResult, ReviewParsedDiff>; |
| 22 | readonly parsed: ReviewParsedDiff; |
| 23 | } |
| 24 | |
| 25 | // Keep only the latest combination per scoped first page; query results own its lifetime. |
| 26 | const parsedSnapshots = new WeakMap< |
| 27 | PullRequestDiffResult, |
| 28 | Map<string, CachedPullRequestDiffPages> |
| 29 | >(); |
| 30 | |
| 31 | export function getCachedPullRequestDiffPages( |
| 32 | pages: readonly PullRequestDiffPage[], |
| 33 | scope: string, |
| 34 | terminology: ChangeRequestTerminology, |
| 35 | resolve: MessageResolver = m, |
| 36 | locale = "", |
| 37 | ): ReviewParsedDiff { |
| 38 | const first = pages[0]; |
| 39 | if (!first) return { kind: "empty" }; |
| 40 | const key = JSON.stringify([scope, terminology.singular, first.cursor, locale]); |
| 41 | let snapshots = parsedSnapshots.get(first.result); |
| 42 | if (!snapshots) { |
| 43 | snapshots = new Map(); |
| 44 | parsedSnapshots.set(first.result, snapshots); |
| 45 | } |
| 46 | const previous = snapshots.get(key); |
| 47 | if ( |
| 48 | previous?.pages.length === pages.length && |
| 49 | pages.every((page, index) => { |
| 50 | const cached = previous.pages[index]; |
| 51 | return cached?.cursor === page.cursor && cached.result === page.result; |
| 52 | }) |
| 53 | ) |
| 54 | return previous.parsed; |
| 55 | const cache = previous?.cache ?? new WeakMap<PullRequestDiffResult, ReviewParsedDiff>(); |
| 56 | const parsed = combinePullRequestDiffPages(pages, cache, scope, terminology, resolve); |
| 57 | snapshots.set(key, { pages, cache, parsed }); |
| 58 | return parsed; |
| 59 | } |
| 60 | |
| 61 | /** A changed page invalidates later cursors, which belong to the previous patch. */ |
| 62 | export function retainPullRequestDiffPage( |
| 63 | pages: readonly PullRequestDiffPage[], |
| 64 | cursor: string | null, |
| 65 | result: PullRequestDiffResult, |
| 66 | ): readonly PullRequestDiffPage[] { |
| 67 | const index = pages.findIndex((page) => page.cursor === cursor); |
| 68 | if (index === -1) return [...pages, { cursor, result }]; |
| 69 | const previous = pages[index]?.result; |
| 70 | if ( |
| 71 | previous === result || |
| 72 | (previous?.patch === result.patch && |
| 73 | previous.truncated === result.truncated && |
| 74 | previous.nextCursor === result.nextCursor && |
| 75 | previous.withheldFilesReadable === result.withheldFilesReadable && |
| 76 | JSON.stringify(previous.omittedFileStats ?? []) === |
| 77 | JSON.stringify(result.omittedFileStats ?? [])) |
| 78 | ) |
| 79 | return pages; |
| 80 | return [...pages.slice(0, index), { cursor, result }]; |
| 81 | } |
| 82 | |
| 83 | export function combinePullRequestDiffPages( |
| 84 | pages: readonly PullRequestDiffPage[], |
| 85 | cache: WeakMap<PullRequestDiffResult, ReviewParsedDiff>, |
| 86 | scope: string, |
| 87 | terminology: ChangeRequestTerminology, |
| 88 | resolve: MessageResolver = m, |
| 89 | ): ReviewParsedDiff { |
| 90 | const parsed = pages.map((page) => { |
| 91 | const cached = cache.get(page.result); |
| 92 | if (cached) return cached; |
| 93 | let value = buildReviewParsedDiff( |
| 94 | page.result.patch, |
| 95 | `${scope}:${page.cursor ?? "first"}`, |
| 96 | resolve, |
| 97 | ); |
| 98 | if (value.kind === "files" && page.result.omittedFileStats?.length) { |
| 99 | const omittedFileStats = new Map( |
| 100 | page.result.omittedFileStats.map((stat) => [stat.path, stat]), |
| 101 | ); |
| 102 | const files = value.files.map((file) => { |
| 103 | const stat = omittedFileStats.get(file.path); |
| 104 | if (!stat) return file; |
| 105 | return { |
| 106 | ...file, |
| 107 | additions: stat.additions, |
| 108 | deletions: stat.deletions, |
| 109 | ...(file.rows.length === 0 ? { withheld: true } : {}), |
| 110 | }; |
| 111 | }); |
| 112 | value = { |
| 113 | ...value, |
| 114 | files, |
| 115 | additions: files.reduce((total, file) => total + file.additions, 0), |
| 116 | deletions: files.reduce((total, file) => total + file.deletions, 0), |
| 117 | }; |
| 118 | } |
| 119 | cache.set(page.result, value); |
| 120 | return value; |
| 121 | }); |
| 122 | const files = orderDiffFilesForReview( |
| 123 | parsed.flatMap((page) => (page.kind === "files" ? page.files : [])), |
| 124 | (file) => file.path, |
| 125 | ); |
| 126 | if (files.length === 0) return parsed[0] ?? { kind: "empty" }; |
| 127 | return { |
| 128 | kind: "files", |
| 129 | files, |
| 130 | fileCount: files.length, |
| 131 | additions: files.reduce((total, file) => total + file.additions, 0), |
| 132 | deletions: files.reduce((total, file) => total + file.deletions, 0), |
| 133 | notice: |
| 134 | pages.some((page) => page.result.truncated) || |
| 135 | parsed.some((page) => page.kind === "raw" || (page.kind === "files" && page.notice !== null)) |
| 136 | ? resolve( |
| 137 | msg( |
| 138 | "Some file contents are unavailable. Open the {term, select, mr {merge request} cr {change request} other {pull request}} on its provider to see the full changes.", |
| 139 | { term: changeRequestTermKind(terminology) }, |
| 140 | ), |
| 141 | ) |
| 142 | : null, |
| 143 | }; |
| 144 | } |
| 145 | |
| 146 | export type WithheldFileLoad = |
| 147 | | { readonly status: "loading" | "error" } |
| 148 | | { readonly status: "loaded"; readonly result: PullRequestDiffResult }; |
| 149 | |
| 150 | const withheldFileParses = new WeakMap< |
| 151 | PullRequestDiffResult, |
| 152 | { readonly locale: string; readonly file: ReviewRenderableFile | null } |
| 153 | >(); |
| 154 | |
| 155 | function parseWithheldFile( |
| 156 | result: PullRequestDiffResult, |
| 157 | scope: string, |
| 158 | resolve: MessageResolver, |
| 159 | locale: string, |
| 160 | ) { |
| 161 | const cached = withheldFileParses.get(result); |
| 162 | if (cached?.locale === locale) return cached.file; |
| 163 | const parsed = buildReviewParsedDiff(result.patch, scope, resolve); |
| 164 | const file = |
| 165 | parsed.kind === "files" ? (parsed.files.find((entry) => entry.rows.length > 0) ?? null) : null; |
| 166 | withheldFileParses.set(result, { locale, file }); |
| 167 | return file; |
| 168 | } |
| 169 | |
| 170 | function withheldFileNotice( |
| 171 | load: WithheldFileLoad | undefined, |
| 172 | canLoad: boolean, |
| 173 | resolve: MessageResolver, |
| 174 | ): string { |
| 175 | if (load === undefined) { |
| 176 | return canLoad |
| 177 | ? resolve(msg("Diff too large to display. Tap to load it.")) |
| 178 | : resolve(msg("Diff too large to display here.")); |
| 179 | } |
| 180 | if (load.status === "loading") return resolve(msg("Loading diff…")); |
| 181 | if (load.status === "error") return resolve(msg("Couldn't load this diff. Tap to try again.")); |
| 182 | return resolve(msg("No text diff for this file.")); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Puts each loaded withheld file in place of its empty one, under the same id so folds and |
| 187 | * targets hold, and gives the rest a notice saying what a tap does. |
| 188 | */ |
| 189 | export function withWithheldFiles( |
| 190 | parsed: ReviewParsedDiff, |
| 191 | loads: ReadonlyMap<string, WithheldFileLoad>, |
| 192 | canLoad: boolean, |
| 193 | scope: string, |
| 194 | resolve: MessageResolver = m, |
| 195 | locale = "", |
| 196 | ): ReviewParsedDiff { |
| 197 | if (parsed.kind !== "files" || !parsed.files.some((file) => file.withheld)) return parsed; |
| 198 | const files = parsed.files.map((file): ReviewRenderableFile => { |
| 199 | if (!file.withheld) return file; |
| 200 | const load = loads.get(file.path); |
| 201 | const loaded = |
| 202 | load?.status === "loaded" |
| 203 | ? parseWithheldFile(load.result, `${scope}:withheld:${file.path}`, resolve, locale) |
| 204 | : null; |
| 205 | if (loaded !== null) return { ...loaded, id: file.id }; |
| 206 | return { |
| 207 | ...file, |
| 208 | notice: withheldFileNotice(load, canLoad, resolve), |
| 209 | noticeTone: "warning", |
| 210 | noticeActionable: canLoad && (load === undefined || load.status === "error"), |
| 211 | }; |
| 212 | }); |
| 213 | return { |
| 214 | ...parsed, |
| 215 | files, |
| 216 | additions: files.reduce((total, file) => total + file.additions, 0), |
| 217 | deletions: files.reduce((total, file) => total + file.deletions, 0), |
| 218 | }; |
| 219 | } |