The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useMessages } from "gt-react"; |
| 2 | import type { FileDiffMetadata } from "@pierre/diffs"; |
| 3 | import type { EnvironmentId, PullRequestRef } from "@t3tools/contracts"; |
| 4 | import { RegistryContext } from "@effect/atom-react"; |
| 5 | import { AsyncResult } from "effect/reactivity"; |
| 6 | import { useContext, useState } from "react"; |
| 7 | |
| 8 | import { getRenderablePatch, resolveFileDiffPath } from "~/lib/diffRendering"; |
| 9 | import { pullRequestEnvironment } from "~/state/pullRequests"; |
| 10 | import { useEnvironmentQuery } from "~/state/query"; |
| 11 | |
| 12 | import { pullRequestReviewKey } from "../pullRequestReviewStore"; |
| 13 | |
| 14 | interface LoadedDiff { |
| 15 | readonly key: string; |
| 16 | readonly cursor: string | null; |
| 17 | readonly done: boolean; |
| 18 | readonly files: ReadonlyMap<string, FileDiffMetadata>; |
| 19 | readonly folded: unknown; |
| 20 | } |
| 21 | |
| 22 | const NO_FILES: ReadonlyMap<string, FileDiffMetadata> = new Map(); |
| 23 | |
| 24 | export function useGuideDiffFiles({ |
| 25 | environmentId, |
| 26 | reference, |
| 27 | headSha, |
| 28 | wantedPaths, |
| 29 | }: { |
| 30 | environmentId: EnvironmentId; |
| 31 | reference: PullRequestRef; |
| 32 | headSha: string; |
| 33 | wantedPaths: ReadonlyArray<string>; |
| 34 | }) { |
| 35 | const key = `${environmentId}:${pullRequestReviewKey(reference)}:${headSha}`; |
| 36 | const [loaded, setLoaded] = useState<LoadedDiff>({ |
| 37 | key, |
| 38 | cursor: null, |
| 39 | done: false, |
| 40 | files: NO_FILES, |
| 41 | folded: null, |
| 42 | }); |
| 43 | const current: LoadedDiff = |
| 44 | loaded.key === key ? loaded : { key, cursor: null, done: false, files: NO_FILES, folded: null }; |
| 45 | const registry = useContext(RegistryContext); |
| 46 | const isMissing = (diff: LoadedDiff) => wantedPaths.some((path) => !diff.files.has(path)); |
| 47 | const pageAtom = (cursor: string | null) => |
| 48 | pullRequestEnvironment.diff({ |
| 49 | environmentId, |
| 50 | input: { ...reference, cacheHeadSha: headSha, ...(cursor === null ? {} : { cursor }) }, |
| 51 | }); |
| 52 | const loading = isMissing(current) && !current.done; |
| 53 | const query = useEnvironmentQuery(loading ? pageAtom(current.cursor) : null, useMessages()); |
| 54 | |
| 55 | // One update for every settled page: an update per page trips React's re-render limit. |
| 56 | let next = current; |
| 57 | let data = query.isPending ? null : query.data; |
| 58 | while (data !== null && !next.done && next.folded !== data && isMissing(next)) { |
| 59 | const parsed = getRenderablePatch( |
| 60 | data.patch, |
| 61 | `pull-request-guide:${key}:${next.cursor ?? "first"}`, |
| 62 | ); |
| 63 | const files = new Map(next.files); |
| 64 | if (parsed?.kind === "files") { |
| 65 | for (const file of parsed.files) files.set(resolveFileDiffPath(file), file); |
| 66 | } |
| 67 | next = { key, cursor: data.nextCursor, done: data.nextCursor === null, files, folded: data }; |
| 68 | const result = next.done || !isMissing(next) ? null : registry.get(pageAtom(next.cursor)); |
| 69 | data = result === null || result.waiting ? null : AsyncResult.getOrElse(result, () => null); |
| 70 | } |
| 71 | if (next !== current) setLoaded(next); |
| 72 | |
| 73 | return { |
| 74 | files: current.files, |
| 75 | loading: loading && query.error === null, |
| 76 | error: loading ? query.error : null, |
| 77 | retry: query.refresh, |
| 78 | }; |
| 79 | } |