@@ -176,6 +176,40 @@ export const usePlansContext = () => {
176
176
[ ctx . subscriptions ] ,
177
177
) ;
178
178
179
+ // returns all subscriptions for a plan that are active or upcoming
180
+ const activeAndUpcomingSubscriptions = useCallback (
181
+ ( plan : CommercePlanResource ) => {
182
+ return ctx . subscriptions . filter ( subscription => subscription . plan . id === plan . id ) ;
183
+ } ,
184
+ [ ctx . subscriptions ] ,
185
+ ) ;
186
+
187
+ // return the active or upcoming subscription for a plan based on the plan period, if there is no subscription for the plan period, return the first subscription
188
+ const activeOrUpcomingSubscriptionWithPlanPeriod = useCallback (
189
+ ( plan : CommercePlanResource , planPeriod : CommerceSubscriptionPlanPeriod = 'month' ) => {
190
+ const plansSubscriptions = activeAndUpcomingSubscriptions ( plan ) ;
191
+ // Handle multiple subscriptions for the same plan
192
+ if ( plansSubscriptions . length > 1 ) {
193
+ const subscriptionBaseOnPanPeriod = plansSubscriptions . find ( subscription => {
194
+ return subscription . planPeriod === planPeriod ;
195
+ } ) ;
196
+
197
+ if ( subscriptionBaseOnPanPeriod ) {
198
+ return subscriptionBaseOnPanPeriod ;
199
+ }
200
+
201
+ return plansSubscriptions [ 0 ] ;
202
+ }
203
+
204
+ if ( plansSubscriptions . length === 1 ) {
205
+ return plansSubscriptions [ 0 ] ;
206
+ }
207
+
208
+ return undefined ;
209
+ } ,
210
+ [ activeAndUpcomingSubscriptions ] ,
211
+ ) ;
212
+
179
213
const canManageSubscription = useCallback (
180
214
( { plan, subscription : sub } : { plan ?: CommercePlanResource ; subscription ?: CommerceSubscriptionResource } ) => {
181
215
const subscription = sub ?? ( plan ? activeOrUpcomingSubscription ( plan ) : undefined ) ;
@@ -212,36 +246,64 @@ export const usePlansContext = () => {
212
246
isDisabled : boolean ;
213
247
disabled : boolean ;
214
248
} => {
215
- const subscription = sub ?? ( plan ? activeOrUpcomingSubscription ( plan ) : undefined ) ;
249
+ const subscription =
250
+ sub ?? ( plan ? activeOrUpcomingSubscriptionWithPlanPeriod ( plan , selectedPlanPeriod ) : undefined ) ;
216
251
let _selectedPlanPeriod = selectedPlanPeriod ;
217
252
if ( _selectedPlanPeriod === 'annual' && sub ?. plan . annualMonthlyAmount === 0 ) {
218
253
_selectedPlanPeriod = 'month' ;
219
254
}
220
255
221
256
const isEligibleForSwitchToAnnual = ( plan ?. annualMonthlyAmount ?? 0 ) > 0 ;
222
257
258
+ const getLocalizationKey = ( ) => {
259
+ // Handle subscription cases
260
+ if ( subscription ) {
261
+ if ( _selectedPlanPeriod !== subscription . planPeriod && subscription . canceledAt ) {
262
+ if ( _selectedPlanPeriod === 'month' ) {
263
+ return localizationKeys ( 'commerce.switchToMonthly' ) ;
264
+ }
265
+
266
+ if ( isEligibleForSwitchToAnnual ) {
267
+ return localizationKeys ( 'commerce.switchToAnnual' ) ;
268
+ }
269
+ }
270
+
271
+ if ( subscription . canceledAt ) {
272
+ return localizationKeys ( 'commerce.reSubscribe' ) ;
273
+ }
274
+
275
+ if ( _selectedPlanPeriod !== subscription . planPeriod ) {
276
+ if ( _selectedPlanPeriod === 'month' ) {
277
+ return localizationKeys ( 'commerce.switchToMonthly' ) ;
278
+ }
279
+
280
+ if ( isEligibleForSwitchToAnnual ) {
281
+ return localizationKeys ( 'commerce.switchToAnnual' ) ;
282
+ }
283
+
284
+ return localizationKeys ( 'commerce.manageSubscription' ) ;
285
+ }
286
+
287
+ return localizationKeys ( 'commerce.manageSubscription' ) ;
288
+ }
289
+
290
+ // Handle non-subscription cases
291
+ const hasNonDefaultSubscriptions =
292
+ ctx . subscriptions . filter ( subscription => ! subscription . plan . isDefault ) . length > 0 ;
293
+ return hasNonDefaultSubscriptions
294
+ ? localizationKeys ( 'commerce.switchPlan' )
295
+ : localizationKeys ( 'commerce.subscribe' ) ;
296
+ } ;
297
+
223
298
return {
224
- localizationKey : subscription
225
- ? subscription . canceledAt
226
- ? localizationKeys ( 'commerce.reSubscribe' )
227
- : selectedPlanPeriod !== subscription . planPeriod
228
- ? selectedPlanPeriod === 'month'
229
- ? localizationKeys ( 'commerce.switchToMonthly' )
230
- : isEligibleForSwitchToAnnual
231
- ? localizationKeys ( 'commerce.switchToAnnual' )
232
- : localizationKeys ( 'commerce.manageSubscription' )
233
- : localizationKeys ( 'commerce.manageSubscription' )
234
- : // If there are no active or grace period subscriptions, show the get started button
235
- ctx . subscriptions . filter ( subscription => ! subscription . plan . isDefault ) . length > 0
236
- ? localizationKeys ( 'commerce.switchPlan' )
237
- : localizationKeys ( 'commerce.subscribe' ) ,
299
+ localizationKey : getLocalizationKey ( ) ,
238
300
variant : isCompact ? 'bordered' : 'solid' ,
239
301
colorScheme : isCompact ? 'secondary' : 'primary' ,
240
302
isDisabled : ! canManageBilling ,
241
303
disabled : ! canManageBilling ,
242
304
} ;
243
305
} ,
244
- [ activeOrUpcomingSubscription , canManageBilling , ctx . subscriptions ] ,
306
+ [ activeOrUpcomingSubscriptionWithPlanPeriod , canManageBilling , ctx . subscriptions ] ,
245
307
) ;
246
308
247
309
const captionForSubscription = useCallback ( ( subscription : CommerceSubscriptionResource ) => {
@@ -261,7 +323,7 @@ export const usePlansContext = () => {
261
323
262
324
const portalRoot = getClosestProfileScrollBox ( mode , event ) ;
263
325
264
- if ( subscription && ! subscription . canceledAt ) {
326
+ if ( subscription && subscription . planPeriod === planPeriod && ! subscription . canceledAt ) {
265
327
clerk . __internal_openPlanDetails ( {
266
328
plan,
267
329
subscriberType,
@@ -303,6 +365,8 @@ export const usePlansContext = () => {
303
365
...ctx ,
304
366
componentName,
305
367
activeOrUpcomingSubscription,
368
+ activeAndUpcomingSubscriptions,
369
+ activeOrUpcomingSubscriptionBasedOnPlanPeriod : activeOrUpcomingSubscriptionWithPlanPeriod ,
306
370
isDefaultPlanImplicitlyActiveOrUpcoming,
307
371
handleSelectPlan,
308
372
buttonPropsForPlan,
0 commit comments