-
Notifications
You must be signed in to change notification settings - Fork 357
feat(clerk-js): Remove dummy default free plan subscription #6109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -35,7 +35,8 @@ export function SubscriptionsList({ | |||||||||||||||||||||
}) { | ||||||||||||||||||||||
const { handleSelectPlan, captionForSubscription, canManageSubscription } = usePlansContext(); | ||||||||||||||||||||||
const subscriberType = useSubscriberTypeContext(); | ||||||||||||||||||||||
const { data: subscriptions } = useSubscriptions(); | ||||||||||||||||||||||
const { data } = useSubscriptions(); | ||||||||||||||||||||||
const { data: subscriptions = [] } = data || {}; | ||||||||||||||||||||||
const canManageBilling = useProtect( | ||||||||||||||||||||||
Comment on lines
+38
to
40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In-place sort mutates SWR cache – clone first Later you call -const sortedSubscriptions = subscriptions.sort((a, b) => {
+const sortedSubscriptions = [...subscriptions].sort((a, b) => { While here, consider the same “double data” shadowing fix suggested for 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||
has => has({ permission: 'org:sys_billing:manage' }) || subscriberType === 'user', | ||||||||||||||||||||||
); | ||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ import type { | |
import { useCallback, useMemo } from 'react'; | ||
import useSWR from 'swr'; | ||
|
||
import { CommerceSubscription } from '../../../core/resources/internal'; | ||
import type { LocalizationKey } from '../../localization'; | ||
import { localizationKeys } from '../../localization'; | ||
import { getClosestProfileScrollBox } from '../../utils'; | ||
|
@@ -80,11 +79,10 @@ export const useStatements = () => { | |
export const useSubscriptions = () => { | ||
const { billing } = useClerk(); | ||
const { organization } = useOrganization(); | ||
const { user, isSignedIn } = useUser(); | ||
const { user } = useUser(); | ||
const subscriberType = useSubscriberTypeContext(); | ||
const { data: plans } = usePlans(); | ||
|
||
const { data: _subscriptions, ...rest } = useSWR( | ||
return useSWR( | ||
{ | ||
key: `commerce-subscriptions`, | ||
userId: user?.id, | ||
|
@@ -93,42 +91,6 @@ export const useSubscriptions = () => { | |
({ args, userId }) => (userId ? billing.getSubscriptions(args) : undefined), | ||
dedupeOptions, | ||
); | ||
|
||
const subscriptions = useMemo(() => { | ||
if (!_subscriptions) { | ||
return []; | ||
} | ||
const defaultFreePlan = plans?.find(plan => plan.hasBaseFee === false && plan.amount === 0); | ||
|
||
// are we signed in, is there a default free plan, and should it be shown as active or upcoming? then add an implicit subscription | ||
if ( | ||
isSignedIn && | ||
defaultFreePlan && | ||
(_subscriptions.data.length === 0 || !_subscriptions.data.some(subscription => !subscription.canceledAt)) | ||
) { | ||
const canceledSubscription = _subscriptions.data.find(subscription => subscription.canceledAt); | ||
return [ | ||
..._subscriptions.data, | ||
new CommerceSubscription({ | ||
object: 'commerce_subscription', | ||
id: '__implicit_default_plan_subscription__', | ||
payment_source_id: '', | ||
plan: defaultFreePlan.__internal_toSnapshot(), | ||
plan_period: 'month', | ||
canceled_at: null, | ||
status: _subscriptions.data.length === 0 ? 'active' : 'upcoming', | ||
period_start: canceledSubscription?.periodEnd || 0, | ||
period_end: 0, | ||
}), | ||
]; | ||
} | ||
return _subscriptions.data; | ||
}, [_subscriptions, plans, isSignedIn]); | ||
|
||
return { | ||
data: subscriptions, | ||
...rest, | ||
}; | ||
}; | ||
|
||
export const usePlans = () => { | ||
|
@@ -172,7 +134,8 @@ export const usePlansContext = () => { | |
return false; | ||
}, [clerk, subscriberType]); | ||
|
||
const { data: subscriptions, mutate: mutateSubscriptions } = useSubscriptions(); | ||
const { data, mutate: mutateSubscriptions } = useSubscriptions(); | ||
const { data: subscriptions = [] } = data || {}; | ||
|
||
Comment on lines
+137
to
139
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
const { data, mutate: mutateSubscriptions } = useSubscriptions();
const { data: subscriptions = [] } = data || {}; Since arrays don’t have a Replace with a simple null-coalesce: -const { data: subscriptions = [] } = data || {};
+const subscriptions = data ?? []; Unit tests around 🤖 Prompt for AI Agents
|
||
// Invalidates cache but does not fetch immediately | ||
const { data: plans, mutate: mutatePlans } = useSWR<Awaited<ReturnType<typeof clerk.billing.getPlans>>>({ | ||
|
@@ -251,13 +214,6 @@ export const usePlansContext = () => { | |
[activeOrUpcomingSubscription], | ||
); | ||
|
||
// should the default plan be shown as active | ||
const upcomingSubscriptionsExist = useMemo(() => { | ||
return ( | ||
subscriptions.some(subscription => subscription.status === 'upcoming') || isDefaultPlanImplicitlyActiveOrUpcoming | ||
); | ||
}, [subscriptions, isDefaultPlanImplicitlyActiveOrUpcoming]); | ||
|
||
// return the CTA button props for a plan | ||
const buttonPropsForPlan = useCallback( | ||
({ | ||
|
@@ -411,7 +367,6 @@ export const usePlansContext = () => { | |
buttonPropsForPlan, | ||
canManageSubscription, | ||
captionForSubscription, | ||
upcomingSubscriptionsExist, | ||
defaultFreePlan, | ||
revalidateAll, | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the pattern we currently use for destructuring paginated SWR responses in PaymentSources, so I used it in these changes – any objections? @panteliselef