TypeScript
perf(web): the pull request linking hook compiles without manual memoization (#20681)
Julius Marminge2a337fea92f3
diff --git a/apps/web/src/hooks/usePullRequestLinking.ts b/apps/web/src/hooks/usePullRequestLinking.tsindex e3249e90888..be2a94fb4c7 100644--- a/apps/web/src/hooks/usePullRequestLinking.ts+++ b/apps/web/src/hooks/usePullRequestLinking.ts@@ -1,4 +1,3 @@-import { useMemo } from "react";import { sourceControlRepositorySelector } from "@t3tools/shared/sourceControl";import type {EnvironmentId,@@ -48,61 +47,57 @@ export function usePullRequestLinking(environmentId: EnvironmentId | null | undeconst link = useAtomCommand(threadEnvironment.linkPullRequest, { reportFailure: false });const unlink = useAtomCommand(threadEnvironment.unlinkPullRequest, { reportFailure: false });const updateMetadata = useAtomCommand(threadEnvironment.updateMetadata, { reportFailure: false });- return useMemo(() => {- const environmentProjects = projects.filter(- (project) => project.environmentId === environmentId,- );- const canLink = (url: string) => {- const parsed = parseChangeRequestUrl(url);- return parsed !== null && canLinkChangeRequest(mode, environmentProjects, parsed);- };- const isLinked = (- thread: {- readonly pullRequests?: readonly ThreadPullRequestLink[];- readonly linkedPullRequest?: ThreadLinkedPullRequest | null | undefined;- } | null,- url: string,- ) => {- if (thread === null || mode === "unsupported") return false;- if (mode !== "multiple")- return (- thread.linkedPullRequest != null &&- matchesLinkedPullRequestUrl(thread.linkedPullRequest, url)- );- const parsed = parseChangeRequestUrl(url);+ const environmentProjects = projects.filter((project) => project.environmentId === environmentId);+ const canLink = (url: string) => {+ const parsed = parseChangeRequestUrl(url);+ return parsed !== null && canLinkChangeRequest(mode, environmentProjects, parsed);+ };+ const isLinked = (+ thread: {+ readonly pullRequests?: readonly ThreadPullRequestLink[];+ readonly linkedPullRequest?: ThreadLinkedPullRequest | null | undefined;+ } | null,+ url: string,+ ) => {+ if (thread === null || mode === "unsupported") return false;+ if (mode !== "multiple")return (- parsed !== null &&- visibleThreadPullRequests(thread.pullRequests ?? []).some((entry) =>- threadPullRequestKeysEqual(entry, parsed),- )+ thread.linkedPullRequest != null &&+ matchesLinkedPullRequestUrl(thread.linkedPullRequest, url));- };- const changeLink = async (threadRef: ScopedThreadRef, url: string, linked: boolean) => {- const parsed = parseChangeRequestUrl(url);- if (parsed === null || threadRef.environmentId !== environmentId || (linked && !canLink(url)))- throw new Error("The pull request is not available in this environment.");- const legacyProject = findProjectForChangeRequest(environmentProjects, parsed);- const mutation = planThreadPullRequestMutation({- capabilities,- threadId: threadRef.threadId,- reference: { ...parsed, url },- legacyProjectId: legacyProject?.id ?? null,- legacyRepository:- sourceControlRepositorySelector(legacyProject?.repositoryIdentity) ?? undefined,- linked,- });- if (mutation === null)- throw new Error("This environment does not support linking this pull request.");- const result = await (mutation.type === "thread.meta.update"- ? updateMetadata({ environmentId: threadRef.environmentId, input: mutation.input })- : mutation.type === "thread.pull-request.link"- ? link({ environmentId: threadRef.environmentId, input: mutation.input })- : unlink({ environmentId: threadRef.environmentId, input: mutation.input }));- if (result._tag === "Failure") {- if (isAtomCommandInterrupted(result)) throw new Error("Link update interrupted.");- throw squashAtomCommandFailure(result);- }- };- return { mode, canLink, isLinked, changeLink };- }, [capabilities, environmentId, link, mode, projects, unlink, updateMetadata]);+ const parsed = parseChangeRequestUrl(url);+ return (+ parsed !== null &&+ visibleThreadPullRequests(thread.pullRequests ?? []).some((entry) =>+ threadPullRequestKeysEqual(entry, parsed),+ )+ );+ };+ const changeLink = async (threadRef: ScopedThreadRef, url: string, linked: boolean) => {+ const parsed = parseChangeRequestUrl(url);+ if (parsed === null || threadRef.environmentId !== environmentId || (linked && !canLink(url)))+ throw new Error("The pull request is not available in this environment.");+ const legacyProject = findProjectForChangeRequest(environmentProjects, parsed);+ const mutation = planThreadPullRequestMutation({+ capabilities,+ threadId: threadRef.threadId,+ reference: { ...parsed, url },+ legacyProjectId: legacyProject?.id ?? null,+ legacyRepository:+ sourceControlRepositorySelector(legacyProject?.repositoryIdentity) ?? undefined,+ linked,+ });+ if (mutation === null)+ throw new Error("This environment does not support linking this pull request.");+ const result = await (mutation.type === "thread.meta.update"+ ? updateMetadata({ environmentId: threadRef.environmentId, input: mutation.input })+ : mutation.type === "thread.pull-request.link"+ ? link({ environmentId: threadRef.environmentId, input: mutation.input })+ : unlink({ environmentId: threadRef.environmentId, input: mutation.input }));+ if (result._tag === "Failure") {+ if (isAtomCommandInterrupted(result)) throw new Error("Link update interrupted.");+ throw squashAtomCommandFailure(result);+ }+ };+ return { mode, canLink, isLinked, changeLink };}