fix(web): add total amount in sponsor card

This commit is contained in:
Aman Varshney
2025-08-23 15:45:44 +05:30
parent 4d5bf0a06b
commit bd1b2f4f72
3 changed files with 138 additions and 32 deletions

View File

@@ -18,21 +18,85 @@ export const getSponsorAmount = (sponsor: Sponsor): number => {
return sponsor.monthlyDollars;
};
export const calculateLifetimeContribution = (sponsor: Sponsor): number => {
// For past sponsors, return 0
if (sponsor.monthlyDollars === -1) {
return 0;
}
// For one-time sponsors, return the one-time amount
if (sponsor.isOneTime && sponsor.tierName) {
const match = sponsor.tierName.match(/\$(\d+(?:\.\d+)?)/);
return match ? Number.parseFloat(match[1]) : 0;
}
// For monthly sponsors, calculate total contribution since they started
const startDate = new Date(sponsor.createdAt);
const currentDate = new Date();
const monthsSinceStart = Math.max(
1,
Math.floor(
(currentDate.getTime() - startDate.getTime()) /
(1000 * 60 * 60 * 24 * 30.44),
),
);
return sponsor.monthlyDollars * monthsSinceStart;
};
export const shouldShowLifetimeTotal = (sponsor: Sponsor): boolean => {
// Don't show for past sponsors
if (sponsor.monthlyDollars === -1) {
return false;
}
// Don't show for one-time sponsors
if (sponsor.isOneTime) {
return false;
}
// Don't show for first month sponsors
const startDate = new Date(sponsor.createdAt);
const currentDate = new Date();
const monthsSinceStart = Math.floor(
(currentDate.getTime() - startDate.getTime()) /
(1000 * 60 * 60 * 24 * 30.44),
);
return monthsSinceStart > 1;
};
export const filterVisibleSponsors = (sponsors: Sponsor[]): Sponsor[] => {
return sponsors.filter((sponsor) => {
const amount = getSponsorAmount(sponsor);
return amount >= 5;
});
};
export const isSpecialSponsor = (sponsor: Sponsor): boolean => {
const amount = getSponsorAmount(sponsor);
return amount >= SPECIAL_SPONSOR_THRESHOLD;
};
export const isLifetimeSpecialSponsor = (sponsor: Sponsor): boolean => {
const lifetimeAmount = calculateLifetimeContribution(sponsor);
return lifetimeAmount >= SPECIAL_SPONSOR_THRESHOLD;
};
export const sortSponsors = (sponsors: Sponsor[]): Sponsor[] => {
return sponsors.sort((a, b) => {
const aAmount = getSponsorAmount(a);
const bAmount = getSponsorAmount(b);
const aLifetime = calculateLifetimeContribution(a);
const bLifetime = calculateLifetimeContribution(b);
const aIsPast = a.monthlyDollars === -1;
const bIsPast = b.monthlyDollars === -1;
const aIsSpecial = isSpecialSponsor(a);
const bIsSpecial = isSpecialSponsor(b);
const aIsLifetimeSpecial = isLifetimeSpecialSponsor(a);
const bIsLifetimeSpecial = isLifetimeSpecialSponsor(b);
// 1. Special sponsors (>=$100) come first, sorted by amount (highest first)
// 1. Special sponsors (>=$100 current) come first
if (aIsSpecial && !bIsSpecial) return -1;
if (!aIsSpecial && bIsSpecial) return 1;
if (aIsSpecial && bIsSpecial) {
@@ -46,26 +110,40 @@ export const sortSponsors = (sponsors: Sponsor[]): Sponsor[] => {
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;
// 2. Lifetime special sponsors (>=$100 total) come next
if (aIsLifetimeSpecial && !bIsLifetimeSpecial) return -1;
if (!aIsLifetimeSpecial && bIsLifetimeSpecial) return 1;
if (aIsLifetimeSpecial && bIsLifetimeSpecial) {
if (aLifetime !== bLifetime) {
return bLifetime - aLifetime;
}
// If amounts equal, prefer monthly over one-time
// If lifetime 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)
// 3. Current sponsors come before past sponsors
if (!aIsPast && bIsPast) return -1;
if (aIsPast && !bIsPast) return 1;
// 4. For current sponsors, sort by lifetime contribution (highest first)
if (!aIsPast && !bIsPast) {
if (aLifetime !== bLifetime) {
return bLifetime - aLifetime;
}
// If lifetime 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();
}
// 5. For past sponsors, sort by lifetime contribution (highest first)
if (aIsPast && bIsPast) {
if (aAmount !== bAmount) {
return bAmount - aAmount;
if (aLifetime !== bLifetime) {
return bLifetime - aLifetime;
}
// Then by creation date (newest first)
return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime();
@@ -77,15 +155,22 @@ export const sortSponsors = (sponsors: Sponsor[]): Sponsor[] => {
export const sortSpecialSponsors = (sponsors: Sponsor[]): Sponsor[] => {
return sponsors.sort((a, b) => {
const aAmount = getSponsorAmount(a);
const bAmount = getSponsorAmount(b);
const aLifetime = calculateLifetimeContribution(a);
const bLifetime = calculateLifetimeContribution(b);
// Sort by actual amount (highest first)
if (aAmount !== bAmount) {
return bAmount - aAmount;
// First, prioritize current special sponsors
const aIsSpecial = isSpecialSponsor(a);
const bIsSpecial = isSpecialSponsor(b);
if (aIsSpecial && !bIsSpecial) return -1;
if (!aIsSpecial && bIsSpecial) return 1;
// Then sort by lifetime contribution (highest first)
if (aLifetime !== bLifetime) {
return bLifetime - aLifetime;
}
// If amounts equal, prefer monthly over one-time
// If lifetime amounts equal, prefer monthly over one-time
if (a.isOneTime && !b.isOneTime) return 1;
if (!a.isOneTime && b.isOneTime) return -1;