import { useState } from 'react' import { useCoupons } from '../hooks/useApi.ts' import { StoreIcon } from '../components/StoreIcon.tsx' const SEVEN_DAYS_MS = 7 * 24 * 60 * 60 * 1000 function isExpiringSoon(expiresAt: string): boolean { return new Date(expiresAt).getTime() - Date.now() < SEVEN_DAYS_MS } export function Coupons() { const { data: coupons = [], isLoading, error } = useCoupons() const [copied, setCopied] = useState(null) function handleCopy(code: string, id: string) { navigator.clipboard?.writeText(code) setCopied(id) setTimeout(() => setCopied(null), 2000) } const storeIds: Record = { Meijer: 'meijer', Kroger: 'kroger', Target: 'target', } if (isLoading) { return (
{[1, 2, 3].map((i) => (
))}
) } if (error) { return (

Failed to load coupons.

) } return (

Coupons & Deals

{coupons.map((coupon) => { const expiringSoon = isExpiringSoon(coupon.expiresAt) return (

{coupon.description}

{coupon.storeName}

Expires{' '} {new Date(coupon.expiresAt).toLocaleDateString('en-US', { month: 'short', day: 'numeric', })} {expiringSoon && ' — expiring soon!'}

{coupon.discount}
{coupon.code && ( )}
) })}
) }