'use client'; /** * Wishlist toggle (bare skeleton): saves or removes a product from the shopper's * wishlist. All behaviour is in `useWishlist()`; this file is only markup. * * Keep: `aria-pressed={saved}` (the saved state), the polite `role="status"` region * (confirmation for screen readers) and the `role="alert"` message (a failed save * must be SAID, the button just flips back). Do not make it hover-only: touch * screens have no hover. Keep it a SIBLING of a card's link, never inside it. */ import { useCallback, useEffect, useRef, useState } from '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; /** `icon` is for a product card, `square` for beside add-to-cart on the product page. */ variant?: 'icon' | 'square'; 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) { e.preventDefault(); e.stopPropagation(); const wasSaved = saved; const result = await toggle(key, { product }); // A newer tap on the same button (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') }); } } return ( {/* DESIGN ME — wishlist toggle: a heart icon (lucide-react `Heart`, filled when saved) is the usual choice. `variant="icon"` sits over a product card's image corner; `variant="square"` is a square button beside add-to-cart. Keep the aria-pressed state and the label. Compose with the shadcn/ui primitives in src/components/ui + lucide-react icons. */} {feedback?.kind === 'status' ? feedback.text : ''} {feedback?.kind === 'alert' && {feedback.text}} ); }