import { useState, useEffect } from 'react'; interface CompareItem { id: string; slug: string; name: string; brand: string; price: number; image: string; shops: number; } const STORAGE_KEY = 'sp_compare'; const MAX_ITEMS = 3; /** Add/remove item from compare. Returns updated list. */ export function toggleCompare(item: CompareItem): CompareItem[] { const stored = getCompareItems(); const exists = stored.findIndex(i => i.id === item.id); let updated: CompareItem[]; if (exists >= 0) { updated = stored.filter(i => i.id !== item.id); } else { if (stored.length >= MAX_ITEMS) { if ((window as any).__showToast) (window as any).__showToast(`Max ${MAX_ITEMS} sneakers vergelijken`, 'info'); return stored; } updated = [...stored, item]; } localStorage.setItem(STORAGE_KEY, JSON.stringify(updated)); window.dispatchEvent(new Event('sp-compare-update')); return updated; } export function getCompareItems(): CompareItem[] { try { return JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]'); } catch { return []; } } export function clearCompare() { localStorage.removeItem(STORAGE_KEY); window.dispatchEvent(new Event('sp-compare-update')); } export default function CompareBar() { const [items, setItems] = useState([]); const [expanded, setExpanded] = useState(false); useEffect(() => { setItems(getCompareItems()); const handler = () => setItems(getCompareItems()); window.addEventListener('sp-compare-update', handler); return () => window.removeEventListener('sp-compare-update', handler); }, []); if (items.length === 0) return null; return (
{/* Expanded comparison overlay */} {expanded && (
setExpanded(false)}>
e.stopPropagation()}>

Vergelijken

{items.map(item => (
{`${item.brand}

{item.brand}

{item.name}

€{item.price.toFixed(2).replace('.', ',')}

{item.shops} shops

Bekijk
))}
)} {/* Sticky bottom bar */}
{items.length}/{MAX_ITEMS} geselecteerd
{items.map(item => (
))}
); }