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

usePullRequestLinking.ts

108 lines4.5 KB

1import { useMemo } from "react";
2import { sourceControlRepositorySelector } from "@t3tools/shared/sourceControl";
3import type {
4 EnvironmentId,
5 ScopedThreadRef,
6 ThreadLinkedPullRequest,
7 ThreadPullRequestLink,
8} from "@t3tools/contracts";
9import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
10import {
11 isAtomCommandInterrupted,
12 squashAtomCommandFailure,
13} from "@t3tools/client-runtime/state/runtime";
14import {
15 planThreadPullRequestMutation,
16 threadPullRequestLinkMode,
17} from "@t3tools/client-runtime/thread-pull-request-compatibility";
18import {
19 threadPullRequestKeysEqual,
20 visibleThreadPullRequests,
21} from "@t3tools/shared/threadPullRequests";
22import {
23 findProjectForChangeRequest,
24 matchesLinkedPullRequestUrl,
25 parseChangeRequestUrl,
26 type ChangeRequestLink,
27} from "~/lib/openPullRequestLink";
28import { useProjects, useServerConfigs } from "~/state/entities";
29import { threadEnvironment } from "~/state/threads";
30import { useAtomCommand } from "~/state/use-atom-command";
31​
32export function canLinkChangeRequest(
33 mode: ReturnType<typeof threadPullRequestLinkMode>,
34 projects: ReadonlyArray<EnvironmentProject>,
35 parsed: ChangeRequestLink,
36): boolean {
37 if (mode === "unsupported") return false;
38 if (mode === "multiple") return true;
39 return findProjectForChangeRequest(projects, parsed) !== undefined;
41​
42export function usePullRequestLinking(environmentId: EnvironmentId | null | undefined) {
43 const configs = useServerConfigs();
44 const projects = useProjects();
45 const capabilities =
46 environmentId == null ? undefined : configs.get(environmentId)?.environment.capabilities;
47 const mode = threadPullRequestLinkMode(capabilities);
48 const link = useAtomCommand(threadEnvironment.linkPullRequest, { reportFailure: false });
49 const unlink = useAtomCommand(threadEnvironment.unlinkPullRequest, { reportFailure: false });
50 const updateMetadata = useAtomCommand(threadEnvironment.updateMetadata, { reportFailure: false });
51 return useMemo(() => {
52 const environmentProjects = projects.filter(
53 (project) => project.environmentId === environmentId,
54 );
55 const canLink = (url: string) => {
56 const parsed = parseChangeRequestUrl(url);
57 return parsed !== null && canLinkChangeRequest(mode, environmentProjects, parsed);
58 };
59 const isLinked = (
60 thread: {
61 readonly pullRequests?: readonly ThreadPullRequestLink[];
62 readonly linkedPullRequest?: ThreadLinkedPullRequest | null | undefined;
63 } | null,
64 url: string,
65 ) => {
66 if (thread === null || mode === "unsupported") return false;
67 if (mode !== "multiple")
68 return (
69 thread.linkedPullRequest != null &&
70 matchesLinkedPullRequestUrl(thread.linkedPullRequest, url)
71 );
72 const parsed = parseChangeRequestUrl(url);
73 return (
74 parsed !== null &&
75 visibleThreadPullRequests(thread.pullRequests ?? []).some((entry) =>
76 threadPullRequestKeysEqual(entry, parsed),
77 )
78 );
79 };
80 const changeLink = async (threadRef: ScopedThreadRef, url: string, linked: boolean) => {
81 const parsed = parseChangeRequestUrl(url);
82 if (parsed === null || threadRef.environmentId !== environmentId || (linked && !canLink(url)))
83 throw new Error("The pull request is not available in this environment.");
84 const legacyProject = findProjectForChangeRequest(environmentProjects, parsed);
85 const mutation = planThreadPullRequestMutation({
86 capabilities,
87 threadId: threadRef.threadId,
88 reference: { ...parsed, url },
89 legacyProjectId: legacyProject?.id ?? null,
90 legacyRepository:
91 sourceControlRepositorySelector(legacyProject?.repositoryIdentity) ?? undefined,
92 linked,
93 });
94 if (mutation === null)
95 throw new Error("This environment does not support linking this pull request.");
96 const result = await (mutation.type === "thread.meta.update"
97 ? updateMetadata({ environmentId: threadRef.environmentId, input: mutation.input })
98 : mutation.type === "thread.pull-request.link"
99 ? link({ environmentId: threadRef.environmentId, input: mutation.input })
100 : unlink({ environmentId: threadRef.environmentId, input: mutation.input }));
101 if (result._tag === "Failure") {
102 if (isAtomCommandInterrupted(result)) throw new Error("Link update interrupted.");
103 throw squashAtomCommandFailure(result);
104 }
105 };
106 return { mode, canLink, isLinked, changeLink };
107 }, [capabilities, environmentId, link, mode, projects, unlink, updateMetadata]);