Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110import {useEffect, useMemo, useRef} from 'react'import {WebView, type WebViewNavigation} from 'react-native-webview'import {type ShouldStartLoadRequest} from 'react-native-webview/lib/WebViewTypes'
import {type SignupState} from '#/screens/Signup/state'
const ALLOWED_HOSTS = [ 'bsky.social', 'bsky.app', 'blacksky.app', 'blacksky.community', 'staging.bsky.app', 'staging.bsky.dev', 'js.hcaptcha.com', 'newassets.hcaptcha.com', 'api2.hcaptcha.com',]
const MIN_DELAY = 3_500
export function CaptchaWebView({ url, stateParam, state, onComplete, onSuccess, onError,}: { url: string stateParam: string state?: SignupState onComplete: () => void onSuccess: (code: string) => void onError: (error: unknown) => void}) { const startedAt = useRef(Date.now()) const successTo = useRef<NodeJS.Timeout>(undefined)
useEffect(() => { return () => { if (successTo.current) { clearTimeout(successTo.current) } } }, [])
const redirectHost = useMemo(() => { if (!state?.serviceUrl) return 'blacksky.community'
return state?.serviceUrl && new URL(state?.serviceUrl).host === 'staging.bsky.dev' ? 'app.staging.bsky.dev' : 'blacksky.community' }, [state?.serviceUrl])
const wasSuccessful = useRef(false)
const onShouldStartLoadWithRequest = (event: ShouldStartLoadRequest) => { const urlp = new URL(event.url) return ALLOWED_HOSTS.includes(urlp.host) }
const onNavigationStateChange = (e: WebViewNavigation) => { if (wasSuccessful.current) return
const urlp = new URL(e.url) if (urlp.host !== redirectHost || urlp.pathname === '/gate/signup') return
const code = urlp.searchParams.get('code') if (urlp.searchParams.get('state') !== stateParam || !code) { onError({error: 'Invalid state or code'}) return }
// We want to delay the completion of this screen ever so slightly so that it doesn't appear to be a glitch if it completes too fast wasSuccessful.current = true onComplete() const now = Date.now() const timeTaken = now - startedAt.current if (timeTaken < MIN_DELAY) { successTo.current = setTimeout(() => { onSuccess(code) }, MIN_DELAY - timeTaken) } else { onSuccess(code) } }
return ( <WebView source={{uri: url}} javaScriptEnabled style={{ flex: 1, backgroundColor: 'transparent', borderRadius: 10, }} onShouldStartLoadWithRequest={onShouldStartLoadWithRequest} onNavigationStateChange={onNavigationStateChange} scrollEnabled={false} onError={e => { onError(e.nativeEvent) }} onHttpError={e => { onError(e.nativeEvent) }} /> )}