import React, { useEffect, useRef, useMemo } from 'react';
import { useChatStore } from '../store/chatStore';
import { useUser } from '@/features/auth/hooks/useUser';
import { Card, CardContent } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { sanitizeChatMessage } from '@/utils/sanitize';
import {
MoreVertical,
Reply,
Smile,
ThumbsUp,
ThumbsDown,
MessageSquare,
} from 'lucide-react';
function formatMessageDate(dateStr: string): string {
const date = new Date(dateStr);
const now = new Date();
const diffDays = Math.floor(
(now.getTime() - date.getTime()) / (1000 * 60 * 60 * 24)
);
if (diffDays === 0) return 'Today';
if (diffDays === 1) return 'Yesterday';
return date.toLocaleDateString('en-US', {
weekday: 'long',
month: 'long',
day: 'numeric',
});
}
function DateSeparator({ date }: { date: string }) {
const formatted = formatMessageDate(date);
return (
);
}
export function ChatMessages() {
const { currentConversationId, conversations, messages, typingUsers } = useChatStore();
const { data: user } = useUser();
const messagesEndRef = useRef(null);
// Action 4.5.1.5: Get current conversation from conversations array using ID
const currentConversation = useMemo(() => {
if (!currentConversationId) return null;
return conversations.find((c) => c.id === currentConversationId) || null;
}, [currentConversationId, conversations]);
const conversationMessages = currentConversationId
? messages[currentConversationId] || []
: [];
const typingUserIds = currentConversationId
? typingUsers[currentConversationId] || []
: [];
useEffect(() => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
}, [conversationMessages]);
if (!currentConversation) {
return (
Sélectionnez une conversation
Choisissez une conversation pour commencer à discuter
);
}
return (
{/* En-tête de la conversation */}
{currentConversation.name ||
`Conversation ${currentConversation.id.slice(0, 8)}`}
{currentConversation.participants?.length || 0} participant
{(currentConversation.participants?.length || 0) > 1 ? 's' : ''}
{/* Messages */}
{conversationMessages.length === 0 ? (
Aucun message dans cette conversation
) : (
conversationMessages.map((message, index) => {
const isOwn = message.sender_id === user?.id;
const prevMessage = index > 0 ? conversationMessages[index - 1] : null;
const showDateSeparator =
!prevMessage ||
new Date(message.created_at).toDateString() !==
new Date(prevMessage.created_at).toDateString();
return (
{showDateSeparator && (
)}
{isOwn ? 'Vous' : `Utilisateur ${message.sender_id}`}
{new Date(message.created_at).toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit',
})}
{/* Réactions */}
{message.reactions && Object.keys(message.reactions).length > 0 && (
{Object.entries(message.reactions).map(([emoji, userIds]) => (
))}
)}
{/* Actions sur le message */}
);
})
)}
{/* Indicateur de frappe */}
{typingUserIds.length > 0 && (
{typingUserIds.length === 1
? "Quelqu'un"
: `${typingUserIds.length} personnes`}{' '}
est en train d'écrire...
)}
);
}