Skip to content

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

adds hcaptcha loaded store

Alaister Young47216df6d040
diff --git a/package-lock.json b/package-lock.json
index 5f6dcf98766..938bb21e236 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -37607,6 +37607,7 @@
"uniforms-bootstrap4": "^3.6.2",
"uniforms-bridge-json-schema": "^3.6.2",
"uuid": "^8.3.2",
+ "valtio": "^1.7.6",
"yup": "^0.32.11",
"yup-password": "^0.2.2",
"zxcvbn": "^4.4.2"
@@ -37829,6 +37830,11 @@
"resolved": "https://registry.npmjs.org/openapi-types/-/openapi-types-9.3.1.tgz",
"integrity": "sha512-/Yvsd2D7miYB4HLJ3hOOS0+vnowQpaT75FsHzr/y5M9P4q9bwa7RcbW2YdH6KZBn8ceLbKGnHxMZ1CHliGHUFw=="
},
+ "studio/node_modules/proxy-compare": {
+ "version": "2.3.0",
+ "resolved": "https://registry.npmjs.org/proxy-compare/-/proxy-compare-2.3.0.tgz",
+ "integrity": "sha512-c3L2CcAi7f7pvlD0D7xsF+2CQIW8C3HaYx2Pfgq8eA4HAl3GAH6/dVYsyBbYF/0XJs2ziGLrzmz5fmzPm6A0pQ=="
+ },
"studio/node_modules/react-resize-detector": {
"version": "6.7.8",
"resolved": "https://registry.npmjs.org/react-resize-detector/-/react-resize-detector-6.7.8.tgz",
@@ -37889,6 +37895,46 @@
"node": ">=4.2.0"
}
},
+ "studio/node_modules/valtio": {
+ "version": "1.7.6",
+ "resolved": "https://registry.npmjs.org/valtio/-/valtio-1.7.6.tgz",
+ "integrity": "sha512-zsGrCCYOIpy8egQxftduFyJusF/BMu3CganhHKUOE/I6t6V6yA1MDfZZkrYoWYCGkC3rSBYcIHEEsoYQM9lV2w==",
+ "dependencies": {
+ "proxy-compare": "2.3.0",
+ "use-sync-external-store": "1.2.0"
+ },
+ "engines": {
+ "node": ">=12.7.0"
+ },
+ "peerDependencies": {
+ "@babel/helper-module-imports": ">=7.12",
+ "@babel/types": ">=7.13",
+ "aslemammad-vite-plugin-macro": ">=1.0.0-alpha.1",
+ "babel-plugin-macros": ">=3.0",
+ "react": ">=16.8",
+ "vite": ">=2.8.6"
+ },
+ "peerDependenciesMeta": {
+ "@babel/helper-module-imports": {
+ "optional": true
+ },
+ "@babel/types": {
+ "optional": true
+ },
+ "aslemammad-vite-plugin-macro": {
+ "optional": true
+ },
+ "babel-plugin-macros": {
+ "optional": true
+ },
+ "react": {
+ "optional": true
+ },
+ "vite": {
+ "optional": true
+ }
+ }
+ },
"studio/node_modules/webpack-bundle-analyzer": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/webpack-bundle-analyzer/-/webpack-bundle-analyzer-4.3.0.tgz",
@@ -63371,6 +63417,7 @@
"uniforms-bootstrap4": "^3.6.2",
"uniforms-bridge-json-schema": "^3.6.2",
"uuid": "^8.3.2",
+ "valtio": "*",
"yup": "^0.32.11",
"yup-password": "^0.2.2",
"zxcvbn": "^4.4.2"
@@ -63521,6 +63568,11 @@
"resolved": "https://registry.npmjs.org/openapi-types/-/openapi-types-9.3.1.tgz",
"integrity": "sha512-/Yvsd2D7miYB4HLJ3hOOS0+vnowQpaT75FsHzr/y5M9P4q9bwa7RcbW2YdH6KZBn8ceLbKGnHxMZ1CHliGHUFw=="
},
+ "proxy-compare": {
+ "version": "2.3.0",
+ "resolved": "https://registry.npmjs.org/proxy-compare/-/proxy-compare-2.3.0.tgz",
+ "integrity": "sha512-c3L2CcAi7f7pvlD0D7xsF+2CQIW8C3HaYx2Pfgq8eA4HAl3GAH6/dVYsyBbYF/0XJs2ziGLrzmz5fmzPm6A0pQ=="
+ },
"react-resize-detector": {
"version": "6.7.8",
"resolved": "https://registry.npmjs.org/react-resize-detector/-/react-resize-detector-6.7.8.tgz",
@@ -63564,6 +63616,15 @@
"integrity": "sha512-QCh+85mCy+h0IGff8r5XWzOVSbBO+KfeYrMQh7NJ58QujwcE22u+NUSmUxqF+un70P9GXKxa2HCNiTTMJknyjQ==",
"dev": true
},
+ "valtio": {
+ "version": "1.7.6",
+ "resolved": "https://registry.npmjs.org/valtio/-/valtio-1.7.6.tgz",
+ "integrity": "sha512-zsGrCCYOIpy8egQxftduFyJusF/BMu3CganhHKUOE/I6t6V6yA1MDfZZkrYoWYCGkC3rSBYcIHEEsoYQM9lV2w==",
+ "requires": {
+ "proxy-compare": "2.3.0",
+ "use-sync-external-store": "1.2.0"
+ }
+ },
"webpack-bundle-analyzer": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/webpack-bundle-analyzer/-/webpack-bundle-analyzer-4.3.0.tgz",
diff --git a/studio/components/interfaces/Billing/AddNewPaymentMethodModal/AddNewPaymentMethodModal.tsx b/studio/components/interfaces/Billing/AddNewPaymentMethodModal/AddNewPaymentMethodModal.tsx
index 5f18c9dff3b..0f553810bc3 100644
--- a/studio/components/interfaces/Billing/AddNewPaymentMethodModal/AddNewPaymentMethodModal.tsx
+++ b/studio/components/interfaces/Billing/AddNewPaymentMethodModal/AddNewPaymentMethodModal.tsx
@@ -1,13 +1,14 @@
-import { FC, useCallback, useEffect, useRef, useState } from 'react'
-import { IconLoader, Modal } from 'ui'
+import HCaptcha from '@hcaptcha/react-hcaptcha'
import { loadStripe } from '@stripe/stripe-js'
import { Elements } from '@stripe/react-stripe-js'
+import { FC, useCallback, useEffect, useState } from 'react'
+import { Modal } from 'ui'
import { useStore } from 'hooks'
+import { useIsHCaptchaLoaded } from 'stores/hcaptcha-loaded-store'
import { post } from 'lib/common/fetch'
import { API_URL, STRIPE_PUBLIC_KEY } from 'lib/constants'
import AddPaymentMethodForm from './AddPaymentMethodForm'
-import HCaptcha from '@hcaptcha/react-hcaptcha'
interface Props {
visible: boolean
@@ -21,7 +22,7 @@ const AddNewPaymentMethodModal: FC<Props> = ({ visible, returnUrl, onCancel }) =
const { ui } = useStore()
const [intent, setIntent] = useState<any>()
- const [captchaLoaded, setCaptchaLoaded] = useState(false)
+ const captchaLoaded = useIsHCaptchaLoaded()
const [captchaToken, setCaptchaToken] = useState<string | null>(null)
const [captchaRef, setCaptchaRef] = useState<HCaptcha | null>(null)
@@ -30,18 +31,6 @@ const AddNewPaymentMethodModal: FC<Props> = ({ visible, returnUrl, onCancel }) =
setCaptchaRef(node)
}, [])
- /**
- * We want to load the Stripe payment elements only after successfully verifying captcha.
- * To execute hCaptcha when displaying the modal, we need to ensure hCaptcha is actually loaded.
- * The `onLoad` method from hCaptcha is only executed ONCE for the entire application, so we cannot listen to it.
- * One way to find out if hCaptcha has been initialized, is checking `window.hcaptcha`. When checking via window,
- * we know if the hCaptcha has been initialized from a different part of the app or through this component.
- */
- useEffect(() => {
- setCaptchaLoaded(true)
- // @ts-ignore
- }, [window.hcaptcha])
-
useEffect(() => {
const loadPaymentForm = async () => {
if (visible && captchaRef && captchaLoaded) {
@@ -118,7 +107,7 @@ const AddNewPaymentMethodModal: FC<Props> = ({ visible, returnUrl, onCancel }) =
onCancel={onLocalCancel}
className="PAYMENT"
>
- <div className="space-y-4 py-4">
+ <div className="py-4 space-y-4">
<Elements stripe={stripePromise} options={options}>
<AddPaymentMethodForm returnUrl={returnUrl} onCancel={onLocalCancel} />
</Elements>
diff --git a/studio/package.json b/studio/package.json
index bf108ae4eda..7a39cefd0d6 100644
--- a/studio/package.json
+++ b/studio/package.json
@@ -89,6 +89,7 @@
"uniforms-bootstrap4": "^3.6.2",
"uniforms-bridge-json-schema": "^3.6.2",
"uuid": "^8.3.2",
+ "valtio": "^1.7.6",
"yup": "^0.32.11",
"yup-password": "^0.2.2",
"zxcvbn": "^4.4.2"
diff --git a/studio/pages/_app.tsx b/studio/pages/_app.tsx
index cd16894f14d..e77baa71827 100644
--- a/studio/pages/_app.tsx
+++ b/studio/pages/_app.tsx
@@ -27,6 +27,7 @@ import { useEffect, useState } from 'react'
import { Hydrate, QueryClientProvider } from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
import { RootStore } from 'stores'
+import HCaptchaLoadedStore from 'stores/hcaptcha-loaded-store'
import { StoreProvider } from 'hooks'
import { GOTRUE_ERRORS } from 'lib/constants'
import { auth } from 'lib/gotrue'
@@ -115,6 +116,7 @@ function MyApp({ Component, pageProps }: AppPropsWithLayout) {
</RouteValidationWrapper>
</PageTelemetry>
+ <HCaptchaLoadedStore />
<PortalToast />
<ReactQueryDevtools initialIsOpen={false} position="bottom-right" />
</FlagProvider>
diff --git a/studio/stores/hcaptcha-loaded-store.tsx b/studio/stores/hcaptcha-loaded-store.tsx
new file mode 100644
index 00000000000..008053d20b0
--- /dev/null
+++ b/studio/stores/hcaptcha-loaded-store.tsx
@@ -0,0 +1,31 @@
+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