From a3a645a7da5ae9d38867f96c61aefdd85380f857 Mon Sep 17 00:00:00 2001 From: jiakeboge <28388982+jiakeboge@users.noreply.github.com> Date: Wed, 22 Apr 2026 11:40:10 +0800 Subject: [PATCH 1/8] perf(web): decouple chat input state to resolve lag in long conversations (#351) Extract `ComposerInput` component to isolate frequent keystroke renders. Refactor `ChatComposer` to use `hasContent` flag instead of full `input` state. Apply `React.memo` to heavy config panels (Quiz, Math, Research, Visualize). Stabilize parent callbacks in `ChatPage` using `useCallback`. Fix residual syntax errors and missing `canSend` definition in composer. Address HKUDS/DeepTutor#351 by implementing deep state colocation. --- .../chat/[[...sessionId]]/page.tsx | 24 +-- web/components/chat/AtMentionPopup.tsx | 5 +- web/components/chat/home/ChatComposer.tsx | 144 ++++++----------- web/components/chat/home/ComposerInput.tsx | 153 ++++++++++++++++++ .../math-animator/MathAnimatorConfigPanel.tsx | 5 +- web/components/quiz/QuizConfigPanel.tsx | 6 +- .../research/ResearchConfigPanel.tsx | 5 +- .../visualize/VisualizeConfigPanel.tsx | 5 +- 8 files changed, 228 insertions(+), 119 deletions(-) create mode 100644 web/components/chat/home/ComposerInput.tsx diff --git a/web/app/(workspace)/chat/[[...sessionId]]/page.tsx b/web/app/(workspace)/chat/[[...sessionId]]/page.tsx index 90112fb35..9e278c237 100644 --- a/web/app/(workspace)/chat/[[...sessionId]]/page.tsx +++ b/web/app/(workspace)/chat/[[...sessionId]]/page.tsx @@ -666,6 +666,18 @@ export default function ChatPage() { const handleRemoveQuestion = useCallback((entryId: number) => { setSelectedQuestionEntries((prev) => prev.filter((entry) => entry.id !== entryId)); }, []); + const handleToggleSkill = useCallback((name: string) => { + setSkillsAutoMode(false); + setSelectedSkills((prev) => + prev.includes(name) ? prev.filter((n) => n !== name) : [...prev, name], + ); + }, []); + + const handleSetSkillsAuto = useCallback((auto: boolean) => { + setSkillsAutoMode(auto); + if (auto) setSelectedSkills([]); + }, []); + const handleTogglePanelCollapsed = useCallback(() => { setPanelCollapsed((prev) => !prev); }, []); const handleCloseNotebookPicker = useCallback(() => { setShowNotebookPicker(false); }, []); const handleApplyNotebookRecords = useCallback((records: SelectedRecord[]) => { setSelectedNotebookRecords(records); }, []); @@ -800,16 +812,8 @@ export default function ChatPage() { onSelectHistoryPicker={handleSelectHistoryPicker} onSelectQuestionBankPicker={handleSelectQuestionBankPicker} onToggleTool={toggleTool} - onToggleSkill={(name) => { - setSkillsAutoMode(false); - setSelectedSkills((prev) => - prev.includes(name) ? prev.filter((n) => n !== name) : [...prev, name], - ); - }} - onSetSkillsAuto={(auto) => { - setSkillsAutoMode(auto); - if (auto) setSelectedSkills([]); - }} + onToggleSkill={handleToggleSkill} + onSetSkillsAuto={handleSetSkillsAuto} onToggleResearchSource={toggleResearchSource} onSend={handleSend} onRemoveAttachment={removeAttachment} diff --git a/web/components/chat/AtMentionPopup.tsx b/web/components/chat/AtMentionPopup.tsx index 30f0b4fce..7ebfd68f8 100644 --- a/web/components/chat/AtMentionPopup.tsx +++ b/web/components/chat/AtMentionPopup.tsx @@ -1,5 +1,6 @@ "use client"; +import { memo } from "react"; import { useTranslation } from "react-i18next"; interface AtMentionPopupProps { @@ -9,7 +10,7 @@ interface AtMentionPopupProps { onSelectQuestionBank: () => void; } -export default function AtMentionPopup({ +export default memo(function AtMentionPopup({ open, onSelectNotebook, onSelectHistory, @@ -41,4 +42,4 @@ export default function AtMentionPopup({ ); -} +}); diff --git a/web/components/chat/home/ChatComposer.tsx b/web/components/chat/home/ChatComposer.tsx index f3951dfe0..698c9bcf9 100644 --- a/web/components/chat/home/ChatComposer.tsx +++ b/web/components/chat/home/ChatComposer.tsx @@ -22,13 +22,13 @@ import { import { useTranslation } from "react-i18next"; import type { SelectedHistorySession } from "@/components/chat/HistorySessionPicker"; import type { SelectedQuestionEntry } from "@/components/chat/QuestionBankPicker"; -import AtMentionPopup from "@/components/chat/AtMentionPopup"; import type { SelectedRecord } from "@/lib/notebook-selection-types"; import type { DeepQuestionFormConfig } from "@/lib/quiz-types"; import type { MathAnimatorFormConfig } from "@/lib/math-animator-types"; import type { VisualizeFormConfig } from "@/lib/visualize-types"; import type { DeepResearchFormConfig, ResearchSource } from "@/lib/research-types"; import { ReferenceChips } from "./ChatMessages"; +import { ComposerInput, type ComposerInputHandle } from "./ComposerInput"; const QuizConfigPanel = dynamic(() => import("@/components/quiz/QuizConfigPanel"), { ssr: false, @@ -77,14 +77,6 @@ interface ResearchSourceDef { icon: LucideIcon; } -function shouldOpenAtPopup(value: string, cursorPos: number): boolean { - const prefix = value.slice(0, cursorPos); - return /(^|\s)@[^\s]*$/.test(prefix); -} - -function stripTrailingAtMention(value: string): string { - return value.replace(/(^|\s)@[^\s]*$/, "$1").replace(/\s+$/, ""); -} export default memo(function ChatComposer({ composerRef, @@ -240,9 +232,9 @@ export default memo(function ChatComposer({ const { t } = useTranslation(); const CapIcon = activeCap.icon; - const [input, setInput] = useState(""); - const [showAtPopup, setShowAtPopup] = useState(false); + const [hasContent, setHasContent] = useState(false); const textareaRef = useRef(null); + const inputHandleRef = useRef(null); const activeCapabilityKey = activeCap.value || "chat"; @@ -250,64 +242,28 @@ export default memo(function ChatComposer({ if (!hasMessages) textareaRef.current?.focus(); }, [hasMessages]); - useLayoutEffect(() => { - const el = textareaRef.current; - if (!el) return; - el.style.height = "28px"; - const next = Math.max(el.scrollHeight, 28); - const bounded = Math.min(next, 200); - el.style.height = `${bounded}px`; - el.style.overflowY = next > 200 ? "auto" : "hidden"; - }, [input, activeCapabilityKey]); - - const handleInputChange = useCallback((e: React.ChangeEvent) => { - const value = e.target.value; - const cursorPos = e.target.selectionStart ?? value.length; - setInput(value); - setShowAtPopup(shouldOpenAtPopup(value, cursorPos)); - }, []); - - const handleTextareaClick = useCallback((e: React.MouseEvent) => { - const target = e.currentTarget; - setShowAtPopup(shouldOpenAtPopup(target.value, target.selectionStart ?? target.value.length)); - }, []); - - const doSend = useCallback(() => { - const content = input.trim(); - onSend(content); - setInput(""); - setShowAtPopup(false); - }, [input, onSend]); - - const handleKeyDown = useCallback((e: React.KeyboardEvent) => { - if (e.key === "Enter" && !e.shiftKey) { - e.preventDefault(); - doSend(); - } else if (e.key === "Escape") { - setShowAtPopup(false); + const handleInputChange = useCallback((val: string) => { + const nextHasContent = !!val.trim(); + if (nextHasContent !== hasContent) { + setHasContent(nextHasContent); } - }, [doSend]); + }, [hasContent]); - const handleSelectNotebook = useCallback(() => { - setInput((prev) => stripTrailingAtMention(prev)); - setShowAtPopup(false); - onSelectNotebookPicker(); - }, [onSelectNotebookPicker]); + const doSend = useCallback((content: string) => { + onSend(content); + setHasContent(false); + inputHandleRef.current?.clear(); + }, [onSend]); - const handleSelectHistory = useCallback(() => { - setInput((prev) => stripTrailingAtMention(prev)); - setShowAtPopup(false); - onSelectHistoryPicker(); - }, [onSelectHistoryPicker]); - - const handleSelectQuestionBank = useCallback(() => { - setInput((prev) => stripTrailingAtMention(prev)); - setShowAtPopup(false); - onSelectQuestionBankPicker(); - }, [onSelectQuestionBankPicker]); + const handleManualSend = useCallback(() => { + const content = inputHandleRef.current?.getValue() || ""; + if (!!content.trim() || attachments.length || selectedNotebookRecords.length || selectedHistorySessions.length || selectedQuestionEntries.length) { + doSend(content); + } + }, [doSend, attachments.length, selectedNotebookRecords.length, selectedHistorySessions.length, selectedQuestionEntries.length]); const canSend = - (!!input.trim() || + (hasContent || !!attachments.length || !!selectedNotebookRecords.length || !!selectedHistorySessions.length || @@ -315,6 +271,8 @@ export default memo(function ChatComposer({ !isStreaming && !(isResearchMode && Object.keys(researchValidationErrors).length > 0); + + return (
- -
)} -
- -