@@ -3,6 +3,7 @@ import type {
33 __experimental_CommercePlanResource ,
44 __experimental_CommerceSubscriberType ,
55 __experimental_CommerceSubscriptionPlanPeriod ,
6+ __experimental_CommerceSubscriptionResource ,
67} from '@clerk/types' ;
78import type { ComponentType , ReactNode } from 'react' ;
89import { createContext , useCallback , useContext , useMemo } from 'react' ;
@@ -19,25 +20,31 @@ type PlansContextProviderProps = {
1920
2021const PlansContext = createContext < __experimental_PlansCtx | null > ( null ) ;
2122
23+ export const useSubscriptions = ( subscriberType ?: __experimental_CommerceSubscriberType ) => {
24+ const { __experimental_commerce } = useClerk ( ) ;
25+ const { organization } = useOrganization ( ) ;
26+
27+ return useFetch (
28+ __experimental_commerce ?. __experimental_billing . getSubscriptions ,
29+ { orgId : subscriberType === 'org' ? organization ?. id : undefined } ,
30+ undefined ,
31+ 'commerce-subscriptions' ,
32+ ) ;
33+ } ;
34+
2235export const PlansContextProvider = ( {
2336 subscriberType = 'user' ,
2437 children,
2538} : PlansContextProviderProps & {
2639 children : ReactNode ;
2740} ) => {
2841 const { __experimental_commerce } = useClerk ( ) ;
29- const { organization } = useOrganization ( ) ;
3042
3143 const {
3244 data : subscriptions ,
3345 isLoading : isLoadingSubscriptions ,
3446 revalidate : revalidateSubscriptions ,
35- } = useFetch (
36- __experimental_commerce ?. __experimental_billing . getSubscriptions ,
37- { orgId : subscriberType === 'org' ? organization ?. id : undefined } ,
38- undefined ,
39- 'commerce-subscriptions' ,
40- ) ;
47+ } = useSubscriptions ( subscriberType ) ;
4148
4249 const {
4350 data : plans ,
@@ -133,12 +140,14 @@ export const usePlansContext = () => {
133140 const buttonPropsForPlan = useCallback (
134141 ( {
135142 plan,
143+ subscription : sub ,
136144 isCompact = false ,
137145 } : {
138- plan : __experimental_CommercePlanResource ;
146+ plan ?: __experimental_CommercePlanResource ;
147+ subscription ?: __experimental_CommerceSubscriptionResource ;
139148 isCompact ?: boolean ;
140149 } ) : { localizationKey : LocalizationKey ; variant : 'bordered' | 'solid' ; colorScheme : 'secondary' | 'primary' } => {
141- const subscription = activeOrUpcomingSubscription ( plan ) ;
150+ const subscription = sub ?? ( plan ? activeOrUpcomingSubscription ( plan ) : undefined ) ;
142151
143152 return {
144153 localizationKey : subscription
@@ -162,7 +171,11 @@ export const usePlansContext = () => {
162171 clerk . __internal_openSubscriptionDetails ( {
163172 subscription,
164173 subscriberType : ctx . subscriberType ,
165- onSubscriptionCancel : onSubscriptionChange ,
174+ onSubscriptionCancel : ( ) => {
175+ ctx . revalidate ( ) ;
176+ onSubscriptionChange ?.( ) ;
177+ clerk . __internal_closeSubscriptionDetails ( ) ;
178+ } ,
166179 portalId :
167180 mode === 'modal'
168181 ? ctx . subscriberType === 'user'
0 commit comments