Skip to content

Commit a3ad75c

Browse files
committed
wip
1 parent a28fa4d commit a3ad75c

File tree

4 files changed

+55
-34
lines changed

4 files changed

+55
-34
lines changed

packages/clerk-js/src/ui/contexts/CoreClerkContextWrapper.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ export function CoreClerkContextWrapper(props: CoreClerkContextWrapperProps): JS
5252
<ClientContext.Provider value={clientCtx}>
5353
<SessionContext.Provider value={sessionCtx}>
5454
<OrganizationProvider
55-
// @ts-expect-error - __internal_queryClient is not typed
56-
queryClient={clerk.__internal_queryClient}
55+
// // @ts-expect-error - __internal_queryClient is not typed
56+
// queryClient={clerk.__internal_queryClient}
5757
{...organizationCtx.value}
5858
swrConfig={props.swrConfig}
5959
>

packages/react/src/contexts/ClerkContextProvider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export function ClerkContextProvider(props: ClerkContextProvider) {
103103
return clerk.__internal_queryClient;
104104
}, [queryStatus, clerkStatus]);
105105

106-
console.log('queryStatus', queryStatus, queryClient);
106+
// console.log('queryStatus', queryStatus, queryClient);
107107

108108
return (
109109
// @ts-expect-error value passed is of type IsomorphicClerk where the context expects LoadedClerk
@@ -113,7 +113,7 @@ export function ClerkContextProvider(props: ClerkContextProvider) {
113113
<OrganizationProvider
114114
key={clerkStatus + queryStatus}
115115
{...organizationCtx.value}
116-
queryClient={queryClient}
116+
// queryClient={queryClient}
117117
>
118118
<AuthContext.Provider value={authCtx}>
119119
<UserContext.Provider value={userCtx}>

packages/shared/src/react/contexts.tsx

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import type {
1010
SignedInSessionResource,
1111
UserResource,
1212
} from '@clerk/types';
13-
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
1413
import type { PropsWithChildren } from 'react';
1514
import React from 'react';
1615

@@ -60,27 +59,24 @@ const OrganizationProvider = ({
6059
children,
6160
organization,
6261
swrConfig,
63-
queryClient,
62+
// queryClient,
6463
}: PropsWithChildren<
6564
OrganizationContextProps & {
6665
// Exporting inferred types directly from SWR will result in error while building declarations
6766
swrConfig?: any;
68-
queryClient?: QueryClient;
67+
// queryClient?: QueryClient;
6968
}
7069
>) => {
71-
const [defaultClient] = React.useState(() => new QueryClient());
7270
return (
73-
<QueryClientProvider client={queryClient || defaultClient}>
74-
<SWRConfig value={swrConfig}>
75-
<OrganizationContextInternal.Provider
76-
value={{
77-
value: { organization },
78-
}}
79-
>
80-
{children}
81-
</OrganizationContextInternal.Provider>
82-
</SWRConfig>
83-
</QueryClientProvider>
71+
<SWRConfig value={swrConfig}>
72+
<OrganizationContextInternal.Provider
73+
value={{
74+
value: { organization },
75+
}}
76+
>
77+
{children}
78+
</OrganizationContextInternal.Provider>
79+
</SWRConfig>
8480
);
8581
};
8682

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

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { EnvironmentResource, ForPayerType } from '@clerk/types';
2-
import { useQuery, useQueryClient } from '@tanstack/react-query';
3-
import { useCallback, useMemo } from 'react';
2+
import { useQuery } from '@tanstack/react-query';
3+
import { useCallback, useEffect, useMemo, useState } from 'react';
44

55
import { eventMethodCalled } from '../../telemetry/events';
66
import { useSWR } from '../clerk-swr';
@@ -80,6 +80,26 @@ export const useSubscriptionPrev = (params?: UseSubscriptionParams) => {
8080
};
8181
};
8282

83+
const useClerkQueryClient = () => {
84+
const clerk = useClerkInstanceContext();
85+
const [queryStatus, setQueryStatus] = useState('loading');
86+
useEffect(() => {
87+
// @ts-expect-error - queryClientStatus is not typed
88+
clerk.on('queryClientStatus', setQueryStatus);
89+
return () => {
90+
// @ts-expect-error - queryClientStatus is not typed
91+
clerk.off('queryClientStatus', setQueryStatus);
92+
};
93+
}, [clerk]);
94+
95+
const queryClient = useMemo(() => {
96+
// @ts-expect-error - __internal_queryClient is not typed
97+
return clerk.__internal_queryClient;
98+
}, [queryStatus, clerk.status]);
99+
100+
return queryClient;
101+
};
102+
83103
export const useSubscription = (params?: UseSubscriptionParams) => {
84104
useAssertWrappedByClerkProvider(hookName);
85105

@@ -99,7 +119,9 @@ export const useSubscription = (params?: UseSubscriptionParams) => {
99119
? environment?.commerceSettings.billing.organization.enabled
100120
: environment?.commerceSettings.billing.user.enabled;
101121

102-
const queryClient = useQueryClient();
122+
const queryClient = useClerkQueryClient();
123+
// console.log('useInfiniteQuery', useInfiniteQuery);
124+
// console.log('useInfiniteQuery', useMutation);
103125

104126
const queryKey = useMemo(() => {
105127
return [
@@ -111,20 +133,23 @@ export const useSubscription = (params?: UseSubscriptionParams) => {
111133
];
112134
}, [user?.id, isOrganization, organization?.id]);
113135

114-
const query = useQuery({
115-
queryKey,
116-
queryFn: ({ queryKey }) => {
117-
const obj = queryKey[1] as {
118-
args: {
119-
orgId?: string;
136+
const query = useQuery(
137+
{
138+
queryKey,
139+
queryFn: ({ queryKey }) => {
140+
const obj = queryKey[1] as {
141+
args: {
142+
orgId?: string;
143+
};
120144
};
121-
};
122-
return clerk.billing.getSubscription(obj.args);
145+
return clerk.billing.getSubscription(obj.args);
146+
},
147+
staleTime: 1_0000 * 60,
148+
enabled: Boolean(user?.id && billingEnabled) && clerk.status === 'ready',
149+
// placeholderData
123150
},
124-
staleTime: 1_0000 * 60,
125-
enabled: Boolean(user?.id && billingEnabled) && clerk.status === 'ready',
126-
// placeholderData
127-
});
151+
queryClient,
152+
);
128153

129154
const revalidate = useCallback(() => queryClient.invalidateQueries({ queryKey }), [queryClient, queryKey]);
130155

0 commit comments

Comments
 (0)