/** * AccordionContactItem - TASK-043 * * Contact item with expandable accordion showing shared chats. * - Click on contact → opens default (most recent) chat * - Click on expand → shows all shared chats * - Click on specific chat → opens that chat */ import { useState } from 'react'; import { ChevronDown, MessageSquare, MessageSquarePlus, User, Bot, Star, UserPlus, UserMinus, Plus, Loader2 } from 'lucide-react'; import { useQuery } from '@tanstack/react-query'; import { apiClient } from '@/shared/utils/apiClient'; import { cn } from '@/shared/utils/cn'; // Types interface SharedChat { id: number; title: string | null; type: string; messages_count: number; last_message_at: string | null; updated_at: string; participants: Array<{ user_id: number; name: string; email?: string; avatar_url?: string; }>; } interface ContactUser { id: number; name: string; email?: string | null; avatar_url?: string | null; managed_by_agent_table_id?: number | null; user_type?: string; } interface AccordionContactItemProps { user: ContactUser; isCurrentPartner: boolean; isInGroup: boolean; isFavorite: boolean; onSelect: (user: ContactUser) => void; onSelectChat: (chatId: number) => void; onToggleFavorite: (userId: number) => void; onAddToGroup: (user: ContactUser) => void; onCreateNewChat: (user: ContactUser) => void; } export function AccordionContactItem({ user, isCurrentPartner, isInGroup, isFavorite, onSelect, onSelectChat, onToggleFavorite, onAddToGroup, onCreateNewChat, }: AccordionContactItemProps) { const [isExpanded, setIsExpanded] = useState(false); const isAgent = user.managed_by_agent_table_id != null || user.user_type === 'agent'; // Fetch shared chats when expanded const { data: sharedChats, isLoading: isLoadingChats } = useQuery({ queryKey: ['shared-chats', user.id], queryFn: async () => { const response = await apiClient.get<{ success: boolean; data: SharedChat[] }>(`/chat/conversations/with/${user.id}`); return response.data || []; }, enabled: isExpanded, staleTime: 30000, // 30 seconds }); const chatCount = sharedChats?.length || 0; const handleMainClick = () => { // Always create a new chat when clicking on a contact name/avatar. // Existing chats are accessible via the accordion expand button. onCreateNewChat(user); }; const handleExpandClick = (e: React.MouseEvent) => { e.stopPropagation(); setIsExpanded(!isExpanded); }; return (
{/* Main row */}
{/* Avatar */}
{user.avatar_url ? ( {user.name} ) : (
{isAgent ? : }
)}
{/* Name & info */}
{user.name} {isCurrentPartner && ( текущий )} {isInGroup && !isCurrentPartner && ( в группе )}
{isAgent ? 'AI Агент' : 'Человек'} {user.email && ` • ${user.email}`}
{/* New Chat button */} {/* Expand button - shows chat count, expands accordion */} {/* Favorite button */} {/* Add to group button */}
{/* Accordion content - shared chats */} {isExpanded && (
{isLoadingChats ? (
) : chatCount === 0 ? (
Нет общих чатов
) : ( sharedChats!.map(chat => ( )) )}
)}
); } export default AccordionContactItem;