| // components/ChatPanel.tsx | |
| import React, { useState, useEffect, useRef } from "react"; | |
| import { useResearchWebSocket } from "@/hooks/useResearchWebSocket"; | |
| import { Button } from "@/components/ui/Button"; | |
| import { Input } from "@/components/ui/Input"; | |
| import { Card } from "@/components/ui/Card"; | |
| export function ChatPanel() { | |
| const [input, setInput] = useState(""); | |
| const [messages, setMessages] = useState< | |
| Array<{ role: "user" | "assistant"; content: string }> | |
| >([]); | |
| const { isConnected, response, sendMessage } = useResearchWebSocket(); | |
| const messagesEndRef = useRef<HTMLDivElement>(null); | |
| useEffect(() => { | |
| if (response) { | |
| setMessages((prev) => { | |
| const last = prev[prev.length - 1]; | |
| if (last?.role === "assistant") { | |
| const updated = [...prev]; | |
| updated[updated.length - 1] = { ...last, content: response }; | |
| return updated; | |
| } else { | |
| return [...prev, { role: "assistant", content: response }]; | |
| } | |
| }); | |
| } | |
| }, [response]); | |
| useEffect(() => { | |
| messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); | |
| }, [messages]); | |
| const handleSend = () => { | |
| if (!input.trim()) return; | |
| setMessages((prev) => [...prev, { role: "user", content: input }]); | |
| sendMessage(input); | |
| setInput(""); | |
| }; | |
| return ( | |
| <Card className="flex flex-col h-[600px] w-full max-w-2xl"> | |
| <div className="flex-1 overflow-y-auto p-4 space-y-4"> | |
| {messages.map((msg, idx) => ( | |
| <div | |
| key={idx} | |
| className={`flex ${msg.role === "user" ? "justify-end" : "justify-start"}`} | |
| > | |
| <div | |
| className={`max-w-[80%] rounded-lg p-3 ${msg.role === "user" ? "bg-blue-600 text-white" : "bg-gray-100 text-gray-800"}`} | |
| > | |
| {msg.content} | |
| </div> | |
| </div> | |
| ))} | |
| <div ref={messagesEndRef} /> | |
| </div> | |
| <div className="border-t p-4 flex gap-2"> | |
| <Input | |
| value={input} | |
| onChange={(e) => setInput(e.target.value)} | |
| onKeyDown={(e) => e.key === "Enter" && handleSend()} | |
| placeholder="Ask about your experiments..." | |
| disabled={!isConnected} | |
| /> | |
| <Button onClick={handleSend} disabled={!isConnected}> | |
| Send | |
| </Button> | |
| </div> | |
| </Card> | |
| ); | |
| } | |
Xet Storage Details
- Size:
- 2.35 kB
- Xet hash:
- 21caf829921c9a074ca6148996fb2b6b4f74b115b2b8faebe3d03188c597395c
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.