2025-12-24 11:51:40 +00:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
2025-12-13 02:34:34 +00:00
|
|
|
import { useChatStore } from '../store/chatStore';
|
2025-12-03 21:56:50 +00:00
|
|
|
import { ChatMessageComponent } from './ChatMessage';
|
|
|
|
|
import { useChat } from '../hooks/useChat';
|
2025-12-24 11:51:40 +00:00
|
|
|
import { MessageSearch } from './MessageSearch';
|
|
|
|
|
import { TypingIndicator } from './TypingIndicator';
|
|
|
|
|
import { Search, X } from 'lucide-react';
|
|
|
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
|
|
|
|
|
|
// FE-PAGE-005: Complete Chat page implementation
|
2025-12-03 21:56:50 +00:00
|
|
|
|
|
|
|
|
interface ChatRoomProps {
|
|
|
|
|
conversationId: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const ChatRoom: React.FC<ChatRoomProps> = ({ conversationId }) => {
|
2025-12-13 02:34:34 +00:00
|
|
|
const { messages } = useChatStore();
|
2025-12-03 21:56:50 +00:00
|
|
|
const { fetchHistory } = useChat();
|
|
|
|
|
const messagesEndRef = useRef<HTMLDivElement>(null);
|
2025-12-24 11:51:40 +00:00
|
|
|
const [showSearch, setShowSearch] = useState(false);
|
|
|
|
|
const [highlightedMessageId, setHighlightedMessageId] = useState<string | null>(null);
|
2025-12-03 21:56:50 +00:00
|
|
|
|
|
|
|
|
const currentMessages = messages[conversationId] || [];
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (conversationId && !messages[conversationId]) {
|
|
|
|
|
fetchHistory(conversationId);
|
|
|
|
|
}
|
|
|
|
|
}, [conversationId, messages, fetchHistory]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
|
|
|
|
}, [currentMessages]);
|
|
|
|
|
|
2025-12-24 11:51:40 +00:00
|
|
|
const handleMessageSelect = (messageId: string) => {
|
|
|
|
|
setHighlightedMessageId(messageId);
|
|
|
|
|
// Scroll to message
|
|
|
|
|
const messageElement = document.getElementById(`message-${messageId}`);
|
|
|
|
|
if (messageElement) {
|
|
|
|
|
messageElement.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
|
|
|
|
// Remove highlight after 3 seconds
|
|
|
|
|
setTimeout(() => setHighlightedMessageId(null), 3000);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-03 21:56:50 +00:00
|
|
|
if (!conversationId) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex-1 flex items-center justify-center text-gray-500">
|
|
|
|
|
Sélectionnez une conversation pour commencer
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex-1 flex flex-col h-full bg-white">
|
2025-12-24 11:51:40 +00:00
|
|
|
{/* FE-PAGE-005: Message Search Bar */}
|
|
|
|
|
<div className="border-b p-2 bg-gray-50">
|
|
|
|
|
{showSearch ? (
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<div className="flex-1">
|
|
|
|
|
<MessageSearch
|
|
|
|
|
conversationId={conversationId}
|
|
|
|
|
onMessageSelect={handleMessageSelect}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={() => setShowSearch(false)}
|
|
|
|
|
>
|
|
|
|
|
<X className="h-4 w-4" />
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="flex justify-end">
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={() => setShowSearch(true)}
|
|
|
|
|
>
|
|
|
|
|
<Search className="h-4 w-4 mr-2" />
|
|
|
|
|
Search Messages
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-12-03 21:56:50 +00:00
|
|
|
<div className="flex-1 overflow-y-auto p-4">
|
|
|
|
|
{currentMessages.length === 0 ? (
|
|
|
|
|
<div className="flex items-center justify-center h-full text-gray-500">
|
|
|
|
|
Aucun message. Soyez le premier à envoyer un message !
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
currentMessages.map((msg) => (
|
2025-12-24 11:51:40 +00:00
|
|
|
<div
|
|
|
|
|
key={msg.id}
|
|
|
|
|
id={`message-${msg.id}`}
|
|
|
|
|
className={
|
|
|
|
|
highlightedMessageId === msg.id
|
|
|
|
|
? 'bg-yellow-100 rounded-lg p-2 -m-2 mb-2'
|
|
|
|
|
: ''
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<ChatMessageComponent message={msg} />
|
|
|
|
|
</div>
|
2025-12-03 21:56:50 +00:00
|
|
|
))
|
|
|
|
|
)}
|
2025-12-24 11:51:40 +00:00
|
|
|
{/* FE-PAGE-005: Typing Indicator */}
|
|
|
|
|
<TypingIndicator conversationId={conversationId} />
|
2025-12-03 21:56:50 +00:00
|
|
|
<div ref={messagesEndRef} />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2025-12-13 02:34:34 +00:00
|
|
|
};
|