Skip to content

Commit 9c5e85f

Browse files
committed
fix(web): rebuild composer state after hidden reasoning changes
A reasoning shortcut during an approval updates the draft while the editor is hidden. Flag the draft for the same cursor and trigger rebuild that pending answers already use, and drop the render-time cursor ref.
1 parent ee9a581 commit 9c5e85f

1 file changed

Lines changed: 11 additions & 12 deletions

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,8 +2430,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
24302430
}
24312431
promptRef.current = prompt;
24322432
if (restoreDraftAfterPendingRef.current) {
2433-
// The cursor and trigger still describe the custom-answer buffer, so
2434-
// rebuild both from the restored draft instead of clamping them.
2433+
// The cursor and trigger still describe the custom-answer buffer, or a
2434+
// draft that changed while hidden behind an approval, so rebuild both
2435+
// from the restored draft instead of clamping them.
24352436
restoreDraftAfterPendingRef.current = false;
24362437
setComposerCursor(collapseExpandedComposerCursor(prompt, prompt.length));
24372438
setComposerTrigger(detectComposerTrigger(prompt, prompt.length));
@@ -4314,12 +4315,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
43144315
setIsStashMenuOpen(false);
43154316
}, [prompt]);
43164317

4317-
// The cursor is only needed as a fallback when the editor has no snapshot,
4318-
// so read it through a ref instead of re-binding the shortcut listener on
4319-
// every caret move.
4320-
const composerCursorRef = useRef(composerCursor);
4321-
composerCursorRef.current = composerCursor;
4322-
43234318
const applyReasoningCycle = useCallback(
43244319
(direction: "increase" | "decrease") => {
43254320
const editorShowsDraftPrompt = !isComposerApprovalState && activePendingProgress === null;
@@ -4357,11 +4352,15 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
43574352
setPrompt(transition.prompt);
43584353
// When a pending question or approval hides the editor, the draft
43594354
// is updated in the store only. The cursor and trigger belong to
4360-
// the visible editor state and are rebuilt when the draft returns.
4361-
if (editorShowsDraftPrompt) {
4355+
// the visible editor state and are rebuilt from the draft by the
4356+
// sync effect once it is shown again.
4357+
if (!editorShowsDraftPrompt) {
4358+
restoreDraftAfterPendingRef.current = true;
4359+
} else {
4360+
// The editor is mounted whenever it shows the draft; the fallback
4361+
// only guards the ref during teardown.
43624362
const currentExpandedCursor =
4363-
composerEditorRef.current?.readSnapshot().expandedCursor ??
4364-
expandCollapsedComposerCursor(currentPrompt, composerCursorRef.current);
4363+
composerEditorRef.current?.readSnapshot().expandedCursor ?? currentPrompt.length;
43654364
const nextExpandedCursor = mapComposerCursorAcrossLeadingPromptChange(
43664365
currentPrompt,
43674366
transition.prompt,

0 commit comments

Comments
 (0)