import { useState, useRef, useEffect } from 'react';
import { Send, Lock, MoreVertical, Phone, ArrowLeft, ShieldCheck } from 'lucide-react';
import { cryptoUtils } from './EncryptionHelper';
export default function ChatWindow({ chat, onBack, onSendMessage }) {
const [messageText, setMessageText] = useState('');
const [isEncrypting, setIsEncrypting] = useState(false);
const messagesEndRef = useRef(null);
const [localMessages, setLocalMessages] = useState(chat ? chat.messages : []);
// Scroll to bottom on new message
useEffect(() => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
}, [localMessages]);
useEffect(() => {
if(chat) setLocalMessages(chat.messages);
}, [chat]);
if (!chat) {
return (
SecureChat
Select a chat to start messaging.
Messages are protected with end-to-end encryption.
);
}
const handleSend = async () => {
if (!messageText.trim()) return;
setIsEncrypting(true);
// Simulate encryption delay
setTimeout(async () => {
// In a real app, we encrypt with the recipient's public key
// Here we use a mock shared secret for the demo
const encryptedContent = await cryptoUtils.encrypt(messageText, "secure-chat-demo-key");
const newMessage = {
id: Date.now(),
text: messageText, // Storing plain text locally for UI demo, but conceptually this is encrypted payload
encryptedPayload: encryptedContent,
sender: 'me',
timestamp: new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }),
status: 'sent'
};
setLocalMessages(prev => [...prev, newMessage]);
onSendMessage(chat.id, newMessage);
setMessageText('');
setIsEncrypting(false);
}, 600);
};
const handleKeyDown = (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSend();
}
};
return (
{/* Chat Header */}
{chat.isGroup ? Grp : chat.name.charAt(0)}
{/* Messages Area */}
{/* Encryption Notice */}
Messages are end-to-end encrypted. No one outside of this chat, not even SecureChat, can read or listen to them.
{localMessages.map((msg) => (
{msg.text}
{msg.timestamp}
{msg.sender === 'me' && (
{msg.status === 'read' ? '✓✓' : '✓'}
)}
))}
{/* Input Area */}
);
}