"use client" import { useAuth } from '@/hooks/useAuth' import { useSidebar } from '@/hooks/useSidebar' import { Button } from '@/components/ui/button' import { Card } from '@/components/ui/card' import { Avatar, AvatarImage, AvatarFallback } from '@/components/ui/avatar' import { useRouter, usePathname } from 'next/navigation' import { useQuery } from '@apollo/client' import { GET_CONVERSATIONS, GET_INCOMING_REQUESTS } from '@/graphql/queries' import { Settings, LogOut, Store, MessageCircle, Wrench, Warehouse, Users, Truck, Handshake, ChevronLeft, ChevronRight } from 'lucide-react' export function Sidebar() { const { user, logout } = useAuth() const router = useRouter() const pathname = usePathname() const { isCollapsed, toggleSidebar } = useSidebar() // Загружаем список чатов для подсчета непрочитанных сообщений const { data: conversationsData } = useQuery(GET_CONVERSATIONS, { pollInterval: 60000, // Обновляем каждую минуту в сайдбаре - этого достаточно fetchPolicy: 'cache-first', errorPolicy: 'ignore', // Игнорируем ошибки чтобы не ломать сайдбар notifyOnNetworkStatusChange: false, // Плавные обновления без мерцания }) // Загружаем входящие заявки для подсчета новых запросов const { data: incomingRequestsData } = useQuery(GET_INCOMING_REQUESTS, { pollInterval: 60000, // Обновляем каждую минуту fetchPolicy: 'cache-first', errorPolicy: 'ignore', notifyOnNetworkStatusChange: false, }) const conversations = conversationsData?.conversations || [] const incomingRequests = incomingRequestsData?.incomingRequests || [] const totalUnreadCount = conversations.reduce((sum: number, conv: { unreadCount?: number }) => sum + (conv.unreadCount || 0), 0) const incomingRequestsCount = incomingRequests.length const getInitials = () => { const orgName = getOrganizationName() return orgName.charAt(0).toUpperCase() } const getOrganizationName = () => { if (user?.organization?.name) { return user.organization.name } if (user?.organization?.fullName) { return user.organization.fullName } return 'Организация' } const getCabinetType = () => { if (!user?.organization?.type) return 'Кабинет' switch (user.organization.type) { case 'FULFILLMENT': return 'Фулфилмент' case 'SELLER': return 'Селлер' case 'LOGIST': return 'Логистика' case 'WHOLESALE': return 'Оптовик' default: return 'Кабинет' } } const handleSettingsClick = () => { router.push('/settings') } const handleMarketClick = () => { router.push('/market') } const handleMessengerClick = () => { router.push('/messenger') } const handleServicesClick = () => { router.push('/services') } const handleWarehouseClick = () => { router.push('/warehouse') } const handleEmployeesClick = () => { router.push('/employees') } const handleSuppliesClick = () => { // Для каждого типа кабинета свой роут switch (user?.organization?.type) { case 'FULFILLMENT': router.push('/fulfillment-supplies') break case 'SELLER': router.push('/supplies') break case 'WHOLESALE': router.push('/supplies') break case 'LOGIST': router.push('/logistics') break default: router.push('/supplies') } } const handlePartnersClick = () => { router.push('/partners') } const isSettingsActive = pathname === '/settings' const isMarketActive = pathname.startsWith('/market') const isMessengerActive = pathname.startsWith('/messenger') const isServicesActive = pathname.startsWith('/services') const isWarehouseActive = pathname.startsWith('/warehouse') const isEmployeesActive = pathname.startsWith('/employees') const isSuppliesActive = pathname.startsWith('/supplies') || pathname.startsWith('/fulfillment-supplies') || pathname.startsWith('/logistics') const isPartnersActive = pathname.startsWith('/partners') return (
{getOrganizationName()}
{getCabinetType()}