55 __experimental_useStatements ,
66 __experimental_useSubscription ,
77 useClerk ,
8+ useOrganization ,
89 useSession ,
910} from '@clerk/shared/react' ;
1011import type {
@@ -15,6 +16,7 @@ import type {
1516} from '@clerk/shared/types' ;
1617import { useCallback , useMemo } from 'react' ;
1718
19+ import { useProtect } from '@/ui/common/Gate' ;
1820import { getClosestProfileScrollBox } from '@/ui/utils/getClosestProfileScrollBox' ;
1921
2022import type { LocalizationKey } from '../../localization' ;
@@ -28,44 +30,42 @@ export function normalizeFormatted(formatted: string) {
2830 return formatted . endsWith ( '.00' ) ? formatted . slice ( 0 , - 3 ) : formatted ;
2931}
3032
31- // TODO(@COMMERCE): Rename payment sources to payment methods at the API level
32- export const usePaymentMethods = ( ) => {
33+ const useBillingHookParams = ( ) => {
3334 const subscriberType = useSubscriberTypeContext ( ) ;
34- return __experimental_usePaymentMethods ( {
35+ const { organization } = useOrganization ( ) ;
36+ const allowBillingRoutes = useProtect (
37+ has =>
38+ has ( {
39+ permission : 'org:sys_billing:read' ,
40+ } ) || has ( { permission : 'org:sys_billing:manage' } ) ,
41+ ) ;
42+
43+ return {
3544 for : subscriberType ,
36- initialPage : 1 ,
37- pageSize : 10 ,
3845 keepPreviousData : true ,
39- } ) ;
46+ // 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+ } ;
49+ } ;
50+
51+ export const usePaymentMethods = ( ) => {
52+ const params = useBillingHookParams ( ) ;
53+ return __experimental_usePaymentMethods ( params ) ;
4054} ;
4155
4256export const usePaymentAttempts = ( ) => {
43- const subscriberType = useSubscriberTypeContext ( ) ;
44- return __experimental_usePaymentAttempts ( {
45- for : subscriberType ,
46- initialPage : 1 ,
47- pageSize : 10 ,
48- keepPreviousData : true ,
49- } ) ;
57+ const params = useBillingHookParams ( ) ;
58+ return __experimental_usePaymentAttempts ( params ) ;
5059} ;
5160
52- export const useStatements = ( params ?: { mode : 'cache' } ) => {
53- const subscriberType = useSubscriberTypeContext ( ) ;
54- return __experimental_useStatements ( {
55- for : subscriberType ,
56- initialPage : 1 ,
57- pageSize : 10 ,
58- keepPreviousData : true ,
59- __experimental_mode : params ?. mode ,
60- } ) ;
61+ export const useStatements = ( externalParams ?: { mode : 'cache' } ) => {
62+ const params = useBillingHookParams ( ) ;
63+ return __experimental_useStatements ( { ...params , __experimental_mode : externalParams ?. mode } ) ;
6164} ;
6265
6366export const useSubscription = ( ) => {
64- const subscriberType = useSubscriberTypeContext ( ) ;
65- const subscription = __experimental_useSubscription ( {
66- for : subscriberType ,
67- keepPreviousData : true ,
68- } ) ;
67+ const params = useBillingHookParams ( ) ;
68+ const subscription = __experimental_useSubscription ( params ) ;
6969 const subscriptionItems = useMemo (
7070 ( ) => subscription . data ?. subscriptionItems || [ ] ,
7171 [ subscription . data ?. subscriptionItems ] ,
@@ -85,6 +85,7 @@ export const usePlans = (params?: { mode: 'cache' }) => {
8585 initialPage : 1 ,
8686 pageSize : 50 ,
8787 keepPreviousData : true ,
88+ enabled : true ,
8889 __experimental_mode : params ?. mode ,
8990 } ) ;
9091} ;
0 commit comments