import React, { useState, useRef, useEffect } from "react"; import { contentItems } from "../../public/data/content"; import { Send } from "lucide-react"; import { useNavigate } from "react-router-dom"; // Commenting out the backend API integration for now // import { useChatApi, ChatMessage } from "../hooks/useChatApi"; // Define the ChatMessage interface locally since we're not importing it interface ChatMessage { sender: string; text: string; imageUrl?: string; robotId?: string; // Add robotId to track which robot this message refers to } // Mock data for quadruped robots const quadrupedRobots = [ { name: "Go2", description: "Go1 is a lightweight, agile quadruped companion robot. It can follow you around, carry small items, and navigate complex indoor and outdoor environments. Perfect for research, education, or as a high-tech assistant in various settings.", imageUrl: "https://mizajlqhooderueazvnp.supabase.co/storage/v1/object/public/robotpicturesbucket/go2_description.png", id: "0f041a8f-88cf-4b9c-93ef-a30e8fe2fdb1", }, { name: "ANYmal", description: "ANYmal is a rugged quadrupedal robot designed for autonomous operation in challenging environments. With its sophisticated perception systems, it excels at inspection and monitoring tasks in industrial settings, even in hazardous areas unsafe for humans.", imageUrl: "https://mizajlqhooderueazvnp.supabase.co/storage/v1/object/public/robotpicturesbucket/anymal_b_description.png", id: "68e827db-4035-4ae0-a43c-158b610e21d5", }, ]; // Function to get a random wait time to simulate thinking const getRandomWaitTime = () => Math.floor(Math.random() * 150) + 50; // 50-200ms const ChatWindow: React.FC = () => { const [messages, setMessages] = useState([]); const [input, setInput] = useState(""); const [isLoading, setIsLoading] = useState(false); const chatContainerRef = useRef(null); const navigate = useNavigate(); // Commenting out the backend API integration // const { sendMessage, streamResponse, isLoading, error } = useChatApi(); const handleSend = async () => { if (input.trim()) { const userMessage = input.trim(); // Add user message to chat setMessages((prevMessages) => [ ...prevMessages, { sender: "User", text: userMessage, }, ]); setInput(""); setIsLoading(true); // Add empty bot message that will be filled with streaming response setMessages((prevMessages) => [ ...prevMessages, { sender: "Bot", text: "", }, ]); // Use our mock response generator instead of the API await generateMockResponse(userMessage); } }; // Generate a fake response about quadruped robots const generateMockResponse = async (userMessage: string) => { try { // Choose a relevant robot based on keywords in user message let selectedRobot; const userMessageLower = userMessage.toLowerCase(); if ( userMessageLower.includes("agile") || userMessageLower.includes("mobility") ) { selectedRobot = quadrupedRobots.find((robot) => robot.name === "Go2"); } else if ( userMessageLower.includes("companion") || userMessageLower.includes("small") ) { selectedRobot = quadrupedRobots.find((robot) => robot.name === "Go2"); } else if ( userMessageLower.includes("inspect") || userMessageLower.includes("monitor") || userMessageLower.includes("industrial") ) { selectedRobot = quadrupedRobots.find( (robot) => robot.name === "ANYmal" ); } else { // If no specific match, pick a random robot const randomIndex = Math.floor(Math.random() * quadrupedRobots.length); selectedRobot = quadrupedRobots[randomIndex]; } // Generate response text const responseIntro = getResponseIntro(userMessage); const fullResponse = `${responseIntro} ${selectedRobot.name}. ${selectedRobot.description}`; // Simulate streaming by adding words one by one with small delays const words = fullResponse.split(" "); for (const word of words) { await new Promise((resolve) => setTimeout(resolve, getRandomWaitTime()) ); setMessages((prevMessages) => { const updatedMessages = [...prevMessages]; const lastMessage = updatedMessages[updatedMessages.length - 1]; if (lastMessage.sender === "Bot") { lastMessage.text += word + " "; } return updatedMessages; }); } // Add the image and robot ID at the end await new Promise((resolve) => setTimeout(resolve, 300)); setMessages((prevMessages) => { const updatedMessages = [...prevMessages]; const lastMessage = updatedMessages[updatedMessages.length - 1]; if (lastMessage.sender === "Bot") { lastMessage.imageUrl = selectedRobot.imageUrl; lastMessage.robotId = selectedRobot.id; // Store the robot ID for navigation } return updatedMessages; }); } catch (error) { console.error("Error generating mock response:", error); // Handle error by providing a generic fallback setMessages((prevMessages) => { const updatedMessages = [...prevMessages]; const lastMessage = updatedMessages[updatedMessages.length - 1]; if (lastMessage.sender === "Bot") { lastMessage.text = "I'm sorry, I couldn't process your request. Please try asking about robots in a different way."; } return updatedMessages; }); } finally { setIsLoading(false); } }; // Navigate to content detail page const navigateToRobotDetail = (robotId: string) => { if (robotId) { navigate(`/content/${robotId}`); } }; // Get varied intro phrases to make responses seem more natural const getResponseIntro = (userMessage: string) => { const introOptions = [ "Based on your query, I recommend", "I think you might be interested in", "A great quadruped robot for your needs is", "You should check out", "Have you considered", "I'd suggest looking at", ]; const randomIndex = Math.floor(Math.random() * introOptions.length); return introOptions[randomIndex]; }; // Fallback response is no longer needed since we're using mock responses useEffect(() => { if (chatContainerRef.current) { setTimeout(() => { chatContainerRef.current!.scrollTop = chatContainerRef.current!.scrollHeight; }, 50); // Add a slight delay to ensure the DOM is updated } }, [messages]); return (
{messages.length === 0 && (
Ask me about robots, and I'll help you find the perfect fit for your needs. NOTE: For this demo, I only know about Go2 and ANYmal + don't want to use up all my LLM credits.
)} {messages.map((message, index) => (
{message.sender === "User" ? "You" : "Assistant"}
{message.text} {message.imageUrl && (
message.robotId && navigateToRobotDetail(message.robotId) } title="Click to view robot details" > Robot
Click to view details
)}
))} {isLoading && messages.length > 0 && !messages[messages.length - 1].text && (
)}