Open History to see the last commit
1import { useMemo } from "react";
2import { EnvironmentId } from "@t3tools/contracts";
3import * as Option from "effect/Option";
4
5import { environmentCatalog } from "../../connection/catalog";
6import { appAtomRegistry } from "../../state/atom-registry";
7import { composerPullRequests } from "../../state/pull-requests";
8import { useAtomCommand } from "../../state/use-atom-command";
9import type { NativeReviewDiffFile } from "../diffs/nativeReviewDiffTypes";
10import { diffHighlightTraceHash, traceDiffHighlight } from "../diffs/nativeReviewDiffTrace";
11import type { ReviewParsedDiff } from "../review/reviewModel";
12import {
13 createPullRequestDiffHighlightCache,
14 createPullRequestDiffHighlightFiles,
15} from "./pullRequestDiffHighlightFiles";
16import { pullRequestTarget, type PullRequestDiffParams } from "./usePullRequestDiff";
17
18function 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
31export 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}