"use client"; import Image from "next/image"; import { useCallback, useEffect, useMemo, useState } from "react"; import { formatEuro, type CustomWaffleConfig, type IngredientPickerItem, } from "@/lib/custom-waffle"; import { isUploadAssetPath } from "@/lib/image-url"; import { WaffleMenuImage } from "./WaffleMenuImage"; function IngredientThumb({ src, alt }: { src: string; alt: string }) { if (src.startsWith("http")) { return ( // eslint-disable-next-line @next/next/no-img-element ); } return ( ); } export function CustomWaffleCard({ config, ingredients, }: { config: CustomWaffleConfig; ingredients: IngredientPickerItem[]; }) { const [open, setOpen] = useState(false); const [selected, setSelected] = useState>(new Set()); const selectedList = useMemo( () => ingredients.filter((ing) => selected.has(ing.id)), [ingredients, selected] ); const extrasTotal = useMemo( () => selectedList.reduce((sum, ing) => sum + ing.price, 0), [selectedList] ); const total = config.basePrice + extrasTotal; const close = useCallback(() => setOpen(false), []); useEffect(() => { if (!open) return; const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") close(); }; document.addEventListener("keydown", onKey); const prev = document.body.style.overflow; document.body.style.overflow = "hidden"; return () => { document.removeEventListener("keydown", onKey); document.body.style.overflow = prev; }; }, [open, close]); function toggle(id: string) { setSelected((prev) => { const next = new Set(prev); if (next.has(id)) next.delete(id); else next.add(id); return next; }); } return ( <> setOpen(true)} className="flex w-full flex-col text-left" > Sur mesure {config.title} {config.description} {selectedList.length > 0 ? ( {selectedList.map((i) => i.name).join(", ")} ) : ( Cliquez pour composer → )} {open ? ( e.stopPropagation()} > {config.title} Base {formatEuro(config.basePrice)} — ajoutez vos ingrédients × {ingredients.length === 0 ? ( Aucun ingrédient disponible pour le moment. ) : ( {ingredients.map((ing) => { const isOn = selected.has(ing.id); return ( toggle(ing.id)} className={`flex w-full items-center gap-3 rounded-2xl border p-3 text-left transition ${ isOn ? "border-gaufre bg-gaufre/15 ring-1 ring-gaufre/40" : "border-chocolat/10 bg-white hover:border-chocolat/20" }`} > {ing.imageUrl ? ( ) : ( — )} {ing.name} +{formatEuro(ing.price)} ✓ ); })} )} {selectedList.length === 0 ? "Aucun ingrédient sélectionné" : `${selectedList.length} ingrédient${selectedList.length > 1 ? "s" : ""}`} {formatEuro(total)} Valider ma composition ) : null} > ); }
{config.description}
{selectedList.map((i) => i.name).join(", ")}
Cliquez pour composer →
Base {formatEuro(config.basePrice)} — ajoutez vos ingrédients
Aucun ingrédient disponible pour le moment.