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 { useMemo } from "react"; |
| 2 | import { EnvironmentId } from "@t3tools/contracts"; |
| 3 | import * as Option from "effect/Option"; |
| 4 | |
| 5 | import { environmentCatalog } from "../../connection/catalog"; |
| 6 | import { appAtomRegistry } from "../../state/atom-registry"; |
| 7 | import { composerPullRequests } from "../../state/pull-requests"; |
| 8 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 9 | import type { NativeReviewDiffFile } from "../diffs/nativeReviewDiffTypes"; |
| 10 | import { diffHighlightTraceHash, traceDiffHighlight } from "../diffs/nativeReviewDiffTrace"; |
| 11 | import type { ReviewParsedDiff } from "../review/reviewModel"; |
| 12 | import { |
| 13 | createPullRequestDiffHighlightCache, |
| 14 | createPullRequestDiffHighlightFiles, |
| 15 | } from "./pullRequestDiffHighlightFiles"; |
| 16 | import { pullRequestTarget, type PullRequestDiffParams } from "./usePullRequestDiff"; |
| 17 | |
| 18 | function traceSourceEndpoint(environmentId: string, url: string) { |
| 19 | try { |
| 20 | const endpoint = new URL(url); |
| 21 | traceDiffHighlight("source-endpoint", { |
| 22 | environment: diffHighlightTraceHash(environmentId), |
| 23 | origin: diffHighlightTraceHash(endpoint.origin), |
| 24 | port: endpoint.port || (endpoint.protocol === "https:" ? "443" : "80"), |
| 25 | }); |
| 26 | } catch { |
| 27 | traceDiffHighlight("source-endpoint-invalid"); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | export function usePullRequestDiffHighlightFiles( |
| 32 | params: PullRequestDiffParams, |
| 33 | parsed: ReviewParsedDiff, |
| 34 | files: ReadonlyArray<NativeReviewDiffFile>, |
| 35 | ) { |
| 36 | const fetchContents = useAtomCommand(composerPullRequests.diffFileContents, { |
| 37 | reportFailure: false, |
| 38 | }); |
| 39 | const { environmentId, projectId, repository, number, host, commit } = params; |
| 40 | const cache = createPullRequestDiffHighlightCache(); |
| 41 | return useMemo(() => { |
| 42 | if (process.env.EXPO_PUBLIC_T3_DIFF_TRACE === "1") { |
| 43 | const catalog = appAtomRegistry.get(environmentCatalog.catalogValueAtom); |
| 44 | const entry = catalog.entries.get(EnvironmentId.make(environmentId)); |
| 45 | const profile = entry && Option.getOrUndefined(entry.profile); |
| 46 | const url = |
| 47 | entry?.target._tag === "PrimaryConnectionTarget" |
| 48 | ? entry.target.httpBaseUrl |
| 49 | : profile?._tag === "BearerConnectionProfile" |
| 50 | ? profile.httpBaseUrl |
| 51 | : undefined; |
| 52 | if (url) { |
| 53 | traceSourceEndpoint(environmentId, url); |
| 54 | } else traceDiffHighlight("source-endpoint-unavailable"); |
| 55 | } |
| 56 | return createPullRequestDiffHighlightFiles( |
| 57 | { |
| 58 | target: pullRequestTarget({ environmentId, projectId, repository, number, host }), |
| 59 | commit, |
| 60 | }, |
| 61 | parsed, |
| 62 | files, |
| 63 | fetchContents, |
| 64 | cache, |
| 65 | ); |
| 66 | }, [ |
| 67 | environmentId, |
| 68 | projectId, |
| 69 | repository, |
| 70 | number, |
| 71 | host, |
| 72 | commit, |
| 73 | parsed, |
| 74 | files, |
| 75 | fetchContents, |
| 76 | cache, |
| 77 | ]); |
| 78 | } |