Add custom option

This commit is contained in:
Eric
2026-10-30 13:57:11 +01:00
parent 6d14e99aee
commit 03bb8c1006
2 changed files with 41 additions and 5 deletions
@@ -5,9 +5,15 @@ import Icon from '@lucide/vue/src/Icon';
import { check } from '../../../data/iconNodes';
import SponsorSection from './SponsorSection.vue';
import SponsorSectionHeader from './SponsorSectionHeader.vue';
import { TIERS, formatEuro, getPreviousTier, getTierForAmount, getTierLinks } from './tiers';
type Frequency = 'monthly' | 'once';
import {
TIERS,
formatEuro,
getAmountLinks,
getPreviousTier,
getTierForAmount,
getTierLinks,
type Frequency,
} from './tiers';
const frequencies: { id: Frequency; label: string }[] = [
{ id: 'monthly', label: 'Monthly' },
@@ -29,8 +35,16 @@ const total = computed(() => {
const tier = computed(() => (isMonthly.value ? getTierForAmount(total.value) : null));
// Only a monthly preset matches a tier's checkout, other amounts go to the general pages.
const links = computed(() => getTierLinks(customAmount.value === '' ? tier.value?.id : undefined));
// A monthly preset opens its tier's checkout, other amounts open a checkout prefilled with
// the amount. The platforms expect whole amounts, so decimals are rounded.
const links = computed(() => {
const value = Math.round(total.value);
if (customAmount.value === '' && tier.value) return getTierLinks(tier.value.id);
if (value > 0) return getAmountLinks(value, frequency.value);
return getTierLinks();
});
const amountText = computed(() => formatEuro(total.value));
@@ -101,4 +101,26 @@ export function getTierLinks(id?: TierId) {
};
}
export type Frequency = 'monthly' | 'once';
/** Checkout links prefilled with a custom amount and frequency. */
export function getAmountLinks(amount: number, frequency: Frequency) {
const isMonthly = frequency === 'monthly';
const openCollectiveParams = new URLSearchParams({
interval: isMonthly ? 'month' : 'oneTime',
amount: String(amount),
contributeAs: 'me',
});
const githubSponsorsParams = new URLSearchParams({
frequency: isMonthly ? 'recurring' : 'one-time',
amount: String(amount),
});
return {
openCollective: `${OPEN_COLLECTIVE_URL}/donate?${openCollectiveParams}`,
githubSponsors: `${GITHUB_SPONSORS_URL}/sponsorships?${githubSponsorsParams}`,
};
}
export const formatEuro = (amount: number) => `€${amount.toLocaleString('en-US')}`;