Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useOpenPanelPullRequestUrl.ts

89 lines3.3 KB

1import { scopeProjectRef } from "@t3tools/client-runtime/environment";
2import { EnvironmentId, ProjectId, type ScopedThreadRef } from "@t3tools/contracts";
3​
4import {
5 readPullRequestDetailSnapshot,
6 resolvePullRequestReferenceHost,
7} from "../components/pullRequest/pullRequestDetail.logic";
8import { gitHubPullRequestBrowserUrl } from "../lib/openPullRequestLink";
9import { selectActiveRightPanelSurface, useRightPanelStore } from "../rightPanelStore";
10import { useProject } from "../state/entities";
11import { pullRequestEnvironment } from "../state/pullRequests";
12import { useEnvironmentQuery } from "../state/query";
13import { useSupportsMultiplePullRequests } from "./useSupportsMultiplePullRequests";
14​
15type PanelPullRequestSurface = Extract<
16 NonNullable<ReturnType<typeof selectActiveRightPanelSurface>>,
17 { kind: "pull-request" }
18>;
19​
20function panelPullRequestReference(
21 requestedReference: PanelPullRequestSurface | null,
22 supportsMultiplePullRequests: boolean,
23 repositoryIdentity: Parameters<typeof resolvePullRequestReferenceHost>[1],
24) {
25 if (requestedReference === null) return null;
26 const input = {
27 projectId: ProjectId.make(requestedReference.projectId),
28 repository: requestedReference.repository,
29 number: requestedReference.number,
30 };
31 return supportsMultiplePullRequests
32 ? resolvePullRequestReferenceHost(
33 {
34 ...input,
35 ...(requestedReference.host === undefined ? {} : { host: requestedReference.host }),
36 },
37 repositoryIdentity,
38 )
39 : input;
41​
42export function useOpenPanelPullRequestUrl(threadRef: ScopedThreadRef | null) {
43 const surface = useRightPanelStore((state) =>
44 selectActiveRightPanelSurface(state.byThreadKey, threadRef),
45 );
46 const requestedReference = surface?.kind === "pull-request" ? surface : null;
47 const environmentId = requestedReference?.environmentId
48 ? EnvironmentId.make(requestedReference.environmentId)
49 : threadRef?.environmentId;
50 const supportsMultiplePullRequests = useSupportsMultiplePullRequests(environmentId ?? null);
51 const project = useProject(
52 requestedReference && environmentId
53 ? scopeProjectRef(environmentId, ProjectId.make(requestedReference.projectId))
54 : null,
55 );
56 const reference = panelPullRequestReference(
57 requestedReference,
58 supportsMultiplePullRequests,
59 project?.repositoryIdentity,
60 );
61 // Everything that reads reference or the project runs before the query hook; a plain
62 // call on either after it would leave reference, and the detail atom it keys, uncached.
63 // The snapshot is already checked against reference, so live detail just wins below.
64 const cachedDetail =
65 reference && environmentId
66 ? readPullRequestDetailSnapshot(
67 typeof window === "undefined" ? undefined : window.localStorage,
68 environmentId,
69 reference,
70 )
71 : null;
72 const fallbackUrl = reference
73 ? (requestedReference?.url ??
74 gitHubPullRequestBrowserUrl(
75 project?.repositoryIdentity,
76 reference.repository,
77 reference.number,
78 ))
79 : undefined;
80 const detail = useEnvironmentQuery(
81 reference && environmentId
82 ? pullRequestEnvironment.detail({
83 environmentId,
84 input: reference,
85 })
86 : null,
87 ).data;
88 return reference ? ((detail ?? cachedDetail)?.url ?? fallbackUrl) : undefined;