import { useEffect, useState } from 'react' import { getRecentPlays, type RecentPlay } from '../../furumiApi' import styles from './header.module.css' function timeAgo(iso: string): string { const diff = Date.now() - new Date(iso).getTime() const mins = Math.floor(diff / 60000) if (mins < 1) return 'just now' if (mins < 60) return `${mins}m ago` const hrs = Math.floor(mins / 60) if (hrs < 24) return `${hrs}h ago` const days = Math.floor(hrs / 24) return `${days}d ago` } export function RecentPlays({ onClose, onPlay }: { onClose: () => void; onPlay: (slug: string) => void }) { const [plays, setPlays] = useState(null) const [loading, setLoading] = useState(true) useEffect(() => { getRecentPlays().then((data) => { setPlays(data) setLoading(false) }) }, []) useEffect(() => { function onKey(e: KeyboardEvent) { if (e.key === 'Escape') onClose() } window.addEventListener('keydown', onKey) return () => window.removeEventListener('keydown', onKey) }, [onClose]) return (
e.stopPropagation()}>

Recent plays

{loading &&

Loading...

} {!loading && (!plays || plays.length === 0) && (

No play history yet

)} {plays?.map((p, i) => (
{ onPlay(p.track_slug); onClose() }} >
{p.track_title}
{p.artist_name}
{timeAgo(p.played_at)}
))}
) }