xonecas.io
53129
Pa

PAT / commit 4096282

feat: loading state

xonecas2025-12-16 40962822573b28890c907bff841642075382ab30 parent 6c50ead

1 file changed

app/page.tsx+36−25
@@ -27,6 +27,7 @@ export default function Home() {
2727 const [isDark, setIsDark] = useState(false);
2828 const [currentThreadId, setCurrentThreadId] = useState<number | null>(null);
2929 const [threads, setThreads] = useState<Thread[]>([]);
30+ const [isLoading, setIsLoading] = useState(false);
3031 const messagesEndRef = useRef<HTMLDivElement>(null);
3132 const messagesContainerRef = useRef<HTMLDivElement>(null);
3233
@@ -124,8 +125,9 @@ export default function Home() {
124125 const [historyIndex, setHistoryIndex] = useState<number | null>(null);
125126 const sendMessage = async (e: React.FormEvent) => {
126127 e.preventDefault();
127- if (!input.trim()) return;
128+ if (!input.trim() || isLoading) return;
128129
130+ setIsLoading(true);
129131 let threadId = currentThreadId;
130132 if (!threadId) {
131133 // Create a new thread if none exists
@@ -145,6 +147,7 @@ export default function Home() {
145147 setThreads(threadsData.threads || []);
146148 } catch (error) {
147149 console.error("Failed to create thread", error);
150+ setIsLoading(false);
148151 return;
149152 }
150153 }
@@ -161,30 +164,36 @@ export default function Home() {
161164 if (!inputValue) return;
162165 setInput("");
163166
164- const res = await fetch("/api/chat", {
165- method: "POST",
166- headers: { "Content-Type": "application/json" },
167- body: JSON.stringify({ message: inputValue, threadId }),
168- });
167+ try {
168+ const res = await fetch("/api/chat", {
169+ method: "POST",
170+ headers: { "Content-Type": "application/json" },
171+ body: JSON.stringify({ message: inputValue, threadId }),
172+ });
169173
170- const data = await res.json();
174+ const data = await res.json();
171175
172- const botMsg: Message = {
173- id: Date.now() + 1,
174- threadId: threadId!,
175- role: "assistant",
176- content: data.answer || "",
177- createdAt: new Date(),
178- };
179- setMessages((prev) => [...prev, botMsg]);
176+ const botMsg: Message = {
177+ id: Date.now() + 1,
178+ threadId: threadId!,
179+ role: "assistant",
180+ content: data.answer || "",
181+ createdAt: new Date(),
182+ };
183+ setMessages((prev) => [...prev, botMsg]);
180184
181- // Reload messages to get correct IDs from database
182- await loadMessages(threadId!);
185+ // Reload messages to get correct IDs from database
186+ await loadMessages(threadId!);
183187
184- // Reload threads to update the order
185- const threadsRes = await fetch("/api/threads");
186- const threadsData = await threadsRes.json();
187- setThreads(threadsData.threads || []);
188+ // Reload threads to update the order
189+ const threadsRes = await fetch("/api/threads");
190+ const threadsData = await threadsRes.json();
191+ setThreads(threadsData.threads || []);
192+ } catch (error) {
193+ console.error("Failed to send message", error);
194+ } finally {
195+ setIsLoading(false);
196+ }
188197
189198 setInput("");
190199 setHistoryIndex(null);
@@ -259,14 +268,16 @@ Once you've created a thread, you can start chatting with me by typing a message
259268 value={input}
260269 onChange={(e) => setInput(e.target.value)}
261270 onKeyDown={handleKeyDown}
262- placeholder="Type a message..."
263- className="flex-1 bg-slate-300 px-3 py-2 placeholder:italic placeholder:text-slate-600 focus:outline-none dark:bg-slate-950 dark:placeholder:text-slate-400"
271+ placeholder={isLoading ? "Loading answer..." : "Type a message..."}
272+ disabled={isLoading}
273+ className="flex-1 bg-slate-300 px-3 py-2 placeholder:italic placeholder:text-slate-600 focus:outline-none disabled:opacity-50 disabled:cursor-not-allowed dark:bg-slate-950 dark:placeholder:text-slate-400"
264274 />
265275 <button
266276 type="submit"
267- className="bg-emerald-900 px-4 py-2 text-slate-100 hover:bg-emerald-800 dark:bg-emerald-500 dark:text-slate-900 dark:hover:bg-emerald-600"
277+ disabled={isLoading}
278+ className="bg-emerald-900 px-4 py-2 text-slate-100 hover:bg-emerald-800 disabled:opacity-50 disabled:cursor-not-allowed dark:bg-emerald-500 dark:text-slate-900 dark:hover:bg-emerald-600"
268279 >
269- <PaperPlaneIcon className="h-5 w-5" />
280+ <PaperPlaneIcon className={`h-5 w-5 ${isLoading ? "animate-spin" : ""}`} />
270281 </button>
271282 </form>
272283 </div>