diff --git a/src/components/channel/MessageBox.tsx b/src/components/channel/MessageBox.tsx index 7ade402..d22cf06 100644 --- a/src/components/channel/MessageBox.tsx +++ b/src/components/channel/MessageBox.tsx @@ -206,17 +206,41 @@ function MessageInput({ text, setText, onEnter, + onTyping, }: { text: string; setText: SetState; onEnter: () => void; + onTyping?: () => void; }) { + const TYPING_INTERVAL = 2000; const inputRef = useRef(null); + const lastTypingSent = useRef(0); + const stopTypingTimeout = useRef(null); useEffect(() => { inputRef.current?.focus(); }, [inputRef]); + useEffect(() => { + if (!onTyping || !text) return; + + const now = Date.now(); + + if (now - lastTypingSent.current > TYPING_INTERVAL) { + lastTypingSent.current = now; + onTyping(); + } + + if (stopTypingTimeout.current) { + clearTimeout(stopTypingTimeout.current); + } + + stopTypingTimeout.current = setTimeout(() => { + lastTypingSent.current = 0; + }, TYPING_INTERVAL); + }, [text]); + return text.includes("\n") ? (