'use client' import { useState, useEffect } from 'react' import Link from 'next/link' import { usePathname } from 'next/navigation' import { LayoutDashboard, MessageSquare, Database, CreditCard, Brain, FileText, Plug, Zap, Users, Bell, Calendar, Shield, Clock, AlertTriangle, Volume2, Bot } from 'lucide-react' import { useAuth } from '@/contexts/AuthContext' import { motion } from 'framer-motion' import { NavItem } from './Sidebar/NavItem' import { api } from '@/lib/api' import { cn } from '@/lib/utils' import { useTranslation } from '@/lib/i18n' import { isOwner } from '@/lib/permissions' // ============================================================================ // Animation Variants // ============================================================================ const sidebarVariants = { hidden: { x: -20, opacity: 0 }, visible: { x: 0, opacity: 1, transition: { duration: 0.4, staggerChildren: 0.05 } } } const itemVariants = { hidden: { x: -10, opacity: 0 }, visible: { x: 0, opacity: 1 } } // ============================================================================ // Navigation Configuration // ============================================================================ const mainNavItems = [ { href: '/dashboard', icon: LayoutDashboard, labelKey: 'nav.analytics', ownerOnly: true }, { href: '/chats', icon: MessageSquare, labelKey: 'nav.chats', ownerOnly: false }, { href: '/appointments', icon: Calendar, labelKey: 'nav.appointments', ownerOnly: false }, ] const integrationItems = [ { href: '/channels', icon: Plug, labelKey: 'nav.channels', ownerOnly: true }, ] const settingsItems = [ { href: '/settings/brain', icon: Brain, labelKey: 'nav.brain', ownerOnly: true }, { href: '/settings/voice', icon: Volume2, labelKey: 'nav.voice', ownerOnly: true }, { href: '/settings/playbook', icon: FileText, labelKey: 'nav.funnel', ownerOnly: true }, { href: '/settings/team', icon: Users, labelKey: 'nav.team', ownerOnly: true }, { href: '/settings/notifications', icon: Bell, labelKey: 'nav.notifications', ownerOnly: false }, { href: '/settings/billing', icon: CreditCard, labelKey: 'nav.billing', ownerOnly: true }, ] // ============================================================================ // Helper Functions // ============================================================================ function getInitials(name?: string | null, email?: string): string { if (name) { return name.split(' ').map(n => n[0]).join('').toUpperCase().slice(0, 2) } if (email) { return email.slice(0, 2).toUpperCase() } return 'U' } // ============================================================================ // Main Component // ============================================================================ export function Sidebar() { const pathname = usePathname() const { user, isLoading } = useAuth() const { t } = useTranslation() const [usageStats, setUsageStats] = useState<{ plan: string conversations_used: number conversations_limit: number | null days_left: number is_expired: boolean } | null>(null) // Load usage stats for trial/subscription display useEffect(() => { const loadUsage = async () => { if (!user) return try { const res = await api.get('/api/v1/tenants/me/usage') setUsageStats(res.data.usage) } catch (e) { console.error('Failed to load usage stats') } } loadUsage() }, [user]) // Don't show sidebar on auth pages if (pathname === '/login' || pathname === '/register' || pathname === '/') { return null } // Hide if not authenticated if (!isLoading && !user) { return null } const isActive = (path: string) => pathname === path || pathname.startsWith(path + '/') const isAdmin = user?.is_superuser || user?.role?.toUpperCase() === 'ADMIN' const isOwnerUser = isOwner(user?.role) || isAdmin // Filter items based on role const visibleMainItems = mainNavItems.filter(item => isOwnerUser || !item.ownerOnly) const visibleIntegrationItems = integrationItems.filter(item => isOwnerUser || !item.ownerOnly) const visibleSettingsItems = settingsItems.filter(item => isOwnerUser || !item.ownerOnly) // Trial banner logic const isTrial = usageStats?.plan === 'trial' const isExpired = usageStats?.is_expired const daysLeft = usageStats?.days_left || 0 const usedDialogs = usageStats?.conversations_used || 0 const limitDialogs = usageStats?.conversations_limit // null = unlimited const isUnlimited = limitDialogs === null || limitDialogs === undefined return ( {/* Logo Section - Hidden on mobile, already in MobileHeader */}
MicroFunnel Logo

MICROFUNNEL

{/* Loading State */} { isLoading ? ( ) : ( ) }
) }