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

usePullRequestLinking.ts

119 lines5.0 KB

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