Skip to content

Commit a7807fd

Browse files
refactor(web): let the React Compiler compile usePendingThreadRequests
The custom answer setter received promptRef during render, so the compiler skipped the whole hook. It now gets a stable writer instead, the manual useMemo calls are gone, and the hook joins the compile-required lint list. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3be52df commit a7807fd

2 files changed

Lines changed: 61 additions & 97 deletions

File tree

‎apps/web/src/components/chat/usePendingThreadRequests.ts‎

Lines changed: 60 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
isAtomCommandInterrupted,
1717
squashAtomCommandFailure,
1818
} from "@t3tools/client-runtime/state/runtime";
19-
import { useEffect, useMemo, useRef, useState, type RefObject, type SetStateAction } from "react";
19+
import { useEffect, useRef, useState, type RefObject, type SetStateAction } from "react";
2020
import { useShallow } from "zustand/react/shallow";
2121
import { DraftId, useComposerDraftStore } from "../../composerDraftStore";
2222
import { useStableCallback } from "../../hooks/useStableCallback";
@@ -53,7 +53,7 @@ const EMPTY_PENDING_USER_INPUT_ANSWERS: Record<string, PendingUserInputDraftAnsw
5353
function makePendingCustomAnswerSetter(input: {
5454
readonly activePendingUserInput: PendingUserInput | null;
5555
readonly activePendingRequestKey: string;
56-
readonly promptRef: { current: string };
56+
readonly writePrompt: (prompt: string) => void;
5757
readonly setPendingUserInputAnswersByRequestId: (
5858
update: SetStateAction<Record<string, Record<string, PendingUserInputDraftAnswer>>>,
5959
) => void;
@@ -67,7 +67,7 @@ function makePendingCustomAnswerSetter(input: {
6767
if (!question || question.allowCustomAnswer === false) {
6868
return;
6969
}
70-
input.promptRef.current = value;
70+
input.writePrompt(value);
7171
input.setPendingUserInputAnswersByRequestId((existing) => ({
7272
...existing,
7373
[activePendingRequestKey]: {
@@ -132,21 +132,12 @@ export function usePendingThreadRequests(input: {
132132
>({});
133133
const [pendingUserInputQuestionIndexByRequestId, setPendingUserInputQuestionIndexByRequestId] =
134134
useState<Record<string, number>>({});
135-
const pendingRequests = useMemo(
136-
() =>
137-
serverProjection === null
138-
? { approvals: [], userInputs: [] }
139-
: derivePendingThreadRequests(serverProjection),
140-
[serverProjection],
141-
);
142-
const pendingApprovals = useMemo(
143-
() => derivePendingApprovals(pendingRequests.approvals),
144-
[pendingRequests.approvals],
145-
);
146-
const pendingUserInputs = useMemo(
147-
() => derivePendingUserInputs(pendingRequests.userInputs),
148-
[pendingRequests.userInputs],
149-
);
135+
const pendingRequests =
136+
serverProjection === null
137+
? { approvals: [], userInputs: [] }
138+
: derivePendingThreadRequests(serverProjection);
139+
const pendingApprovals = derivePendingApprovals(pendingRequests.approvals);
140+
const pendingUserInputs = derivePendingUserInputs(pendingRequests.userInputs);
150141
const activePendingUserInput = pendingUserInputs[0] ?? null;
151142
const activePendingRequestKey = JSON.stringify([
152143
environmentId,
@@ -166,22 +157,13 @@ export function usePendingThreadRequests(input: {
166157
return seeded === drafts ? existing : { ...existing, [activePendingRequestKey]: seeded };
167158
});
168159
}
169-
const pendingQuestionDraftKeys = useMemo(
170-
() =>
171-
activeThreadId
172-
? pendingUserInputs.flatMap((request) =>
173-
request.questions.map((question) =>
174-
questionAttachmentDraftId(
175-
environmentId,
176-
activeThreadId,
177-
request.requestId,
178-
question.id,
179-
),
180-
),
181-
)
182-
: [],
183-
[activeThreadId, environmentId, pendingUserInputs],
184-
);
160+
const pendingQuestionDraftKeys = activeThreadId
161+
? pendingUserInputs.flatMap((request) =>
162+
request.questions.map((question) =>
163+
questionAttachmentDraftId(environmentId, activeThreadId, request.requestId, question.id),
164+
),
165+
)
166+
: [];
185167
const questionComposerDrafts = useComposerDraftStore(
186168
useShallow((state) =>
187169
Object.fromEntries(
@@ -237,63 +219,45 @@ export function usePendingThreadRequests(input: {
237219
clearQuestionAttachmentDraft(DraftId.make(key));
238220
}
239221
}, [environmentId, activeThread, pendingUserInputs]);
240-
const activePendingDraftAnswers = useMemo(() => {
241-
if (!activePendingUserInput || !activeThreadId) return EMPTY_PENDING_USER_INPUT_ANSWERS;
242-
return Object.fromEntries(
243-
activePendingUserInput.questions.map((question) => {
244-
const key = questionAttachmentDraftId(
245-
environmentId,
246-
activeThreadId,
247-
activePendingUserInput.requestId,
248-
question.id,
222+
const activePendingDraftAnswers =
223+
!activePendingUserInput || !activeThreadId
224+
? EMPTY_PENDING_USER_INPUT_ANSWERS
225+
: Object.fromEntries(
226+
activePendingUserInput.questions.map((question) => {
227+
const key = questionAttachmentDraftId(
228+
environmentId,
229+
activeThreadId,
230+
activePendingUserInput.requestId,
231+
question.id,
232+
);
233+
const draft = questionComposerDrafts[key];
234+
const attachments = draft ? [...draft.images, ...draft.files] : [];
235+
return [
236+
question.id,
237+
{
238+
...pendingUserInputAnswersByRequestId[activePendingRequestKey]?.[question.id],
239+
attachmentCount: attachments.length,
240+
attachmentsBlocked:
241+
(attachments.length > 0 && !supportsQuestionAttachments) ||
242+
(questionPreparations[key] ?? 0) > 0 ||
243+
questionUploadsBlocked[key] === true,
244+
},
245+
];
246+
}),
249247
);
250-
const draft = questionComposerDrafts[key];
251-
const attachments = draft ? [...draft.images, ...draft.files] : [];
252-
return [
253-
question.id,
254-
{
255-
...pendingUserInputAnswersByRequestId[activePendingRequestKey]?.[question.id],
256-
attachmentCount: attachments.length,
257-
attachmentsBlocked:
258-
(attachments.length > 0 && !supportsQuestionAttachments) ||
259-
(questionPreparations[key] ?? 0) > 0 ||
260-
questionUploadsBlocked[key] === true,
261-
},
262-
];
263-
}),
264-
);
265-
}, [
266-
activePendingUserInput,
267-
activeThreadId,
268-
environmentId,
269-
questionComposerDrafts,
270-
questionUploadsBlocked,
271-
supportsQuestionAttachments,
272-
questionPreparations,
273-
pendingUserInputAnswersByRequestId,
274-
activePendingRequestKey,
275-
]);
276248
const activePendingQuestionIndex = activePendingUserInput
277249
? (pendingUserInputQuestionIndexByRequestId[activePendingRequestKey] ?? 0)
278250
: 0;
279-
const activePendingProgress = useMemo(
280-
() =>
281-
activePendingUserInput
282-
? derivePendingUserInputProgress(
283-
activePendingUserInput.questions,
284-
activePendingDraftAnswers,
285-
activePendingQuestionIndex,
286-
)
287-
: null,
288-
[activePendingDraftAnswers, activePendingQuestionIndex, activePendingUserInput],
289-
);
290-
const activePendingResolvedAnswers = useMemo(
291-
() =>
292-
activePendingUserInput
293-
? buildPendingUserInputAnswers(activePendingUserInput.questions, activePendingDraftAnswers)
294-
: null,
295-
[activePendingDraftAnswers, activePendingUserInput],
296-
);
251+
const activePendingProgress = activePendingUserInput
252+
? derivePendingUserInputProgress(
253+
activePendingUserInput.questions,
254+
activePendingDraftAnswers,
255+
activePendingQuestionIndex,
256+
)
257+
: null;
258+
const activePendingResolvedAnswers = activePendingUserInput
259+
? buildPendingUserInputAnswers(activePendingUserInput.questions, activePendingDraftAnswers)
260+
: null;
297261
const activePendingIsResponding = activePendingUserInput
298262
? activePendingUserInput.responseCapability === "not_resumable" ||
299263
respondingUserInputRequestIds.includes(activePendingUserInput.requestId)
@@ -490,16 +454,15 @@ export function usePendingThreadRequests(input: {
490454
onSelectActivePendingUserInputOption,
491455
);
492456

493-
const onChangeActivePendingUserInputCustomAnswer = useMemo(
494-
() =>
495-
makePendingCustomAnswerSetter({
496-
activePendingUserInput,
497-
activePendingRequestKey,
498-
promptRef,
499-
setPendingUserInputAnswersByRequestId,
500-
}),
501-
[activePendingUserInput, activePendingRequestKey, promptRef],
502-
);
457+
const writePrompt = useStableCallback((prompt: string) => {
458+
promptRef.current = prompt;
459+
});
460+
const onChangeActivePendingUserInputCustomAnswer = makePendingCustomAnswerSetter({
461+
activePendingUserInput,
462+
activePendingRequestKey,
463+
writePrompt,
464+
setPendingUserInputAnswersByRequestId,
465+
});
503466

504467
const onAdvanceActivePendingUserInput = () => {
505468
if (

‎vite.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ export default defineConfig({
327327
// Add a file once it compiles cleanly. ChatView is being split toward this list.
328328
files: [
329329
"apps/web/src/components/PersistentThreadTerminals.tsx",
330+
"apps/web/src/components/chat/usePendingThreadRequests.ts",
330331
"apps/web/src/components/terminal/**",
331332
],
332333
rules: {

0 commit comments

Comments
 (0)