/* eslint-disable */ // [TSM.ID].[11031972] -- All Rights Reserved. Proprietary & Confidential. "use client"; import { useEffect, useState, useRef } from "react"; import { XCUTelepathyMatrix, DecryptedMessage } from "../lib/xcu-telepathy-matrix"; export function XCUltraChat({ roomName, username }: { roomName: string, username: string }) { const [logs, setLogs] = useState([]); const [isMatrixActive, setIsMatrixActive] = useState(false); const [chatInput, setChatInput] = useState(""); const [chatMessages, setChatMessages] = useState<{id: string, sender: string, text: string, time: string}[]>([]); const [typingUsers, setTypingUsers] = useState([]); const [activeReactions, setActiveReactions] = useState<{id: number, type: string, ts: number}[]>([]); const chatEndRef = useRef(null); const matrixRef = useRef(null); const addLog = (msg: string) => { setLogs(prev => [...prev, msg]); }; const setStatus = (s: string) => addLog(`[SYS] ${s}`); useEffect(() => { if (chatEndRef.current) { chatEndRef.current.scrollIntoView({ behavior: 'smooth' }); } }, [chatMessages, typingUsers]); useEffect(() => { let matrix: XCUTelepathyMatrix | null = null; const initMatrix = async () => { try { setStatus("Memulai Koneksi XCU Telepathy Matrix (XTM) E2EE..."); matrix = new XCUTelepathyMatrix(roomName); matrixRef.current = matrix; matrix.onMessagesUpdate = (messages: DecryptedMessage[]) => { // Format time based on timestamp const formatted = messages.map(m => { const date = new Date(m.timestamp); return { id: m.id, sender: m.sender, text: m.content, time: date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }) }; }); // CRDT arrays are append-only mostly for chat, but we sync everything setChatMessages(formatted); }; matrix.onTypingUpdate = (typings: Record) => { const active = Object.keys(typings).filter(key => key !== username); setTypingUsers(active); }; // PKEPX Zoom-Killer Handlers matrix.onQuantumResonance = (id, type) => { setActiveReactions(prev => [...prev, { id, type, ts: Date.now() }]); setTimeout(() => { setActiveReactions(prev => prev.filter(r => Date.now() - r.ts < 4000)); }, 4000); }; matrix.onSovereignSignal = (command, targetId) => { if (command === 'MUTE_ALL') { setStatus("Host telah mengunci matriks komunikasi (Sovereign Lock)."); } }; // Determine server URL dynamically for backup WebSocket const proto = window.location.protocol; const host = window.location.host; let serverUrl = `${proto}//${host}`; if (typeof window === 'undefined' || host === '') { serverUrl = 'http://localhost:4003'; } matrix.ignite(serverUrl, username); setIsMatrixActive(true); setStatus("XTM CRDT Engine Siap. Enkripsi AES-256 Aktif."); } catch (e: any) { console.error("XTM ERROR DETAILS:", e, e.name, e.message); setStatus("Koneksi Gagal: " + e.name + " - " + e.message); } }; initMatrix(); return () => { // Dihapus untuk mencegah React 18 race condition yang memotong koneksi QUIC secara prematur if (matrixRef.current) { // matrixRef.current.shutdown(); } }; }, []); // (Moved setStatus up) const handleSendChat = async (e?: React.FormEvent) => { if (e) e.preventDefault(); if (!chatInput.trim() || !matrixRef.current) return; const currentInput = chatInput; // XTM Send Message (Automatically encrypted and synced via CRDT) matrixRef.current.sendMessage(username, currentInput); setChatInput(""); // FASE 87: OMNIBRAIN AVATAR PROTOCOL (CLIENT-SIDE TELEPATHY) if (currentInput.includes('@OmniBrain')) { // The browser acts as the medium for the AI to preserve E2EE matrixRef.current.sendMessage('OmniBrain', 'Sedang mengurai matriks kuantum...'); try { // Take last 5 decrypted messages for context const recentHistory = chatMessages.slice(-5); const res = await fetch('/api/omnibrain', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ history: recentHistory, prompt: currentInput.replace('@OmniBrain', '').trim(), sender: username }) }); const data = await res.json(); if (data.reply && matrixRef.current) { matrixRef.current.sendMessage('OmniBrain', data.reply); } } catch (err) { if (matrixRef.current) { matrixRef.current.sendMessage('OmniBrain', 'Gagal memanggil entitas di peladen pusat.'); } } } }; const handleInputChange = (e: React.ChangeEvent) => { const val = e.target.value; setChatInput(val); if (matrixRef.current && val.length > 0) { matrixRef.current.setTyping(username, val.length); } }; return (
{/* Top Banner */}

XCU OMNI CHAT

{roomName}

{/* PKEPX Resonance Button */}
CRDT QUANTUM ENGINE
{/* PKEPX Floating Emojis */} {activeReactions.map(r => (
{r.type}
))} {!isMatrixActive ? (
{logs[logs.length-1] || "INisialisasi..."}
) : (
{chatMessages.length === 0 ? (
P2P Kuantum Siap. 0ms Latensi.
) : ( chatMessages.map(msg => { const isMe = msg.sender === username; return (
{isMe ? 'Anda' : msg.sender} {msg.time}

{msg.text}

); }) )} {/* FASE 86: XTM Typing Indicator */} {typingUsers.length > 0 && (
{typingUsers.join(", ")} sedang merajut matriks teks...
)}
)}