'use client'; import { useAuth } from '@/contexts/AuthContext'; import { useRouter } from 'next/navigation'; import { useEffect, useState, useRef, FormEvent } from 'react'; interface Message { id: string; role: 'user' | 'assistant'; content: string; timestamp: Date; } export default function ChatPage() { const { user, isLoading } = useAuth(); const router = useRouter(); const [messages, setMessages] = useState([]); const [input, setInput] = useState(''); const [sending, setSending] = useState(false); const messagesEndRef = useRef(null); useEffect(() => { if (!isLoading && !user) { router.push('/auth'); } }, [user, isLoading, router]); useEffect(() => { // Scroll to bottom when messages change messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }, [messages]); if (isLoading) { return (
Loading...
); } if (!user) return null; const handleSendMessage = async (e: FormEvent) => { e.preventDefault(); if (!input.trim() || sending) return; const userMessage: Message = { id: Date.now().toString(), role: 'user', content: input.trim(), timestamp: new Date(), }; setMessages((prev) => [...prev, userMessage]); setInput(''); setSending(true); // Simulate AI response (replace with real AI later) setTimeout(() => { const aiMessage: Message = { id: (Date.now() + 1).toString(), role: 'assistant', content: `Hola! I received your message: "${userMessage.content}". I'm your AI companion, ready to help! (AI integration coming soon)`, timestamp: new Date(), }; setMessages((prev) => [...prev, aiMessage]); setSending(false); }, 1000); }; return (
{/* Navigation */} {/* Chat Messages */}
{messages.length === 0 ? (
👋

Welcome to HolaCompi!

Start a conversation with your AI companion

) : ( messages.map((message) => (
{message.role === 'user' ? '👤' : '🤖'}

{message.role === 'user' ? 'You' : 'HolaCompi'}

{message.content}

{message.timestamp.toLocaleTimeString()}

)) )} {sending && (
🤖
)}
{/* Input Area */}
setInput(e.target.value)} placeholder="Type your message..." className="flex-1 rounded-xl px-6 py-4 bg-black/30 border border-white/10 text-white placeholder-gray-500 outline-none focus:border-purple-400 transition-colors" disabled={sending} />
); }