From a37827047c1330a27431585e6801b98e717d7a53 Mon Sep 17 00:00:00 2001 From: Aman Varshney Date: Sat, 23 Aug 2025 05:40:47 +0530 Subject: [PATCH] fix(web): improve sponsors sorting logic --- apps/web/src/lib/sponsor-utils.ts | 67 +++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 16 deletions(-) diff --git a/apps/web/src/lib/sponsor-utils.ts b/apps/web/src/lib/sponsor-utils.ts index 2770a11..249217d 100644 --- a/apps/web/src/lib/sponsor-utils.ts +++ b/apps/web/src/lib/sponsor-utils.ts @@ -25,36 +25,71 @@ export const isSpecialSponsor = (sponsor: Sponsor): boolean => { export const sortSponsors = (sponsors: Sponsor[]): Sponsor[] => { return sponsors.sort((a, b) => { - // Past sponsors (monthlyDollars === -1) go to the end - if (a.monthlyDollars === -1 && b.monthlyDollars !== -1) return 1; - if (a.monthlyDollars !== -1 && b.monthlyDollars === -1) return -1; + const aAmount = getSponsorAmount(a); + const bAmount = getSponsorAmount(b); + const aIsPast = a.monthlyDollars === -1; + const bIsPast = b.monthlyDollars === -1; + const aIsSpecial = isSpecialSponsor(a); + const bIsSpecial = isSpecialSponsor(b); - // If both are past sponsors, sort by creation date (newest first) - if (a.monthlyDollars === -1 && b.monthlyDollars === -1) { + // 1. Special sponsors (>=$100) come first, sorted by amount (highest first) + if (aIsSpecial && !bIsSpecial) return -1; + if (!aIsSpecial && bIsSpecial) return 1; + if (aIsSpecial && bIsSpecial) { + if (aAmount !== bAmount) { + return bAmount - aAmount; + } + // If amounts equal, prefer monthly over one-time + if (a.isOneTime && !b.isOneTime) return 1; + if (!a.isOneTime && b.isOneTime) return -1; + // Then by creation date (oldest first) + return new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime(); + } + + // 2. Current sponsors come before past sponsors + if (!aIsPast && bIsPast) return -1; + if (aIsPast && !bIsPast) return 1; + + // 3. For current sponsors, sort by amount (highest first) + if (!aIsPast && !bIsPast) { + if (aAmount !== bAmount) { + return bAmount - aAmount; + } + // If amounts equal, prefer monthly over one-time + if (a.isOneTime && !b.isOneTime) return 1; + if (!a.isOneTime && b.isOneTime) return -1; + // Then by creation date (oldest first) + return new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime(); + } + + // 4. For past sponsors, sort by amount (highest first) + if (aIsPast && bIsPast) { + if (aAmount !== bAmount) { + return bAmount - aAmount; + } + // Then by creation date (newest first) return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(); } - // For current sponsors, sort by actual amount (highest first) - const aAmount = getSponsorAmount(a); - const bAmount = getSponsorAmount(b); - if (aAmount !== bAmount) { - return bAmount - aAmount; - } - - // If amounts are equal, sort by creation date (oldest first) - return new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime(); + return 0; }); }; export const sortSpecialSponsors = (sponsors: Sponsor[]): Sponsor[] => { return sponsors.sort((a, b) => { - // Sort by actual amount (highest first) const aAmount = getSponsorAmount(a); const bAmount = getSponsorAmount(b); + + // Sort by actual amount (highest first) if (aAmount !== bAmount) { return bAmount - aAmount; } - // If amounts are equal, sort by creation date (oldest first) + + // If amounts equal, prefer monthly over one-time + if (a.isOneTime && !b.isOneTime) return 1; + if (!a.isOneTime && b.isOneTime) return -1; + + // Then by creation date (oldest first) return new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime(); }); };