Spaces:
Running
Running
Download frontend/components/Sidebar.tsx from axeronis/microfunnel-api: direct link, hf CLI and curl.
- Browser
- Download file 12.7 kB
-
https://huggingface.co/spaces/axeronis/microfunnel-api/resolve/main/frontend/components/Sidebar.tsx
- Command line
-
hf download hf://spaces/axeronis/microfunnel-api/frontend/components/Sidebar.tsx
-
curl -L -o Sidebar.tsx https://huggingface.co/spaces/axeronis/microfunnel-api/resolve/main/frontend/components/Sidebar.tsx
12.7 kB
| '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 ( | |
| <motion.aside | |
| initial="hidden" | |
| animate="visible" | |
| variants={sidebarVariants} | |
| className="w-72 flex flex-col relative shrink-0 h-screen sticky top-0 pb-14 lg:pb-0 border-r border-white/5 z-20 bg-[#050510]/80 backdrop-blur-xl backdrop-saturate-[1.8]" | |
| > | |
| {/* Logo Section - Hidden on mobile, already in MobileHeader */} | |
| <div className="p-6 relative z-50 hidden lg:block"> | |
| <Link href="/settings/brain" className="flex items-center gap-3 group"> | |
| <div className="w-8 h-8 flex items-center justify-center relative"> | |
| <img src="/logo-v2.png" alt="MicroFunnel Logo" className="w-full h-full object-contain" /> | |
| </div> | |
| <h1 className="text-lg font-bold tracking-[0.2em] uppercase text-white"> | |
| MICROFUNNEL | |
| </h1> | |
| </Link> | |
| </div> | |
| {/* Loading State */} | |
| { | |
| isLoading ? ( | |
| <nav className="flex-1 overflow-y-auto p-4 space-y-6 relative z-10"> | |
| {[1, 2, 3].map((i) => ( | |
| <div key={i}> | |
| <div className="h-3 w-20 bg-md-surface-container-high rounded mb-3 animate-pulse" /> | |
| <div className="space-y-2"> | |
| {[1, 2, 3].map((j) => ( | |
| <div key={j} className="h-10 w-full bg-md-surface-container rounded-lg animate-pulse" /> | |
| ))} | |
| </div> | |
| </div> | |
| ))} | |
| </nav> | |
| ) : ( | |
| <nav className="flex-1 overflow-y-auto p-4 space-y-6 relative z-10 scrollbar-thin scrollbar-thumb-md-outline-variant scrollbar-track-transparent"> | |
| {/* Main Navigation */} | |
| <motion.div initial="hidden" animate="visible" variants={itemVariants}> | |
| <p className="px-4 text-[10px] font-bold text-md-on-surface-variant uppercase tracking-widest mb-2 font-mono"> | |
| / {t('sidebar.main').toUpperCase()} | |
| </p> | |
| <div className="space-y-1"> | |
| {/* Admin Link */} | |
| {isAdmin && ( | |
| <NavItem | |
| href="/admin" | |
| icon={Shield} | |
| label={t('nav.admin')} | |
| isActive={isActive('/admin')} | |
| variant="admin" | |
| /> | |
| )} | |
| {visibleMainItems.map((item) => ( | |
| <NavItem | |
| key={item.href} | |
| href={item.href} | |
| icon={item.icon} | |
| label={t(item.labelKey)} | |
| isActive={isActive(item.href)} | |
| /> | |
| ))} | |
| </div> | |
| </motion.div> | |
| {/* Demo Chat Button - HIDDEN AS REQUESTED */} | |
| {/* {isOwner && ( | |
| <motion.div initial="hidden" animate="visible" variants={itemVariants}> | |
| <Link | |
| href="/demo-chat" | |
| className="w-full flex items-center gap-3 px-4 py-3 text-sm font-medium text-purple-400 hover:text-purple-300 bg-purple-500/10 hover:bg-purple-500/20 rounded-xl border border-purple-500/20 transition-all group" | |
| > | |
| <Bot className="w-5 h-5" /> | |
| <span>Корректировка</span> | |
| </Link> | |
| </motion.div> | |
| )} */} | |
| {/* Integrations - only show section if has visible items */} | |
| {visibleIntegrationItems.length > 0 && ( | |
| <motion.div initial="hidden" animate="visible" variants={itemVariants}> | |
| <p className="px-4 text-[10px] font-bold text-md-on-surface-variant uppercase tracking-widest mb-2 font-mono"> | |
| / {t('sidebar.integrations').toUpperCase()} | |
| </p> | |
| <div className="space-y-1"> | |
| {visibleIntegrationItems.map((item) => ( | |
| <NavItem | |
| key={item.href} | |
| href={item.href} | |
| icon={item.icon} | |
| label={t(item.labelKey)} | |
| isActive={isActive(item.href)} | |
| /> | |
| ))} | |
| </div> | |
| </motion.div> | |
| )} | |
| {/* Settings - only show section if has visible items */} | |
| {visibleSettingsItems.length > 0 && ( | |
| <motion.div initial="hidden" animate="visible" variants={itemVariants}> | |
| <p className="px-4 text-[10px] font-bold text-md-on-surface-variant uppercase tracking-widest mb-2 font-mono"> | |
| / {t('sidebar.settings').toUpperCase()} | |
| </p> | |
| <div className="space-y-1"> | |
| {visibleSettingsItems.map((item) => ( | |
| <NavItem | |
| key={item.href} | |
| href={item.href} | |
| icon={item.icon} | |
| label={t(item.labelKey)} | |
| isActive={isActive(item.href)} | |
| /> | |
| ))} | |
| </div> | |
| </motion.div> | |
| )} | |
| {/* Trial/Subscription Banner */} | |
| {usageStats && isOwnerUser && ( | |
| <motion.div | |
| initial={{ opacity: 0 }} | |
| animate={{ opacity: 1 }} | |
| className={cn( | |
| "mx-2 p-4 rounded-xl border mt-4", | |
| isExpired | |
| ? "bg-red-500/10 border-red-500/30" | |
| : isTrial | |
| ? "bg-amber-500/10 border-amber-500/30" | |
| : "bg-indigo-500/10 border-indigo-500/30" | |
| )} | |
| > | |
| <div className="flex items-center gap-2 mb-2"> | |
| {isExpired ? ( | |
| <AlertTriangle className="w-4 h-4 text-red-400" /> | |
| ) : ( | |
| <Clock className="w-4 h-4 text-amber-400" /> | |
| )} | |
| <span className={cn( | |
| "text-xs font-bold uppercase", | |
| isExpired ? "text-red-400" : isTrial ? "text-amber-400" : "text-indigo-400" | |
| )}> | |
| {isExpired ? "Подписка истекла" : isTrial ? `Trial: ${daysLeft} дн.` : usageStats.plan.toUpperCase()} | |
| </span> | |
| </div> | |
| {!isUnlimited && ( | |
| <div className="mb-2"> | |
| <div className="flex justify-between text-[10px] text-slate-500 mb-1"> | |
| <span>Диалоги</span> | |
| <span>{usedDialogs} / {isUnlimited ? '∞' : limitDialogs}</span> | |
| </div> | |
| <div className="w-full bg-white/10 rounded-full h-1.5"> | |
| <div | |
| className={cn( | |
| "h-1.5 rounded-full transition-all", | |
| usedDialogs >= (limitDialogs || 0) | |
| ? "bg-red-500" | |
| : usedDialogs >= (limitDialogs || 0) * 0.8 | |
| ? "bg-amber-500" | |
| : "bg-indigo-500" | |
| )} | |
| style={{ width: `${Math.min((usedDialogs / (limitDialogs || 1)) * 100, 100)}%` }} | |
| /> | |
| </div> | |
| </div> | |
| )} | |
| {(isTrial || isExpired) && ( | |
| <Link | |
| href="/settings/billing" | |
| className={cn( | |
| "block w-full py-2 text-center text-xs font-bold rounded-lg transition", | |
| isExpired | |
| ? "bg-red-600 hover:bg-red-500 text-white" | |
| : "bg-amber-600 hover:bg-amber-500 text-white" | |
| )} | |
| > | |
| {isExpired ? "Продлить подписку" : "Выбрать тариф"} | |
| </Link> | |
| )} | |
| </motion.div> | |
| )} | |
| </nav> | |
| ) | |
| } | |
| </motion.aside > | |
| ) | |
| } | |