diff --git a/src/components/dashboard/sidebar.tsx b/src/components/dashboard/sidebar.tsx index 60ed55f..69001b9 100644 --- a/src/components/dashboard/sidebar.tsx +++ b/src/components/dashboard/sidebar.tsx @@ -30,9 +30,10 @@ export function Sidebar() { // Загружаем список чатов для подсчета непрочитанных сообщений const { data: conversationsData } = useQuery(GET_CONVERSATIONS, { - pollInterval: 10000, // Обновляем каждые 10 секунд - fetchPolicy: 'cache-and-network', + pollInterval: 60000, // Обновляем каждую минуту в сайдбаре - этого достаточно + fetchPolicy: 'cache-first', errorPolicy: 'ignore', // Игнорируем ошибки чтобы не ломать сайдбар + notifyOnNetworkStatusChange: false, // Плавные обновления без мерцания }) const conversations = conversationsData?.conversations || [] diff --git a/src/components/messenger/messenger-attachments.tsx b/src/components/messenger/messenger-attachments.tsx new file mode 100644 index 0000000..6f81efb --- /dev/null +++ b/src/components/messenger/messenger-attachments.tsx @@ -0,0 +1,315 @@ +"use client" + +import { useState } from 'react' +import { useQuery } from '@apollo/client' +import { GET_MESSAGES } from '@/graphql/queries' +import { useAuth } from '@/hooks/useAuth' +import { Card } from '@/components/ui/card' +import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs' +import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar' +import { Badge } from '@/components/ui/badge' +import { + FileText, + Image, + Music, + Video, + Download, + Calendar, + User +} from 'lucide-react' + +interface Organization { + id: string + inn: string + name?: string + fullName?: string + type: 'FULFILLMENT' | 'SELLER' | 'LOGIST' | 'WHOLESALE' + users?: Array<{ id: string, avatar?: string, managerName?: string }> +} + +interface Message { + id: string + content?: string + type?: 'TEXT' | 'VOICE' | 'IMAGE' | 'FILE' + voiceUrl?: string + voiceDuration?: number + fileUrl?: string + fileName?: string + fileSize?: number + fileType?: string + senderId: string + senderOrganization: Organization + receiverOrganization: Organization + createdAt: string + isRead: boolean +} + +interface MessengerAttachmentsProps { + counterparty: Organization +} + +export function MessengerAttachments({ counterparty }: MessengerAttachmentsProps) { + const { user } = useAuth() + const [activeTab, setActiveTab] = useState('all') + + // Загружаем все сообщения для получения вложений + const { data: messagesData, loading } = useQuery(GET_MESSAGES, { + variables: { counterpartyId: counterparty.id, limit: 1000 }, // Увеличиваем лимит для получения всех файлов + fetchPolicy: 'cache-first', + }) + + const messages: Message[] = messagesData?.messages || [] + + // Фильтруем только сообщения с вложениями + const attachmentMessages = messages.filter(msg => + msg.type && ['VOICE', 'IMAGE', 'FILE'].includes(msg.type) && msg.fileUrl + ) + + // Группируем по типам + const imageMessages = attachmentMessages.filter(msg => msg.type === 'IMAGE') + const voiceMessages = attachmentMessages.filter(msg => msg.type === 'VOICE') + const fileMessages = attachmentMessages.filter(msg => msg.type === 'FILE') + + const getOrganizationName = (org: Organization) => { + return org.name || org.fullName || 'Организация' + } + + const getManagerName = (org: Organization) => { + return org.users?.[0]?.managerName || 'Управляющий' + } + + const getInitials = (org: Organization) => { + const name = getOrganizationName(org) + return name.charAt(0).toUpperCase() + } + + const formatFileSize = (bytes?: number) => { + if (!bytes) return '0 B' + const k = 1024 + const sizes = ['B', 'KB', 'MB', 'GB'] + const i = Math.floor(Math.log(bytes) / Math.log(k)) + return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i] + } + + const formatDate = (dateString: string) => { + const date = new Date(dateString) + return date.toLocaleDateString('ru-RU', { + day: '2-digit', + month: '2-digit', + year: 'numeric' + }) + } + + const formatTime = (dateString: string) => { + const date = new Date(dateString) + return date.toLocaleTimeString('ru-RU', { + hour: '2-digit', + minute: '2-digit' + }) + } + + const handleDownload = (fileUrl: string, fileName: string) => { + const link = document.createElement('a') + link.href = fileUrl + link.download = fileName + document.body.appendChild(link) + link.click() + document.body.removeChild(link) + } + + const renderFileIcon = (fileType?: string) => { + if (!fileType) return + + if (fileType.startsWith('image/')) return + if (fileType.startsWith('audio/')) return + if (fileType.startsWith('video/')) return