|
| 1 | +import React, { FC, useCallback, useEffect, useRef, useState } from 'react' |
| 2 | +import { BackHandler } from 'react-native' |
| 3 | +import ConfirmHcaptcha from '@hcaptcha/react-native-hcaptcha' |
| 4 | +import { useDispatch, useSelector } from 'react-redux' |
| 5 | +import { captcha } from '@quiet/state-manager' |
| 6 | +import { defaultTheme } from '../../styles/themes/default.theme' |
| 7 | +import { createLogger } from '../../utils/logger' |
| 8 | + |
| 9 | +const logger = createLogger('CaptchaModal') |
| 10 | + |
| 11 | +interface HCaptchaMessageEvent { |
| 12 | + nativeEvent: { data: string } |
| 13 | + success: boolean |
| 14 | + markUsed?: () => void |
| 15 | + reset: () => void |
| 16 | +} |
| 17 | + |
| 18 | +const SHOW_DELAY_MS = 100 |
| 19 | + |
| 20 | +// Polyfill: react-native-modal@13 (bundled inside @hcaptcha/react-native-hcaptcha) |
| 21 | +// calls BackHandler.removeEventListener which was removed in RN 0.72+. Its |
| 22 | +// absence crashes the modal on hide. Install a no-op shim if missing. |
| 23 | +// (addEventListener still returns a subscription with .remove() so leak is minor.) |
| 24 | +const bhMut = BackHandler as unknown as { removeEventListener?: (...args: unknown[]) => boolean } |
| 25 | +if (typeof bhMut.removeEventListener !== 'function') { |
| 26 | + bhMut.removeEventListener = () => true |
| 27 | +} |
| 28 | + |
| 29 | +export const CaptchaModal: FC = () => { |
| 30 | + const dispatch = useDispatch() |
| 31 | + const captchaRef = useRef<ConfirmHcaptcha | null>(null) |
| 32 | + // Guard against the library's hide()/onMessage emitting repeated cancels |
| 33 | + // after we've already dispatched a terminal response for this session. |
| 34 | + const respondedRef = useRef(false) |
| 35 | + |
| 36 | + const captchaRequested = useSelector(captcha.selectors.captchaRequested) |
| 37 | + const siteKey = useSelector(captcha.selectors.siteKey) |
| 38 | + |
| 39 | + // Latch the first non-empty siteKey so the native modal stays mounted |
| 40 | + // across community-leave flows that wipe redux state. Unmounting the |
| 41 | + // library's RN modal mid-presentation crashes iOS WebView. |
| 42 | + const [mountedSiteKey, setMountedSiteKey] = useState('') |
| 43 | + useEffect(() => { |
| 44 | + if (siteKey && siteKey !== mountedSiteKey) { |
| 45 | + setMountedSiteKey(siteKey) |
| 46 | + } |
| 47 | + }, [siteKey, mountedSiteKey]) |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + if (!mountedSiteKey) return |
| 51 | + if (captchaRequested && siteKey !== '') { |
| 52 | + logger.info(`Showing captcha for ${siteKey}`) |
| 53 | + respondedRef.current = false |
| 54 | + const id = setTimeout(() => captchaRef.current?.show(), SHOW_DELAY_MS) |
| 55 | + return () => clearTimeout(id) |
| 56 | + } |
| 57 | + // Pass no argument: the library synthesizes an onMessage({data:'cancel'}) |
| 58 | + // when hide() is called with a truthy source, which would re-enter our |
| 59 | + // handler and loop. |
| 60 | + captchaRef.current?.hide() |
| 61 | + return undefined |
| 62 | + }, [captchaRequested, siteKey, mountedSiteKey]) |
| 63 | + |
| 64 | + const respond = useCallback( |
| 65 | + (payload: Parameters<typeof captcha.actions.captchaFormResponse>[0]) => { |
| 66 | + if (respondedRef.current) return |
| 67 | + respondedRef.current = true |
| 68 | + dispatch(captcha.actions.captchaFormResponse(payload)) |
| 69 | + }, |
| 70 | + [dispatch] |
| 71 | + ) |
| 72 | + |
| 73 | + const handleMessage = useCallback( |
| 74 | + (event: HCaptchaMessageEvent) => { |
| 75 | + const data = event?.nativeEvent?.data |
| 76 | + if (!data) return |
| 77 | + if (respondedRef.current) return |
| 78 | + |
| 79 | + if (data === 'open') return |
| 80 | + |
| 81 | + if (data === 'cancel' || data === 'challenge-closed') { |
| 82 | + logger.info('hCaptcha cancelled') |
| 83 | + respond({ error: 'Captcha cancelled by user' }) |
| 84 | + captchaRef.current?.hide() |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + if (data === 'challenge-expired' || data === 'expired') { |
| 89 | + logger.warn('hCaptcha expired, resetting') |
| 90 | + event.reset?.() |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + if (event.success) { |
| 95 | + event.markUsed?.() |
| 96 | + logger.info('hCaptcha solved') |
| 97 | + respond({ token: data }) |
| 98 | + captchaRef.current?.hide() |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + logger.error('hCaptcha verification failed', data) |
| 103 | + respond({ error: `Captcha error: ${data}` }) |
| 104 | + captchaRef.current?.hide() |
| 105 | + }, |
| 106 | + [respond] |
| 107 | + ) |
| 108 | + |
| 109 | + if (mountedSiteKey === '') return null |
| 110 | + |
| 111 | + return ( |
| 112 | + <ConfirmHcaptcha |
| 113 | + ref={captchaRef} |
| 114 | + siteKey={mountedSiteKey} |
| 115 | + onMessage={handleMessage} |
| 116 | + size='invisible' |
| 117 | + orientation='portrait' |
| 118 | + languageCode='en' |
| 119 | + showLoading |
| 120 | + loadingIndicatorColor={defaultTheme.palette.background.lushSky} |
| 121 | + /> |
| 122 | + ) |
| 123 | +} |
| 124 | + |
| 125 | +export default CaptchaModal |
0 commit comments