Open History to see the last commit
1import { scopeProjectRef } from "@t3tools/client-runtime/environment";
2import { EnvironmentId, ProjectId, type ScopedThreadRef } from "@t3tools/contracts";
3import { useMemo } from "react";
4
5import {
6 readPullRequestDetailSnapshot,
7 resolveDisplayedPullRequestDetail,
8} from "../components/pullRequest/pullRequestDetail.logic";
9import { gitHubPullRequestBrowserUrl } from "../lib/openPullRequestLink";
10import { selectActiveRightPanelSurface, useRightPanelStore } from "../rightPanelStore";
11import { useProject } from "../state/entities";
12import { pullRequestEnvironment } from "../state/pullRequests";
13import { useEnvironmentQuery } from "../state/query";
14
15export function useOpenPanelPullRequestUrl(threadRef: ScopedThreadRef | null) {
16 const surface = useRightPanelStore((state) =>
17 selectActiveRightPanelSurface(state.byThreadKey, threadRef),
18 );
19 const reference = surface?.kind === "pull-request" ? surface : null;
20 const environmentId = reference?.environmentId
21 ? EnvironmentId.make(reference.environmentId)
22 : threadRef?.environmentId;
23 const project = useProject(
24 reference && environmentId
25 ? scopeProjectRef(environmentId, ProjectId.make(reference.projectId))
26 : null,
27 );
28 const detail = useEnvironmentQuery(
29 reference && environmentId
30 ? pullRequestEnvironment.detail({
31 environmentId,
32 input: {
33 projectId: ProjectId.make(reference.projectId),
34 repository: reference.repository,
35 number: reference.number,
36 },
37 })
38 : null,
39 ).data;
40 const cachedDetail = useMemo(
41 () =>
42 reference && environmentId
43 ? readPullRequestDetailSnapshot(
44 typeof window === "undefined" ? undefined : window.localStorage,
45 environmentId,
46 reference,
47 )
48 : null,
49 [environmentId, reference],
50 );
51 return reference
52 ? (resolveDisplayedPullRequestDetail({ live: detail, cached: cachedDetail, reference })?.url ??
53 gitHubPullRequestBrowserUrl(
54 project?.repositoryIdentity,
55 reference.repository,
56 reference.number,
57 ))
58 : undefined;
59}