Skip to content

Commit e9d2f2f

Browse files
refactor(shared): rename billing hook and update semantics (#7687)
Co-authored-by: Robert Soriano <sorianorobertc@gmail.com>
1 parent ba1918d commit e9d2f2f

File tree

11 files changed

+34
-21
lines changed

11 files changed

+34
-21
lines changed

.changeset/fiery-carrots-call.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/shared': major
3+
---
4+
5+
Rename internal `useBillingHookEnabled` to `useBillingIsEnabled` with improved semantics for authentication and organization context checks.

packages/shared/src/react/billing/__tests__/useInitializePaymentMethod.spec.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ describe('useInitializePaymentMethod', () => {
6666
expect(result.current.initializePaymentMethod).toBeInstanceOf(Function);
6767
});
6868

69-
it('does not fetch when billing disabled for user', () => {
69+
it('does not fetch when billing disabled', () => {
7070
mockClerk.__internal_environment.commerceSettings.billing.user.enabled = false;
71+
mockClerk.__internal_environment.commerceSettings.billing.organization.enabled = false;
7172

7273
const { result } = renderHook(() => useInitializePaymentMethod(), { wrapper });
7374

packages/shared/src/react/billing/useInitializePaymentMethod.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { defineKeepPreviousDataFn } from '../clerk-rq/keep-previous-data';
55
import { useClerkQueryClient } from '../clerk-rq/use-clerk-query-client';
66
import { useClerkQuery } from '../clerk-rq/useQuery';
77
import { useOrganizationContext, useUserContext } from '../contexts';
8-
import { useBillingHookEnabled } from '../hooks/useBillingHookEnabled';
8+
import { useBillingIsEnabled } from '../hooks/useBillingIsEnabled';
99
import { useClearQueriesOnSignOut } from '../hooks/useClearQueriesOnSignOut';
1010

1111
type InitializePaymentMethodOptions = {
@@ -27,7 +27,7 @@ function useInitializePaymentMethod(options?: InitializePaymentMethodOptions): U
2727

2828
const resource = forType === 'organization' ? organization : user;
2929

30-
const billingEnabled = useBillingHookEnabled(options);
30+
const billingEnabled = useBillingIsEnabled(options);
3131

3232
const stableKey = 'billing-payment-method-initialize';
3333
const authenticated = true;

packages/shared/src/react/billing/useStripeLoader.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useMemo } from 'react';
33

44
import { defineKeepPreviousDataFn } from '../clerk-rq/keep-previous-data';
55
import { useClerkQuery } from '../clerk-rq/useQuery';
6-
import { useBillingHookEnabled } from '../hooks/useBillingHookEnabled';
6+
import { useBillingIsEnabled } from '../hooks/useBillingIsEnabled';
77
import type { UseStripeClerkLibsResult } from './useStripeClerkLibs';
88

99
type StripeLoaderOptions = {
@@ -24,7 +24,7 @@ function useStripeLoader(options: StripeLoaderOptions): UseStripeLoaderResult {
2424
return ['stripe-sdk', { externalGatewayId, stripePublishableKey }] as const;
2525
}, [externalGatewayId, stripePublishableKey]);
2626

27-
const billingEnabled = useBillingHookEnabled({ authenticated: true });
27+
const billingEnabled = useBillingIsEnabled({ authenticated: true });
2828

2929
const isEnabled = Boolean(stripeClerkLibs && externalGatewayId && stripePublishableKey) && billingEnabled;
3030

packages/shared/src/react/hooks/__tests__/useSubscription.spec.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ describe('useSubscription', () => {
5757
defaultQueryClient.client.clear();
5858
});
5959

60-
it('does not fetch when billing disabled for user', () => {
60+
it('does not fetch when billing disabled', () => {
6161
mockClerk.__internal_environment.commerceSettings.billing.user.enabled = false;
62+
mockClerk.__internal_environment.commerceSettings.billing.organization.enabled = false;
6263

6364
const { result } = renderHook(() => useSubscription(), { wrapper });
6465

packages/shared/src/react/hooks/createBillingPaginatedHook.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
import type { ResourceCacheStableKey } from '../stable-keys';
1010
import type { PagesOrInfiniteOptions, PaginatedHookConfig, PaginatedResources } from '../types';
1111
import { createCacheKeys } from './createCacheKeys';
12-
import { useBillingHookEnabled } from './useBillingHookEnabled';
12+
import { useBillingIsEnabled } from './useBillingIsEnabled';
1313
import { usePagesOrInfinite, useWithSafeValues } from './usePagesOrInfinite';
1414

1515
/**
@@ -106,7 +106,7 @@ export function createBillingPaginatedHook<TResource extends ClerkResource, TPar
106106

107107
const isForOrganization = safeFor === 'organization';
108108

109-
const billingEnabled = useBillingHookEnabled({
109+
const billingEnabled = useBillingIsEnabled({
110110
for: safeFor,
111111
enabled: externalEnabled,
112112
authenticated: !options?.unauthenticated,

packages/shared/src/react/hooks/useBillingHookEnabled.ts renamed to packages/shared/src/react/hooks/useBillingIsEnabled.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useClerkInstanceContext, useOrganizationContext, useUserContext } from
44
/**
55
* @internal
66
*/
7-
export function useBillingHookEnabled(params?: { for?: ForPayerType; enabled?: boolean; authenticated?: boolean }) {
7+
export function useBillingIsEnabled(params?: { for?: ForPayerType; enabled?: boolean; authenticated?: boolean }) {
88
const clerk = useClerkInstanceContext();
99

1010
const enabledFromParam = params?.enabled ?? true;
@@ -15,11 +15,17 @@ export function useBillingHookEnabled(params?: { for?: ForPayerType; enabled?: b
1515
const user = useUserContext();
1616
const { organization } = useOrganizationContext();
1717

18-
const isOrganization = params?.for === 'organization';
19-
const billingEnabled = isOrganization
20-
? environment?.commerceSettings.billing.organization.enabled
21-
: environment?.commerceSettings.billing.user.enabled;
18+
const userBillingEnabled = environment?.commerceSettings.billing.user.enabled;
19+
const orgBillingEnabled = environment?.commerceSettings.billing.organization.enabled;
20+
21+
const billingEnabled =
22+
params?.for === 'organization'
23+
? orgBillingEnabled
24+
: params?.for === 'user'
25+
? userBillingEnabled
26+
: userBillingEnabled || orgBillingEnabled;
2227

28+
const isOrganization = params?.for === 'organization';
2329
const requireUserAndOrganizationWhenAuthenticated =
2430
(params?.authenticated ?? true) ? (isOrganization ? Boolean(organization?.id) : true) && Boolean(user?.id) : true;
2531

packages/shared/src/react/hooks/usePaymentAttemptQuery.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineKeepPreviousDataFn } from '../clerk-rq/keep-previous-data';
22
import { useClerkQuery } from '../clerk-rq/useQuery';
33
import { useClerkInstanceContext, useOrganizationContext, useUserContext } from '../contexts';
4-
import { useBillingHookEnabled } from './useBillingHookEnabled';
4+
import { useBillingIsEnabled } from './useBillingIsEnabled';
55
import { useClearQueriesOnSignOut } from './useClearQueriesOnSignOut';
66
import { usePaymentAttemptQueryCacheKeys } from './usePaymentAttemptQuery.shared';
77
import type { PaymentAttemptQueryResult, UsePaymentAttemptQueryParams } from './usePaymentAttemptQuery.types';
@@ -25,7 +25,7 @@ function usePaymentAttemptQuery(params: UsePaymentAttemptQueryParams): PaymentAt
2525
for: forType,
2626
});
2727

28-
const billingEnabled = useBillingHookEnabled(params);
28+
const billingEnabled = useBillingIsEnabled(params);
2929

3030
const queryEnabled = Boolean(paymentAttemptId) && billingEnabled;
3131

packages/shared/src/react/hooks/usePlanDetailsQuery.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineKeepPreviousDataFn } from '../clerk-rq/keep-previous-data';
22
import { useClerkQuery } from '../clerk-rq/useQuery';
33
import { useClerkInstanceContext } from '../contexts';
4-
import { useBillingHookEnabled } from './useBillingHookEnabled';
4+
import { useBillingIsEnabled } from './useBillingIsEnabled';
55
import { usePlanDetailsQueryCacheKeys } from './usePlanDetailsQuery.shared';
66
import type { PlanDetailsQueryResult, UsePlanDetailsQueryParams } from './usePlanDetailsQuery.types';
77

@@ -16,7 +16,7 @@ export function __internal_usePlanDetailsQuery(params: UsePlanDetailsQueryParams
1616

1717
const { queryKey } = usePlanDetailsQueryCacheKeys({ planId: targetPlanId });
1818

19-
const billingEnabled = useBillingHookEnabled({
19+
const billingEnabled = useBillingIsEnabled({
2020
authenticated: false,
2121
});
2222

packages/shared/src/react/hooks/useStatementQuery.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineKeepPreviousDataFn } from '../clerk-rq/keep-previous-data';
22
import { useClerkQuery } from '../clerk-rq/useQuery';
33
import { useClerkInstanceContext, useOrganizationContext, useUserContext } from '../contexts';
4-
import { useBillingHookEnabled } from './useBillingHookEnabled';
4+
import { useBillingIsEnabled } from './useBillingIsEnabled';
55
import { useClearQueriesOnSignOut } from './useClearQueriesOnSignOut';
66
import { useStatementQueryCacheKeys } from './useStatementQuery.shared';
77
import type { StatementQueryResult, UseStatementQueryParams } from './useStatementQuery.types';
@@ -25,7 +25,7 @@ function useStatementQuery(params: UseStatementQueryParams = {}): StatementQuery
2525
for: forType,
2626
});
2727

28-
const billingEnabled = useBillingHookEnabled(params);
28+
const billingEnabled = useBillingIsEnabled(params);
2929

3030
const queryEnabled = Boolean(statementId) && billingEnabled;
3131

0 commit comments

Comments
 (0)