From 5c1bb87b50f17e14d52496979d62fa9b91015e4e Mon Sep 17 00:00:00 2001 From: Vaggelis Yfantis Date: Thu, 8 May 2025 13:46:30 +0300 Subject: [PATCH 1/6] refactor(clerk-js): Simplify plans action localization logic --- .../PricingTable/PricingTableDefault.tsx | 8 ++- .../src/ui/contexts/components/Plans.tsx | 55 ++++++++++++++----- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/packages/clerk-js/src/ui/components/PricingTable/PricingTableDefault.tsx b/packages/clerk-js/src/ui/components/PricingTable/PricingTableDefault.tsx index c34e0f3863c..d229ceedf59 100644 --- a/packages/clerk-js/src/ui/components/PricingTable/PricingTableDefault.tsx +++ b/packages/clerk-js/src/ui/components/PricingTable/PricingTableDefault.tsx @@ -127,13 +127,17 @@ function Card(props: CardProps) { const hasFeatures = plan.features.length > 0; const isPlanActive = subscription?.status === 'active'; const showStatusRow = !!subscription; - const isEligibleForSwitchToAnnual = plan.annualMonthlyAmount > 0 && planPeriod === 'annual'; + const isEligibleForSwitch = planPeriod !== subscription?.planPeriod && !plan.isDefault; + const isEligibleForSwitchToAnnual = isEligibleForSwitch && plan.annualMonthlyAmount > 0 && planPeriod === 'annual'; + const isEligibleForSwitchToMonthly = isEligibleForSwitch && planPeriod === 'month'; const shouldShowFooter = !subscription || subscription?.status === 'upcoming' || subscription?.canceledAt || - (planPeriod !== subscription?.planPeriod && !plan.isDefault && isEligibleForSwitchToAnnual); + isEligibleForSwitchToAnnual || + isEligibleForSwitchToMonthly; + const shouldShowFooterNotice = subscription?.status === 'upcoming' && (planPeriod === subscription.planPeriod || plan.isDefault); diff --git a/packages/clerk-js/src/ui/contexts/components/Plans.tsx b/packages/clerk-js/src/ui/contexts/components/Plans.tsx index fc661f5c5ac..d771f029540 100644 --- a/packages/clerk-js/src/ui/contexts/components/Plans.tsx +++ b/packages/clerk-js/src/ui/contexts/components/Plans.tsx @@ -218,21 +218,48 @@ export const usePlansContext = () => { const isEligibleForSwitchToAnnual = (plan?.annualMonthlyAmount ?? 0) > 0; + const getLocalizationKey = () => { + // Handle subscription cases + if (subscription) { + if (selectedPlanPeriod !== subscription.planPeriod && subscription.canceledAt) { + if (selectedPlanPeriod === 'month') { + return localizationKeys('commerce.switchToMonthly'); + } + + if (isEligibleForSwitchToAnnual) { + return localizationKeys('commerce.switchToAnnual'); + } + } + + if (subscription.canceledAt) { + return localizationKeys('commerce.reSubscribe'); + } + + if (selectedPlanPeriod !== subscription.planPeriod) { + if (selectedPlanPeriod === 'month') { + return localizationKeys('commerce.switchToMonthly'); + } + + if (isEligibleForSwitchToAnnual) { + return localizationKeys('commerce.switchToAnnual'); + } + + return localizationKeys('commerce.manageSubscription'); + } + + return localizationKeys('commerce.manageSubscription'); + } + + // Handle non-subscription cases + const hasNonDefaultSubscriptions = + ctx.subscriptions.filter(subscription => !subscription.plan.isDefault).length > 0; + return hasNonDefaultSubscriptions + ? localizationKeys('commerce.switchPlan') + : localizationKeys('commerce.subscribe'); + }; + return { - localizationKey: subscription - ? subscription.canceledAt - ? localizationKeys('commerce.reSubscribe') - : selectedPlanPeriod !== subscription.planPeriod - ? selectedPlanPeriod === 'month' - ? localizationKeys('commerce.switchToMonthly') - : isEligibleForSwitchToAnnual - ? localizationKeys('commerce.switchToAnnual') - : localizationKeys('commerce.manageSubscription') - : localizationKeys('commerce.manageSubscription') - : // If there are no active or grace period subscriptions, show the get started button - ctx.subscriptions.filter(subscription => !subscription.plan.isDefault).length > 0 - ? localizationKeys('commerce.switchPlan') - : localizationKeys('commerce.subscribe'), + localizationKey: getLocalizationKey(), variant: isCompact ? 'bordered' : 'solid', colorScheme: isCompact ? 'secondary' : 'primary', isDisabled: !canManageBilling, From f71ffdd6596807d6a2d0ce23acc8971e9f134ed2 Mon Sep 17 00:00:00 2001 From: Vaggelis Yfantis Date: Thu, 8 May 2025 14:59:29 +0300 Subject: [PATCH 2/6] fix(clerk-js): Improve flows for switching between plans # Conflicts: # packages/clerk-js/src/ui/components/PricingTable/PricingTableDefault.tsx --- packages/clerk-js/sandbox/template.html | 2 +- .../PricingTable/PricingTableDefault.tsx | 11 ++-- .../src/ui/contexts/components/Plans.tsx | 53 ++++++++++++++++--- 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/packages/clerk-js/sandbox/template.html b/packages/clerk-js/sandbox/template.html index cd4207e24c4..bdaa9c7fee9 100644 --- a/packages/clerk-js/sandbox/template.html +++ b/packages/clerk-js/sandbox/template.html @@ -437,7 +437,7 @@