Skip to content

Commit 5e5e24a

Browse files
authored
chore(clerk-js): Replace useOrganization with useOrganizationContext within billing components (#7257)
1 parent edbd18c commit 5e5e24a

File tree

7 files changed

+35
-23
lines changed

7 files changed

+35
-23
lines changed

.changeset/cuddly-cougars-buy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
Internal change, not user-facing: Replace `useOrganization` with `useOrganizationContext` in billing components

packages/clerk-js/src/ui/components/PaymentAttempts/PaymentAttemptPage.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useClerk, useOrganization } from '@clerk/shared/react';
1+
import { useClerk, useOrganizationContext } from '@clerk/shared/react';
22
import type { BillingSubscriptionItemResource } from '@clerk/shared/types';
33
import useSWR from 'swr';
44

@@ -29,10 +29,11 @@ import { useRouter } from '../../router';
2929
export const PaymentAttemptPage = () => {
3030
const { params, navigate } = useRouter();
3131
const subscriberType = useSubscriberTypeContext();
32-
const { organization } = useOrganization();
3332
const localizationRoot = useSubscriberTypeLocalizationRoot();
3433
const { t, translateError } = useLocalizations();
3534
const clerk = useClerk();
35+
// Do not use `useOrganization` to avoid triggering the in-app enable organizations prompt in development instance
36+
const organizationCtx = useOrganizationContext();
3637

3738
const {
3839
data: paymentAttempt,
@@ -43,13 +44,13 @@ export const PaymentAttemptPage = () => {
4344
? {
4445
type: 'payment-attempt',
4546
id: params.paymentAttemptId,
46-
orgId: subscriberType === 'organization' ? organization?.id : undefined,
47+
orgId: subscriberType === 'organization' ? organizationCtx?.organization?.id : undefined,
4748
}
4849
: null,
4950
() =>
5051
clerk.billing.getPaymentAttempt({
5152
id: params.paymentAttemptId,
52-
orgId: subscriberType === 'organization' ? organization?.id : undefined,
53+
orgId: subscriberType === 'organization' ? organizationCtx?.organization?.id : undefined,
5354
}),
5455
);
5556

packages/clerk-js/src/ui/components/PaymentMethods/PaymentMethods.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useClerk, useOrganization } from '@clerk/shared/react';
1+
import { useClerk, useOrganizationContext } from '@clerk/shared/react';
22
import type { BillingPaymentMethodResource } from '@clerk/shared/types';
33
import { Fragment, useMemo, useRef } from 'react';
44

@@ -60,19 +60,20 @@ const RemoveScreen = ({
6060
const { close } = useActionContext();
6161
const card = useCardState();
6262
const subscriberType = useSubscriberTypeContext();
63-
const { organization } = useOrganization();
6463
const localizationRoot = useSubscriberTypeLocalizationRoot();
6564
const ref = useRef(
6665
`${paymentMethod.paymentType === 'card' ? paymentMethod.cardType : paymentMethod.paymentType} ${paymentMethod.paymentType === 'card' ? `⋯ ${paymentMethod.last4}` : '-'}`,
6766
);
67+
// Do not use `useOrganization` to avoid triggering the in-app enable organizations prompt in development instance
68+
const organizationCtx = useOrganizationContext();
6869

6970
if (!ref.current) {
7071
return null;
7172
}
7273

7374
const removePaymentMethod = async () => {
7475
await paymentMethod
75-
.remove({ orgId: subscriberType === 'organization' ? organization?.id : undefined })
76+
.remove({ orgId: subscriberType === 'organization' ? organizationCtx?.organization?.id : undefined })
7677
.then(revalidate)
7778
.catch((error: Error) => {
7879
handleError(error, [], card.setError);
@@ -196,9 +197,10 @@ const PaymentMethodMenu = ({
196197
}) => {
197198
const { open } = useActionContext();
198199
const card = useCardState();
199-
const { organization } = useOrganization();
200200
const subscriberType = useSubscriberTypeContext();
201201
const localizationRoot = useSubscriberTypeLocalizationRoot();
202+
// Do not use `useOrganization` to avoid triggering the in-app enable organizations prompt in development instance
203+
const organizationCtx = useOrganizationContext();
202204

203205
const actions = [
204206
{
@@ -215,7 +217,7 @@ const PaymentMethodMenu = ({
215217
isDestructive: false,
216218
onClick: () => {
217219
paymentMethod
218-
.makeDefault({ orgId: subscriberType === 'organization' ? organization?.id : undefined })
220+
.makeDefault({ orgId: subscriberType === 'organization' ? organizationCtx?.organization?.id : undefined })
219221
.then(revalidate)
220222
.catch((error: Error) => {
221223
handleError(error, [], card.setError);

packages/clerk-js/src/ui/components/PricingTable/PricingTableDefault.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useClerk, useOrganization, useSession } from '@clerk/shared/react';
1+
import { useClerk, useOrganizationContext, useSession } from '@clerk/shared/react';
22
import type { BillingPlanResource, BillingSubscriptionPlanPeriod, PricingTableProps } from '@clerk/shared/types';
33
import * as React from 'react';
44

@@ -104,7 +104,8 @@ function Card(props: CardProps) {
104104
const { isSignedIn } = useSession();
105105
const { mode = 'mounted', ctaPosition: ctxCtaPosition } = usePricingTableContext();
106106
const subscriberType = useSubscriberTypeContext();
107-
const { organization } = useOrganization();
107+
// Do not use `useOrganization` to avoid triggering the in-app enable organizations prompt in development instance
108+
const organizationCtx = useOrganizationContext();
108109

109110
const ctaPosition = pricingTableProps.ctaPosition || ctxCtaPosition || 'bottom';
110111
const collapseFeatures = pricingTableProps.collapseFeatures || false;
@@ -137,7 +138,7 @@ function Card(props: CardProps) {
137138
plan,
138139
planPeriod,
139140
for: pricingTableProps.for,
140-
hasActiveOrganization: !!organization,
141+
hasActiveOrganization: !!organizationCtx?.organization,
141142
});
142143

143144
return (

packages/clerk-js/src/ui/components/Statements/StatementPage.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useClerk, useOrganization } from '@clerk/shared/react';
1+
import { useClerk, useOrganizationContext } from '@clerk/shared/react';
22
import useSWR from 'swr';
33

44
import { Alert } from '@/ui/elements/Alert';
@@ -23,10 +23,11 @@ import { Statement } from './Statement';
2323
export const StatementPage = () => {
2424
const { params, navigate } = useRouter();
2525
const subscriberType = useSubscriberTypeContext();
26-
const { organization } = useOrganization();
2726
const localizationRoot = useSubscriberTypeLocalizationRoot();
2827
const { t, translateError } = useLocalizations();
2928
const clerk = useClerk();
29+
// Do not use `useOrganization` to avoid triggering the in-app enable organizations prompt in development instance
30+
const organizationCtx = useOrganizationContext();
3031

3132
const {
3233
data: statement,
@@ -37,13 +38,13 @@ export const StatementPage = () => {
3738
? {
3839
type: 'statement',
3940
id: params.statementId,
40-
orgId: subscriberType === 'organization' ? organization?.id : undefined,
41+
orgId: subscriberType === 'organization' ? organizationCtx?.organization?.id : undefined,
4142
}
4243
: null,
4344
() =>
4445
clerk.billing.getStatement({
4546
id: params.statementId,
46-
orgId: subscriberType === 'organization' ? organization?.id : undefined,
47+
orgId: subscriberType === 'organization' ? organizationCtx?.organization?.id : undefined,
4748
}),
4849
);
4950

packages/clerk-js/src/ui/components/SubscriptionDetails/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useClerk, useOrganization } from '@clerk/shared/react';
1+
import { useClerk, useOrganizationContext } from '@clerk/shared/react';
22
import type {
33
__internal_CheckoutProps,
44
__internal_SubscriptionDetailsProps,
@@ -173,7 +173,6 @@ const SubscriptionDetailsInternal = (props: __internal_SubscriptionDetailsProps)
173173

174174
const SubscriptionDetailsFooter = withCardStateProvider(() => {
175175
const subscriberType = useSubscriberTypeContext();
176-
const { organization } = useOrganization();
177176
const { isLoading, error, setError, setLoading, setIdle } = useCardState();
178177
const {
179178
subscription: selectedSubscription,
@@ -183,6 +182,8 @@ const SubscriptionDetailsFooter = withCardStateProvider(() => {
183182
const { data: subscription } = useSubscription();
184183
const { setIsOpen } = useDrawerContext();
185184
const { onSubscriptionCancel } = useSubscriptionDetailsContext();
185+
// Do not use `useOrganization` to avoid triggering the in-app enable organizations prompt in development instance
186+
const organizationCtx = useOrganizationContext();
186187

187188
const onOpenChange = useCallback((open: boolean) => setConfirmationOpen(open), [setConfirmationOpen]);
188189

@@ -195,7 +196,7 @@ const SubscriptionDetailsFooter = withCardStateProvider(() => {
195196
setLoading();
196197

197198
await selectedSubscription
198-
.cancel({ orgId: subscriberType === 'organization' ? organization?.id : undefined })
199+
.cancel({ orgId: subscriberType === 'organization' ? organizationCtx?.organization?.id : undefined })
199200
.then(() => {
200201
onSubscriptionCancel?.();
201202
if (setIsOpen) {
@@ -213,7 +214,7 @@ const SubscriptionDetailsFooter = withCardStateProvider(() => {
213214
setError,
214215
setLoading,
215216
subscriberType,
216-
organization?.id,
217+
organizationCtx?.organization?.id,
217218
onSubscriptionCancel,
218219
setIsOpen,
219220
setIdle,

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
__experimental_useStatements,
66
__experimental_useSubscription,
77
useClerk,
8-
useOrganization,
8+
useOrganizationContext,
99
useSession,
1010
} from '@clerk/shared/react';
1111
import type {
@@ -32,19 +32,20 @@ export function normalizeFormatted(formatted: string) {
3232

3333
const useBillingHookParams = () => {
3434
const subscriberType = useSubscriberTypeContext();
35-
const { organization } = useOrganization();
3635
const allowBillingRoutes = useProtect(
3736
has =>
3837
has({
3938
permission: 'org:sys_billing:read',
4039
}) || has({ permission: 'org:sys_billing:manage' }),
4140
);
41+
// Do not use `useOrganization` to avoid triggering the in-app enable organizations prompt in development instance
42+
const organizationCtx = useOrganizationContext();
4243

4344
return {
4445
for: subscriberType,
4546
keepPreviousData: true,
4647
// If the user is in an organization, only fetch billing data if they have the necessary permissions
47-
enabled: subscriberType === 'organization' ? Boolean(organization) && allowBillingRoutes : true,
48+
enabled: subscriberType === 'organization' ? Boolean(organizationCtx?.organization) && allowBillingRoutes : true,
4849
};
4950
};
5051

0 commit comments

Comments
 (0)