Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -1,57 +1,221 @@
|
||||
import React from 'react';
|
||||
import { BellIcon, SunIcon, MoonIcon, Bars3Icon } from '@heroicons/react/24/outline';
|
||||
import { useUiStore } from '../../stores/uiStore';
|
||||
import { useLocation } from 'react-router-dom';
|
||||
import {
|
||||
BellIcon, SunIcon, MoonIcon, Bars3Icon, Cog6ToothIcon,
|
||||
MagnifyingGlassIcon, XMarkIcon, CheckIcon,
|
||||
} from '@heroicons/react/24/outline';
|
||||
import { ChevronLeftIcon } from '@heroicons/react/24/outline';
|
||||
import { useUiStore, HUES, type BrandHue } from '../../stores/uiStore';
|
||||
|
||||
export default function Topbar() {
|
||||
const toggleSidebar = useUiStore((s) => s.toggleSidebar);
|
||||
const darkMode = useUiStore((s) => s.darkMode);
|
||||
const toggleDarkMode = useUiStore((s) => s.toggleDarkMode);
|
||||
const CRUMBS: Record<string, string[]> = {
|
||||
'/admin/dashboard': ['داشبورد'],
|
||||
'/admin/users': ['کاربران'],
|
||||
'/admin/doctors': ['پزشکان'],
|
||||
'/admin/clinics': ['کلینیکها'],
|
||||
'/admin/appointments': ['مدیریت', 'نوبتها'],
|
||||
'/admin/payments': ['مدیریت', 'پرداختها'],
|
||||
'/admin/settlements': ['مدیریت', 'تسویهحساب'],
|
||||
'/admin/comments': ['محتوا', 'نظرات'],
|
||||
'/admin/ratings': ['محتوا', 'امتیازها'],
|
||||
'/admin/blogs': ['محتوا', 'بلاگ'],
|
||||
'/admin/sms': ['محتوا', 'پیامک'],
|
||||
'/admin/categories': ['سیستم', 'دستهبندیها'],
|
||||
'/admin/representations': ['سیستم', 'نمایندگان'],
|
||||
'/admin/secretaries': ['سیستم', 'منشیها'],
|
||||
};
|
||||
|
||||
function getCrumbs(pathname: string): string[] {
|
||||
if (CRUMBS[pathname]) return CRUMBS[pathname];
|
||||
const entry = Object.entries(CRUMBS).find(([k]) => pathname.startsWith(k + '/'));
|
||||
return entry ? entry[1] : ['داشبورد'];
|
||||
}
|
||||
|
||||
interface Props {
|
||||
onMobileMenuOpen?: () => void;
|
||||
}
|
||||
|
||||
export default function Topbar({ onMobileMenuOpen }: Props) {
|
||||
const location = useLocation();
|
||||
const toggleSidebar = useUiStore((s) => s.toggleSidebar);
|
||||
const darkMode = useUiStore((s) => s.darkMode);
|
||||
const toggleDarkMode = useUiStore((s) => s.toggleDarkMode);
|
||||
const density = useUiStore((s) => s.density);
|
||||
const setDensity = useUiStore((s) => s.setDensity);
|
||||
const brandHue = useUiStore((s) => s.brandHue);
|
||||
const setBrandHue = useUiStore((s) => s.setBrandHue);
|
||||
const sidebarOpen = useUiStore((s) => s.sidebarOpen);
|
||||
const settingsPanelOpen = useUiStore((s) => s.settingsPanelOpen);
|
||||
const toggleSettingsPanel = useUiStore((s) => s.toggleSettingsPanel);
|
||||
|
||||
const crumbs = getCrumbs(location.pathname);
|
||||
|
||||
return (
|
||||
<header className="h-14 bg-white dark:bg-gray-900 border-b border-slate-200 dark:border-gray-700/50 flex items-center justify-between px-4 lg:px-6 shrink-0 transition-colors duration-200">
|
||||
{/* Left side (RTL: right side visually) */}
|
||||
<button
|
||||
onClick={toggleSidebar}
|
||||
className="lg:hidden w-9 h-9 flex items-center justify-center rounded-xl text-slate-500 dark:text-slate-400 hover:bg-slate-100 dark:hover:bg-gray-700 transition-colors"
|
||||
>
|
||||
<Bars3Icon className="w-5 h-5" />
|
||||
</button>
|
||||
<>
|
||||
<header className="topbar">
|
||||
{/* Sidebar toggle — desktop only */}
|
||||
<button className="icon-btn hidden lg:grid" onClick={toggleSidebar}
|
||||
title={sidebarOpen ? 'جمع کردن منو' : 'باز کردن منو'}>
|
||||
<Bars3Icon style={{ width: 20, height: 20 }} />
|
||||
</button>
|
||||
|
||||
<div className="flex-1" />
|
||||
{/* Mobile menu open — mobile only */}
|
||||
<button className="icon-btn grid lg:hidden" onClick={onMobileMenuOpen} title="منو">
|
||||
<Bars3Icon style={{ width: 20, height: 20 }} />
|
||||
</button>
|
||||
|
||||
{/* Right side actions */}
|
||||
<div className="flex items-center gap-1">
|
||||
{/* Dark mode toggle */}
|
||||
<button
|
||||
onClick={toggleDarkMode}
|
||||
className="w-9 h-9 flex items-center justify-center rounded-xl text-slate-500 dark:text-slate-400 hover:text-slate-700 dark:hover:text-slate-200 hover:bg-slate-100 dark:hover:bg-gray-700 transition-colors"
|
||||
title={darkMode ? 'حالت روشن' : 'حالت تاریک'}
|
||||
>
|
||||
{/* Search bar */}
|
||||
<div className="topbar-search">
|
||||
<MagnifyingGlassIcon style={{ width: 18, height: 18, flexShrink: 0 }} />
|
||||
<input placeholder="جستجوی بیمار، پزشک، نوبت..." readOnly />
|
||||
<kbd>⌘K</kbd>
|
||||
</div>
|
||||
|
||||
<div style={{ flex: 1 }} />
|
||||
|
||||
{/* Breadcrumbs */}
|
||||
<nav className="topbar-crumbs">
|
||||
{crumbs.map((crumb, i) => (
|
||||
<React.Fragment key={i}>
|
||||
{i > 0 && <ChevronLeftIcon style={{ width: 13, height: 13 }} />}
|
||||
{i === crumbs.length - 1 ? <b>{crumb}</b> : <span>{crumb}</span>}
|
||||
</React.Fragment>
|
||||
))}
|
||||
</nav>
|
||||
|
||||
<div style={{ width: 1, height: 24, background: 'var(--border)' }} />
|
||||
|
||||
{/* Settings */}
|
||||
<button className="icon-btn" onClick={toggleSettingsPanel} title="شخصیسازی">
|
||||
<Cog6ToothIcon style={{ width: 19, height: 19 }} />
|
||||
</button>
|
||||
|
||||
{/* Dark mode */}
|
||||
<button className="icon-btn" onClick={toggleDarkMode} title={darkMode ? 'حالت روشن' : 'حالت تاریک'}>
|
||||
{darkMode
|
||||
? <SunIcon className="w-5 h-5 text-amber-400" />
|
||||
: <MoonIcon className="w-5 h-5" />}
|
||||
? <SunIcon style={{ width: 19, height: 19, color: '#f59e0b' }} />
|
||||
: <MoonIcon style={{ width: 19, height: 19 }} />}
|
||||
</button>
|
||||
|
||||
{/* Notifications */}
|
||||
<button className="relative w-9 h-9 flex items-center justify-center rounded-xl text-slate-500 dark:text-slate-400 hover:text-slate-700 dark:hover:text-slate-200 hover:bg-slate-100 dark:hover:bg-gray-700 transition-colors">
|
||||
<BellIcon className="w-5 h-5" />
|
||||
<span className="absolute top-2 right-2 w-1.5 h-1.5 bg-red-500 rounded-full ring-2 ring-white dark:ring-gray-900" />
|
||||
{/* Bell */}
|
||||
<button className="icon-btn" title="اعلانها">
|
||||
<BellIcon style={{ width: 19, height: 19 }} />
|
||||
<span className="dot" />
|
||||
</button>
|
||||
|
||||
{/* Divider */}
|
||||
<div className="w-px h-6 bg-slate-200 dark:bg-gray-700 mx-1" />
|
||||
|
||||
{/* Avatar */}
|
||||
<div className="flex items-center gap-2.5 cursor-pointer pl-1">
|
||||
<div className="w-8 h-8 rounded-full bg-gradient-to-br from-violet-500 to-purple-700 flex items-center justify-center text-white text-sm font-bold shadow-sm">
|
||||
A
|
||||
</div>
|
||||
<div className="hidden sm:block">
|
||||
<p className="text-sm font-semibold text-slate-700 dark:text-slate-200 leading-none">Admin</p>
|
||||
<p className="text-[11px] text-slate-400 dark:text-slate-500 leading-none mt-0.5">مدیر سیستم</p>
|
||||
<div className="cp-avatar" style={{ cursor: 'pointer' }}>A</div>
|
||||
</header>
|
||||
|
||||
{/* Settings Panel */}
|
||||
{settingsPanelOpen && (
|
||||
<div
|
||||
className="overlay"
|
||||
style={{ placeItems: 'stretch', justifyContent: 'flex-start' }}
|
||||
onClick={toggleSettingsPanel}
|
||||
>
|
||||
<div
|
||||
className="modal settings-panel"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
<div className="modal-head">
|
||||
<h2>شخصیسازی</h2>
|
||||
<button className="mini-btn" onClick={toggleSettingsPanel}>
|
||||
<XMarkIcon style={{ width: 18, height: 18 }} />
|
||||
</button>
|
||||
</div>
|
||||
<div className="modal-body" style={{ display: 'flex', flexDirection: 'column', gap: 0 }}>
|
||||
|
||||
{/* Theme */}
|
||||
<div className="form-row">
|
||||
<label>حالت نمایش</label>
|
||||
<div className="seg" style={{ width: '100%' }}>
|
||||
<button
|
||||
className={!darkMode ? 'on' : ''}
|
||||
style={{ flex: 1 }}
|
||||
onClick={() => { if (darkMode) toggleDarkMode(); }}
|
||||
>
|
||||
<SunIcon style={{ width: 15, height: 15, display: 'inline', verticalAlign: 'middle', marginInlineEnd: 6 }} />
|
||||
روشن
|
||||
</button>
|
||||
<button
|
||||
className={darkMode ? 'on' : ''}
|
||||
style={{ flex: 1 }}
|
||||
onClick={() => { if (!darkMode) toggleDarkMode(); }}
|
||||
>
|
||||
<MoonIcon style={{ width: 15, height: 15, display: 'inline', verticalAlign: 'middle', marginInlineEnd: 6 }} />
|
||||
تیره
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Brand color */}
|
||||
<div className="form-row">
|
||||
<label>رنگ اصلی</label>
|
||||
<div style={{ display: 'flex', gap: 10, flexWrap: 'wrap' }}>
|
||||
{HUES.map((hue) => (
|
||||
<button
|
||||
key={hue.h}
|
||||
onClick={() => setBrandHue(hue as BrandHue)}
|
||||
title={hue.name}
|
||||
style={{
|
||||
width: 42, height: 42, borderRadius: 12,
|
||||
background: `oklch(0.55 ${hue.c} ${hue.h})`,
|
||||
border: brandHue.h === hue.h ? '3px solid var(--text)' : '3px solid transparent',
|
||||
boxShadow: 'var(--shadow-sm)',
|
||||
display: 'grid', placeItems: 'center', color: '#fff', cursor: 'pointer',
|
||||
}}
|
||||
>
|
||||
{brandHue.h === hue.h && <CheckIcon style={{ width: 18, height: 18 }} />}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Density */}
|
||||
<div className="form-row">
|
||||
<label>تراکم اطلاعات</label>
|
||||
<div className="seg" style={{ width: '100%' }}>
|
||||
<button
|
||||
className={density === 'comfortable' ? 'on' : ''}
|
||||
style={{ flex: 1 }}
|
||||
onClick={() => setDensity('comfortable')}
|
||||
>جادار</button>
|
||||
<button
|
||||
className={density === 'compact' ? 'on' : ''}
|
||||
style={{ flex: 1 }}
|
||||
onClick={() => setDensity('compact')}
|
||||
>فشرده</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Sidebar */}
|
||||
<div className="form-row">
|
||||
<label>سایدبار</label>
|
||||
<div className="seg" style={{ width: '100%' }}>
|
||||
<button
|
||||
className={sidebarOpen ? 'on' : ''}
|
||||
style={{ flex: 1 }}
|
||||
onClick={() => { if (!sidebarOpen) toggleSidebar(); }}
|
||||
>کامل</button>
|
||||
<button
|
||||
className={!sidebarOpen ? 'on' : ''}
|
||||
style={{ flex: 1 }}
|
||||
onClick={() => { if (sidebarOpen) toggleSidebar(); }}
|
||||
>جمعشده</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div style={{
|
||||
padding: 14, background: 'var(--surface-2)', borderRadius: 'var(--r)',
|
||||
fontSize: 12.5, color: 'var(--text-2)', lineHeight: 1.9, marginTop: 8,
|
||||
}}>
|
||||
<Cog6ToothIcon style={{ width: 15, height: 15, verticalAlign: 'middle', marginInlineEnd: 6 }} />
|
||||
تنظیمات شما بهصورت خودکار ذخیره میشود و در بازدید بعدی حفظ میماند.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user