'use client' import { Container, Grid, Card, CardContent, Typography, Box, TextField, Button, Paper, List, ListItem, Avatar, Chip, IconButton, Divider, useTheme, CircularProgress, Skeleton, } from '@mui/material' import { Chat, Send, Person, SmartToy, ContentCopy, ThumbUp, ThumbDown, Refresh, } from '@mui/icons-material' import { useState, useRef, useEffect } from 'react' import { useTranslations, useLocale } from 'next-intl' interface ChatMessage { id: string role: 'user' | 'assistant' content: string timestamp: Date } export default function ChatPage() { const theme = useTheme() const t = useTranslations('chat') const locale = useLocale() const [messages, setMessages] = useState([ { id: '1', role: 'assistant', content: t('subtitle'), timestamp: new Date(), } ]) const [inputMessage, setInputMessage] = useState('') const [isLoading, setIsLoading] = useState(false) const messagesEndRef = useRef(null) const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }) } useEffect(() => { scrollToBottom() }, [messages]) const handleSendMessage = async () => { if (!inputMessage.trim() || isLoading) return const userMessage: ChatMessage = { id: Date.now().toString(), role: 'user', content: inputMessage, timestamp: new Date(), } setMessages(prev => [...prev, userMessage]) setInputMessage('') setIsLoading(true) try { const response = await fetch('/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ message: inputMessage, locale, history: messages.slice(-5).map(m => ({ id: m.id, role: m.role, content: m.content, timestamp: m.timestamp.toISOString(), })), }), }) if (!response.ok) { throw new Error('Failed to get response') } const data = await response.json() const assistantMessage: ChatMessage = { id: (Date.now() + 1).toString(), role: 'assistant', content: data.response || t('error') || 'Error', timestamp: new Date(), } setMessages(prev => [...prev, assistantMessage]) } catch (error) { console.error('Error sending message:', error) const errorMessage: ChatMessage = { id: (Date.now() + 1).toString(), role: 'assistant', content: t('error') || 'Error', timestamp: new Date(), } setMessages(prev => [...prev, errorMessage]) } finally { setIsLoading(false) } } const handleKeyPress = (event: React.KeyboardEvent) => { if (event.key === 'Enter' && !event.shiftKey) { event.preventDefault() handleSendMessage() } } const copyToClipboard = (text: string) => { navigator.clipboard.writeText(text) } const suggestedQuestions = [ 'Ce spune Biblia despre iubire?', 'Explică-mi parabola semănătorului', 'Care sunt fructele Duhului?', 'Ce înseamnă să fii născut din nou?', 'Cum pot să mă rog mai bine?', ] return ( {/* Header */} {t('title')} {t('subtitle')} {/* Suggested Questions Sidebar */} {t('suggestions.title')} {t('suggestions.title')} { (t.raw('suggestions.questions') as string[]).map((question, index) => ( setInputMessage(question)} sx={{ mb: 1, mr: 1, cursor: 'pointer', '&:hover': { bgcolor: 'primary.light', color: 'white', }, }} variant="outlined" size="small" /> ))} {/* Tips section can be localized later via messages */} {/* Main Chat Area */} {/* Chat Messages */} {messages.map((message) => ( {message.role === 'user' ? : } {message.content} {message.role === 'assistant' && ( copyToClipboard(message.content)} title="Copiază răspunsul" > )} {message.timestamp.toLocaleTimeString(locale === 'en' ? 'en-US' : 'ro-RO', { hour: '2-digit', minute: '2-digit', })} ))} {isLoading && ( {t('loading')} )}
{/* Message Input */} setInputMessage(e.target.value)} onKeyPress={handleKeyPress} disabled={isLoading} variant="outlined" /> {t('enterToSend')} ) }