Skip to content

Commit 7f341dc

Browse files
authored
feat(clerk-js): Remove dummy default free plan subscription (#6109)
1 parent f5f8ddd commit 7f341dc

File tree

2 files changed

+13
-62
lines changed

2 files changed

+13
-62
lines changed

.changeset/lemon-walls-brush.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---

packages/clerk-js/src/ui/contexts/components/Plans.tsx

Lines changed: 10 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
__experimental_usePaymentAttempts,
33
__experimental_usePaymentMethods,
44
__experimental_useStatements,
5+
__experimental_useSubscriptionItems,
56
useClerk,
67
useOrganization,
78
useSession,
@@ -16,7 +17,6 @@ import type {
1617
import { useCallback, useMemo } from 'react';
1718
import useSWR from 'swr';
1819

19-
import { CommerceSubscription } from '@/core/resources/CommerceSubscription';
2020
import { getClosestProfileScrollBox } from '@/ui/utils/getClosestProfileScrollBox';
2121

2222
import type { LocalizationKey } from '../../localization';
@@ -72,58 +72,14 @@ export const useStatements = (params?: { mode: 'cache' }) => {
7272
};
7373

7474
export const useSubscriptions = () => {
75-
const { billing } = useClerk();
76-
const { organization } = useOrganization();
77-
const { user, isSignedIn } = useUser();
7875
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]);
12276

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+
});
12783
};
12884

12985
export const usePlans = () => {
@@ -167,7 +123,7 @@ export const usePlansContext = () => {
167123
return false;
168124
}, [clerk, subscriberType]);
169125

170-
const { data: subscriptions, mutate: mutateSubscriptions } = useSubscriptions();
126+
const { data: subscriptions, revalidate: revalidateSubscriptions } = useSubscriptions();
171127

172128
// Invalidates cache but does not fetch immediately
173129
const { data: plans, mutate: mutatePlans } = useSWR<Awaited<ReturnType<typeof clerk.billing.getPlans>>>({
@@ -182,11 +138,11 @@ export const usePlansContext = () => {
182138

183139
const revalidateAll = useCallback(() => {
184140
// Revalidate the plans and subscriptions
185-
void mutateSubscriptions();
141+
void revalidateSubscriptions();
186142
void mutatePlans();
187143
void revalidateStatements();
188144
void revalidatePaymentSources();
189-
}, [mutateSubscriptions, mutatePlans, revalidateStatements, revalidatePaymentSources]);
145+
}, [revalidateSubscriptions, mutatePlans, revalidateStatements, revalidatePaymentSources]);
190146

191147
// should the default plan be shown as active
192148
const isDefaultPlanImplicitlyActiveOrUpcoming = useMemo(() => {
@@ -245,13 +201,6 @@ export const usePlansContext = () => {
245201
[activeOrUpcomingSubscription],
246202
);
247203

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-
255204
// return the CTA button props for a plan
256205
const buttonPropsForPlan = useCallback(
257206
({
@@ -409,7 +358,6 @@ export const usePlansContext = () => {
409358
buttonPropsForPlan,
410359
canManageSubscription,
411360
captionForSubscription,
412-
upcomingSubscriptionsExist,
413361
defaultFreePlan,
414362
revalidateAll,
415363
};

0 commit comments

Comments
 (0)