|
| 1 | +import { useState } from "react"; |
| 2 | +import { useParams } from "react-router-dom"; |
| 3 | +import { useMutation } from "@tanstack/react-query"; |
| 4 | +import { Input } from "@/components/ui/input"; |
| 5 | +import { Button } from "@/components/ui/button"; |
| 6 | +import "./App.css"; |
| 7 | + |
| 8 | +type TextResponse = { |
| 9 | + text: string; |
| 10 | + user: string; |
| 11 | +}; |
| 12 | + |
| 13 | +export default function Chat() { |
| 14 | + const { agentId } = useParams(); |
| 15 | + const [input, setInput] = useState(""); |
| 16 | + const [messages, setMessages] = useState<TextResponse[]>([]); |
| 17 | + |
| 18 | + const mutation = useMutation({ |
| 19 | + mutationFn: async (text: string) => { |
| 20 | + const res = await fetch(`/api/${agentId}/message`, { |
| 21 | + method: "POST", |
| 22 | + headers: { |
| 23 | + "Content-Type": "application/json", |
| 24 | + }, |
| 25 | + body: JSON.stringify({ |
| 26 | + text, |
| 27 | + userId: "user", |
| 28 | + roomId: `default-room-${agentId}`, |
| 29 | + }), |
| 30 | + }); |
| 31 | + return res.json() as Promise<TextResponse[]>; |
| 32 | + }, |
| 33 | + onSuccess: (data) => { |
| 34 | + setMessages((prev) => [...prev, ...data]); |
| 35 | + }, |
| 36 | + }); |
| 37 | + |
| 38 | + const handleSubmit = async (e: React.FormEvent) => { |
| 39 | + e.preventDefault(); |
| 40 | + if (!input.trim()) return; |
| 41 | + |
| 42 | + // Add user message immediately to state |
| 43 | + const userMessage: TextResponse = { |
| 44 | + text: input, |
| 45 | + user: "user", |
| 46 | + }; |
| 47 | + setMessages((prev) => [...prev, userMessage]); |
| 48 | + |
| 49 | + mutation.mutate(input); |
| 50 | + setInput(""); |
| 51 | + }; |
| 52 | + |
| 53 | + return ( |
| 54 | + <div className="flex flex-col h-screen max-h-screen w-full"> |
| 55 | + <div className="flex-1 min-h-0 overflow-y-auto p-4"> |
| 56 | + <div className="max-w-3xl mx-auto space-y-4"> |
| 57 | + {messages.length > 0 ? ( |
| 58 | + messages.map((message, index) => ( |
| 59 | + <div |
| 60 | + key={index} |
| 61 | + className={`flex ${ |
| 62 | + message.user === "user" |
| 63 | + ? "justify-end" |
| 64 | + : "justify-start" |
| 65 | + }`} |
| 66 | + > |
| 67 | + <div |
| 68 | + className={`max-w-[80%] rounded-lg px-4 py-2 ${ |
| 69 | + message.user === "user" |
| 70 | + ? "bg-primary text-primary-foreground" |
| 71 | + : "bg-muted" |
| 72 | + }`} |
| 73 | + > |
| 74 | + {message.text} |
| 75 | + </div> |
| 76 | + </div> |
| 77 | + )) |
| 78 | + ) : ( |
| 79 | + <div className="text-center text-muted-foreground"> |
| 80 | + No messages yet. Start a conversation! |
| 81 | + </div> |
| 82 | + )} |
| 83 | + </div> |
| 84 | + </div> |
| 85 | + |
| 86 | + <div className="border-t p-4 bg-background"> |
| 87 | + <div className="max-w-3xl mx-auto"> |
| 88 | + <form onSubmit={handleSubmit} className="flex gap-2"> |
| 89 | + <Input |
| 90 | + value={input} |
| 91 | + onChange={(e) => setInput(e.target.value)} |
| 92 | + placeholder="Type a message..." |
| 93 | + className="flex-1" |
| 94 | + disabled={mutation.isPending} |
| 95 | + /> |
| 96 | + <Button type="submit" disabled={mutation.isPending}> |
| 97 | + {mutation.isPending ? "..." : "Send"} |
| 98 | + </Button> |
| 99 | + </form> |
| 100 | + </div> |
| 101 | + </div> |
| 102 | + </div> |
| 103 | + ); |
| 104 | +} |
0 commit comments