|
1 | | -import { useMemo } from "react"; |
2 | 1 | import { sourceControlRepositorySelector } from "@t3tools/shared/sourceControl"; |
3 | 2 | import type { |
4 | 3 | EnvironmentId, |
@@ -48,61 +47,57 @@ export function usePullRequestLinking(environmentId: EnvironmentId | null | unde |
48 | 47 | const link = useAtomCommand(threadEnvironment.linkPullRequest, { reportFailure: false }); |
49 | 48 | const unlink = useAtomCommand(threadEnvironment.unlinkPullRequest, { reportFailure: false }); |
50 | 49 | 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); |
| 50 | + const environmentProjects = projects.filter((project) => project.environmentId === environmentId); |
| 51 | + const canLink = (url: string) => { |
| 52 | + const parsed = parseChangeRequestUrl(url); |
| 53 | + return parsed !== null && canLinkChangeRequest(mode, environmentProjects, parsed); |
| 54 | + }; |
| 55 | + const isLinked = ( |
| 56 | + thread: { |
| 57 | + readonly pullRequests?: readonly ThreadPullRequestLink[]; |
| 58 | + readonly linkedPullRequest?: ThreadLinkedPullRequest | null | undefined; |
| 59 | + } | null, |
| 60 | + url: string, |
| 61 | + ) => { |
| 62 | + if (thread === null || mode === "unsupported") return false; |
| 63 | + if (mode !== "multiple") |
73 | 64 | return ( |
74 | | - parsed !== null && |
75 | | - visibleThreadPullRequests(thread.pullRequests ?? []).some((entry) => |
76 | | - threadPullRequestKeysEqual(entry, parsed), |
77 | | - ) |
| 65 | + thread.linkedPullRequest != null && |
| 66 | + matchesLinkedPullRequestUrl(thread.linkedPullRequest, url) |
78 | 67 | ); |
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]); |
| 68 | + const parsed = parseChangeRequestUrl(url); |
| 69 | + return ( |
| 70 | + parsed !== null && |
| 71 | + visibleThreadPullRequests(thread.pullRequests ?? []).some((entry) => |
| 72 | + threadPullRequestKeysEqual(entry, parsed), |
| 73 | + ) |
| 74 | + ); |
| 75 | + }; |
| 76 | + const changeLink = async (threadRef: ScopedThreadRef, url: string, linked: boolean) => { |
| 77 | + const parsed = parseChangeRequestUrl(url); |
| 78 | + if (parsed === null || threadRef.environmentId !== environmentId || (linked && !canLink(url))) |
| 79 | + throw new Error("The pull request is not available in this environment."); |
| 80 | + const legacyProject = findProjectForChangeRequest(environmentProjects, parsed); |
| 81 | + const mutation = planThreadPullRequestMutation({ |
| 82 | + capabilities, |
| 83 | + threadId: threadRef.threadId, |
| 84 | + reference: { ...parsed, url }, |
| 85 | + legacyProjectId: legacyProject?.id ?? null, |
| 86 | + legacyRepository: |
| 87 | + sourceControlRepositorySelector(legacyProject?.repositoryIdentity) ?? undefined, |
| 88 | + linked, |
| 89 | + }); |
| 90 | + if (mutation === null) |
| 91 | + throw new Error("This environment does not support linking this pull request."); |
| 92 | + const result = await (mutation.type === "thread.meta.update" |
| 93 | + ? updateMetadata({ environmentId: threadRef.environmentId, input: mutation.input }) |
| 94 | + : mutation.type === "thread.pull-request.link" |
| 95 | + ? link({ environmentId: threadRef.environmentId, input: mutation.input }) |
| 96 | + : unlink({ environmentId: threadRef.environmentId, input: mutation.input })); |
| 97 | + if (result._tag === "Failure") { |
| 98 | + if (isAtomCommandInterrupted(result)) throw new Error("Link update interrupted."); |
| 99 | + throw squashAtomCommandFailure(result); |
| 100 | + } |
| 101 | + }; |
| 102 | + return { mode, canLink, isLinked, changeLink }; |
108 | 103 | } |
0 commit comments