'use client' import { useState, useEffect } from 'react' import Image from 'next/image' interface PastDrop { id: number item: string size: number fill: number unit: string ppu: number image_url: string | null created_at: string soldOutInHours: number } interface PastDropsProps { limit?: number showMoreLink?: boolean } export default function PastDrops({ limit, showMoreLink = false }: PastDropsProps = {}) { const [drops, setDrops] = useState([]) const [loading, setLoading] = useState(true) useEffect(() => { fetchPastDrops() }, []) const fetchPastDrops = async () => { try { const response = await fetch('/api/drops/past') if (response.ok) { const data = await response.json() setDrops(data) } } catch (error) { console.error('Error fetching past drops:', error) } finally { setLoading(false) } } const formatSoldOutTime = (hours: number) => { if (hours < 1) { return 'Sold out in less than 1h' } else if (hours === 1) { return 'Sold out in 1h' } else if (hours < 24) { return `Sold out in ${hours}h` } else { const days = Math.floor(hours / 24) const remainingHours = hours % 24 if (remainingHours === 0) { return days === 1 ? 'Sold out in 1 day' : `Sold out in ${days} days` } else { return `Sold out in ${days}d ${remainingHours}h` } } } const formatDateAndTime = (dateString: string) => { const date = new Date(dateString) const day = date.getDate() const monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] const month = monthNames[date.getMonth()] const hours = date.getHours().toString().padStart(2, '0') const minutes = date.getMinutes().toString().padStart(2, '0') return `${day} ${month} · ${hours}:${minutes}` } const formatQuantity = (size: number, unit: string) => { return `${size}${unit}` } if (loading) { return (

Loading past drops...

) } if (drops.length === 0) { return (

No past drops yet. Check back soon!

) } const displayedDrops = limit ? drops.slice(0, limit) : drops const hasMore = limit && drops.length > limit return ( <>
{displayedDrops.map((drop) => (
{drop.image_url ? (
{drop.item}
) : (
No Image
)} {drop.item}
{formatSoldOutTime(drop.soldOutInHours)}
{formatQuantity(drop.size, drop.unit)} · {formatDateAndTime(drop.created_at)}
))}
{showMoreLink && hasMore && (
More →
)} ) }