import { useState, useRef } from 'react'; import { Link } from 'react-router-dom'; import type { MegaMenuPayload } from '../store/menuStore'; import { useTranslation } from 'react-i18next'; interface DesktopMegaMenuProps { items: MegaMenuPayload[]; } const getLocalizedText = (textObj: Record | string | undefined, lang: string) => { if (typeof textObj === 'string') return textObj; if (!textObj) return ''; return textObj[lang] || textObj['en'] || Object.values(textObj)[0] || ''; }; export function DesktopMegaMenu({ items }: DesktopMegaMenuProps) { const [openId, setOpenId] = useState(null); const closeTimer = useRef | null>(null); const { i18n } = useTranslation(); const lang = i18n.language ? i18n.language.substring(0, 2) : 'en'; // Open: cancel any pending close, then open this menu const handleOpen = (id: string) => { if (closeTimer.current) clearTimeout(closeTimer.current); setOpenId(id); }; // Close: wait 150ms — if mouse enters the dropdown in time, the close is cancelled const handleClose = () => { closeTimer.current = setTimeout(() => setOpenId(null), 150); }; return (
{items.map((item) => { const uniqueId = item.nodeId || item.id || `menu-${item.url}`; const isOpen = openId === uniqueId; const hasColumns = item.columns && item.columns.length > 0; return (
handleOpen(uniqueId)} onMouseLeave={handleClose} > {/* Nav trigger */} {getLocalizedText(item.label, lang)} {hasColumns && ( )} {/* Dropdown panel — fixed, full-width. It handles its own onMouseEnter/Leave so the delay timer is cancelled the moment the mouse enters the panel. */} {hasColumns && (
handleOpen(uniqueId)} onMouseLeave={handleClose} >
{item.columns!.map((col, idx) => (

{getLocalizedText(col.title, lang)}

    {col.links.map((link, lIdx) => (
  • {getLocalizedText(link.label, lang)}
  • ))}
))}
)}
); })}
); }