Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

perf(web): resolve residual input lag in tutorbot interface (#351) Extract lightweight `SimpleComposerInput` to isolate keystroke renders. Colocate input state in `agents/[botId]/chat/page.tsx` to prevent full-page lag. Export @mention utility helpers from `ComposerInput.tsx` for cross-component use. Address follow-up feedback from StarNight2021 regarding tutorbot-only performance. This completes the performance overhaul by ensuring all chat entry points use optimized state colocation patterns.

jiakeboge3bd857cf7e5e
diff --git a/web/app/(workspace)/agents/[botId]/chat/page.tsx b/web/app/(workspace)/agents/[botId]/chat/page.tsx
index 307265f7e..0bb928cdf 100644
--- a/web/app/(workspace)/agents/[botId]/chat/page.tsx
+++ b/web/app/(workspace)/agents/[botId]/chat/page.tsx
@@ -6,6 +6,7 @@ import { useTranslation } from "react-i18next";
import { ArrowLeft, Bot, Loader2, Send } from "lucide-react";
import { apiUrl, wsUrl } from "@/lib/api";
import AssistantResponse from "@/components/common/AssistantResponse";
+import { SimpleComposerInput } from "@/components/chat/home/SimpleComposerInput";
interface BotInfo {
bot_id: string;
@@ -26,7 +27,6 @@ export default function BotChatPage() {
const [bot, setBot] = useState<BotInfo | null>(null);
const [messages, setMessages] = useState<ChatMsg[]>([]);
- const [input, setInput] = useState("");
const [streaming, setStreaming] = useState(false);
const [thinking, setThinking] = useState<string[]>([]);
const thinkingRef = useRef<string[]>([]);
@@ -101,27 +101,23 @@ export default function BotChatPage() {
};
}, [botId, scrollToBottom]);
- const send = useCallback(() => {
- const text = input.trim();
- if (!text || streaming || !wsRef.current || wsRef.current.readyState !== WebSocket.OPEN) return;
+ const handleSend = useCallback((content: string) => {
+ if (streaming || !wsRef.current || wsRef.current.readyState !== WebSocket.OPEN) return;
- setMessages((msgs) => [...msgs, { role: "user", content: text }]);
- setInput("");
+ setMessages((msgs) => [...msgs, { role: "user", content }]);
setStreaming(true);
setThinking([]);
- wsRef.current.send(JSON.stringify({ content: text }));
+ wsRef.current.send(JSON.stringify({ content }));
scrollToBottom();
- }, [input, streaming, scrollToBottom]);
+ }, [streaming, scrollToBottom]);
- const handleKeyDown = useCallback(
- (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
- if (e.key === "Enter" && !e.shiftKey) {
- e.preventDefault();
- send();
- }
- },
- [send],
- );
+ const handleManualSend = useCallback(() => {
+ const content = inputRef.current?.value.trim();
+ if (content) {
+ handleSend(content);
+ if (inputRef.current) inputRef.current.value = "";
+ }
+ }, [handleSend]);
return (
<div className="flex h-full flex-col">
@@ -207,25 +203,21 @@ export default function BotChatPage() {
{/* Input */}
<div className="border-t border-[var(--border)] px-5 py-3">
<div className="mx-auto flex max-w-[720px] items-end gap-2">
- <textarea
- ref={inputRef}
- value={input}
- onChange={(e) => setInput(e.target.value)}
- onKeyDown={handleKeyDown}
- placeholder={t("Type a message...")}
- rows={1}
+ <SimpleComposerInput
+ textareaRef={inputRef}
+ onSend={handleSend}
disabled={streaming}
- className="flex-1 resize-none rounded-xl border border-[var(--border)] bg-transparent px-4 py-2.5 text-[14px] text-[var(--foreground)] outline-none transition-colors focus:border-[var(--ring)] disabled:opacity-50 placeholder:text-[var(--muted-foreground)]/40"
/>
<button
- onClick={send}
- disabled={streaming || !input.trim()}
+ onClick={handleManualSend}
+ disabled={streaming}
className="flex h-[42px] w-[42px] items-center justify-center rounded-xl bg-[var(--primary)] text-[var(--primary-foreground)] transition-opacity hover:opacity-90 disabled:opacity-30"
>
<Send className="h-4 w-4" />
</button>
</div>
</div>
+
</div>
);
}
diff --git a/web/components/chat/home/ComposerInput.tsx b/web/components/chat/home/ComposerInput.tsx
index 3c3133f35..663fccd81 100644
--- a/web/components/chat/home/ComposerInput.tsx
+++ b/web/components/chat/home/ComposerInput.tsx
@@ -22,12 +22,12 @@ export interface ComposerInputHandle {
getValue: () => string;
}
-function shouldOpenAtPopup(value: string, cursorPos: number): boolean {
+export function shouldOpenAtPopup(value: string, cursorPos: number): boolean {
const prefix = value.slice(0, cursorPos);
return /(^|\s)@[^\s]*$/.test(prefix);
}
-function stripTrailingAtMention(value: string): string {
+export function stripTrailingAtMention(value: string): string {
return value.replace(/(^|\s)@[^\s]*$/, "$1").replace(/\s+$/, "");
}
diff --git a/web/components/chat/home/SimpleComposerInput.tsx b/web/components/chat/home/SimpleComposerInput.tsx
new file mode 100644
index 000000000..63b567070
--- /dev/null
+++ b/web/components/chat/home/SimpleComposerInput.tsx
@@ -0,0 +1,56 @@
+"use client";
+
+import { memo, useCallback, useLayoutEffect, useState, type RefObject } from "react";
+import { useTranslation } from "react-i18next";
+
+interface SimpleComposerInputProps {
+ textareaRef: RefObject<HTMLTextAreaElement | null>;
+ onSend: (content: string) => void;
+ disabled?: boolean;
+}
+
+export const SimpleComposerInput = memo(function SimpleComposerInput({
+ textareaRef,
+ onSend,
+ disabled,
+}: SimpleComposerInputProps) {
+ const { t } = useTranslation();
+ const [input, setInput] = useState("");
+
+ useLayoutEffect(() => {
+ const el = textareaRef.current;
+ if (!el) return;
+ el.style.height = "auto";
+ const next = Math.max(el.scrollHeight, 42);
+ el.style.height = `${Math.min(next, 200)}px`;
+ }, [input, textareaRef]);
+
+ const handleInputChange = useCallback((e: React.ChangeEvent<HTMLTextAreaElement>) => {
+ setInput(e.target.value);
+ }, []);
+
+ const handleKeyDown = useCallback((e: React.KeyboardEvent<HTMLTextAreaElement>) => {
+ if (e.key === "Enter" && !e.shiftKey) {
+ e.preventDefault();
+ const content = input.trim();
+ if (content && !disabled) {
+ onSend(content);
+ setInput("");
+ }
+ }
+ }, [input, onSend, disabled]);
+
+ return (
+ <textarea
+ ref={textareaRef}
+ value={input}
+ onChange={handleInputChange}
+ onKeyDown={handleKeyDown}
+ placeholder={t("Type a message...")}
+ rows={1}
+ disabled={disabled}
+ className="flex-1 resize-none rounded-xl border border-[var(--border)] bg-transparent px-4 py-2.5 text-[14px] text-[var(--foreground)] outline-none transition-colors focus:border-[var(--ring)] disabled:opacity-50 placeholder:text-[var(--muted-foreground)]/40"
+ style={{ minHeight: 42 }}
+ />
+ );
+});