import React, { useState, useEffect, useRef } from 'react'; const getApiBase = () => { if (import.meta.env.VITE_API_URL) return import.meta.env.VITE_API_URL; return window.location.origin + "/api"; }; const getWsBase = () => { if (import.meta.env.VITE_WS_URL) return import.meta.env.VITE_WS_URL; const protocol = window.location.protocol === "https:" ? "wss:" : "ws:"; return `${protocol}//${window.location.host}/ws`; }; const API_BASE = getApiBase(); const WS_BASE = getWsBase(); export default function Chat() { const [me, setMe] = useState(null); const [messages, setMessages] = useState([]); const [text, setText] = useState(""); const ws = useRef(null); const chatRef = useRef(null); const target = me === "Alice" ? "Bob" : "Alice"; useEffect(() => { if (!me) return; fetch(`${API_BASE}/messages/${me}/${target}`).then(r => r.json()).then(setMessages); let alive = true; const connect = () => { ws.current = new WebSocket(`${WS_BASE}/${me}`); ws.current.onmessage = (e) => { const msg = JSON.parse(e.data); setMessages(prev => { const match = prev.find(m => (msg._id && m._id === msg._id) || (msg.client_id && m.client_id === msg.client_id)); return match ? prev.map(m => ((msg._id && m._id === msg._id) || (msg.client_id && m.client_id === msg.client_id)) ? { ...m, ...msg } : m) : [...prev, msg]; }); }; ws.current.onclose = () => alive && setTimeout(connect, 2000); }; connect(); return () => { alive = false; ws.current?.close(); }; }, [me]); useEffect(() => { chatRef.current?.scrollTo(0, chatRef.current.scrollHeight); }, [messages]); const send = () => { if (!text.trim() || !ws.current) return; const tempId = Date.now().toString(); const msg = { sender: me, receiver: target, text, timestamp: new Date().toISOString(), _id: tempId, client_id: tempId, emotion: null, trust_score: null, is_sarcasm: null }; setMessages(prev => [...prev, msg]); ws.current.send(JSON.stringify({ receiver: target, text, client_id: tempId })); setText(""); }; const aiText = (m) => { if (!m.emotion) return "analyzing..."; if (m.is_sarcasm) return "๐ Sarcasm โ ๏ธ"; const map = { joy: "๐", anger: "๐ก", fear: "๐จ", sadness: "๐ข", neutral: "๐", surprise: "๐ฒ" }; const label = m.emotion.charAt(0).toUpperCase() + m.emotion.slice(1); return `${map[m.emotion] || "๐"} ${label} ยท ${m.trust_score ?? 0}% trust`; }; if (!me) return (