Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -630,23 +630,46 @@ export default function ChatView({ threadId }: ChatViewProps) {
pendingUserInputs.length > 0 ||
(showPlanFollowUpPrompt && activeProposedPlan !== null);
const composerFooterHasWideActions = showPlanFollowUpPrompt || activePendingProgress !== null;
const lastSyncedPendingInputRef = useRef<{
requestId: string | null;
questionId: string | null;
} | null>(null);
useEffect(() => {
if (!activePendingProgress) {
const nextCustomAnswer = activePendingProgress?.customAnswer;
if (typeof nextCustomAnswer !== "string") {
lastSyncedPendingInputRef.current = null;
return;
}
promptRef.current = activePendingProgress.customAnswer;
setComposerCursor(activePendingProgress.customAnswer.length);
const nextRequestId = activePendingUserInput?.requestId ?? null;
const nextQuestionId = activePendingProgress?.activeQuestion?.id ?? null;
const questionChanged =
lastSyncedPendingInputRef.current?.requestId !== nextRequestId ||
lastSyncedPendingInputRef.current?.questionId !== nextQuestionId;
const textChangedExternally = promptRef.current !== nextCustomAnswer;

lastSyncedPendingInputRef.current = {
requestId: nextRequestId,
questionId: nextQuestionId,
};

if (!questionChanged && !textChangedExternally) {
return;
}

promptRef.current = nextCustomAnswer;
setComposerCursor(nextCustomAnswer.length);
setComposerTrigger(
detectComposerTrigger(
activePendingProgress.customAnswer,
expandCollapsedComposerCursor(
activePendingProgress.customAnswer,
activePendingProgress.customAnswer.length,
),
nextCustomAnswer,
expandCollapsedComposerCursor(nextCustomAnswer, nextCustomAnswer.length),
),
);
setComposerHighlightedItemId(null);
}, [activePendingProgress, activePendingUserInput?.requestId]);
}, [
activePendingProgress?.customAnswer,
activePendingUserInput?.requestId,
activePendingProgress?.activeQuestion?.id,
]);
useEffect(() => {
attachmentPreviewHandoffByMessageIdRef.current = attachmentPreviewHandoffByMessageId;
}, [attachmentPreviewHandoffByMessageId]);
Expand Down
29 changes: 18 additions & 11 deletions apps/web/src/components/ComposerPromptEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ function ComposerPromptEditorInner({
const [editor] = useLexicalComposerContext();
const onChangeRef = useRef(onChange);
const snapshotRef = useRef({ value, cursor: clampCursor(value, cursor) });
const isApplyingControlledUpdateRef = useRef(false);

useEffect(() => {
onChangeRef.current = onChange;
Expand All @@ -645,21 +646,26 @@ function ComposerPromptEditorInner({
return;
}

if (previousSnapshot.value !== value) {
editor.update(() => {
$setComposerEditorPrompt(value);
});
}

snapshotRef.current = { value, cursor: normalizedCursor };

const rootElement = editor.getRootElement();
if (!rootElement || document.activeElement !== rootElement) {
const isFocused = Boolean(rootElement && document.activeElement === rootElement);
if (previousSnapshot.value === value && !isFocused) {
return;
}

isApplyingControlledUpdateRef.current = true;
editor.update(() => {
$setSelectionAtComposerOffset(normalizedCursor);
const valueChanged = previousSnapshot.value !== value;
if (previousSnapshot.value !== value) {
$setComposerEditorPrompt(value);
}
if (valueChanged || isFocused) {
$setSelectionAtComposerOffset(normalizedCursor);
}
});
queueMicrotask(() => {
isApplyingControlledUpdateRef.current = false;
});
}, [cursor, editor, value]);

Expand Down Expand Up @@ -705,9 +711,7 @@ function ComposerPromptEditorInner({
focus: () => {
focusAt(snapshotRef.current.cursor);
},
focusAt: (nextCursor: number) => {
focusAt(nextCursor);
},
focusAt,
focusAtEnd: () => {
focusAt(snapshotRef.current.value.length);
},
Expand All @@ -728,6 +732,9 @@ function ComposerPromptEditorInner({
if (previousSnapshot.value === nextValue && previousSnapshot.cursor === nextCursor) {
return;
}
if (isApplyingControlledUpdateRef.current) {
return;
}
snapshotRef.current = {
value: nextValue,
cursor: nextCursor,
Expand Down
Loading