'use client'; import React, { useEffect, useState } from 'react'; import Link from 'next/link'; import { RiArrowDownSLine } from '@remixicon/react'; import { useCollectionsOnDemand } from '@/hooks/use-shopify-collections'; import { cn } from '@/lib/utils'; interface ShopMenuProps { label?: string; /** Renders inline inside the mobile menu instead of as a floating panel. */ mobile?: boolean; /** Fires after a collection is picked, so the mobile menu can close itself. */ onNavigate?: () => void; } const ShopMenu: React.FC = ({ label = 'Shop', mobile = false, onNavigate, }) => { const [open, setOpen] = useState(false); const { collections, loading, error, load } = useCollectionsOnDemand(); // The collection list is only worth fetching once someone opens the menu. const toggle = () => { const next = !open; setOpen(next); if (next) load(); }; const close = () => { setOpen(false); onNavigate?.(); }; useEffect(() => { if (!open) return; const handleKeyDown = (event: KeyboardEvent) => { if (event.key === 'Escape') setOpen(false); }; window.addEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown); }, [open]); const itemClasses = cn( 'block text-sm text-foreground hover:bg-accent transition-colors', mobile ? 'px-3 py-2' : 'px-4 py-2' ); const body = ( <> {loading && Array.from({ length: 5 }).map((_, index) => (
))} {error && (

{error}

)} {!loading && !error && collections.length === 0 && (

No collections yet.

)} {collections.map((collection) => ( {collection.title} ))} {collections.length > 0 && ( <>
View all collections )} ); const trigger = ( ); if (mobile) { return (
{trigger} {open && (
{body}
)}
); } return (
{trigger} {open && ( <> {/* Catches the click that dismisses the panel. */}
setOpen(false)} />
{body}
)}
); }; export default ShopMenu;