import { ChallengePrompt, ChallengeValidation, ProtectionSessionDurations, ReactNativeToWebEvent, } from '@standardnotes/snjs' import { FunctionComponent, useCallback, useEffect, useRef } from 'react' import DecoratedInput from '@/Components/Input/DecoratedInput' import DecoratedPasswordInput from '@/Components/Input/DecoratedPasswordInput' import { ChallengeModalValues } from './ChallengeModalValues' import { WebApplication } from '@/Application/Application' import { InputValue } from './InputValue' import BiometricsPrompt from './BiometricsPrompt' type Props = { application: WebApplication prompt: ChallengePrompt values: ChallengeModalValues index: number onValueChange: (value: InputValue['value'], prompt: ChallengePrompt) => void isInvalid: boolean } const ChallengeModalPrompt: FunctionComponent = ({ application, prompt, values, index, onValueChange, isInvalid, }) => { const inputRef = useRef(null) const biometricsButtonRef = useRef(null) const activatePrompt = useCallback(async () => { if (prompt.validation === ChallengeValidation.Biometric) { if (application.isNativeMobileWeb()) { const appState = await application.mobileDevice().getAppState() if (appState !== 'active') { return } } biometricsButtonRef.current?.click() } else { inputRef.current?.focus() } }, [application, prompt.validation]) useEffect(() => { if (!application.isNativeMobileWeb()) { return } const disposeListener = application.addNativeMobileEventListener((event: ReactNativeToWebEvent) => { if (event === ReactNativeToWebEvent.GainingFocus) { void activatePrompt() } }) return () => { if (disposeListener) { disposeListener() } } }, [activatePrompt, application]) useEffect(() => { const isNotFirstPrompt = index !== 0 if (isNotFirstPrompt) { return } void activatePrompt() }, [activatePrompt, index]) useEffect(() => { if (isInvalid) { inputRef.current?.focus() } }, [isInvalid]) return (
{prompt.validation === ChallengeValidation.ProtectionSessionDuration ? (
Allow protected access for
{ProtectionSessionDurations.map((option) => { const selected = option.valueInSeconds === values[prompt.id].value return ( ) })}
) : prompt.validation === ChallengeValidation.Biometric ? ( ) : prompt.secureTextEntry ? ( onValueChange(value, prompt)} /> ) : ( onValueChange(value, prompt)} /> )} {isInvalid &&
Invalid authentication, please try again.
}
) } export default ChallengeModalPrompt