@@ -1,6 +1,6 @@
11 "use client";
22
3-import { useState, useRef, useImperativeHandle, forwardRef, useEffect } from "react";
3+import { useImperativeHandle, forwardRef } from "react";
44 import PaperPlaneIcon from "@/src/components/icons/PaperPlaneIcon";
55 import PrimaryButton from "@/src/components/buttons/PrimaryButton";
66 import ThinkingNotification from "@/src/components/ThinkingNotification";
@@ -8,6 +8,8 @@ import ThreadSettingsButton from "@/src/components/ThreadSettingsButton";
88 import ErrorNotification from "@/src/components/ErrorNotification";
99 import type { Message } from "@/src/lib/db/schema";
1010 import type { Thread } from "@/src/lib/db/schema";
11+import { useAutoResizeTextarea } from "@/src/hooks/useAutoResizeTextarea";
12+import { useMessageHistory } from "@/src/hooks/useMessageHistory";
1113
1214 type MessageInputProps = {
1315 isLoading: boolean;
@@ -65,10 +67,8 @@ const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
6567 },
6668 ref
6769 ) => {
68- const [input, setInput] = useState("");
69- const [historyIndex, setHistoryIndex] = useState<number | null>(null);
70- const textareaRef = useRef<HTMLTextAreaElement>(null);
71- const draftInputRef = useRef<string>("");
70+ const { input, setInput, handleKeyDown, resetHistory } = useMessageHistory(messages);
71+ const textareaRef = useAutoResizeTextarea(input);
7272
7373 useImperativeHandle(ref, () => ({
7474 focus: () => {
@@ -76,34 +76,6 @@ const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
7676 },
7777 }));
7878
79- // Auto-resize textarea to keep an empty line at the bottom
80- useEffect(() => {
81- const textarea = textareaRef.current;
82- if (!textarea) return;
83-
84- // Reset height to auto to get the correct scrollHeight
85- textarea.style.height = "auto";
86-
87- // Calculate line height from computed styles
88- const computedStyle = getComputedStyle(textarea);
89- const lineHeight = parseFloat(computedStyle.lineHeight) || 24;
90- const paddingTop = parseFloat(computedStyle.paddingTop) || 8;
91- const paddingBottom = parseFloat(computedStyle.paddingBottom) || 8;
92-
93- // Minimum height for 3 lines
94- const minHeight = lineHeight * 3 + paddingTop + paddingBottom;
95-
96- // Maximum height for 10 lines
97- const maxHeight = lineHeight * 10 + paddingTop + paddingBottom;
98-
99- // Set height to scrollHeight or minHeight, whichever is larger
100- // Add one extra line height to keep an empty line at the bottom
101- // Cap at maxHeight to enable scrolling after 10 lines
102- const calculatedHeight = textarea.scrollHeight + lineHeight;
103- const newHeight = Math.min(maxHeight, Math.max(minHeight, calculatedHeight));
104- textarea.style.height = `${newHeight}px`;
105- }, [input]);
106-
10779 const isAnyLoading = isLoading || isLoadingMessages;
10880
10981 const handleSubmit = (e?: React.FormEvent) => {
@@ -115,46 +87,19 @@ const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
11587 const inputValue = input.trim();
11688 onSubmit(inputValue);
11789 setInput("");
118- setHistoryIndex(null);
90+ resetHistory();
11991
120- // Reset textarea height after submit
12192 if (textareaRef.current) {
12293 textareaRef.current.style.height = "auto";
12394 }
12495 };
12596
126- const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
127- // Only cycle through user messages, not assistant messages
128- const userMessages = messages.filter((m) => m.role === "user");
129-
97+ const handleKeyDownWithSubmit = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
13098 if (e.key === "Enter" && !e.shiftKey) {
13199 e.preventDefault();
132100 handleSubmit();
133- } else if (e.key === "ArrowUp" && (e.ctrlKey || e.metaKey)) {
134- e.preventDefault();
135- // Save current input as draft if we're starting to cycle from the current state
136- if (historyIndex === null) {
137- draftInputRef.current = input;
138- }
139- const idx = historyIndex ?? userMessages.length;
140- if (idx > 0) {
141- const newIdx = idx - 1;
142- setHistoryIndex(newIdx);
143- setInput(userMessages[newIdx].content);
144- }
145- } else if (e.key === "ArrowDown" && (e.ctrlKey || e.metaKey)) {
146- e.preventDefault();
147- if (historyIndex !== null) {
148- const newIdx = historyIndex + 1;
149- if (newIdx < userMessages.length) {
150- setHistoryIndex(newIdx);
151- setInput(userMessages[newIdx].content);
152- } else {
153- // Restore draft input when cycling back to current state
154- setHistoryIndex(null);
155- setInput(draftInputRef.current);
156- }
157- }
101+ } else {
102+ handleKeyDown(e);
158103 }
159104 };
160105
@@ -201,7 +146,7 @@ const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
201146 ref={textareaRef}
202147 value={input}
203148 onChange={(e) => setInput(e.target.value)}
204- onKeyDown={handleKeyDown}
149+ onKeyDown={handleKeyDownWithSubmit}
205150 placeholder={isAnyLoading ? "" : "Type a message..."}
206151 disabled={isAnyLoading}
207152 rows={3}