@@ -1,6 +1,6 @@
11 "use client";
22
3-import { useState } from "react";
3+import { useState, useRef, useImperativeHandle, forwardRef } from "react";
44 import PaperPlaneIcon from "@/src/components/icons/PaperPlaneIcon";
55 import type { Message } from "@/src/types";
66
@@ -10,66 +10,84 @@ type MessageInputProps = {
1010 onSubmit: (message: string) => void;
1111 };
1212
13-export default function MessageInput({ isLoading, messages, onSubmit }: MessageInputProps) {
14- const [input, setInput] = useState("");
15- const [historyIndex, setHistoryIndex] = useState<number | null>(null);
16-
17- const handleSubmit = (e: React.FormEvent) => {
18- e.preventDefault();
19- if (!input.trim() || isLoading) return;
13+export type MessageInputRef = {
14+ focus: () => void;
15+};
2016
21- const inputValue = input.trim();
22- onSubmit(inputValue);
23- setInput("");
24- setHistoryIndex(null);
25- };
17+const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
18+ ({ isLoading, messages, onSubmit }, ref) => {
19+ const [input, setInput] = useState("");
20+ const [historyIndex, setHistoryIndex] = useState<number | null>(null);
21+ const inputRef = useRef<HTMLInputElement>(null);
2622
27- const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
28- // Only cycle through user messages, not assistant messages
29- const userMessages = messages.filter((m) => m.role === "user");
23+ useImperativeHandle(ref, () => ({
24+ focus: () => {
25+ inputRef.current?.focus();
26+ },
27+ }));
3028
31- if (e.key === "ArrowUp") {
29+ const handleSubmit = (e: React.FormEvent) => {
3230 e.preventDefault();
33- const idx = historyIndex ?? userMessages.length;
34- if (idx > 0) {
35- const newIdx = idx - 1;
36- setHistoryIndex(newIdx);
37- setInput(userMessages[newIdx].content);
38- }
39- } else if (e.key === "ArrowDown") {
40- e.preventDefault();
41- if (historyIndex !== null) {
42- const newIdx = historyIndex + 1;
43- if (newIdx < userMessages.length) {
31+ if (!input.trim() || isLoading) return;
32+
33+ const inputValue = input.trim();
34+ onSubmit(inputValue);
35+ setInput("");
36+ setHistoryIndex(null);
37+ };
38+
39+ const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
40+ // Only cycle through user messages, not assistant messages
41+ const userMessages = messages.filter((m) => m.role === "user");
42+
43+ if (e.key === "ArrowUp") {
44+ e.preventDefault();
45+ const idx = historyIndex ?? userMessages.length;
46+ if (idx > 0) {
47+ const newIdx = idx - 1;
4448 setHistoryIndex(newIdx);
4549 setInput(userMessages[newIdx].content);
46- } else {
47- setHistoryIndex(null);
48- setInput("");
50+ }
51+ } else if (e.key === "ArrowDown") {
52+ e.preventDefault();
53+ if (historyIndex !== null) {
54+ const newIdx = historyIndex + 1;
55+ if (newIdx < userMessages.length) {
56+ setHistoryIndex(newIdx);
57+ setInput(userMessages[newIdx].content);
58+ } else {
59+ setHistoryIndex(null);
60+ setInput("");
61+ }
4962 }
5063 }
51- }
52- };
64+ };
65+
66+ return (
67+ <form onSubmit={handleSubmit} className="flex shadow-[0_-4px_6px_-1px_rgba(0,0,0,0.1),0_-2px_4px_-1px_rgba(0,0,0,0.06)] dark:shadow-[0_-4px_6px_-1px_rgba(0,0,0,0.3),0_-2px_4px_-1px_rgba(0,0,0,0.2)]">
68+ <input
69+ ref={inputRef}
70+ type="text"
71+ value={input}
72+ onChange={(e) => setInput(e.target.value)}
73+ onKeyDown={handleKeyDown}
74+ placeholder={isLoading ? "Loading answer..." : "Type a message..."}
75+ disabled={isLoading}
76+ className="flex-1 bg-neutral-200 px-2 py-2 placeholder:italic placeholder:text-neutral-600 focus:outline-none disabled:opacity-50 disabled:cursor-not-allowed dark:bg-neutral-900 dark:placeholder:text-neutral-400"
77+ />
78+ <button
79+ type="submit"
80+ disabled={isLoading}
81+ className="bg-green-900 px-3 py-1 text-neutral-100 hover:bg-green-800 disabled:opacity-50 disabled:cursor-not-allowed dark:bg-green-500 dark:text-neutral-900 dark:hover:bg-green-600"
82+ >
83+ <PaperPlaneIcon className={`h-5 w-5 ${isLoading ? "animate-spin" : ""}`} />
84+ </button>
85+ </form>
86+ );
87+ }
88+);
89+
90+MessageInput.displayName = "MessageInput";
5391
54- return (
55- <form onSubmit={handleSubmit} className="flex shadow-[0_-4px_6px_-1px_rgba(0,0,0,0.1),0_-2px_4px_-1px_rgba(0,0,0,0.06)] dark:shadow-[0_-4px_6px_-1px_rgba(0,0,0,0.3),0_-2px_4px_-1px_rgba(0,0,0,0.2)]">
56- <input
57- type="text"
58- value={input}
59- onChange={(e) => setInput(e.target.value)}
60- onKeyDown={handleKeyDown}
61- placeholder={isLoading ? "Loading answer..." : "Type a message..."}
62- disabled={isLoading}
63- className="flex-1 bg-neutral-200 px-2 py-2 placeholder:italic placeholder:text-neutral-600 focus:outline-none disabled:opacity-50 disabled:cursor-not-allowed dark:bg-neutral-900 dark:placeholder:text-neutral-400"
64- />
65- <button
66- type="submit"
67- disabled={isLoading}
68- className="bg-green-900 px-3 py-1 text-neutral-100 hover:bg-green-800 disabled:opacity-50 disabled:cursor-not-allowed dark:bg-green-500 dark:text-neutral-900 dark:hover:bg-green-600"
69- >
70- <PaperPlaneIcon className={`h-5 w-5 ${isLoading ? "animate-spin" : ""}`} />
71- </button>
72- </form>
73- );
74-}
92+export default MessageInput;