Avoid make stripe API calls on development. Update stripe.tsx (#5743)

This commit is contained in:
Dimitri POSTOLOV 2024-10-27 13:30:55 +03:00 committed by GitHub
parent 315bba4033
commit 9e81779cfa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,21 +1,22 @@
import { ReactElement, ReactNode, Suspense, useRef } from 'react';
import { FC, ReactNode, Suspense, useState } from 'react';
import { env } from '@/env/frontend';
import { Elements as ElementsProvider } from '@stripe/react-stripe-js';
import { loadStripe } from '@stripe/stripe-js';
import { getStripePublicKey } from './stripe-public-key';
export const HiveStripeWrapper = ({ children }: { children: ReactNode }): ReactElement => {
const stripeRef = useRef<ReturnType<typeof loadStripe> | null>(null);
if (!stripeRef.current) {
export const HiveStripeWrapper: FC<{ children: ReactNode }> = ({ children }) => {
// eslint-disable-next-line react/hook-use-state -- we don't need setter
const [stripe] = useState<ReturnType<typeof loadStripe> | void>(() => {
if (env.nodeEnv !== 'production') {
return;
}
const stripePublicKey = getStripePublicKey();
if (stripePublicKey) {
stripeRef.current = loadStripe(stripePublicKey);
return loadStripe(stripePublicKey);
}
}
});
const stripe = stripeRef.current;
if (stripe === null) {
if (!stripe) {
return children as any;
}