import React from 'react'; import ReactMarkdown from 'react-markdown'; import type { Message } from '../../../../types/broker'; import { APP_CONSTANTS } from '../../../../constants/app'; interface ChatMessageProps { message: Message; onVerify?: (chatId: string) => void; onAcknowledge?: (chatId: string) => void; onDispute?: (chatId: string) => void; onWithdrawFee?: (chatId: string) => void; } export const ChatMessage: React.FC = ({ message, onVerify, onAcknowledge, onDispute, onWithdrawFee, }) => { const isVerificationExpired = (timestamp: number): boolean => { const now = Date.now(); const expiryTime = timestamp + (APP_CONSTANTS.UI.VERIFICATION_EXPIRY_MINUTES * 60 * 1000); return now > expiryTime; }; const shouldShowVerificationButton = (message: Message): boolean => { return ( message.role === "assistant" && message.chatId !== undefined && !message.isVerified && !message.isVerifying && message.timestamp !== undefined && !isVerificationExpired(message.timestamp) ); }; const shouldShowAcknowledgeButton = (message: Message): boolean => { return ( message.role === "assistant" && message.chatId !== undefined && message.isVerified === true && !message.isVerifying ); }; const shouldShowDisputeButton = (message: Message): boolean => { return ( message.role === "assistant" && message.chatId !== undefined && message.isVerified === false && !message.isVerifying ); }; const getVerificationStatus = (message: Message) => { if (message.isVerifying) { return { text: "Verifying...", className: "text-yellow-600" }; } if (message.isVerified === true) { return { text: "✓ Verified", className: "text-green-600" }; } if (message.isVerified === false) { return { text: "✗ Verification Failed", className: "text-red-600" }; } if (message.timestamp && isVerificationExpired(message.timestamp)) { return { text: "⏰ Verification Expired", className: "text-gray-500" }; } return null; }; return (
{message.content} {message.role === "assistant" && (
{new Date(message.timestamp || Date.now()).toLocaleTimeString()} {(() => { const status = getVerificationStatus(message); return status ? ( {status.text} ) : null; })()}
{/* Verification Controls */}
{shouldShowVerificationButton(message) && ( )} {shouldShowAcknowledgeButton(message) && ( )} {shouldShowDisputeButton(message) && ( <> )}
)}
); };