2
2
__experimental_usePaymentAttempts ,
3
3
__experimental_usePaymentMethods ,
4
4
__experimental_useStatements ,
5
+ __experimental_useSubscriptionItems ,
5
6
useClerk ,
6
7
useOrganization ,
7
8
useSession ,
@@ -16,7 +17,6 @@ import type {
16
17
import { useCallback , useMemo } from 'react' ;
17
18
import useSWR from 'swr' ;
18
19
19
- import { CommerceSubscription } from '@/core/resources/CommerceSubscription' ;
20
20
import { getClosestProfileScrollBox } from '@/ui/utils/getClosestProfileScrollBox' ;
21
21
22
22
import type { LocalizationKey } from '../../localization' ;
@@ -72,58 +72,14 @@ export const useStatements = (params?: { mode: 'cache' }) => {
72
72
} ;
73
73
74
74
export const useSubscriptions = ( ) => {
75
- const { billing } = useClerk ( ) ;
76
- const { organization } = useOrganization ( ) ;
77
- const { user, isSignedIn } = useUser ( ) ;
78
75
const subscriberType = useSubscriberTypeContext ( ) ;
79
- const { data : plans } = usePlans ( ) ;
80
-
81
- const { data : _subscriptions , ...rest } = useSWR (
82
- {
83
- key : `commerce-subscriptions` ,
84
- userId : user ?. id ,
85
- args : { orgId : subscriberType === 'org' ? organization ?. id : undefined } ,
86
- } ,
87
- ( { args, userId } ) => ( userId ? billing . getSubscriptions ( args ) : undefined ) ,
88
- dedupeOptions ,
89
- ) ;
90
-
91
- const subscriptions = useMemo ( ( ) => {
92
- if ( ! _subscriptions ) {
93
- return [ ] ;
94
- }
95
- const defaultFreePlan = plans ?. find ( plan => plan . hasBaseFee === false && plan . amount === 0 ) ;
96
-
97
- // are we signed in, is there a default free plan, and should it be shown as active or upcoming? then add an implicit subscription
98
- if (
99
- isSignedIn &&
100
- defaultFreePlan &&
101
- ( _subscriptions . data . length === 0 || ! _subscriptions . data . some ( subscription => ! subscription . canceledAtDate ) )
102
- ) {
103
- const canceledSubscription = _subscriptions . data . find ( subscription => subscription . canceledAtDate ) ;
104
- return [
105
- ..._subscriptions . data ,
106
- new CommerceSubscription ( {
107
- object : 'commerce_subscription' ,
108
- id : '__implicit_default_plan_subscription__' ,
109
- payment_source_id : '' ,
110
- plan : defaultFreePlan . __internal_toSnapshot ( ) ,
111
- plan_period : 'month' ,
112
- canceled_at : null ,
113
- status : _subscriptions . data . length === 0 ? 'active' : 'upcoming' ,
114
- created_at : canceledSubscription ?. periodEndDate ?. getTime ( ) || 0 ,
115
- period_start : canceledSubscription ?. periodEndDate ?. getTime ( ) || 0 ,
116
- period_end : 0 ,
117
- } ) ,
118
- ] ;
119
- }
120
- return _subscriptions . data ;
121
- } , [ _subscriptions , plans , isSignedIn ] ) ;
122
76
123
- return {
124
- data : subscriptions ,
125
- ...rest ,
126
- } ;
77
+ return __experimental_useSubscriptionItems ( {
78
+ for : subscriberType === 'org' ? 'organization' : 'user' ,
79
+ initialPage : 1 ,
80
+ pageSize : 10 ,
81
+ keepPreviousData : true ,
82
+ } ) ;
127
83
} ;
128
84
129
85
export const usePlans = ( ) => {
@@ -167,7 +123,7 @@ export const usePlansContext = () => {
167
123
return false ;
168
124
} , [ clerk , subscriberType ] ) ;
169
125
170
- const { data : subscriptions , mutate : mutateSubscriptions } = useSubscriptions ( ) ;
126
+ const { data : subscriptions , revalidate : revalidateSubscriptions } = useSubscriptions ( ) ;
171
127
172
128
// Invalidates cache but does not fetch immediately
173
129
const { data : plans , mutate : mutatePlans } = useSWR < Awaited < ReturnType < typeof clerk . billing . getPlans > > > ( {
@@ -182,11 +138,11 @@ export const usePlansContext = () => {
182
138
183
139
const revalidateAll = useCallback ( ( ) => {
184
140
// Revalidate the plans and subscriptions
185
- void mutateSubscriptions ( ) ;
141
+ void revalidateSubscriptions ( ) ;
186
142
void mutatePlans ( ) ;
187
143
void revalidateStatements ( ) ;
188
144
void revalidatePaymentSources ( ) ;
189
- } , [ mutateSubscriptions , mutatePlans , revalidateStatements , revalidatePaymentSources ] ) ;
145
+ } , [ revalidateSubscriptions , mutatePlans , revalidateStatements , revalidatePaymentSources ] ) ;
190
146
191
147
// should the default plan be shown as active
192
148
const isDefaultPlanImplicitlyActiveOrUpcoming = useMemo ( ( ) => {
@@ -245,13 +201,6 @@ export const usePlansContext = () => {
245
201
[ activeOrUpcomingSubscription ] ,
246
202
) ;
247
203
248
- // should the default plan be shown as active
249
- const upcomingSubscriptionsExist = useMemo ( ( ) => {
250
- return (
251
- subscriptions . some ( subscription => subscription . status === 'upcoming' ) || isDefaultPlanImplicitlyActiveOrUpcoming
252
- ) ;
253
- } , [ subscriptions , isDefaultPlanImplicitlyActiveOrUpcoming ] ) ;
254
-
255
204
// return the CTA button props for a plan
256
205
const buttonPropsForPlan = useCallback (
257
206
( {
@@ -409,7 +358,6 @@ export const usePlansContext = () => {
409
358
buttonPropsForPlan,
410
359
canManageSubscription,
411
360
captionForSubscription,
412
- upcomingSubscriptionsExist,
413
361
defaultFreePlan,
414
362
revalidateAll,
415
363
} ;
0 commit comments