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