Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 470
Revert "feat(shared): Remove SWR (#7455)"#7565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { useCallback, useMemo } from 'react'; | ||
| import type { BillingInitializedPaymentMethodResource, ForPayerType } from '../../types'; | ||
| import { defineKeepPreviousDataFn } from '../clerk-rq/keep-previous-data'; | ||
| import { useClerkQueryClient } from '../clerk-rq/use-clerk-query-client'; | ||
| import { useClerkQuery } from '../clerk-rq/useQuery'; | ||
| import { useOrganizationContext, useUserContext } from '../contexts'; | ||
| import { useBillingHookEnabled } from '../hooks/useBillingHookEnabled'; | ||
| type InitializePaymentMethodOptions = { | ||
| for?: ForPayerType; | ||
| }; | ||
| export type UseInitializePaymentMethodResult = { | ||
| initializedPaymentMethod: BillingInitializedPaymentMethodResource | undefined; | ||
| initializePaymentMethod: () => Promise<BillingInitializedPaymentMethodResource | undefined>; | ||
| }; | ||
| /** | ||
| * @internal | ||
| */ | ||
| function useInitializePaymentMethod(options?: InitializePaymentMethodOptions): UseInitializePaymentMethodResult { | ||
| const { for: forType } = options ?? {}; | ||
| const { organization } = useOrganizationContext(); | ||
| const user = useUserContext(); | ||
| const resource = forType === 'organization' ? organization : user; | ||
| const billingEnabled = useBillingHookEnabled(options); | ||
| const queryKey = useMemo(() => { | ||
| return ['billing-payment-method-initialize', { resourceId: resource?.id }] as const; | ||
| }, [resource?.id]); | ||
| const isEnabled = Boolean(resource?.id) && billingEnabled; | ||
| const query = useClerkQuery({ | ||
| queryKey, | ||
| queryFn: async () => { | ||
| if (!resource) { | ||
| return undefined; | ||
| } | ||
| return resource.initializePaymentMethod({ | ||
| gateway: 'stripe', | ||
| }); | ||
| }, | ||
| enabled: isEnabled, | ||
| staleTime: 1_000 * 60, | ||
| refetchOnWindowFocus: false, | ||
| placeholderData: defineKeepPreviousDataFn(true), | ||
| }); | ||
| const [queryClient] = useClerkQueryClient(); | ||
| const initializePaymentMethod = useCallback(async () => { | ||
| if (!resource) { | ||
| return undefined; | ||
| } | ||
| const result = await resource.initializePaymentMethod({ | ||
| gateway: 'stripe', | ||
| }); | ||
| queryClient.setQueryData(queryKey, result); | ||
| return result; | ||
| }, [queryClient, queryKey, resource]); | ||
| return { | ||
| initializedPaymentMethod: query.data ?? undefined, | ||
| initializePaymentMethod, | ||
| }; | ||
| } | ||
| export { useInitializePaymentMethod as __internal_useInitializePaymentMethod }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { useEffect } from 'react'; | ||
| import useSWRMutation from 'swr/mutation'; | ||
| import type { BillingInitializedPaymentMethodResource, ForPayerType } from '../../types'; | ||
| import { useOrganizationContext, useUserContext } from '../contexts'; | ||
| type InitializePaymentMethodOptions = { | ||
| for?: ForPayerType; | ||
| }; | ||
| export type UseInitializePaymentMethodResult = { | ||
| initializedPaymentMethod: BillingInitializedPaymentMethodResource | undefined; | ||
| initializePaymentMethod: () => Promise<BillingInitializedPaymentMethodResource | undefined>; | ||
| }; | ||
| /** | ||
| * This is the existing implementation of the payment method initializer using SWR. | ||
| * It is kept here for backwards compatibility until our next major version. | ||
| * | ||
| * @internal | ||
| */ | ||
| function useInitializePaymentMethod(options?: InitializePaymentMethodOptions): UseInitializePaymentMethodResult { | ||
| const { for: forType = 'user' } = options ?? {}; | ||
| const { organization } = useOrganizationContext(); | ||
| const user = useUserContext(); | ||
| const resource = forType === 'organization' ? organization : user; | ||
| const { data, trigger } = useSWRMutation( | ||
| resource?.id | ||
| ? { | ||
| key: 'billing-payment-method-initialize', | ||
| resourceId: resource.id, | ||
| for: forType, | ||
| } | ||
| : null, | ||
| () => { | ||
| return resource?.initializePaymentMethod({ | ||
| gateway: 'stripe', | ||
| }); | ||
| }, | ||
| ); | ||
| useEffect(() => { | ||
| if (!resource?.id) { | ||
| return; | ||
| } | ||
| trigger().catch(() => { | ||
| // ignore errors | ||
| }); | ||
| }, [resource?.id, trigger]); | ||
| return { | ||
| initializedPaymentMethod: data, | ||
| initializePaymentMethod: trigger, | ||
| }; | ||
| } | ||
| export { useInitializePaymentMethod as __internal_useInitializePaymentMethod }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,76 +1,2 @@ | ||
| import { useCallback, useMemo } from 'react'; | ||
| import type { BillingInitializedPaymentMethodResource, ForPayerType } from '../../types'; | ||
| import { defineKeepPreviousDataFn } from '../clerk-rq/keep-previous-data'; | ||
| import { useClerkQueryClient } from '../clerk-rq/use-clerk-query-client'; | ||
| import { useClerkQuery } from '../clerk-rq/useQuery'; | ||
| import { useOrganizationContext, useUserContext } from '../contexts'; | ||
| import { useBillingHookEnabled } from '../hooks/useBillingHookEnabled'; | ||
| type InitializePaymentMethodOptions = { | ||
| for?: ForPayerType; | ||
| }; | ||
| export type UseInitializePaymentMethodResult = { | ||
| initializedPaymentMethod: BillingInitializedPaymentMethodResource | undefined; | ||
| initializePaymentMethod: () => Promise<BillingInitializedPaymentMethodResource | undefined>; | ||
| }; | ||
| /** | ||
| * @internal | ||
| */ | ||
| function useInitializePaymentMethod(options?: InitializePaymentMethodOptions): UseInitializePaymentMethodResult { | ||
| const { for: forType } = options ?? {}; | ||
| const { organization } = useOrganizationContext(); | ||
| const user = useUserContext(); | ||
| const resource = forType === 'organization' ? organization : user; | ||
| const billingEnabled = useBillingHookEnabled(options); | ||
| const queryKey = useMemo(() => { | ||
| return ['billing-payment-method-initialize', { resourceId: resource?.id }] as const; | ||
| }, [resource?.id]); | ||
| const isEnabled = Boolean(resource?.id) && billingEnabled; | ||
| const query = useClerkQuery({ | ||
| queryKey, | ||
| queryFn: async () => { | ||
| if (!resource) { | ||
| return undefined; | ||
| } | ||
| return resource.initializePaymentMethod({ | ||
| gateway: 'stripe', | ||
| }); | ||
| }, | ||
| enabled: isEnabled, | ||
| staleTime: 1_000 * 60, | ||
| refetchOnWindowFocus: false, | ||
| placeholderData: defineKeepPreviousDataFn(true), | ||
| }); | ||
| const [queryClient] = useClerkQueryClient(); | ||
| const initializePaymentMethod = useCallback(async () => { | ||
| if (!resource) { | ||
| return undefined; | ||
| } | ||
| const result = await resource.initializePaymentMethod({ | ||
| gateway: 'stripe', | ||
| }); | ||
| queryClient.setQueryData(queryKey, result); | ||
| return result; | ||
| }, [queryClient, queryKey, resource]); | ||
| return { | ||
| initializedPaymentMethod: query.data ?? undefined, | ||
| initializePaymentMethod, | ||
| }; | ||
| } | ||
| export { useInitializePaymentMethod as __internal_useInitializePaymentMethod }; | ||
| export type { UseInitializePaymentMethodResult } from 'virtual:data-hooks/useInitializePaymentMethod'; | ||
| export { __internal_useInitializePaymentMethod } from 'virtual:data-hooks/useInitializePaymentMethod'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import type { loadStripe } from '@stripe/stripe-js'; | ||
| import { defineKeepPreviousDataFn } from '../clerk-rq/keep-previous-data'; | ||
| import { useClerkQuery } from '../clerk-rq/useQuery'; | ||
| import { useBillingHookEnabled } from '../hooks/useBillingHookEnabled'; | ||
| import { useClerk } from '../hooks/useClerk'; | ||
| type LoadStripeFn = typeof loadStripe; | ||
| type StripeClerkLibs = { | ||
| loadStripe: LoadStripeFn; | ||
| }; | ||
| /** | ||
| * @internal | ||
| */ | ||
| function useStripeClerkLibs(): StripeClerkLibs | null { | ||
| const clerk = useClerk(); | ||
| const billingEnabled = useBillingHookEnabled(); | ||
| const query = useClerkQuery({ | ||
| queryKey: ['clerk-stripe-sdk'], | ||
| queryFn: async () => { | ||
| const loadStripe = (await clerk.__internal_loadStripeJs()) as LoadStripeFn; | ||
| return { loadStripe }; | ||
| }, | ||
| enabled: billingEnabled, | ||
| staleTime: Infinity, | ||
| refetchOnWindowFocus: false, | ||
| placeholderData: defineKeepPreviousDataFn(true), | ||
| }); | ||
| return query.data ?? null; | ||
| } | ||
| export { useStripeClerkLibs as __internal_useStripeClerkLibs }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import type { loadStripe } from '@stripe/stripe-js'; | ||
| import { useSWR } from '../clerk-swr'; | ||
| import { useClerk } from '../hooks/useClerk'; | ||
| type LoadStripeFn = typeof loadStripe; | ||
| type StripeClerkLibs = { | ||
| loadStripe: LoadStripeFn; | ||
| }; | ||
| export type UseStripeClerkLibsResult = StripeClerkLibs | null; | ||
| /** | ||
| * This is the existing implementation of the Stripe libraries loader using SWR. | ||
| * It is kept here for backwards compatibility until our next major version. | ||
| * | ||
| * @internal | ||
| */ | ||
| function useStripeClerkLibs(): UseStripeClerkLibsResult { | ||
| const clerk = useClerk(); | ||
| const swr = useSWR( | ||
| 'clerk-stripe-sdk', | ||
| async () => { | ||
| const loadStripe = (await clerk.__internal_loadStripeJs()) as LoadStripeFn; | ||
| return { loadStripe }; | ||
| }, | ||
| { | ||
| keepPreviousData: true, | ||
| revalidateOnFocus: false, | ||
| dedupingInterval: Infinity, | ||
| }, | ||
| ); | ||
| return swr.data ?? null; | ||
| } | ||
| export { useStripeClerkLibs as __internal_useStripeClerkLibs }; | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Behavioral inconsistency: Missing
billingEnabledguard.The React Query implementation (
useStripeClerkLibs.rq.tsx) usesuseBillingHookEnabled()to conditionally enable the query, but this SWR implementation fetches unconditionally. This means:This behavioral difference could cause unexpected network requests or errors when billing is disabled.
Proposed fix to align behavior
import type { loadStripe } from '@stripe/stripe-js'; import { useSWR } from '../clerk-swr'; import { useClerk } from '../hooks/useClerk'; +import { useBillingHookEnabled } from './useBillingHookEnabled'; // ... types ... function useStripeClerkLibs(): UseStripeClerkLibsResult { const clerk = useClerk(); + const billingEnabled = useBillingHookEnabled(); const swr = useSWR( - 'clerk-stripe-sdk',+ billingEnabled ? 'clerk-stripe-sdk' : null, async () => { const loadStripe = (await clerk.__internal_loadStripeJs()) as LoadStripeFn; return { loadStripe }; }, { keepPreviousData: true, revalidateOnFocus: false, dedupingInterval: Infinity, }, ); return swr.data ?? null; }🤖 Prompt for AI Agents