import React, { useEffect } from 'react'; import { useChatStore } from '../store/chatStore'; import { useAuthStore } from '@/stores/auth'; import { apiClient } from '@/services/api/client'; import { useQuery } from '@tanstack/react-query'; import { cn } from '@/lib/utils'; import { Loader2 } from 'lucide-react'; interface ConversationItemProps { conversation: { id: string; name: string; type: string }; onSelect: (id: string) => void; isSelected: boolean; } const ConversationItem: React.FC = ({ conversation, onSelect, isSelected, }) => { return (
onSelect(conversation.id)} className={cn( 'flex items-center p-3 rounded-lg cursor-pointer transition-colors', isSelected ? 'bg-blue-100 text-blue-800' : 'hover:bg-gray-100', )} > {conversation.name || `Conversation ${conversation.id.substring(0, 8)}`} {/* 3 */}
); }; export const ChatSidebar: React.FC = () => { const { user } = useAuthStore(); const userId = user?.id; const { conversations, currentConversationId, setCurrentConversation, addConversation, } = useChatStore(); // Fetch conversations from backend const { data, isLoading, error } = useQuery({ queryKey: ['chatConversations', userId], queryFn: async () => { if (!userId) return []; const response = await apiClient.get('/conversations'); return response.data.conversations; }, enabled: !!userId, }); useEffect(() => { if (data) { data.forEach((conv: any) => addConversation({ id: conv.id, name: conv.name, type: conv.type, participants: conv.participants, unread_count: 0, // Default for now }), ); } }, [data, addConversation]); if (isLoading) { return (
); } if (error) { return (
Erreur:{' '} {(error as any).message || 'Impossible de charger les conversations'}
); } return (

Conversations

{conversations.length === 0 ? (
Aucune conversation. Créez-en une !
) : ( conversations.map((conv) => ( )) )}
{/* Button to create new conversation (TODO) */}
); };