339 lines
13 KiB
TypeScript
339 lines
13 KiB
TypeScript
import { useState, useEffect, useRef } from 'react';
|
|
import { Link, useLocation } from 'react-router-dom';
|
|
import {
|
|
LayoutDashboard,
|
|
TrendingUp,
|
|
ShoppingCart,
|
|
Receipt,
|
|
Package,
|
|
FolderTree,
|
|
Tag,
|
|
Sparkles,
|
|
FileText,
|
|
Users,
|
|
Heart,
|
|
MessageSquare,
|
|
Building2,
|
|
PhoneCall,
|
|
Image,
|
|
FlaskConical,
|
|
MessageSquareQuote,
|
|
BookOpen,
|
|
Video,
|
|
Settings,
|
|
ShieldCheck,
|
|
Languages,
|
|
UserCheck,
|
|
DollarSign,
|
|
CreditCard,
|
|
Truck,
|
|
Sliders,
|
|
Globe,
|
|
ChevronDown,
|
|
Star,
|
|
Layers,
|
|
HelpCircle,
|
|
Menu,
|
|
RotateCcw,
|
|
Activity,
|
|
} from 'lucide-react';
|
|
import api from '../services/api';
|
|
|
|
interface SubMenuItem {
|
|
icon: React.ComponentType<{ className?: string }>;
|
|
label: string;
|
|
path: string;
|
|
badge?: string | number;
|
|
}
|
|
|
|
interface MenuGroup {
|
|
id: string;
|
|
title: string;
|
|
icon: React.ComponentType<{ className?: string }>;
|
|
items: SubMenuItem[];
|
|
}
|
|
|
|
interface SidebarProps {
|
|
isOpen: boolean;
|
|
setIsOpen: (val: boolean) => void;
|
|
}
|
|
|
|
export default function Sidebar({ isOpen, setIsOpen }: SidebarProps) {
|
|
const location = useLocation();
|
|
const [newOrdersCount, setNewOrdersCount] = useState(0);
|
|
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
|
|
|
// Auto-close sidebar on mobile navigation and manage body overflow
|
|
useEffect(() => {
|
|
setIsOpen(false);
|
|
}, [location.pathname, setIsOpen]);
|
|
|
|
useEffect(() => {
|
|
if (isOpen) {
|
|
document.body.classList.add('overflow-hidden', 'lg:overflow-auto');
|
|
} else {
|
|
document.body.classList.remove('overflow-hidden', 'lg:overflow-auto');
|
|
}
|
|
return () => {
|
|
document.body.classList.remove('overflow-hidden', 'lg:overflow-auto');
|
|
};
|
|
}, [isOpen]);
|
|
|
|
useEffect(() => {
|
|
const fetchOrdersCount = async () => {
|
|
try {
|
|
const response = await api.get('/admin/dashboard/stats');
|
|
if (response.data?.success) {
|
|
setNewOrdersCount(response.data.data.newOrders || 0);
|
|
}
|
|
} catch (err) {
|
|
console.error('Failed to fetch stats in sidebar', err);
|
|
}
|
|
};
|
|
fetchOrdersCount();
|
|
intervalRef.current = setInterval(fetchOrdersCount, 60000);
|
|
return () => {
|
|
if (intervalRef.current) clearInterval(intervalRef.current);
|
|
};
|
|
}, []);
|
|
|
|
const menuGroups: MenuGroup[] = [
|
|
{
|
|
id: 'main',
|
|
title: 'داشبورد و تحلیل داده',
|
|
icon: LayoutDashboard,
|
|
items: [
|
|
{ icon: LayoutDashboard, label: 'نمای کلی داشبورد', path: '/' },
|
|
{ icon: TrendingUp, label: 'گزارشات و تحلیل فروش', path: '/reports' },
|
|
],
|
|
},
|
|
{
|
|
id: 'monitoring',
|
|
title: 'مرکز مانیتورینگ و سلامت',
|
|
icon: Activity,
|
|
items: [
|
|
{ icon: Activity, label: 'مانیتورینگ زنده سرویسها', path: '/monitoring' },
|
|
{ icon: RotateCcw, label: 'پرتال مالی و استرداد زیبال', path: '/zibal-portal' },
|
|
],
|
|
},
|
|
{
|
|
id: 'orders',
|
|
title: 'سفارشات و مالی',
|
|
icon: ShoppingCart,
|
|
items: [
|
|
{
|
|
icon: ShoppingCart,
|
|
label: 'مدیریت سفارشات',
|
|
path: '/orders',
|
|
badge: newOrdersCount > 0 ? newOrdersCount : undefined,
|
|
},
|
|
{ icon: Receipt, label: 'تراکنشها و لاگ پرداخت', path: '/transactions' },
|
|
{ icon: RotateCcw, label: 'پرتال مالی و استرداد زیبال', path: '/zibal-portal' },
|
|
{ icon: CreditCard, label: 'روشهای پرداخت و زیبال', path: '/settings/payment-methods' },
|
|
{ icon: Truck, label: 'تنظیمات ارسال و کرایه', path: '/settings/shipping' },
|
|
{ icon: DollarSign, label: 'تنظیمات مالی و مالیات', path: '/settings/financial' },
|
|
],
|
|
},
|
|
{
|
|
id: 'products',
|
|
title: 'محصولات و کاتالوگ',
|
|
icon: Package,
|
|
items: [
|
|
{ icon: Package, label: 'لیست محصولات', path: '/products' },
|
|
{ icon: Star, label: 'نظرات و امتیازات', path: '/reviews' },
|
|
{ icon: FolderTree, label: 'دستهبندیهای تخصصی', path: '/categories' },
|
|
{ icon: Tag, label: 'کدهای تخفیف', path: '/coupons' },
|
|
{ icon: Sparkles, label: 'مشاور هوشمند بالینی', path: '/smart-advisor' },
|
|
{ icon: FileText, label: 'نسخههای دریافتی', path: '/prescriptions' },
|
|
],
|
|
},
|
|
{
|
|
id: 'crm',
|
|
title: 'کاربران و همکاران',
|
|
icon: Users,
|
|
items: [
|
|
{ icon: Users, label: 'مدیریت کاربران', path: '/users' },
|
|
{ icon: Heart, label: 'پرونده پتها (همدمها)', path: '/pets' },
|
|
{ icon: MessageSquare, label: 'تیکتها و مشاوره تخصصی', path: '/tickets' },
|
|
{ icon: Building2, label: 'پرتال B2B و همکاران', path: '/b2b' },
|
|
{ icon: PhoneCall, label: 'اطلاعات تماس و پیامها', path: '/contact' },
|
|
],
|
|
},
|
|
{
|
|
id: 'content',
|
|
title: 'محتوا و دانشنامه',
|
|
icon: Layers,
|
|
items: [
|
|
{ icon: Image, label: 'بنرها و اسلایدرها', path: '/banners' },
|
|
{ icon: FileText, label: 'وبلاگ و مقالات', path: '/blogs' },
|
|
{ icon: FlaskConical, label: 'دانشنامه ترکیبات', path: '/ingredients' },
|
|
{ icon: BookOpen, label: 'دانشنامه عمومی و علمی', path: '/wiki' },
|
|
{ icon: UserCheck, label: 'پزشکان و کادر علمی', path: '/doctors' },
|
|
{ icon: MessageSquareQuote, label: 'نظرات و گواهی متخصصان', path: '/testimonials' },
|
|
{ icon: Video, label: 'ویدئوها و آموزشها', path: '/videos' },
|
|
{ icon: HelpCircle, label: 'سوالات متداول (FAQ)', path: '/faq' },
|
|
{ icon: Menu, label: 'مدیریت منو و فوتر', path: '/menu' },
|
|
{ icon: Image, label: 'کتابخانه رسانه', path: '/media' },
|
|
],
|
|
},
|
|
{
|
|
id: 'settings',
|
|
title: 'سیستم و تنظیمات',
|
|
icon: Settings,
|
|
items: [
|
|
{ icon: Settings, label: 'تنظیمات اصلی فروشگاه', path: '/settings' },
|
|
{ icon: Languages, label: 'ویرایشگر محتوای صفحات (Page Builder)', path: '/ui-texts' },
|
|
{ icon: CreditCard, label: 'روشهای پرداخت و زیبال', path: '/settings/payment-methods' },
|
|
{ icon: Truck, label: 'تنظیمات ارسال و کرایه', path: '/settings/shipping' },
|
|
{ icon: DollarSign, label: 'تنظیمات مالی و مالیات', path: '/settings/financial' },
|
|
{ icon: MessageSquare, label: 'درگاه پیامک (MeliPayamak)', path: '/settings/sms' },
|
|
{ icon: ShieldCheck, label: 'گواهی SSL و امنیت', path: '/settings/ssl' },
|
|
{ icon: Globe, label: 'تنظیمات سئو (SEO)', path: '/settings/seo' },
|
|
{ icon: Sliders, label: 'تنظیمات سیستمی پیشرفته', path: '/settings/system' },
|
|
],
|
|
},
|
|
];
|
|
|
|
// Store only the active group ID so only one group stays expanded at a time
|
|
const [activeGroupId, setActiveGroupId] = useState<string | null>(() => {
|
|
for (const g of menuGroups) {
|
|
const hasActive = g.items.some(
|
|
(item) =>
|
|
location.pathname === item.path ||
|
|
(item.path !== '/' && location.pathname.startsWith(item.path))
|
|
);
|
|
if (hasActive) return g.id;
|
|
}
|
|
return 'main';
|
|
});
|
|
|
|
// Keep active group synchronized with URL navigation
|
|
useEffect(() => {
|
|
for (const g of menuGroups) {
|
|
const hasActive = g.items.some(
|
|
(item) =>
|
|
location.pathname === item.path ||
|
|
(item.path !== '/' && location.pathname.startsWith(item.path))
|
|
);
|
|
if (hasActive) {
|
|
setActiveGroupId(g.id);
|
|
break;
|
|
}
|
|
}
|
|
}, [location.pathname]);
|
|
|
|
const toggleGroup = (groupId: string) => {
|
|
setActiveGroupId((prev) => (prev === groupId ? null : groupId));
|
|
};
|
|
|
|
return (
|
|
<>
|
|
{/* Mobile Backdrop */}
|
|
{isOpen && (
|
|
<div
|
|
className="fixed inset-0 bg-black/50 z-40 lg:hidden backdrop-blur-sm transition-opacity"
|
|
onClick={() => setIsOpen(false)}
|
|
/>
|
|
)}
|
|
|
|
<aside
|
|
className={`w-64 bg-white border-l border-gray-200 h-screen h-[100dvh] fixed lg:fixed lg:right-0 lg:top-0 lg:bottom-0 flex flex-col font-vazir shadow-sm z-50 transform transition-transform duration-300 ease-in-out overscroll-contain ${
|
|
isOpen ? 'translate-x-0' : 'translate-x-full lg:translate-x-0'
|
|
}`}
|
|
>
|
|
{/* Header */}
|
|
<div className="h-16 flex items-center justify-between px-5 border-b border-gray-200 shrink-0 bg-white">
|
|
<div className="flex items-center gap-2">
|
|
<div className="w-8 h-8 rounded-xl bg-purple-600 flex items-center justify-center text-white font-black text-sm italic shadow-md">
|
|
C
|
|
</div>
|
|
<span className="text-base font-black text-gray-900">کنینا | ادمینپنل</span>
|
|
</div>
|
|
<span className="text-[10px] bg-purple-50 text-purple-700 font-bold px-2 py-0.5 rounded-md border border-purple-200">
|
|
v2.4
|
|
</span>
|
|
</div>
|
|
|
|
{/* Menu Navigation - touch friendly & full scrolling */}
|
|
<nav className="flex-1 overflow-y-auto overscroll-contain py-3 px-3 space-y-2 pb-16 lg:pb-6">
|
|
{menuGroups.map((group) => {
|
|
const GroupIcon = group.icon;
|
|
const isExpanded = activeGroupId === group.id;
|
|
const hasActiveChild = group.items.some(
|
|
(item) =>
|
|
location.pathname === item.path ||
|
|
(item.path !== '/' && location.pathname.startsWith(item.path))
|
|
);
|
|
|
|
return (
|
|
<div key={group.id} className="rounded-2xl overflow-hidden bg-gray-50/50 border border-gray-100 transition-colors">
|
|
{/* Group Accordion Header */}
|
|
<button
|
|
type="button"
|
|
onClick={() => toggleGroup(group.id)}
|
|
className={`w-full flex items-center justify-between px-3 py-2.5 text-xs font-black transition-all cursor-pointer ${
|
|
hasActiveChild ? 'text-purple-700 bg-purple-50/70' : 'text-gray-600 hover:bg-gray-100/60'
|
|
}`}
|
|
>
|
|
<div className="flex items-center gap-2">
|
|
<GroupIcon className={`w-4 h-4 ${hasActiveChild ? 'text-purple-600' : 'text-gray-400'}`} />
|
|
<span>{group.title}</span>
|
|
</div>
|
|
<ChevronDown
|
|
className={`w-4 h-4 transition-transform duration-200 text-gray-400 ${
|
|
isExpanded ? 'rotate-180 text-purple-600' : ''
|
|
}`}
|
|
/>
|
|
</button>
|
|
|
|
{/* Submenu Items */}
|
|
{isExpanded && (
|
|
<div className="p-1 space-y-0.5 bg-white border-t border-gray-100 animate-in slide-in-from-top-1 duration-150">
|
|
{group.items.map((item) => {
|
|
const isActive =
|
|
location.pathname === item.path ||
|
|
(item.path !== '/' && location.pathname.startsWith(item.path));
|
|
const Icon = item.icon;
|
|
|
|
return (
|
|
<Link
|
|
key={item.path}
|
|
to={item.path}
|
|
onClick={() => setIsOpen(false)}
|
|
className={`flex items-center justify-between px-3 py-2 rounded-xl text-xs font-bold transition-all ${
|
|
isActive
|
|
? 'bg-purple-600 text-white shadow-sm shadow-purple-600/30'
|
|
: 'text-gray-600 hover:bg-purple-50/50 hover:text-purple-700'
|
|
}`}
|
|
>
|
|
<div className="flex items-center gap-2.5">
|
|
<Icon className={`w-4 h-4 ${isActive ? 'text-white' : 'text-gray-400'}`} />
|
|
<span>{item.label}</span>
|
|
</div>
|
|
|
|
{item.badge !== undefined && (
|
|
<span
|
|
className={`text-[10px] font-black px-1.5 py-0.5 rounded-full min-w-[18px] text-center ${
|
|
isActive
|
|
? 'bg-white text-purple-700'
|
|
: 'bg-purple-600 text-white'
|
|
}`}
|
|
>
|
|
{item.badge}
|
|
</span>
|
|
)}
|
|
</Link>
|
|
);
|
|
})}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
})}
|
|
</nav>
|
|
</aside>
|
|
</>
|
|
);
|
|
}
|
|
|