A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
refactor(app): share savings helpers, drop old price formatter
Hampusc9ced02a8534
diff --git a/fluxer_app/src/features/premium/utils/PremiumSavings.ts b/fluxer_app/src/features/premium/utils/PremiumSavings.tsnew file mode 100644index 000000000..7867783e1--- /dev/null+++ b/fluxer_app/src/features/premium/utils/PremiumSavings.ts@@ -0,0 +1,10 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++export function yearlySavingsPercent(+ monthlyMinor: number | null | undefined,+ yearlyMinor: number | null | undefined,+): number | null {+ if (monthlyMinor == null || yearlyMinor == null || monthlyMinor <= 0) return null;+ const percent = Math.round((1 - yearlyMinor / (monthlyMinor * 12)) * 100);+ return percent > 0 ? percent : null;+}diff --git a/fluxer_app/src/features/premium/utils/PremiumSubscriptionClassification.ts b/fluxer_app/src/features/premium/utils/PremiumSubscriptionClassification.tsindex eb7e58a87..8f7000729 100644--- a/fluxer_app/src/features/premium/utils/PremiumSubscriptionClassification.ts+++ b/fluxer_app/src/features/premium/utils/PremiumSubscriptionClassification.ts@@ -9,6 +9,13 @@ const BLOCKING_SUBSCRIPTION_STATUSES: ReadonlySet<string> = new Set(['paused',]);+const RENEWING_SUBSCRIPTION_STATUSES: ReadonlySet<string> = new Set(['active', 'trialing']);++export interface BillingSubscriptionPeriod {+ status: string | null;+ current_period_end: string | null;+}+export interface PremiumSubscriptionClassificationInput {hasPaidPremium: boolean;isVisionary: boolean;@@ -19,7 +26,7 @@ export interface PremiumSubscriptionClassificationInput {isFullyExpired: boolean;billingKnown: boolean;storeOwnershipKnown: boolean;- billingSubscription: {status: string | null; current_period_end: string | null} | null;+ billingSubscription: BillingSubscriptionPeriod | null;}export interface PremiumSubscriptionClassification {@@ -59,3 +66,17 @@ export function classifyPremiumSubscription(canStartSubscription,};}++export function getSubscriptionRenewalDate(+ premiumUntil: Date | null,+ billingSubscription: BillingSubscriptionPeriod | null,+): Date | null {+ if (+ !billingSubscription?.current_period_end ||+ !RENEWING_SUBSCRIPTION_STATUSES.has(billingSubscription.status ?? '')+ ) {+ return premiumUntil;+ }+ const periodEnd = new Date(billingSubscription.current_period_end);+ return Number.isNaN(periodEnd.getTime()) ? premiumUntil : periodEnd;+}diff --git a/fluxer_app/src/features/premium/utils/PricingUtils.ts b/fluxer_app/src/features/premium/utils/PricingUtils.tsdeleted file mode 100644index 0e2ea20cb..000000000--- a/fluxer_app/src/features/premium/utils/PricingUtils.ts+++ /dev/null@@ -1,29 +0,0 @@-// SPDX-License-Identifier: AGPL-3.0-or-later--import {getCachedNumberFormat} from '@app/features/i18n/utils/IntlCache';--const STRIPE_TWO_DECIMAL_WHOLE_UNIT_CURRENCIES = new Set(['ISK', 'HUF', 'UGX']);--export function formatMinorUnitPrice(- amountMinor: number | null | undefined,- currency: string | null | undefined,- locale: string,-): string | null {- if (amountMinor == null || !currency) {- return null;- }- const wholeUnitDisplay = STRIPE_TWO_DECIMAL_WHOLE_UNIT_CURRENCIES.has(currency.toUpperCase());- const displayFractionDigits = wholeUnitDisplay- ? 0- : (getCachedNumberFormat(locale, {- style: 'currency',- currency,- }).resolvedOptions().maximumFractionDigits ?? 2);- const exponent = wholeUnitDisplay ? 2 : displayFractionDigits;- return getCachedNumberFormat(locale, {- style: 'currency',- currency,- minimumFractionDigits: displayFractionDigits,- maximumFractionDigits: displayFractionDigits,- }).format(amountMinor / 10 ** exponent);-}