import { ChevronDown, ChevronUp, Github, Globe, Heart, Terminal, } from "lucide-react"; import Image from "next/image"; import { useEffect, useState } from "react"; import type { Sponsor } from "@/lib/types"; export default function SponsorsSection() { const [sponsors, setSponsors] = useState([]); const [loadingSponsors, setLoadingSponsors] = useState(true); const [sponsorError, setSponsorError] = useState(null); const [showPastSponsors, setShowPastSponsors] = useState(false); useEffect(() => { fetch("https://sponsors.amanv.dev/sponsors.json") .then((res) => { if (!res.ok) throw new Error("Failed to fetch sponsors"); return res.json(); }) .then((data) => { const sponsorsData = Array.isArray(data) ? data : []; const sortedSponsors = sponsorsData.sort((a, b) => { const getAmount = (sponsor: Sponsor) => { if (!sponsor.isOneTime && sponsor.monthlyDollars > 0) { return sponsor.monthlyDollars; } if (sponsor.tierName) { const match = sponsor.tierName.match(/\$(\d+(?:\.\d+)?)/); return match ? Number.parseFloat(match[1]) : 0; } return 0; }; if (a.monthlyDollars === -1 && b.monthlyDollars !== -1) return 1; if (a.monthlyDollars !== -1 && b.monthlyDollars === -1) return -1; if (a.monthlyDollars === -1 && b.monthlyDollars === -1) { return ( new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime() ); } const aIsMonthly = !a.isOneTime; const bIsMonthly = !b.isOneTime; if (aIsMonthly && !bIsMonthly) return -1; if (!aIsMonthly && bIsMonthly) return 1; return getAmount(b) - getAmount(a); }); setSponsors(sortedSponsors); setLoadingSponsors(false); }) .catch(() => { setSponsorError("Could not load sponsors"); setLoadingSponsors(false); }); }, []); const currentSponsors = sponsors.filter( (sponsor) => sponsor.monthlyDollars !== -1, ); const pastSponsors = sponsors.filter( (sponsor) => sponsor.monthlyDollars === -1, ); return (
SPONSORS_DATABASE.JSON
[{loadingSponsors ? "LOADING..." : sponsors.length} RECORDS]
{loadingSponsors ? (
LOADING_SPONSORS.SH
) : sponsorError ? (
ERROR: {sponsorError}
) : sponsors.length === 0 ? (
NO_SPONSORS_FOUND.NULL
$ Be the first to support this project!
) : (
{currentSponsors.length > 0 && (
{/*
ACTIVE_SPONSORS.SH ({currentSponsors.length})
*/}
{currentSponsors.map((entry, index) => { const since = new Date(entry.createdAt).toLocaleDateString( undefined, { year: "numeric", month: "short" }, ); return (
{entry.isOneTime ? "ONE-TIME" : "MONTHLY"} SINCE {since.toUpperCase()}
{entry.sponsor.name

{entry.sponsor.name || entry.sponsor.login}

{entry.tierName && (

{entry.tierName}

)}
); })}
)} {pastSponsors.length > 0 && (
{showPastSponsors && (
{pastSponsors.map((entry, index) => { const since = new Date(entry.createdAt).toLocaleDateString( undefined, { year: "numeric", month: "short" }, ); return (
PAST SINCE {since.toUpperCase()}
{entry.sponsor.name

{entry.sponsor.name || entry.sponsor.login}

{entry.tierName && (

{entry.tierName}

)}
); })}
)}
)}
)}
); }