Skip to content

Commit eb0bae1

Browse files
committed
feat(shared): Remove SWR
1 parent 395a4ca commit eb0bae1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+688
-1633
lines changed

packages/shared/global.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ declare const JS_PACKAGE_VERSION: string;
44
declare const UI_PACKAGE_VERSION: string;
55
declare const __DEV__: boolean;
66
declare const __BUILD_DISABLE_RHC__: boolean;
7-
declare const __CLERK_USE_RQ__: boolean;
87

98
interface ImportMetaEnv {
109
readonly [key: string]: string;

packages/shared/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,17 @@
124124
"test:coverage": "vitest --collectCoverage && open coverage/lcov-report/index.html"
125125
},
126126
"dependencies": {
127+
"@tanstack/query-core": "5.87.4",
127128
"dequal": "2.0.3",
128129
"glob-to-regexp": "0.4.1",
129130
"js-cookie": "3.0.5",
130-
"std-env": "^3.9.0",
131-
"swr": "2.3.4"
131+
"std-env": "^3.9.0"
132132
},
133133
"devDependencies": {
134134
"@base-org/account": "catalog:module-manager",
135135
"@coinbase/wallet-sdk": "catalog:module-manager",
136136
"@stripe/react-stripe-js": "3.1.1",
137137
"@stripe/stripe-js": "5.6.0",
138-
"@tanstack/query-core": "5.87.4",
139138
"@types/glob-to-regexp": "0.4.4",
140139
"@types/js-cookie": "3.0.6",
141140
"@zxcvbn-ts/core": "catalog:module-manager",

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

Lines changed: 0 additions & 76 deletions
This file was deleted.

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

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,76 @@
1-
export type { UseInitializePaymentMethodResult } from 'virtual:data-hooks/useInitializePaymentMethod';
2-
export { __internal_useInitializePaymentMethod } from 'virtual:data-hooks/useInitializePaymentMethod';
1+
import { useCallback, useMemo } from 'react';
2+
3+
import type { BillingInitializedPaymentMethodResource, ForPayerType } from '../../types';
4+
import { defineKeepPreviousDataFn } from '../clerk-rq/keep-previous-data';
5+
import { useClerkQueryClient } from '../clerk-rq/use-clerk-query-client';
6+
import { useClerkQuery } from '../clerk-rq/useQuery';
7+
import { useOrganizationContext, useUserContext } from '../contexts';
8+
import { useBillingHookEnabled } from '../hooks/useBillingHookEnabled';
9+
10+
type InitializePaymentMethodOptions = {
11+
for?: ForPayerType;
12+
};
13+
14+
export type UseInitializePaymentMethodResult = {
15+
initializedPaymentMethod: BillingInitializedPaymentMethodResource | undefined;
16+
initializePaymentMethod: () => Promise<BillingInitializedPaymentMethodResource | undefined>;
17+
};
18+
19+
/**
20+
* @internal
21+
*/
22+
function useInitializePaymentMethod(options?: InitializePaymentMethodOptions): UseInitializePaymentMethodResult {
23+
const { for: forType } = options ?? {};
24+
const { organization } = useOrganizationContext();
25+
const user = useUserContext();
26+
27+
const resource = forType === 'organization' ? organization : user;
28+
29+
const billingEnabled = useBillingHookEnabled(options);
30+
31+
const queryKey = useMemo(() => {
32+
return ['billing-payment-method-initialize', { resourceId: resource?.id }] as const;
33+
}, [resource?.id]);
34+
35+
const isEnabled = Boolean(resource?.id) && billingEnabled;
36+
37+
const query = useClerkQuery({
38+
queryKey,
39+
queryFn: async () => {
40+
if (!resource) {
41+
return undefined;
42+
}
43+
44+
return resource.initializePaymentMethod({
45+
gateway: 'stripe',
46+
});
47+
},
48+
enabled: isEnabled,
49+
staleTime: 1_000 * 60,
50+
refetchOnWindowFocus: false,
51+
placeholderData: defineKeepPreviousDataFn(true),
52+
});
53+
54+
const [queryClient] = useClerkQueryClient();
55+
56+
const initializePaymentMethod = useCallback(async () => {
57+
if (!resource) {
58+
return undefined;
59+
}
60+
61+
const result = await resource.initializePaymentMethod({
62+
gateway: 'stripe',
63+
});
64+
65+
queryClient.setQueryData(queryKey, result);
66+
67+
return result;
68+
}, [queryClient, queryKey, resource]);
69+
70+
return {
71+
initializedPaymentMethod: query.data ?? undefined,
72+
initializePaymentMethod,
73+
};
74+
}
75+
76+
export { useInitializePaymentMethod as __internal_useInitializePaymentMethod };

packages/shared/src/react/billing/useStripeClerkLibs.rq.tsx

Lines changed: 0 additions & 37 deletions
This file was deleted.

packages/shared/src/react/billing/useStripeClerkLibs.swr.tsx

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,39 @@
1-
export type { UseStripeClerkLibsResult } from 'virtual:data-hooks/useStripeClerkLibs';
2-
export { __internal_useStripeClerkLibs } from 'virtual:data-hooks/useStripeClerkLibs';
1+
import type { loadStripe } from '@stripe/stripe-js';
2+
3+
import { defineKeepPreviousDataFn } from '../clerk-rq/keep-previous-data';
4+
import { useClerkQuery } from '../clerk-rq/useQuery';
5+
import { useBillingHookEnabled } from '../hooks/useBillingHookEnabled';
6+
import { useClerk } from '../hooks/useClerk';
7+
8+
type LoadStripeFn = typeof loadStripe;
9+
10+
type StripeClerkLibs = {
11+
loadStripe: LoadStripeFn;
12+
};
13+
14+
export type UseStripeClerkLibsResult = StripeClerkLibs | null;
15+
16+
/**
17+
* @internal
18+
*/
19+
function useStripeClerkLibs(): UseStripeClerkLibsResult {
20+
const clerk = useClerk();
21+
22+
const billingEnabled = useBillingHookEnabled();
23+
24+
const query = useClerkQuery({
25+
queryKey: ['clerk-stripe-sdk'],
26+
queryFn: async () => {
27+
const loadStripe = (await clerk.__internal_loadStripeJs()) as LoadStripeFn;
28+
return { loadStripe };
29+
},
30+
enabled: billingEnabled,
31+
staleTime: Infinity,
32+
refetchOnWindowFocus: false,
33+
placeholderData: defineKeepPreviousDataFn(true),
34+
});
35+
36+
return query.data ?? null;
37+
}
38+
39+
export { useStripeClerkLibs as __internal_useStripeClerkLibs };

0 commit comments

Comments
 (0)