xonecas.io
53129
Pa

PAT / commit dfaa4ea

feat: focus the input on new thread

xonecas2025-12-16 dfaa4eafdc6e3051d41f669a337c85d3725c201e parent 09f8835

2 files changed

app/page.tsx+7−2
@@ -4,7 +4,7 @@ import ReactMarkdown from "react-markdown";
44 import remarkGfm from "remark-gfm";
55 import rehypeHighlight from "rehype-highlight";
66 import Sidebar from "@/src/components/Sidebar";
7-import MessageInput from "@/src/components/MessageInput";
7+import MessageInput, { type MessageInputRef } from "@/src/components/MessageInput";
88 import NoThreadSelected from "@/src/components/NoThreadSelected";
99 import { useTheme } from "@/src/hooks/useTheme";
1010 import type { Thread, Message } from "@/src/types";
@@ -19,6 +19,7 @@ export default function Home() {
1919 const messagesContainerRef = useRef<HTMLDivElement>(null);
2020 const messageRefs = useRef<Map<number, HTMLDivElement>>(new Map());
2121 const prevMessagesLengthRef = useRef<number>(0);
22+ const messageInputRef = useRef<MessageInputRef>(null);
2223
2324
2425 useEffect(() => {
@@ -99,6 +100,10 @@ export default function Home() {
99100 setCurrentThreadId(null);
100101 setMessages([]);
101102 prevMessagesLengthRef.current = 0;
103+ // Focus the input after a short delay to ensure DOM has updated
104+ setTimeout(() => {
105+ messageInputRef.current?.focus();
106+ }, 0);
102107 };
103108
104109 const sendMessage = async (message: string) => {
@@ -218,7 +223,7 @@ export default function Home() {
218223 )}
219224 </div>
220225 </div>
221- <MessageInput isLoading={isLoading} messages={messages} onSubmit={sendMessage} />
226+ <MessageInput ref={messageInputRef} isLoading={isLoading} messages={messages} onSubmit={sendMessage} />
222227 </div>
223228 <Sidebar
224229 threads={threads}
src/components/MessageInput.tsx+72−54
@@ -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;