import { useEffect, useRef } from "react"; import { getSponsorClickUrl, patchSponsorView, type SponsorHomepageItem } from "../api"; interface SponsorCardProps { sponsor: SponsorHomepageItem; } function getDaysLeft(expiresAt: number | null): number { if (!expiresAt) return 0; const now = Math.floor(Date.now() / 1000); return Math.max(0, Math.ceil((expiresAt - now) / 86400)); } function extractDomain(url: string): string { try { const u = new URL(url); return u.hostname.replace(/^www\./, ""); } catch { return "Sponsor"; } } export function SponsorCard({ sponsor }: SponsorCardProps) { const cardRef = useRef(null); const viewedRef = useRef(false); useEffect(() => { const el = cardRef.current; if (!el || viewedRef.current) return; const observer = new IntersectionObserver( (entries) => { if (entries[0]?.isIntersecting && !viewedRef.current) { viewedRef.current = true; patchSponsorView(sponsor.id).catch(() => {}); } }, { threshold: 0.5 } ); observer.observe(el); return () => observer.disconnect(); }, [sponsor.id]); const daysLeft = getDaysLeft(sponsor.expires_at); const clickUrl = getSponsorClickUrl(sponsor.id); return (
{sponsor.image_url ? ( { (e.target as HTMLImageElement).style.display = "none"; (e.target as HTMLImageElement).nextElementSibling?.classList.remove("hidden"); }} /> ) : null}
🔗 {extractDomain(sponsor.link_url)}

{sponsor.title}

{sponsor.description}

Visit sponsor
{daysLeft} days left
); }