11import {
22 __experimental_usePaymentAttempts ,
33 __experimental_usePaymentMethods ,
4+ __experimental_usePlans ,
45 __experimental_useStatements ,
56 __experimental_useSubscriptionItems ,
67 useClerk ,
7- useOrganization ,
88 useSession ,
9- useUser ,
109} from '@clerk/shared/react' ;
1110import type {
1211 Appearance ,
@@ -15,30 +14,13 @@ import type {
1514 CommerceSubscriptionResource ,
1615} from '@clerk/types' ;
1716import { useCallback , useMemo } from 'react' ;
18- import useSWR from 'swr' ;
1917
2018import { getClosestProfileScrollBox } from '@/ui/utils/getClosestProfileScrollBox' ;
2119
2220import type { LocalizationKey } from '../../localization' ;
2321import { localizationKeys } from '../../localization' ;
2422import { useSubscriberTypeContext } from './SubscriberType' ;
2523
26- const dedupeOptions = {
27- dedupingInterval : 1_000 * 60 , // 1 minute,
28- keepPreviousData : true ,
29- } ;
30-
31- export const usePaymentSourcesCacheKey = ( ) => {
32- const { organization } = useOrganization ( ) ;
33- const { user } = useUser ( ) ;
34- const subscriberType = useSubscriberTypeContext ( ) ;
35-
36- return {
37- key : `commerce-payment-sources` ,
38- resourceId : subscriberType === 'org' ? organization ?. id : user ?. id ,
39- } ;
40- } ;
41-
4224// TODO(@COMMERCE): Rename payment sources to payment methods at the API level
4325export const usePaymentMethods = ( ) => {
4426 const subscriberType = useSubscriberTypeContext ( ) ;
@@ -82,18 +64,16 @@ export const useSubscriptions = () => {
8264 } ) ;
8365} ;
8466
85- export const usePlans = ( ) => {
86- const { billing } = useClerk ( ) ;
67+ export const usePlans = ( params ?: { mode : 'cache' } ) => {
8768 const subscriberType = useSubscriberTypeContext ( ) ;
8869
89- return useSWR (
90- {
91- key : `commerce-plans` ,
92- args : { subscriberType } ,
93- } ,
94- ( { args } ) => billing . getPlans ( args ) ,
95- dedupeOptions ,
96- ) ;
70+ return __experimental_usePlans ( {
71+ for : subscriberType === 'org' ? 'organization' : 'user' ,
72+ initialPage : 1 ,
73+ pageSize : 50 ,
74+ keepPreviousData : true ,
75+ __experimental_mode : params ?. mode ,
76+ } ) ;
9777} ;
9878
9979type HandleSelectPlanProps = {
@@ -125,10 +105,7 @@ export const usePlansContext = () => {
125105 const { data : subscriptions , revalidate : revalidateSubscriptions } = useSubscriptions ( ) ;
126106
127107 // Invalidates cache but does not fetch immediately
128- const { data : plans , mutate : mutatePlans } = useSWR < Awaited < ReturnType < typeof clerk . billing . getPlans > > > ( {
129- key : `commerce-plans` ,
130- args : { subscriberType } ,
131- } ) ;
108+ const { data : plans , revalidate : revalidatePlans } = usePlans ( { mode : 'cache' } ) ;
132109
133110 // Invalidates cache but does not fetch immediately
134111 const { revalidate : revalidateStatements } = useStatements ( { mode : 'cache' } ) ;
@@ -138,10 +115,10 @@ export const usePlansContext = () => {
138115 const revalidateAll = useCallback ( ( ) => {
139116 // Revalidate the plans and subscriptions
140117 void revalidateSubscriptions ( ) ;
141- void mutatePlans ( ) ;
118+ void revalidatePlans ( ) ;
142119 void revalidateStatements ( ) ;
143120 void revalidatePaymentSources ( ) ;
144- } , [ revalidateSubscriptions , mutatePlans , revalidateStatements , revalidatePaymentSources ] ) ;
121+ } , [ revalidateSubscriptions , revalidatePlans , revalidateStatements , revalidatePaymentSources ] ) ;
145122
146123 // should the default plan be shown as active
147124 const isDefaultPlanImplicitlyActiveOrUpcoming = useMemo ( ( ) => {
0 commit comments