'use client'; /** * The heart that saves a product to the shopper's wishlist. * * Two shapes, one behaviour: * - `variant="icon"` (default): a small round heart for the corner of a product card. * - `variant="square"`: a full-height square button for the product page, beside * add-to-cart. * * State and writes come from `useWishlist()`: the heart fills the moment it is * tapped and goes back by itself when the save fails. Feedback is the filled heart * plus a polite live region (no toast library); a refusal is printed next to the * heart and announced as an alert. It is always visible, never hover-only, because * touch screens have no hover. * * It is a sibling of a card's link, never inside it: a button inside an is not * valid HTML and gets two activations on some screen readers. */ import { useCallback, useEffect, useRef, useState } from 'react'; import { Heart } from 'lucide-react'; import type { Product } from 'brainerce'; import { useWishlist } from '@/core/hooks/use-wishlist'; import { useTranslations } from '@/core/lib/translations'; import { cn } from '@/core/lib/utils'; interface WishlistButtonProps { productId: string; /** Leave unset to save the product as a whole (what a card and the product page do). */ variantId?: string | null; /** Only used to describe the item to the marketing tags (name and price). */ product?: Product; variant?: 'icon' | 'square'; /** Position it from the parent: `absolute end-2 top-2 z-10`. */ className?: string; } type Feedback = { kind: 'status' | 'alert'; text: string }; export function WishlistButton({ productId, variantId, product, variant = 'icon', className, }: WishlistButtonProps) { const t = useTranslations('wishlist'); const { status, isSaved, toggle } = useWishlist(); const [feedback, setFeedback] = useState(null); const timerRef = useRef | null>(null); const key = { productId, variantId: variantId ?? null }; const saved = isSaved(key); useEffect( () => () => { if (timerRef.current) clearTimeout(timerRef.current); }, [] ); const say = useCallback((next: Feedback) => { setFeedback(next); if (timerRef.current) clearTimeout(timerRef.current); timerRef.current = setTimeout(() => setFeedback(null), 4000); }, []); async function handleClick(e: React.MouseEvent) { // Harmless where the heart is a sibling of the link; keeps it safe if a design nests it. e.preventDefault(); e.stopPropagation(); const wasSaved = saved; const result = await toggle(key, { product }); // A newer tap on the same heart (or a session change) owns what it shows now: say nothing // about this older one. if (!result.ok && result.superseded) return; if (result.ok) { say({ kind: 'status', text: wasSaved ? t('removedAnnouncement') : t('savedAnnouncement') }); } else if (result.reason === 'full') { say({ kind: 'alert', text: t('listFull') }); } else if (status === 'error') { say({ kind: 'alert', text: t('notReady') }); } else { say({ kind: 'alert', text: wasSaved ? t('removeFailed') : t('saveFailed') }); } } // One label for both states: `aria-pressed` carries saved or not, and a label that also // flipped would be announced twice (and contradict itself). The product name tells the // hearts of a grid apart. const label = product ? `${t('save')}: ${product.name}` : t('save'); return ( {/* Polite confirmation for screen readers. The filled heart is the visual one. */} {feedback?.kind === 'status' ? feedback.text : ''} {/* A refusal is shown, not only announced: the heart going back is easy to miss. */} {feedback?.kind === 'alert' && ( {feedback.text} )} ); }