"use client"; import { useEffect, useId, useState } from "react"; import { getSpecialBusinesses, saveSpecialBusinesses, type SpecialCarousel, } from "@/services/special-businesses"; import type { MarketplaceBusiness } from "@/types/business"; import styles from "./MakeSpecialModal.module.css"; export default function MakeSpecialModal({ business, open, onClose, onSaved, }: { business: MarketplaceBusiness | null; open: boolean; onClose: () => void; onSaved?: () => void; }) { const titleId = useId(); const [carousels, setCarousels] = useState([]); const [loading, setLoading] = useState(false); const [savingId, setSavingId] = useState(null); const [error, setError] = useState(""); const [status, setStatus] = useState(""); useEffect(() => { if (!open) return; let cancelled = false; void (async () => { setLoading(true); setError(""); setStatus(""); try { const config = await getSpecialBusinesses(); if (!cancelled) setCarousels(config.carousels); } catch (err) { if (!cancelled) { setError( err instanceof Error ? err.message : "Could not load carousels.", ); } } finally { if (!cancelled) setLoading(false); } })(); return () => { cancelled = true; }; }, [open]); useEffect(() => { if (!open) return; const previousOverflow = document.body.style.overflow; document.body.style.overflow = "hidden"; const onKey = (event: KeyboardEvent) => { if (event.key === "Escape" && !savingId) onClose(); }; window.addEventListener("keydown", onKey); return () => { document.body.style.overflow = previousOverflow; window.removeEventListener("keydown", onKey); }; }, [open, savingId, onClose]); if (!open || !business) return null; const name = business.name || business.slug; async function pickCarousel(carousel: SpecialCarousel) { if (!business) return; setSavingId(carousel.id); setError(""); setStatus(""); try { const config = await getSpecialBusinesses(); const nextCarousels = config.carousels.map((item) => { if (item.id !== carousel.id) return item; if (item.businessIds.includes(business.id)) return item; return { ...item, businessIds: [...item.businessIds, business.id], }; }); await saveSpecialBusinesses({ carousels: nextCarousels }); setStatus(`Added to ${carousel.title}`); onSaved?.(); window.setTimeout(() => onClose(), 500); } catch (err) { setError( err instanceof Error ? err.message : "Could not add business to carousel.", ); } finally { setSavingId(null); } } return (
{ if (event.target === event.currentTarget && !savingId) onClose(); }} >

Make special

Choose a carousel

Add {name} to one of the homepage carousels.

{loading ? (

Loading carousels…

) : error && !carousels.length ? (

{error}

) : carousels.length ? (
    {carousels.map((carousel) => { const alreadyIn = carousel.businessIds.includes(business.id); return (
  • ); })}
) : (

No carousels yet. Create one under Website → Carousels.

)} {error && carousels.length ? (

{error}

) : null} {status ?

{status}

: null}
); }