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

usePullRequestLinking.ts

117 lines4.9 KB

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