@@ -217,27 +217,54 @@ export const usePlansContext = () => {
217
217
218
218
const isEligibleForSwitchToAnnual = ( plan ?. annualMonthlyAmount ?? 0 ) > 0 ;
219
219
220
+ const getLocalizationKey = ( ) => {
221
+ // Handle subscription cases
222
+ if ( subscription ) {
223
+ if ( selectedPlanPeriod !== subscription . planPeriod && subscription . canceledAt ) {
224
+ if ( selectedPlanPeriod === 'month' ) {
225
+ return localizationKeys ( 'commerce.switchToMonthly' ) ;
226
+ }
227
+
228
+ if ( isEligibleForSwitchToAnnual ) {
229
+ return localizationKeys ( 'commerce.switchToAnnual' ) ;
230
+ }
231
+ }
232
+
233
+ if ( subscription . canceledAt ) {
234
+ return localizationKeys ( 'commerce.reSubscribe' ) ;
235
+ }
236
+
237
+ if ( selectedPlanPeriod !== subscription . planPeriod ) {
238
+ if ( selectedPlanPeriod === 'month' ) {
239
+ return localizationKeys ( 'commerce.switchToMonthly' ) ;
240
+ }
241
+
242
+ if ( isEligibleForSwitchToAnnual ) {
243
+ return localizationKeys ( 'commerce.switchToAnnual' ) ;
244
+ }
245
+
246
+ return localizationKeys ( 'commerce.manageSubscription' ) ;
247
+ }
248
+
249
+ return localizationKeys ( 'commerce.manageSubscription' ) ;
250
+ }
251
+
252
+ // Handle non-subscription cases
253
+ const hasNonDefaultSubscriptions =
254
+ ctx . subscriptions . filter ( subscription => ! subscription . plan . isDefault ) . length > 0 ;
255
+ return hasNonDefaultSubscriptions
256
+ ? localizationKeys ( 'commerce.switchPlan' )
257
+ : localizationKeys ( 'commerce.subscribe' ) ;
258
+ } ;
259
+
220
260
return {
221
- localizationKey : subscription
222
- ? subscription . canceledAt
223
- ? localizationKeys ( 'commerce.reSubscribe' )
224
- : selectedPlanPeriod !== subscription . planPeriod
225
- ? selectedPlanPeriod === 'month'
226
- ? localizationKeys ( 'commerce.switchToMonthly' )
227
- : isEligibleForSwitchToAnnual
228
- ? localizationKeys ( 'commerce.switchToAnnual' )
229
- : localizationKeys ( 'commerce.manageSubscription' )
230
- : localizationKeys ( 'commerce.manageSubscription' )
231
- : // If there are no active or grace period subscriptions, show the get started button
232
- ctx . subscriptions . filter ( subscription => ! subscription . plan . isDefault ) . length > 0
233
- ? localizationKeys ( 'commerce.switchPlan' )
234
- : localizationKeys ( 'commerce.subscribe' ) ,
261
+ localizationKey : getLocalizationKey ( ) ,
235
262
variant : isCompact ? 'bordered' : 'solid' ,
236
263
colorScheme : isCompact ? 'secondary' : 'primary' ,
237
264
isDisabled : ! canManageBilling ,
238
265
} ;
239
266
} ,
240
- [ activeOrUpcomingSubscription ] ,
267
+ [ activeOrUpcomingSubscription , canManageBilling , ctx . subscriptions ] ,
241
268
) ;
242
269
243
270
const captionForSubscription = useCallback ( ( subscription : CommerceSubscriptionResource ) => {
0 commit comments