Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

perf: load hcaptcha on demand (#21745)

Kevin Grünebergf16810af06fd
diff --git a/apps/studio/components/interfaces/Billing/Payment/AddNewPaymentMethodModal.tsx b/apps/studio/components/interfaces/Billing/Payment/AddNewPaymentMethodModal.tsx
index 0041cbb6207..f822b55b0b5 100644
--- a/apps/studio/components/interfaces/Billing/Payment/AddNewPaymentMethodModal.tsx
+++ b/apps/studio/components/interfaces/Billing/Payment/AddNewPaymentMethodModal.tsx
@@ -8,7 +8,6 @@ import { Modal } from 'ui'
import { useOrganizationPaymentMethodSetupIntent } from 'data/organizations/organization-payment-method-setup-intent-mutation'
import { useSelectedOrganization, useStore } from 'hooks'
import { STRIPE_PUBLIC_KEY } from 'lib/constants'
-import { useIsHCaptchaLoaded } from 'stores/hcaptcha-loaded-store'
import AddPaymentMethodForm from './AddPaymentMethodForm'
interface AddNewPaymentMethodModalProps {
@@ -31,7 +30,7 @@ const AddNewPaymentMethodModal = ({
const [intent, setIntent] = useState<any>()
const selectedOrganization = useSelectedOrganization()
- const captchaLoaded = useIsHCaptchaLoaded()
+ const [captchaLoaded, setHCaptchaLoaded] = useState(false)
const [captchaToken, setCaptchaToken] = useState<string | null>(null)
const [captchaRef, setCaptchaRef] = useState<HCaptcha | null>(null)
@@ -126,6 +125,7 @@ const AddNewPaymentMethodModal = ({
onExpire={() => {
setCaptchaToken(null)
}}
+ onLoad={() => setHCaptchaLoaded(true)}
/>
<Modal
diff --git a/apps/studio/pages/new/index.tsx b/apps/studio/pages/new/index.tsx
index 95fbc992686..9a82234d40e 100644
--- a/apps/studio/pages/new/index.tsx
+++ b/apps/studio/pages/new/index.tsx
@@ -9,7 +9,6 @@ import { NewOrgForm } from 'components/interfaces/Organization'
import { WizardLayout } from 'components/layouts'
import { useSetupIntent } from 'data/stripe/setup-intent-mutation'
import { STRIPE_PUBLIC_KEY } from 'lib/constants'
-import { useIsHCaptchaLoaded } from 'stores/hcaptcha-loaded-store'
import type { NextPageWithLayout } from 'types'
const stripePromise = loadStripe(STRIPE_PUBLIC_KEY)
@@ -21,7 +20,7 @@ const Wizard: NextPageWithLayout = () => {
const { resolvedTheme } = useTheme()
const [intent, setIntent] = useState<any>()
- const captchaLoaded = useIsHCaptchaLoaded()
+ const [captchaLoaded, setHCaptchaLoaded] = useState(false)
const [captchaToken, setCaptchaToken] = useState<string | null>(null)
const [captchaRef, setCaptchaRef] = useState<HCaptcha | null>(null)
@@ -86,13 +85,19 @@ const Wizard: NextPageWithLayout = () => {
ref={captchaRefCallback}
sitekey={process.env.NEXT_PUBLIC_HCAPTCHA_SITE_KEY!}
size="invisible"
+ onOpen={() => {
+ // [Joshen] This is to ensure that hCaptcha popup remains clickable
+ if (document !== undefined) document.body.classList.add('!pointer-events-auto')
+ }}
+ onClose={() => {
+ onLocalCancel()
+ if (document !== undefined) document.body.classList.remove('!pointer-events-auto')
+ }}
onVerify={(token) => {
setCaptchaToken(token)
+ if (document !== undefined) document.body.classList.remove('!pointer-events-auto')
}}
- onClose={onLocalCancel}
- onExpire={() => {
- setCaptchaToken(null)
- }}
+ onLoad={() => setHCaptchaLoaded(true)}
/>
{intent && (
diff --git a/apps/studio/stores/hcaptcha-loaded-store.tsx b/apps/studio/stores/hcaptcha-loaded-store.tsx
deleted file mode 100644
index 008053d20b0..00000000000
--- a/apps/studio/stores/hcaptcha-loaded-store.tsx
+++ /dev/null
@@ -1,31 +0,0 @@
-import HCaptcha from '@hcaptcha/react-hcaptcha'
-import { proxy, useSnapshot } from 'valtio'
-
-const hCaptchaLoadedStoreState = proxy({
- loaded: false,
- setLoaded() {
- hCaptchaLoadedStoreState.loaded = true
- },
-})
-
-export const useIsHCaptchaLoaded = () => {
- const snap = useSnapshot(hCaptchaLoadedStoreState)
-
- return snap.loaded
-}
-
-const HCaptchaLoadedStore = () => {
- const onLoad = () => {
- hCaptchaLoadedStoreState.setLoaded()
- }
-
- return (
- <HCaptcha
- sitekey={process.env.NEXT_PUBLIC_HCAPTCHA_SITE_KEY!}
- size="invisible"
- onLoad={onLoad}
- />
- )
-}
-
-export default HCaptchaLoadedStore