:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(max): keep chat input on local state to stop typing lag in long threads (#64785)
Georgiy Tarasov7476d7cf0c4b
diff --git a/frontend/src/scenes/max/components/QuestionInput.tsx b/frontend/src/scenes/max/components/QuestionInput.tsxindex 4a5d19f4209d..a747e81affd0 100644--- a/frontend/src/scenes/max/components/QuestionInput.tsx+++ b/frontend/src/scenes/max/components/QuestionInput.tsx@@ -4,6 +4,7 @@ import { offset } from '@floating-ui/react'import { useActions, useValues } from 'kea'import posthog from 'posthog-js'import React, { ReactNode, useEffect, useMemo, useRef, useState } from 'react'+import { useDebouncedCallback } from 'use-debounce'import { IconArrowRight, IconCheck, IconPencil, IconStopFilled, IconTrash, IconX } from '@posthog/icons'import { LemonButton, LemonSwitch, LemonTextArea, Spinner } from '@posthog/lemon-ui'@@ -151,7 +152,8 @@ export const QuestionInput = React.forwardRef<HTMLDivElement, QuestionInputPropsconversation,threadLoading,inputDisabled,- submissionDisabledReason,+ contextDisabledReason,+ queueDisabledReason,isSharedThread,cancelLoading,pendingPrompt,@@ -181,13 +183,62 @@ export const QuestionInput = React.forwardRef<HTMLDivElement, QuestionInputPropsconst [autocompleteDismissed, setAutocompleteDismissed] = useState(false)const [editingQueueId, setEditingQueueId] = useState<string | null>(null)const displayQueuedMessages = useMemo(() => [...queuedMessages].reverse(), [queuedMessages])- const hasQuestion = question.trim().length > 0++ // Hold the textarea value in local state so each keystroke is an isolated, cheap re-render+ // rather than a global kea dispatch. Binding the input directly to kea made every keystroke+ // notify every store subscriber, so input lag grew with conversation length (more mounted+ // messages = more subscriptions to sweep). kea remains the source of truth for submit, slash+ // commands, and draft persistence — we sync to it on a debounce, immediately for slash+ // commands so the autocomplete stays responsive, and on submit/blur.+ const [inputValue, setInputValue] = useState(question)+ const debouncedSetQuestion = useDebouncedCallback((value: string) => setQuestion(value), 150)++ // Flush any pending debounce when the component unmounts so that a draft typed just+ // before programmatic navigation (no blur event) is still persisted to kea.+ useEffect(() => {+ return () => debouncedSetQuestion.flush()+ }, [debouncedSetQuestion])++ // Mirror external question changes (draft restore, slash command insertion, clear on submit)+ // back into local state. Writing the same value is a no-op, so the debounced sync below+ // doesn't cause an extra render.+ useEffect(() => {+ setInputValue(question)+ }, [question])++ const handleQuestionChange = (value: string): void => {+ setInputValue(value)+ if (value.startsWith('/')) {+ // Slash commands drive the autocomplete off kea's `question`, so sync immediately.+ debouncedSetQuestion.cancel()+ setQuestion(value)+ } else {+ debouncedSetQuestion(value)+ }+ }++ const submit = (prompt: string): void => {+ // askMax reads the prompt arg directly and clears `question` afterwards, so drop any+ // pending debounce to stop it from re-populating the just-sent text.+ debouncedSetQuestion.cancel()+ askMax(prompt)+ }++ const hasQuestion = inputValue.trim().length > 0const isQueueingSubmission = queueingEnabled && threadLoading && hasQuestionconst showStopButton = threadLoading && !isQueueingSubmission- // Update autocomplete visibility when question changes+ // Mirrors maxThreadLogic's `submissionDisabledReason` selector, but using the local input+ // value so the submit guard stays correct while the debounced sync to kea is still pending.+ const submissionDisabledReason = contextDisabledReason+ ? contextDisabledReason+ : !inputValue+ ? 'I need some input first'+ : queueDisabledReason++ // Update autocomplete visibility when the input changesuseEffect(() => {- const isSlashCommand = question[0] === '/'+ const isSlashCommand = inputValue[0] === '/'// Once the input is no longer a slash command, clear any prior dismissal so// typing "/" again later reopens the popover.if (!isSlashCommand && autocompleteDismissed) {@@ -198,7 +249,7 @@ export const QuestionInput = React.forwardRef<HTMLDivElement, QuestionInputPropsposthog.capture('Max slash command autocomplete shown')}setShowAutocomplete(shouldShow)- }, [question, showAutocomplete, autocompleteDismissed])+ }, [inputValue, showAutocomplete, autocompleteDismissed])let disabledReason = submissionDisabledReasonif (threadLoading && !isQueueingSubmission) {@@ -292,7 +343,7 @@ export const QuestionInput = React.forwardRef<HTMLDivElement, QuestionInputProps}}><div className="relative w-full">- {!question && (+ {!inputValue && (<divid="textarea-hint"className="text-secondary absolute top-4 left-4 text-sm pointer-events-none"@@ -321,12 +372,13 @@ export const QuestionInput = React.forwardRef<HTMLDivElement, QuestionInputProps</div>)}<LemonTextArea- aria-describedby={!question ? 'textarea-hint' : undefined}+ aria-describedby={!inputValue ? 'textarea-hint' : undefined}id="question-input"data-attr="max-chat-input"ref={textAreaRef}- value={isSharedThread ? '' : question}- onChange={(value) => setQuestion(value)}+ value={isSharedThread ? '' : inputValue}+ onChange={handleQuestionChange}+ onBlur={() => debouncedSetQuestion.flush()}onPressEnter={() => {if (hasQuestion &&@@ -334,13 +386,13 @@ export const QuestionInput = React.forwardRef<HTMLDivElement, QuestionInputProps(!threadLoading || queueingEnabled)) {onSubmit?.()- askMax(question)+ submit(inputValue)}}}onKeyDown={(event) => {if (event.key === 'ArrowUp' &&- !question.trim() &&+ !inputValue.trim() &&queuedMessages.length > 0 &&!editingQueueId) {@@ -417,7 +469,7 @@ export const QuestionInput = React.forwardRef<HTMLDivElement, QuestionInputPropsplacement="bottom-end"showArrowignoreDismissal- onApprove={() => askMax(pendingPrompt || question)}+ onApprove={() => submit(pendingPrompt || inputValue)}onDismiss={() => completeThreadGeneration()}middleware={[offset((state) => ({@@ -436,7 +488,7 @@ export const QuestionInput = React.forwardRef<HTMLDivElement, QuestionInputPropstextAreaRef?.current?.focus()return}- askMax(question)+ submit(inputValue)return}stopGeneration()@@ -446,7 +498,7 @@ export const QuestionInput = React.forwardRef<HTMLDivElement, QuestionInputPropstextAreaRef?.current?.focus()return}- askMax(question)+ submit(inputValue)}}tooltip={disabledReason ? (@@ -473,7 +525,7 @@ export const QuestionInput = React.forwardRef<HTMLDivElement, QuestionInputPropsshowStopButton ? (<IconStopFilled />) : (- MAX_SLASH_COMMANDS.find((cmd) => cmd.name === question.split(' ', 1)[0])+ MAX_SLASH_COMMANDS.find((cmd) => cmd.name === inputValue.split(' ', 1)[0])?.icon || <IconArrowRight />)}