The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | squashAtomCommandFailure, |
| 3 | type AtomCommandResult, |
| 4 | } from "@t3tools/client-runtime/state/runtime"; |
| 5 | import type { |
| 6 | PullRequestDiffFileContentsInput, |
| 7 | PullRequestDiffFileContentsResult, |
| 8 | } from "@t3tools/contracts"; |
| 9 | |
| 10 | import { createNativeReviewDiffSourceLoader } from "../diffs/nativeReviewDiffSource"; |
| 11 | import { |
| 12 | diffHighlightTraceHash, |
| 13 | diffHighlightTraceId, |
| 14 | traceDiffHighlight, |
| 15 | } from "../diffs/nativeReviewDiffTrace"; |
| 16 | import type { NativeReviewDiffFile, NativeReviewDiffSource } from "../diffs/nativeReviewDiffTypes"; |
| 17 | import type { ReviewParsedDiff, ReviewRenderableFile } from "../review/reviewModel"; |
| 18 | import type { pullRequestTarget } from "./usePullRequestDiff"; |
| 19 | |
| 20 | type FetchContents<E> = (request: { |
| 21 | readonly environmentId: ReturnType<typeof pullRequestTarget>["environmentId"]; |
| 22 | readonly input: PullRequestDiffFileContentsInput; |
| 23 | }) => Promise<AtomCommandResult<PullRequestDiffFileContentsResult, E>>; |
| 24 | |
| 25 | type SourceRequest = () => Promise<NativeReviewDiffSource>; |
| 26 | |
| 27 | interface SourceSnapshot { |
| 28 | readonly firstFile: ReviewRenderableFile; |
| 29 | readonly scope: string; |
| 30 | readonly load: ReturnType<typeof createNativeReviewDiffSourceLoader<SourceRequest>>; |
| 31 | readonly requests: WeakMap< |
| 32 | ReviewRenderableFile, |
| 33 | { readonly completePatch: boolean; readonly request: SourceRequest } |
| 34 | >; |
| 35 | } |
| 36 | |
| 37 | export function createPullRequestDiffHighlightCache(): { snapshot?: SourceSnapshot } { |
| 38 | return {}; |
| 39 | } |
| 40 | |
| 41 | export function createPullRequestDiffHighlightFiles<E>( |
| 42 | source: { |
| 43 | readonly target: ReturnType<typeof pullRequestTarget>; |
| 44 | readonly commit?: string; |
| 45 | }, |
| 46 | parsed: ReviewParsedDiff, |
| 47 | files: ReadonlyArray<NativeReviewDiffFile>, |
| 48 | fetchContents: FetchContents<E>, |
| 49 | cache = createPullRequestDiffHighlightCache(), |
| 50 | ): ReadonlyArray<NativeReviewDiffFile> { |
| 51 | if (parsed.kind !== "files" || !parsed.files[0]) return files; |
| 52 | const byId = new Map(parsed.files.map((file) => [file.id, file])); |
| 53 | const { target, commit } = source; |
| 54 | traceDiffHighlight("source-target", { |
| 55 | environment: diffHighlightTraceHash(target.environmentId), |
| 56 | project: diffHighlightTraceHash(target.input.projectId), |
| 57 | pr: target.input.number, |
| 58 | gitcafe: target.input.host === "git.cafe", |
| 59 | }); |
| 60 | const scope = JSON.stringify([ |
| 61 | target.environmentId, |
| 62 | target.input.projectId, |
| 63 | target.input.repository, |
| 64 | target.input.number, |
| 65 | target.input.host, |
| 66 | commit, |
| 67 | ]); |
| 68 | const firstFile = parsed.files[0]; |
| 69 | // Appended pages share one loader budget; a new first file starts a new snapshot. |
| 70 | let snapshot = cache.snapshot; |
| 71 | if (!snapshot || snapshot.firstFile !== firstFile || snapshot.scope !== scope) { |
| 72 | snapshot = { |
| 73 | firstFile, |
| 74 | scope, |
| 75 | load: createNativeReviewDiffSourceLoader<SourceRequest>((request) => request()), |
| 76 | requests: new WeakMap(), |
| 77 | }; |
| 78 | cache.snapshot = snapshot; |
| 79 | } |
| 80 | const { load, requests } = snapshot; |
| 81 | return files.map((nativeFile) => { |
| 82 | const file = byId.get(nativeFile.id); |
| 83 | if (!file) return nativeFile; |
| 84 | const completePatch = parsed.notice === null && !!file.completePatchSource; |
| 85 | const cached = requests.get(file); |
| 86 | if (cached?.completePatch === completePatch) |
| 87 | return { ...nativeFile, loadContents: (signal) => load(cached.request, signal) }; |
| 88 | const request: SourceRequest = async () => { |
| 89 | if (completePatch && file.completePatchSource) return file.completePatchSource(); |
| 90 | traceDiffHighlight("source-params", { |
| 91 | file: diffHighlightTraceId(file.id), |
| 92 | oldPath: diffHighlightTraceHash(file.previousPath ?? file.path), |
| 93 | newPath: diffHighlightTraceHash(file.path), |
| 94 | changeType: file.changeType, |
| 95 | hasCommit: !!commit, |
| 96 | }); |
| 97 | const result = await fetchContents({ |
| 98 | ...target, |
| 99 | input: { |
| 100 | ...target.input, |
| 101 | ...(commit ? { commit } : {}), |
| 102 | changeType: file.changeType, |
| 103 | oldPath: file.previousPath ?? file.path, |
| 104 | newPath: file.path, |
| 105 | }, |
| 106 | }); |
| 107 | if (result._tag !== "Success") throw squashAtomCommandFailure(result); |
| 108 | return result.value; |
| 109 | }; |
| 110 | requests.set(file, { completePatch, request }); |
| 111 | return { ...nativeFile, loadContents: (signal) => load(request, signal) }; |
| 112 | }); |
| 113 | } |