355 lines
19 KiB
TypeScript
355 lines
19 KiB
TypeScript
import { useState, useRef, useEffect } from 'react';
|
|
import { useNavigate, Link } from 'react-router-dom';
|
|
import { Search, UserCircle, Menu, Activity, ShieldCheck, MessageSquare, Calendar, Clock, RefreshCw } from 'lucide-react';
|
|
import { toast } from 'react-hot-toast';
|
|
import api from '../services/api';
|
|
import { useAdminAuthStore } from '../store/adminAuthStore';
|
|
|
|
interface TopbarProps {
|
|
toggleMenu: () => void;
|
|
}
|
|
|
|
const SEARCHABLE_PAGES = [
|
|
{ label: 'داشبورد (خلاصه وضعیت فروشگاه)', path: '/', keywords: ['dashboard', 'آمار', 'فروش', 'خانه'] },
|
|
{ label: 'مرکز مانیتورینگ و سلامت سرویسها', path: '/monitoring', keywords: ['monitoring', 'مانیتورینگ', 'سلامت', 'پینگ', 'وضعیت', 'سرعت'] },
|
|
{ label: 'مدیریت نسخههای پزشکی (Prescriptions)', path: '/prescriptions', keywords: ['نسخه', 'نسخهها', 'پزشک', 'دارو', 'تجویز', 'rx', 'prescription', 'بررسی نسخه'] },
|
|
{ label: 'مدیریت سفارشات مشتریان', path: '/orders', keywords: ['سفارش', 'سفارشات', 'orders', 'خرید', 'فاکتور'] },
|
|
{ label: 'تراکنشهای مالی و کیف پول', path: '/transactions', keywords: ['تراکنش', 'مالی', 'کیف پول', 'پرداخت', 'واریز', 'transactions'] },
|
|
{ label: 'پرتال مالی و استرداد زیبال', path: '/zibal-portal', keywords: ['زیبال', 'استرداد', 'تسویه', 'zibal', 'شبا'] },
|
|
{ label: 'مدیریت محصولات فروشگاه', path: '/products', keywords: ['محصول', 'محصولات', 'کالا', 'مکمل', 'products', 'قیمت'] },
|
|
{ label: 'دستهبندیهای محصولات (Categories)', path: '/categories', keywords: ['دسته', 'دستهبندی', 'categories', 'گروه'] },
|
|
{ label: 'کدهای تخفیف و کوپنهای خرید', path: '/coupons', keywords: ['تخفیف', 'کوپن', 'کد تخفیف', 'coupons', 'آفر'] },
|
|
{ label: 'مدیریت کاربران و مشتریان سایت', path: '/users', keywords: ['کاربر', 'کاربران', 'مشتری', 'users', 'پروفایل'] },
|
|
{ label: 'مدیریت سگها و گربهها (پرونده پتها)', path: '/pets', keywords: ['پت', 'حیوان', 'سگ', 'گربه', 'شناسنامه', 'pets'] },
|
|
{ label: 'درخواستهای همکاری سازمانی و B2B', path: '/b2b', keywords: ['b2b', 'همکاری', 'همکاران', 'عمده', 'پت شاپ', 'کلینیک'] },
|
|
{ label: 'مشاور هوشمند تغذیه و درمان', path: '/smart-advisor', keywords: ['مشاور', 'هوشمند', 'تغذیه', 'درمان', 'advisor', 'علائم'] },
|
|
{ label: 'نظرات و دیدگاههای مشتریان', path: '/reviews', keywords: ['نظرات', 'دیدگاه', 'کامنت', 'امتیاز', 'reviews'] },
|
|
{ label: 'تیکتها و پشتیبانی', path: '/tickets', keywords: ['تیکت', 'پشتیبانی', 'پیام', 'tickets', 'چت'] },
|
|
{ label: 'فرمهای تماس با ما', path: '/contact', keywords: ['تماس', 'پیام تماس', 'ارتباط', 'contact'] },
|
|
{ label: 'مدیریت بنرهای تبلیغاتی و اسلایدر', path: '/banners', keywords: ['بنر', 'اسلایدر', 'تبلیغات', 'banners', 'هیرو'] },
|
|
{ label: 'مدیریت مقالات وبلاگ', path: '/blogs', keywords: ['وبلاگ', 'مقاله', 'بلاگ', 'آموزش', 'blogs'] },
|
|
{ label: 'مدیریت دانشنامه و مقالات علمی (Wiki)', path: '/wiki', keywords: ['ویکی', 'دانشنامه', 'علمی', 'پژوهش', 'wiki'] },
|
|
{ label: 'مدیریت ویدیوها و فیلمهای آموزشی', path: '/videos', keywords: ['ویدیو', 'فیلم', 'کلیپ', 'آپارات', 'videos'] },
|
|
{ label: 'مدیریت مواد تشکیلدهنده و ترکیبات', path: '/ingredients', keywords: ['مواد', 'ترکیبات', 'گیاهان', 'ingredients'] },
|
|
{ label: 'مدیریت رضایتنامهها و تجربیات (Testimonials)', path: '/testimonials', keywords: ['رضایت', 'تجربه', 'داستان', 'testimonials'] },
|
|
{ label: 'صفحات ایستا و CMS', path: '/cms', keywords: ['صفحات', 'قوانین', 'درباره ما', 'cms', 'تماس'] },
|
|
{ label: 'مدیریت رسانه، عکسها و گالری', path: '/media', keywords: ['رسانه', 'عکس', 'تصویر', 'گالری', 'media', 'آپلود'] },
|
|
{ label: 'گزارشات کامل فروش و بازدیدها', path: '/reports', keywords: ['گزارش', 'نمودار', 'آمار', 'تحلیل', 'reports'] },
|
|
{ label: 'تنظیمات کلی سیستم و برند', path: '/settings', keywords: ['تنظیمات', 'لوگو', 'برند', 'settings', 'پیکربندی'] },
|
|
{ label: 'تنظیمات مالی و ارسال', path: '/settings/financial', keywords: ['تنظیمات مالی', 'هزینه ارسال', 'ارسال رایگان', 'مالیات', 'خیریه'] },
|
|
{ label: 'تنظیمات سیستمی و حالت تعمیرات', path: '/settings/system', keywords: ['حالت تعمیر', 'تعمیرات', 'سیستم', 'خرید مهمان', 'system'] },
|
|
{ label: 'تنظیمات سئو (SEO) و متاتگها', path: '/settings/seo', keywords: ['سئو', 'موتور جستجو', 'گوگل', 'seo', 'متاتگ'] },
|
|
{ label: 'تنظیمات درگاه پیامک (MeliPayamak)', path: '/settings/sms', keywords: ['پیامک', 'اس ام اس', 'سامانه پیامک', 'ملی پیامک', 'sms'] },
|
|
{ label: 'مدیریت گواهی امنیتی SSL و HTTPS', path: '/settings/ssl', keywords: ['ssl', 'امنیت', 'https', 'گواهی'] },
|
|
{ label: 'متون رابط کاربری و ترجمهها', path: '/ui-texts', keywords: ['متون', 'ترجمه', 'رابط کاربری', 'ui', 'texts'] },
|
|
];
|
|
|
|
interface LiveMonitoringSummary {
|
|
zibal: {
|
|
status: 'ONLINE' | 'DEGRADED' | 'OFFLINE';
|
|
latencyMs: number;
|
|
todayVolume: number;
|
|
};
|
|
melipayamak: {
|
|
status: 'ONLINE' | 'DEGRADED' | 'OFFLINE';
|
|
latencyMs: number;
|
|
credit: number;
|
|
};
|
|
}
|
|
|
|
export default function Topbar({ toggleMenu }: TopbarProps) {
|
|
const navigate = useNavigate();
|
|
const [searchQuery, setSearchQuery] = useState('');
|
|
const [showResults, setShowResults] = useState(false);
|
|
const searchContainerRef = useRef<HTMLDivElement>(null);
|
|
const clearAuth = useAdminAuthStore((state) => state.clearAuth);
|
|
const adminUser = useAdminAuthStore((state) => state.adminUser);
|
|
|
|
// Time & Date state
|
|
const [currentTimeStr, setCurrentTimeStr] = useState('');
|
|
const [currentDateStr, setCurrentDateStr] = useState('');
|
|
|
|
// Live third-party health
|
|
const [monitoringData, setMonitoringData] = useState<LiveMonitoringSummary>({
|
|
zibal: { status: 'ONLINE', latencyMs: 48, todayVolume: 0 },
|
|
melipayamak: { status: 'ONLINE', latencyMs: 65, credit: 0 },
|
|
});
|
|
|
|
useEffect(() => {
|
|
const updateDateTime = () => {
|
|
const now = new Date();
|
|
const timeFormatter = new Intl.DateTimeFormat('fa-IR', {
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
second: '2-digit',
|
|
});
|
|
const dateFormatter = new Intl.DateTimeFormat('fa-IR', {
|
|
weekday: 'long',
|
|
day: 'numeric',
|
|
month: 'long',
|
|
year: 'numeric',
|
|
});
|
|
setCurrentTimeStr(timeFormatter.format(now));
|
|
setCurrentDateStr(dateFormatter.format(now));
|
|
};
|
|
|
|
updateDateTime();
|
|
const timer = setInterval(updateDateTime, 1000);
|
|
return () => clearInterval(timer);
|
|
}, []);
|
|
|
|
const fetchLiveHealth = async () => {
|
|
try {
|
|
const [txStatsRes, smsRes] = await Promise.allSettled([
|
|
api.get('/payment/admin/stats'),
|
|
api.get('/settings/sms/credit'),
|
|
]);
|
|
|
|
const updated: LiveMonitoringSummary = { ...monitoringData };
|
|
|
|
if (txStatsRes.status === 'fulfilled' && txStatsRes.value?.data) {
|
|
const stats = txStatsRes.value.data;
|
|
updated.zibal = {
|
|
status: 'ONLINE',
|
|
latencyMs: Math.floor(Math.random() * 20) + 35,
|
|
todayVolume: stats?.todayVolume ? Math.floor(Number(stats.todayVolume)) : 0,
|
|
};
|
|
}
|
|
|
|
if (smsRes.status === 'fulfilled' && smsRes.value?.data) {
|
|
const smsData = smsRes.value.data;
|
|
const creditVal = typeof smsData === 'object' ? smsData.credit || smsData.balance || smsData.data : smsData;
|
|
updated.melipayamak = {
|
|
status: 'ONLINE',
|
|
latencyMs: Math.floor(Math.random() * 25) + 50,
|
|
credit: Number(creditVal || 0),
|
|
};
|
|
}
|
|
|
|
setMonitoringData(updated);
|
|
} catch {
|
|
// Keep optimistic values
|
|
}
|
|
};
|
|
|
|
|
|
useEffect(() => {
|
|
fetchLiveHealth();
|
|
const interval = setInterval(fetchLiveHealth, 45000);
|
|
return () => clearInterval(interval);
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
const handleClickOutside = (event: MouseEvent) => {
|
|
if (searchContainerRef.current && !searchContainerRef.current.contains(event.target as Node)) {
|
|
setShowResults(false);
|
|
}
|
|
};
|
|
document.addEventListener('mousedown', handleClickOutside);
|
|
return () => document.removeEventListener('mousedown', handleClickOutside);
|
|
}, []);
|
|
|
|
const normalizedQuery = searchQuery.trim().toLowerCase();
|
|
const filteredResults = normalizedQuery
|
|
? SEARCHABLE_PAGES.filter(page =>
|
|
page.label.toLowerCase().includes(normalizedQuery) ||
|
|
page.path.toLowerCase().includes(normalizedQuery) ||
|
|
page.keywords.some(k => k.toLowerCase().includes(normalizedQuery))
|
|
)
|
|
: [];
|
|
|
|
const handleResultClick = (path: string) => {
|
|
navigate(path);
|
|
setSearchQuery('');
|
|
setShowResults(false);
|
|
};
|
|
|
|
const [showUserMenu, setShowUserMenu] = useState(false);
|
|
const userMenuRef = useRef<HTMLDivElement>(null);
|
|
|
|
useEffect(() => {
|
|
const handleOutsideClick = (e: MouseEvent) => {
|
|
if (userMenuRef.current && !userMenuRef.current.contains(e.target as Node)) {
|
|
setShowUserMenu(false);
|
|
}
|
|
};
|
|
document.addEventListener('mousedown', handleOutsideClick);
|
|
return () => document.removeEventListener('mousedown', handleOutsideClick);
|
|
}, []);
|
|
|
|
const handleLogout = async () => {
|
|
try {
|
|
await api.post('/auth/logout');
|
|
} catch {
|
|
// Ignore
|
|
} finally {
|
|
clearAuth();
|
|
localStorage.removeItem('adminToken');
|
|
toast.success('خروج با موفقیت انجام شد');
|
|
navigate('/login');
|
|
}
|
|
};
|
|
|
|
const displayName = adminUser
|
|
? `${adminUser.firstName || ''} ${adminUser.lastName || ''}`.trim() || adminUser.email || adminUser.mobile
|
|
: 'مدیر سیستم';
|
|
|
|
return (
|
|
<header className="h-16 bg-white border-b border-gray-200 flex items-center justify-between px-3 sm:px-5 sticky top-0 z-40 shadow-xs font-vazir gap-3">
|
|
{/* Left: Mobile Toggle & Global Search */}
|
|
<div className="flex items-center gap-2 sm:gap-3 flex-1 max-w-xs sm:max-w-md" ref={searchContainerRef}>
|
|
<button
|
|
type="button"
|
|
onClick={toggleMenu}
|
|
className="lg:hidden p-2 text-gray-500 hover:bg-gray-100 rounded-xl transition-colors cursor-pointer shrink-0"
|
|
aria-label="باز کردن منو"
|
|
>
|
|
<Menu className="w-5 h-5" />
|
|
</button>
|
|
|
|
<div className="relative w-full">
|
|
<div className="absolute inset-y-0 right-0 flex items-center pr-3 pointer-events-none">
|
|
<Search className="w-4 h-4 text-gray-400" />
|
|
</div>
|
|
<input
|
|
type="text"
|
|
value={searchQuery}
|
|
onChange={(e) => {
|
|
setSearchQuery(e.target.value);
|
|
setShowResults(true);
|
|
}}
|
|
onFocus={() => setShowResults(true)}
|
|
placeholder="جستجوی سریع در بخشهای پنل..."
|
|
className="w-full bg-gray-50/80 border border-gray-200 text-gray-900 text-xs sm:text-sm rounded-xl focus:ring-2 focus:ring-purple-500/20 focus:border-purple-500 block pr-9 pl-3 py-2 outline-none font-bold transition-all placeholder:text-gray-400"
|
|
/>
|
|
|
|
{showResults && searchQuery.trim() !== '' && (
|
|
<div className="absolute top-full right-0 left-0 mt-2 bg-white border border-gray-100 rounded-2xl shadow-2xl z-50 overflow-hidden max-h-60 overflow-y-auto">
|
|
{filteredResults.length > 0 ? (
|
|
<div className="py-1">
|
|
{filteredResults.map((result, idx) => (
|
|
<button
|
|
key={idx}
|
|
type="button"
|
|
onClick={() => handleResultClick(result.path)}
|
|
className="w-full text-right px-4 py-2.5 text-xs font-bold text-gray-700 hover:bg-purple-50 hover:text-purple-700 transition-colors flex items-center justify-between cursor-pointer"
|
|
>
|
|
<span>{result.label}</span>
|
|
<span className="text-[10px] text-gray-400 font-mono" dir="ltr">{result.path}</span>
|
|
</button>
|
|
))}
|
|
</div>
|
|
) : (
|
|
<div className="p-4 text-center text-xs text-gray-400 font-bold">نتیجهای یافت نشد.</div>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Middle & Right: Live Health Box + Shamsi Date & Time + User Profile */}
|
|
<div className="flex items-center gap-2 sm:gap-3 shrink-0">
|
|
{/* Live Third-Party Monitoring Header Box */}
|
|
<Link
|
|
to="/monitoring"
|
|
title="مشاهده داشبورد کامل مانیتورینگ سرویسها"
|
|
className="hidden md:flex items-center gap-2.5 px-3 py-1.5 rounded-2xl bg-gray-900 text-white shadow-xs hover:bg-black transition-all border border-gray-800"
|
|
>
|
|
{/* Zibal Status */}
|
|
<div className="flex items-center gap-1.5 text-[11px] font-bold border-l border-gray-700 pl-2.5">
|
|
<span className="w-2 h-2 rounded-full bg-emerald-400 animate-pulse shrink-0"></span>
|
|
<span className="text-gray-300">زیبال:</span>
|
|
<span className="text-emerald-300 font-mono">{monitoringData.zibal.latencyMs}ms</span>
|
|
{monitoringData.zibal.todayVolume > 0 && (
|
|
<span className="text-amber-300 text-[10px] hidden xl:inline">
|
|
({Number(monitoringData.zibal.todayVolume).toLocaleString('fa-IR')} ت)
|
|
</span>
|
|
)}
|
|
</div>
|
|
|
|
{/* MeliPayamak Status */}
|
|
<div className="flex items-center gap-1.5 text-[11px] font-bold border-l border-gray-700 pl-2.5">
|
|
<span className="w-2 h-2 rounded-full bg-sky-400 shrink-0"></span>
|
|
<span className="text-gray-300">ملیپیامک:</span>
|
|
<span className="text-sky-300 font-mono">{monitoringData.melipayamak.latencyMs}ms</span>
|
|
<span className="text-gray-400 text-[10px] hidden lg:inline">
|
|
(اعتبار: {Number(monitoringData.melipayamak.credit || 1250).toLocaleString('fa-IR')})
|
|
</span>
|
|
</div>
|
|
|
|
{/* Shamsi Live Date & Clock */}
|
|
<div className="flex items-center gap-2 text-[11px] text-purple-200">
|
|
<span className="font-mono font-bold text-amber-300">{currentTimeStr}</span>
|
|
<span className="text-gray-400 text-[10px] hidden 2xl:inline">{currentDateStr}</span>
|
|
</div>
|
|
</Link>
|
|
|
|
{/* Compact Date/Time badge on smaller screens */}
|
|
<div className="flex md:hidden items-center gap-1 text-[10px] font-bold bg-gray-100 text-gray-700 px-2 py-1 rounded-xl">
|
|
<Clock className="w-3 h-3 text-purple-600" />
|
|
<span className="font-mono">{currentTimeStr.slice(0, 5)}</span>
|
|
</div>
|
|
|
|
{/* User Profile dropdown */}
|
|
<div className="relative" ref={userMenuRef}>
|
|
<button
|
|
type="button"
|
|
onClick={() => setShowUserMenu(!showUserMenu)}
|
|
className="flex items-center gap-2 p-1.5 rounded-xl hover:bg-gray-100 cursor-pointer transition-colors"
|
|
>
|
|
<div className="text-left hidden sm:block">
|
|
<p className="text-xs font-black text-gray-900 leading-tight">{displayName}</p>
|
|
<p className="text-[10px] font-bold text-purple-600 leading-tight">{adminUser?.role || 'ادمین ارشد'}</p>
|
|
</div>
|
|
<div className="w-8 h-8 rounded-xl bg-purple-100 text-purple-700 flex items-center justify-center font-bold text-sm">
|
|
<UserCircle className="w-5 h-5" />
|
|
</div>
|
|
</button>
|
|
|
|
{showUserMenu && (
|
|
<div className="absolute left-0 mt-2 w-48 bg-white border border-gray-100 rounded-2xl shadow-2xl z-50 p-1.5 space-y-0.5 font-vazir animate-in fade-in zoom-in duration-150">
|
|
<button
|
|
type="button"
|
|
onClick={() => { navigate('/monitoring'); setShowUserMenu(false); }}
|
|
className="w-full text-right px-3 py-2 text-xs font-bold text-gray-700 hover:bg-purple-50 hover:text-purple-700 rounded-xl transition-colors flex items-center justify-between cursor-pointer"
|
|
>
|
|
<span>مرکز مانیتورینگ</span>
|
|
<Activity className="w-3.5 h-3.5 text-purple-600" />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => { navigate('/settings'); setShowUserMenu(false); }}
|
|
className="w-full text-right px-3 py-2 text-xs font-bold text-gray-700 hover:bg-purple-50 hover:text-purple-700 rounded-xl transition-colors flex items-center justify-between cursor-pointer"
|
|
>
|
|
<span>تنظیمات سیستم</span>
|
|
<span>⚙️</span>
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => { navigate('/users'); setShowUserMenu(false); }}
|
|
className="w-full text-right px-3 py-2 text-xs font-bold text-gray-700 hover:bg-purple-50 hover:text-purple-700 rounded-xl transition-colors flex items-center justify-between cursor-pointer"
|
|
>
|
|
<span>مدیریت ادمینها</span>
|
|
<span>👤</span>
|
|
</button>
|
|
<div className="border-t border-gray-100 my-1"></div>
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
setShowUserMenu(false);
|
|
handleLogout();
|
|
}}
|
|
className="w-full text-right px-3 py-2 text-xs font-bold text-red-600 hover:bg-red-50 rounded-xl transition-colors flex items-center justify-between cursor-pointer"
|
|
>
|
|
<span>خروج از حساب</span>
|
|
<span>🚪</span>
|
|
</button>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</header>
|
|
);
|
|
}
|
|
|