/* eslint-disable */ // [TSM.ID].[11031972] -- All Rights Reserved. Proprietary & Confidential. // @ts-nocheck "use client"; import { useEffect, useState, useRef, useCallback } from "react"; import { io, Socket } from "../lib/zero-socket"; import CryptoJS from "crypto-js"; import { useOmni } from "../components/OmniSyncProvider"; interface Message { id: string; client_id?: string; sender: string; content: string; timestamp: string; status: 'sent' | 'delivered' | 'read' | 'deleted'; isEdited?: boolean; type?: 'text' | 'image' | 'video' | 'file' | 'audio'; } interface Chat { id: string; name: string; lastMessage?: string; timestamp?: string; unreadCount?: number; isGroup?: boolean; avatar?: string; } export default function JumpaChat() { const { theme, setTheme, locale, setLocale } = useOmni(); const [socket, setSocket] = useState(null); const [messages, setMessages] = useState([]); const [chats, setChats] = useState([]); const [activeChat, setActiveChat] = useState(null); const [inputValue, setInputValue] = useState(""); const [username, setUsername] = useState(""); const [isConnected, setIsConnected] = useState(false); const [isJoined, setIsJoined] = useState(false); // BYOK & Security const [quantumToken, setQuantumToken] = useState(null); const [byokKey, setByokKey] = useState("xcu_default_vault_2026"); const [byokLevel, setByokLevel] = useState("SYSTEM"); const [allowedModules, setAllowedModules] = useState([]); const [uiMatrix, setUiMatrix] = useState | null>(null); // Call State const [isVideoCallActive, setIsVideoCallActive] = useState(false); const [incomingCall, setIncomingCall] = useState<{ caller: string, room: string } | null>(null); // UI States const [showSettings, setShowSettings] = useState(false); const messagesEndRef = useRef(null); // 1. Fetch Quantum Identity & BYOK useEffect(() => { const initIdentity = async () => { try { const iamBaseUrl = process.env.NEXT_PUBLIC_IAM_URL || 'http://forge.ultramodul.xyz'; const [meResp, tokenResp, usersResp] = await Promise.all([ fetch("/c/api/auth/me"), fetch(`${iamBaseUrl}/api/auth/quantum_token`, { credentials: 'include' }), fetch("/c/api/users") ]); const meData = await meResp.json(); const tokenData = await tokenResp.json(); if (meData.error) { window.location.href = "/dashboard"; return; } setUsername(meData.email); setQuantumToken(tokenData.token); setAllowedModules(tokenData.modules || []); if (tokenData.ui) setUiMatrix(tokenData.ui); if (tokenData.byokActive && tokenData.byok !== 'none') { setByokKey(tokenData.byok); setByokLevel(tokenData.byokLevel); } // Map Real Postgres Users to Chats if (usersResp.ok) { const usersData = await usersResp.json(); if (usersData.users) { const mappedChats = usersData.users.map((u: any) => ({ id: u.email, name: u.email.split('@')[0], lastMessage: "No recent messages", timestamp: "", unreadCount: 0, isGroup: false })); // Add Global Omni Room if cross-group is allowed if (meData.allowCrossGroup) { mappedChats.unshift({ id: "GENERAL-HQ", name: "JUMPA General HQ", lastMessage: "Omni-Tenant Broadcast Active", timestamp: "", unreadCount: 0, isGroup: true }); } setChats(mappedChats); } } setIsJoined(true); } catch (e) { window.location.href = "/dashboard"; } }; initIdentity(); }, []); // 2. Socket Orchestration useEffect(() => { if (isJoined && username) { const newSocket = io(window.location.origin, { path: '/c/socket.io', transports: ['websocket'] }); setSocket(newSocket); newSocket.on("connect", () => { setIsConnected(true); newSocket.emit("register_user", username); }); newSocket.on("new_message", (msg: Message) => { // Decrypt message if it's encrypted try { if (msg.content.startsWith("XCU|")) { const encrypted = msg.content.substring(4); const bytes = CryptoJS.AES.decrypt(encrypted, byokKey); msg.content = bytes.toString(CryptoJS.enc.Utf8); } } catch (e) {} setMessages(prev => [...prev, msg]); // Update chat list last message setChats(prev => prev.map(c => c.id === msg.room ? { ...c, lastMessage: msg.content, timestamp: 'Now' } : c)); }); newSocket.on("incoming_call", (data: { caller: string, room: string }) => { if (data.caller !== username) setIncomingCall(data); }); return () => { newSocket.disconnect(); }; } }, [isJoined, username, byokKey]); useEffect(() => { if (socket && isConnected && activeChat && username) { socket.emit("join_chat", { username, room: activeChat.id }); } }, [activeChat, socket, isConnected, username]); useEffect(() => { messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); }, [messages]); // 3. WhatsApp-Native Actions const handleSend = (e?: React.FormEvent) => { if (e) e.preventDefault(); if (!inputValue.trim() || !socket || !activeChat) return; // BYOK Encryption const encrypted = "XCU|" + CryptoJS.AES.encrypt(inputValue.trim(), byokKey).toString(); const newMsg: Message = { id: 'temp-' + Date.now(), sender: username, content: inputValue.trim(), // Local show unencrypted timestamp: new Date().toISOString(), status: 'sent', type: 'text' }; setMessages(prev => [...prev, newMsg]); socket.emit("send_message", { ...newMsg, content: encrypted, room: activeChat.id }); setInputValue(""); }; const handleStartCall = (audioOnly = false) => { if (!activeChat) return; setIsVideoCallActive(true); if (socket) { socket.emit("initiate_call", { caller: username, room: activeChat.id, isAudioOnly: audioOnly }); } }; const canSendMessage = uiMatrix?.['jc.ui.send_message'] !== false && allowedModules.includes('CHAT_SEND_MESSAGE'); const canAttach = uiMatrix?.['jc.ui.file_upload'] !== false && allowedModules.includes('CHAT_ATTACH_FILE'); const canVideoCall = uiMatrix?.['jc.ui.video_call'] !== false && allowedModules.includes('CHAT_VIDEO_CALL'); const canVoiceCall = uiMatrix?.['jc.ui.voice_call'] !== false && allowedModules.includes('CHAT_VOICE_CALL'); const canReactions = uiMatrix?.['jc.ui.reactions'] !== false; const canVoiceNotes = uiMatrix?.['jc.ui.voice_notes'] !== false; // WhatsApp-Native Bubble Logic const renderMessage = (msg: Message, i: number) => { const isMe = msg.sender === username; const isFirstInGroup = i === 0 || messages[i-1].sender !== msg.sender; return (
{!isMe && isFirstInGroup &&
{msg.sender.split('@')[0]}
}
BYOK XChaCha20
{msg.content}
{new Date(msg.timestamp).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', hour12: false })} {isMe && (
)}
); }; return (
{/* Background Quantum */}
{/* Sidebar - Supreme Matrix Chat List */} {/* Main Chat Area - Supreme Matrix UI */}
{!activeChat ? (

JUMPA XCOM ULTRA

Kanal Transmisi Terenkripsi Kuantum
FORGE SECURE

) : ( <> {/* Chat Header */}
{activeChat.name.substring(0,1).toUpperCase()}

{activeChat.name}

Terhubung

{canVideoCall && ( )} {canVoiceCall && ( )}
{/* Chat Background & Messages */}
XCU MILITARY-GRADE ZERO-KNOWLEDGE SHIELD ENGAGED
AES-GCM 256-BIT / XCHACHA20-POLY1305
{messages.length === 0 && (

Kanal Transmisi Kosong

)} {messages.map((msg, i) => renderMessage(msg, i))}
{/* Chat Input - Supreme Matrix */}