'use client'; import { useState, useRef, useEffect } from 'react'; import { Box, TextField, IconButton, Paper, Typography, Avatar, CircularProgress, Chip, } from '@mui/material'; import { Send, SmartToy, Person, AutoAwesome } from '@mui/icons-material'; import { motion, AnimatePresence } from 'framer-motion'; import { useAuth } from '@/lib/auth/AuthContext'; import apiClient from '@/lib/api/client'; import ReactMarkdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; interface Message { id: string; role: 'user' | 'assistant'; content: string; timestamp: Date; } const suggestedQuestions = [ 'How much should my baby sleep at 3 months?', 'What are normal feeding patterns?', 'When should I introduce solid foods?', 'Tips for better sleep routine', ]; const thinkingMessages = [ 'Gathering baby wisdom...', 'Consulting the baby books...', 'Mixing up the perfect answer...', 'Warming up some advice...', 'Preparing your bottle of knowledge...', 'Counting tiny fingers and toes...', 'Connecting the building blocks...', 'Peeking into the toy box...', 'Arranging the puzzle pieces...', 'Stirring the baby food jar...', 'Polishing the pacifier of wisdom...', 'Tiptoeing through naptime...', 'Organizing the diaper bag...', 'Wrapping up your answer with love...', 'Brewing a warm cup of guidance...', 'Knitting together some thoughts...', 'Tucking in the details...', 'Sprinkling some magic dust...', 'Humming a lullaby while I think...', ]; // Get a random selection of 3-5 thinking messages const getRandomThinkingMessages = () => { const count = Math.floor(Math.random() * 3) + 3; // 3 to 5 const shuffled = [...thinkingMessages].sort(() => Math.random() - 0.5); return shuffled.slice(0, count); }; export const AIChatInterface: React.FC = () => { const [messages, setMessages] = useState([]); const [input, setInput] = useState(''); const [isLoading, setIsLoading] = useState(false); const [currentThinkingMessages, setCurrentThinkingMessages] = useState([]); const [currentThinkingIndex, setCurrentThinkingIndex] = useState(0); const messagesEndRef = useRef(null); const thinkingIntervalRef = useRef(null); const { user } = useAuth(); const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }; useEffect(() => { scrollToBottom(); }, [messages]); // Cycle through thinking messages while loading useEffect(() => { if (isLoading) { const randomMessages = getRandomThinkingMessages(); setCurrentThinkingMessages(randomMessages); setCurrentThinkingIndex(0); thinkingIntervalRef.current = setInterval(() => { setCurrentThinkingIndex((prev) => (prev + 1) % randomMessages.length); }, 2000); // Change message every 2 seconds } else { if (thinkingIntervalRef.current) { clearInterval(thinkingIntervalRef.current); thinkingIntervalRef.current = null; } setCurrentThinkingMessages([]); setCurrentThinkingIndex(0); } return () => { if (thinkingIntervalRef.current) { clearInterval(thinkingIntervalRef.current); } }; }, [isLoading]); const handleSend = async (message?: string) => { const messageText = message || input.trim(); if (!messageText || isLoading) return; const userMessage: Message = { id: Date.now().toString(), role: 'user', content: messageText, timestamp: new Date(), }; setMessages((prev) => [...prev, userMessage]); setInput(''); setIsLoading(true); try { const response = await apiClient.post('/api/v1/ai/chat', { message: messageText, conversationId: null, }); const assistantMessage: Message = { id: (Date.now() + 1).toString(), role: 'assistant', content: response.data.data.message, timestamp: new Date(), }; setMessages((prev) => [...prev, assistantMessage]); } catch (error) { console.error('AI chat error:', error); const errorMessage: Message = { id: (Date.now() + 1).toString(), role: 'assistant', content: 'Sorry, I encountered an error. Please try again.', timestamp: new Date(), }; setMessages((prev) => [...prev, errorMessage]); } finally { setIsLoading(false); } }; const handleSuggestedQuestion = (question: string) => { handleSend(question); }; return ( {/* Header */} AI Parenting Assistant Ask me anything about parenting and childcare {/* Messages Container */} {messages.length === 0 && ( Hi {user?.name}! How can I help you today? {suggestedQuestions.map((question, index) => ( handleSuggestedQuestion(question)} sx={{ borderRadius: 3, '&:hover': { bgcolor: 'primary.light', color: 'white', }, }} /> ))} )} {messages.map((message) => ( {message.role === 'assistant' && ( )} {message.role === 'assistant' ? ( {message.content} ) : ( {message.content} )} {message.timestamp.toLocaleTimeString()} {message.role === 'user' && ( )} ))} {isLoading && ( {currentThinkingMessages[currentThinkingIndex] || 'Thinking...'} )}
{/* Input Area */} setInput(e.target.value)} onKeyPress={(e) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend(); } }} disabled={isLoading} sx={{ '& .MuiOutlinedInput-root': { borderRadius: 3, }, }} /> handleSend()} disabled={!input.trim() || isLoading} sx={{ width: 48, height: 48, bgcolor: 'primary.main', color: 'white', '&:hover': { bgcolor: 'primary.dark', }, '&:disabled': { bgcolor: 'action.disabledBackground', }, }} > This AI assistant provides general information. Always consult healthcare professionals for medical advice. ); };