TypeScript
perf(web): the pull request thread dialog compiles without manual memoization (#20615)
Julius Marminged6df197b438a
diff --git a/apps/web/src/components/PullRequestThreadDialog.tsx b/apps/web/src/components/PullRequestThreadDialog.tsxindex 47e4b361aae..8b0bcd13ca6 100644--- a/apps/web/src/components/PullRequestThreadDialog.tsx+++ b/apps/web/src/components/PullRequestThreadDialog.tsx@@ -8,7 +8,7 @@ import {squashAtomCommandFailure,} from "@t3tools/client-runtime/state/runtime";import { useDebouncedValue } from "@tanstack/react-pacer";-import { useCallback, useEffect, useMemo, useRef, useState } from "react";+import { useEffect, useRef, useState } from "react";import {readCachedPullRequestResolution,@@ -44,6 +44,26 @@ interface PullRequestThreadDialogProps {onPrepared: (input: { branch: string; worktreePath: string | null }) => Promise<void> | void;}+// The prepare action keys on this scope, and the compiler would rebuild it every+// render because hooks run after it.+function useSourceControlScope(environmentId: EnvironmentId, cwd: string | null) {+ "use memo";+ return { environmentId, cwd };+}++function pullRequestStatusTone(state: string | undefined): string {+ switch (state) {+ case "merged":+ return "text-violet-600 dark:text-violet-300/90";+ case "closed":+ return "text-zinc-500 dark:text-zinc-400/80";+ case "open":+ return "text-emerald-600 dark:text-emerald-300/90";+ default:+ return "text-muted-foreground";+ }+}+export function PullRequestThreadDialog({open,environmentId,@@ -71,10 +91,7 @@ export function PullRequestThreadDialog({input: { cwd },}),);- const sourceControlPresentation = useMemo(- () => getSourceControlPresentation(gitStatus?.sourceControlProvider),- [gitStatus?.sourceControlProvider],- );+ const sourceControlPresentation = getSourceControlPresentation(gitStatus?.sourceControlProvider);const terminology = sourceControlPresentation.terminology;const SourceControlIcon = sourceControlPresentation.Icon;@@ -91,25 +108,16 @@ export function PullRequestThreadDialog({const parsedReference = parsePullRequestReference(reference);const parsedDebouncedReference = parsePullRequestReference(debouncedReference);- const sourceControlScope = useMemo(- () => ({- environmentId,- cwd,- }),- [cwd, environmentId],- );+ const sourceControlScope = useSourceControlScope(environmentId, cwd);const pullRequestResolution = usePullRequestResolution({...sourceControlScope,reference: open ? parsedDebouncedReference : null,});- const cachedPullRequest = useMemo(() => {- return (- readCachedPullRequestResolution({- ...sourceControlScope,- reference: parsedReference,- })?.pullRequest ?? null- );- }, [parsedReference, sourceControlScope]);+ const cachedPullRequest =+ readCachedPullRequestResolution({+ ...sourceControlScope,+ reference: parsedReference,+ })?.pullRequest ?? null;const preparePullRequestThreadAction = usePreparePullRequestThreadAction(sourceControlScope);const canOperateThread = useEnvironmentScope(environmentId, AuthOrchestrationOperateScope);@@ -126,74 +134,50 @@ export function PullRequestThreadDialog({parsedReference !== parsedDebouncedReference ||pullRequestResolution.isPending ||pullRequestResolution.isFetching);- const statusTone = useMemo(() => {- switch (resolvedPullRequest?.state) {- case "merged":- return "text-violet-600 dark:text-violet-300/90";- case "closed":- return "text-zinc-500 dark:text-zinc-400/80";- case "open":- return "text-emerald-600 dark:text-emerald-300/90";- default:- return "text-muted-foreground";- }- }, [resolvedPullRequest?.state]);+ const statusTone = pullRequestStatusTone(resolvedPullRequest?.state);- const handleConfirm = useCallback(- async (mode: "local" | "worktree") => {- if (!preparePullRequestThreadAction.isAllowed) return;- if (- mode === "worktree" &&- !readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)- ) {- return;- }- if (!parsedReference) {- setReferenceDirty(true);- return;- }- if (!parsedReference || !resolvedPullRequest || !cwd) {- return;- }- setPrepareErrorMessage(null);- setPreparingMode(mode);- const result = await preparePullRequestThreadAction.run({- reference: parsedReference,- mode,- ...(mode === "worktree" ? { threadId } : {}),- });- setPreparingMode(null);- if (result._tag === "Failure") {- if (isAtomCommandInterrupted(result)) {- preparePullRequestThreadAction.resetError();- } else {- const error = squashAtomCommandFailure(result);- setPrepareErrorMessage(- error instanceof Error- ? error.message- : `Failed to prepare ${terminology.singular} thread.`,- );- }- return;+ const handleConfirm = async (mode: "local" | "worktree") => {+ if (!preparePullRequestThreadAction.isAllowed) return;+ if (+ mode === "worktree" &&+ !readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)+ ) {+ return;+ }+ if (!parsedReference) {+ setReferenceDirty(true);+ return;+ }+ if (!parsedReference || !resolvedPullRequest || !cwd) {+ return;+ }+ setPrepareErrorMessage(null);+ setPreparingMode(mode);+ const result = await preparePullRequestThreadAction.run({+ reference: parsedReference,+ mode,+ ...(mode === "worktree" ? { threadId } : {}),+ });+ setPreparingMode(null);+ if (result._tag === "Failure") {+ if (isAtomCommandInterrupted(result)) {+ preparePullRequestThreadAction.resetError();+ } else {+ const error = squashAtomCommandFailure(result);+ setPrepareErrorMessage(+ error instanceof Error+ ? error.message+ : `Failed to prepare ${terminology.singular} thread.`,+ );}- await onPrepared({- branch: result.value.branch,- worktreePath: result.value.worktreePath,- });- onOpenChange(false);- },- [- cwd,- environmentId,- onOpenChange,- onPrepared,- parsedReference,- preparePullRequestThreadAction,- resolvedPullRequest,- terminology.singular,- threadId,- ],- );+ return;+ }+ await onPrepared({+ branch: result.value.branch,+ worktreePath: result.value.worktreePath,+ });+ onOpenChange(false);+ };const validationMessage = !referenceDirty? null