fix(header): attach dropdown mega menu to sub-navbar button & provide static categories fallback

- Ensure fallback categories & symptoms are populated immediately in state so menuItems is never empty
- Attach mega menu popup panel directly to the sub-navbar button for 800px-1224px screen range
This commit is contained in:
parsa aghaei committed 2026-07-21 14:19:17 +03:30
1 parent 99ebfea8d0
commit 3e3372d01b
1 file changed
+85 -24
+85 -24
View File
@@ -47,27 +47,46 @@ export default function Header({
title: string;
icon: React.ReactNode;
solutions: string[];
}[]>([]);
const petMenuRef = useRef<HTMLDivElement>(null);
const { getTotalItems } = useCartStore();
const { pets, activePetId, setActivePet, getActivePet } = usePetStore();
const { role, isLoggedIn, logout, profile } = useUserStore();
const activePet = getActivePet();
const getText = useSettingsStore(state => state.getText);
}[]>([
{
id: "joints",
title: "مفاصل و استخوان",
icon: MENU_ICONS["joints"],
solutions: ["لنگیدن", "سختی در بلند شدن", "درد مفاصل", "رشد سریع توله‌سگ", "پاهای پرانتزی", "ضعف تاندون"]
},
{
id: "immune",
title: "تقویت سیستم ایمنی و گوارش",
icon: MENU_ICONS["immune"],
solutions: ["ضعف بعد از بیماری", "بی‌اشتهایی", "بعد از زایمان", "اسهال", "مشکلات گوارشی"]
},
{
id: "energy",
title: "ویتامین‌ها و انرژی‌بخش‌ها",
icon: MENU_ICONS["energy"],
solutions: ["نفس‌نفس زدن", "خستگی", "سن بالا", "بی‌حالی"]
},
{
id: "special-care",
title: "مراقبت‌های ویژه (پوست، دندان و چشم)",
icon: MENU_ICONS["special-care"],
solutions: ["ریزش مو", "خشکی پوست", "خارش", "جرم دندان", "سوزش چشم"]
}
]);
useEffect(() => {
const loadNavFilters = async () => {
try {
const data = await productService.getNavigationFilters();
const mapped = data.map(item => ({
id: item.slug,
title: item.name,
icon: MENU_ICONS[item.slug] || <Pill className="w-5 h-5" />,
solutions: item.symptoms
}));
setMenuItems(mapped);
if (data && data.length > 0) {
const mapped = data.map(item => ({
id: item.slug,
title: item.name,
icon: MENU_ICONS[item.slug] || <Pill className="w-5 h-5" />,
solutions: item.symptoms || []
}));
setMenuItems(mapped);
}
} catch (e) {
console.error("Failed to load navigation filters:", e);
}
@@ -417,14 +436,56 @@ export default function Header({
</div>
{/* Medium Screen Sub-Navbar (Visible only between 800px and 1224px) */}
<div className="hidden min-[800px]:flex xl:hidden border-t border-medical-gray-100 bg-medical-gray-50/50 py-2 px-6 justify-center items-center gap-2 font-vazir" dir="rtl">
<button
onClick={() => setIsMegaMenuOpen(!isMegaMenuOpen)}
className="px-3 py-1.5 rounded-lg text-xs font-black text-medical-gray-700 hover:bg-white transition-all flex items-center gap-1.5 whitespace-nowrap"
>
{getText('nav_solutions', "دسته بندی درمانی")}
<ChevronDown className="w-3.5 h-3.5 text-canina-blue" />
</button>
<div className="hidden min-[800px]:flex xl:hidden border-t border-medical-gray-100 bg-medical-gray-50/50 py-2 px-6 justify-center items-center gap-2 font-vazir relative" dir="rtl">
<div className="relative">
<button
onClick={() => setIsMegaMenuOpen(!isMegaMenuOpen)}
className={`px-3 py-1.5 rounded-lg text-xs font-black transition-all flex items-center gap-1.5 whitespace-nowrap ${isMegaMenuOpen ? 'bg-canina-blue text-white shadow-sm' : 'text-medical-gray-700 hover:bg-white'}`}
>
{getText('nav_solutions', "دسته بندی درمانی")}
<ChevronDown className={`w-3.5 h-3.5 transition-transform ${isMegaMenuOpen ? 'rotate-180 text-white' : 'text-canina-blue'}`} />
</button>
<AnimatePresence>
{isMegaMenuOpen && (
<motion.div
initial={{ opacity: 0, y: 10, scale: 0.98 }}
animate={{ opacity: 1, y: 0, scale: 1 }}
exit={{ opacity: 0, y: 10, scale: 0.98 }}
className="absolute top-[120%] right-0 w-[600px] max-w-[90vw] bg-white border border-medical-gray-100 shadow-2xl rounded-[2rem] p-6 grid grid-cols-2 gap-6 z-50 pointer-events-auto"
>
{menuItems.map((item, idx) => (
<div key={idx} className="group/item">
<Link
href={`/shop?category=${item.id}`}
onClick={() => setIsMegaMenuOpen(false)}
className="flex items-center gap-3 mb-3 cursor-pointer block"
>
<div className="p-2 bg-medical-gray-50 rounded-xl text-canina-blue group-hover/item:bg-canina-blue group-hover/item:text-white transition-all shadow-sm">
{item.icon}
</div>
<h4 className="font-black text-medical-gray-900 text-xs font-vazir">{item.title}</h4>
</Link>
{item.solutions.length > 0 && (
<div className="flex flex-wrap gap-1 border-r-2 border-medical-gray-100 pr-2">
{item.solutions.slice(0, 6).map((sol, sIdx) => (
<Link
key={sIdx}
href={`/shop?category=${item.id}&symptom=${encodeURIComponent(sol)}`}
className="inline-flex items-center px-2 py-0.5 rounded-full text-[9px] font-bold bg-medical-gray-50 border border-medical-gray-200 text-medical-gray-500 hover:bg-canina-blue/5 hover:border-canina-blue/30 hover:text-canina-blue transition-all cursor-pointer font-vazir whitespace-nowrap"
onClick={() => setIsMegaMenuOpen(false)}
>
{sol}
</Link>
))}
</div>
)}
</div>
))}
</motion.div>
)}
</AnimatePresence>
</div>
<div className="w-[1px] h-4 bg-medical-gray-200" />
{[
{ id: 'shop', label: getText('nav_products', 'محصولات تخصصی'), href: '/shop' },