Files
DeepTutor/web/components/chat/home/SimpleComposerInput.tsx
Bingxi Zhao (Frank) 28e225ee0e chore: prepare v1.3.1 release
Stability release after v1.3.0 covering safer RAG/KB routing, stronger
embedding validation, more reliable Docker/TutorBot restarts, Web UX
fixes, and Windows/encoding hardening. See assets/releases/ver1-3-1.md
for the full notes.

Made-with: Cursor
2026-04-28 12:00:11 +08:00

86 lines
2.3 KiB
TypeScript

"use client";
import {
memo,
useCallback,
useLayoutEffect,
useRef,
useState,
type RefObject,
} from "react";
import { useTranslation } from "react-i18next";
import { shouldSubmitOnEnter } from "@/lib/composer-keyboard";
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("");
const isComposingRef = useRef(false);
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 (shouldSubmitOnEnter(e, isComposingRef.current)) {
e.preventDefault();
const content = input.trim();
if (content && !disabled) {
onSend(content);
setInput("");
}
}
},
[input, onSend, disabled],
);
const handleCompositionStart = useCallback(() => {
isComposingRef.current = true;
}, []);
const handleCompositionEnd = useCallback(() => {
// Some IMEs fire compositionend before the Enter keydown that confirms
// a candidate, so keep the guard through the current event turn.
setTimeout(() => {
isComposingRef.current = false;
}, 0);
}, []);
return (
<textarea
ref={textareaRef}
value={input}
onChange={handleInputChange}
onKeyDown={handleKeyDown}
onCompositionStart={handleCompositionStart}
onCompositionEnd={handleCompositionEnd}
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 }}
/>
);
});