TypeScript
refactor(web): let the React Compiler compile usePendingThreadRequests (#20294)
Julius Marmingef7916ad07cc1
diff --git a/apps/web/src/components/chat/usePendingThreadRequests.ts b/apps/web/src/components/chat/usePendingThreadRequests.tsindex 8ec231d2f91..52406fbca0f 100644--- a/apps/web/src/components/chat/usePendingThreadRequests.ts+++ b/apps/web/src/components/chat/usePendingThreadRequests.ts@@ -16,7 +16,7 @@ import {isAtomCommandInterrupted,squashAtomCommandFailure,} from "@t3tools/client-runtime/state/runtime";-import { useEffect, useMemo, useRef, useState, type RefObject, type SetStateAction } from "react";+import { useEffect, useRef, useState, type RefObject, type SetStateAction } from "react";import { useShallow } from "zustand/react/shallow";import { DraftId, useComposerDraftStore } from "../../composerDraftStore";import { useStableCallback } from "../../hooks/useStableCallback";@@ -53,7 +53,7 @@ const EMPTY_PENDING_USER_INPUT_ANSWERS: Record<string, PendingUserInputDraftAnswfunction makePendingCustomAnswerSetter(input: {readonly activePendingUserInput: PendingUserInput | null;readonly activePendingRequestKey: string;- readonly promptRef: { current: string };+ readonly writePrompt: (prompt: string) => void;readonly setPendingUserInputAnswersByRequestId: (update: SetStateAction<Record<string, Record<string, PendingUserInputDraftAnswer>>>,) => void;@@ -67,7 +67,7 @@ function makePendingCustomAnswerSetter(input: {if (!question || question.allowCustomAnswer === false) {return;}- input.promptRef.current = value;+ input.writePrompt(value);input.setPendingUserInputAnswersByRequestId((existing) => ({...existing,[activePendingRequestKey]: {@@ -132,21 +132,12 @@ export function usePendingThreadRequests(input: {>({});const [pendingUserInputQuestionIndexByRequestId, setPendingUserInputQuestionIndexByRequestId] =useState<Record<string, number>>({});- const pendingRequests = useMemo(- () =>- serverProjection === null- ? { approvals: [], userInputs: [] }- : derivePendingThreadRequests(serverProjection),- [serverProjection],- );- const pendingApprovals = useMemo(- () => derivePendingApprovals(pendingRequests.approvals),- [pendingRequests.approvals],- );- const pendingUserInputs = useMemo(- () => derivePendingUserInputs(pendingRequests.userInputs),- [pendingRequests.userInputs],- );+ const pendingRequests =+ serverProjection === null+ ? { approvals: [], userInputs: [] }+ : derivePendingThreadRequests(serverProjection);+ const pendingApprovals = derivePendingApprovals(pendingRequests.approvals);+ const pendingUserInputs = derivePendingUserInputs(pendingRequests.userInputs);const activePendingUserInput = pendingUserInputs[0] ?? null;const activePendingRequestKey = JSON.stringify([environmentId,@@ -166,22 +157,13 @@ export function usePendingThreadRequests(input: {return seeded === drafts ? existing : { ...existing, [activePendingRequestKey]: seeded };});}- const pendingQuestionDraftKeys = useMemo(- () =>- activeThreadId- ? pendingUserInputs.flatMap((request) =>- request.questions.map((question) =>- questionAttachmentDraftId(- environmentId,- activeThreadId,- request.requestId,- question.id,- ),- ),- )- : [],- [activeThreadId, environmentId, pendingUserInputs],- );+ const pendingQuestionDraftKeys = activeThreadId+ ? pendingUserInputs.flatMap((request) =>+ request.questions.map((question) =>+ questionAttachmentDraftId(environmentId, activeThreadId, request.requestId, question.id),+ ),+ )+ : [];const questionComposerDrafts = useComposerDraftStore(useShallow((state) =>Object.fromEntries(@@ -237,63 +219,45 @@ export function usePendingThreadRequests(input: {clearQuestionAttachmentDraft(DraftId.make(key));}}, [environmentId, activeThread, pendingUserInputs]);- const activePendingDraftAnswers = useMemo(() => {- if (!activePendingUserInput || !activeThreadId) return EMPTY_PENDING_USER_INPUT_ANSWERS;- return Object.fromEntries(- activePendingUserInput.questions.map((question) => {- const key = questionAttachmentDraftId(- environmentId,- activeThreadId,- activePendingUserInput.requestId,- question.id,+ const activePendingDraftAnswers =+ !activePendingUserInput || !activeThreadId+ ? EMPTY_PENDING_USER_INPUT_ANSWERS+ : Object.fromEntries(+ activePendingUserInput.questions.map((question) => {+ const key = questionAttachmentDraftId(+ environmentId,+ activeThreadId,+ activePendingUserInput.requestId,+ question.id,+ );+ const draft = questionComposerDrafts[key];+ const attachments = draft ? [...draft.images, ...draft.files] : [];+ return [+ question.id,+ {+ ...pendingUserInputAnswersByRequestId[activePendingRequestKey]?.[question.id],+ attachmentCount: attachments.length,+ attachmentsBlocked:+ (attachments.length > 0 && !supportsQuestionAttachments) ||+ (questionPreparations[key] ?? 0) > 0 ||+ questionUploadsBlocked[key] === true,+ },+ ];+ }),);- const draft = questionComposerDrafts[key];- const attachments = draft ? [...draft.images, ...draft.files] : [];- return [- question.id,- {- ...pendingUserInputAnswersByRequestId[activePendingRequestKey]?.[question.id],- attachmentCount: attachments.length,- attachmentsBlocked:- (attachments.length > 0 && !supportsQuestionAttachments) ||- (questionPreparations[key] ?? 0) > 0 ||- questionUploadsBlocked[key] === true,- },- ];- }),- );- }, [- activePendingUserInput,- activeThreadId,- environmentId,- questionComposerDrafts,- questionUploadsBlocked,- supportsQuestionAttachments,- questionPreparations,- pendingUserInputAnswersByRequestId,- activePendingRequestKey,- ]);const activePendingQuestionIndex = activePendingUserInput? (pendingUserInputQuestionIndexByRequestId[activePendingRequestKey] ?? 0): 0;- const activePendingProgress = useMemo(- () =>- activePendingUserInput- ? derivePendingUserInputProgress(- activePendingUserInput.questions,- activePendingDraftAnswers,- activePendingQuestionIndex,- )- : null,- [activePendingDraftAnswers, activePendingQuestionIndex, activePendingUserInput],- );- const activePendingResolvedAnswers = useMemo(- () =>- activePendingUserInput- ? buildPendingUserInputAnswers(activePendingUserInput.questions, activePendingDraftAnswers)- : null,- [activePendingDraftAnswers, activePendingUserInput],- );+ const activePendingProgress = activePendingUserInput+ ? derivePendingUserInputProgress(+ activePendingUserInput.questions,+ activePendingDraftAnswers,+ activePendingQuestionIndex,+ )+ : null;+ const activePendingResolvedAnswers = activePendingUserInput+ ? buildPendingUserInputAnswers(activePendingUserInput.questions, activePendingDraftAnswers)+ : null;const activePendingIsResponding = activePendingUserInput? activePendingUserInput.responseCapability === "not_resumable" ||respondingUserInputRequestIds.includes(activePendingUserInput.requestId)@@ -490,16 +454,15 @@ export function usePendingThreadRequests(input: {onSelectActivePendingUserInputOption,);- const onChangeActivePendingUserInputCustomAnswer = useMemo(- () =>- makePendingCustomAnswerSetter({- activePendingUserInput,- activePendingRequestKey,- promptRef,- setPendingUserInputAnswersByRequestId,- }),- [activePendingUserInput, activePendingRequestKey, promptRef],- );+ const writePrompt = useStableCallback((prompt: string) => {+ promptRef.current = prompt;+ });+ const onChangeActivePendingUserInputCustomAnswer = makePendingCustomAnswerSetter({+ activePendingUserInput,+ activePendingRequestKey,+ writePrompt,+ setPendingUserInputAnswersByRequestId,+ });const onAdvanceActivePendingUserInput = () => {if (diff --git a/vite.config.ts b/vite.config.tsindex f140ca60fbd..81cae3d0461 100644--- a/vite.config.ts+++ b/vite.config.ts@@ -327,6 +327,7 @@ export default defineConfig({// Add a file once it compiles cleanly. ChatView is being split toward this list.files: ["apps/web/src/components/PersistentThreadTerminals.tsx",+ "apps/web/src/components/chat/usePendingThreadRequests.ts","apps/web/src/components/terminal/**",],rules: {