import { useState, useEffect } from 'react'; import { module, moduleEventEmitter } from '../module'; import { PLATFORM_ANDROID } from '../utils/constants'; import type { FaceLivenessSettings, FaceLivenessResponse, FaceLivenessResult, FaceLivenessError, } from '../types'; import { Time, Filter } from '../types'; let responseFormattedOptions: string = ''; const formattedOptions = (settings: FaceLivenessSettings): string => { const imageUrlExpirationTimeFormatted = PLATFORM_ANDROID ? settings?.imageUrlExpirationTime === Time.THIRTY_MIN ? null : settings?.imageUrlExpirationTime : settings?.imageUrlExpirationTime; const filter = PLATFORM_ANDROID ? Filter[settings?.filter ?? Filter.LINE_DRAWING] : settings?.filter; const formatToJSON = JSON.stringify({ ...settings, filter, imageUrlExpirationTime: PLATFORM_ANDROID ? Time[imageUrlExpirationTimeFormatted!] : settings?.imageUrlExpirationTime, }); return formatToJSON; }; /** * @deprecated This package is deprecated since version 4.0.0 and will no longer receive updates. * Please use @caf.io/react-native-face-liveness instead. * See https://docs.caf.io/caf-sdk/react-native/getting-started-with-the-sdk */ const useFaceLiveness = (settings?: FaceLivenessSettings) => { const [response, setResponse] = useState({ result: null, error: null, failure: null, cancelled: false, isLoading: false, }); responseFormattedOptions = formattedOptions(settings!); useEffect(() => { moduleEventEmitter.addListener( 'FaceLiveness_Success', (event: FaceLivenessResult) => { setResponse({ result: event, error: null, failure: null, cancelled: false, isLoading: false, }); }, ); moduleEventEmitter.addListener( 'FaceLiveness_Error', (event: FaceLivenessError) => { setResponse({ result: null, error: event, failure: null, cancelled: false, isLoading: false, }); }, ); moduleEventEmitter.addListener( 'FaceLiveness_Failure', (event: { signedResponse: string; message: string; type: string }) => { setResponse({ result: { signedResponse: event.signedResponse, }, error: null, failure: { message: event.message, type: event.type, }, cancelled: false, isLoading: false, }); }, ); moduleEventEmitter.addListener( 'FaceLiveness_Cancelled', (event: boolean) => { setResponse({ result: null, error: null, failure: null, cancelled: event, isLoading: false, }); }, ); moduleEventEmitter.addListener('FaceLiveness_Loading', (event: boolean) => { setResponse({ result: null, error: null, failure: null, cancelled: false, isLoading: event, }); }); moduleEventEmitter.addListener('FaceLiveness_Loaded', (event: boolean) => { setResponse({ result: null, error: null, failure: null, cancelled: false, isLoading: event, }); }); return () => { moduleEventEmitter.removeAllListeners('FaceLiveness_Success'); moduleEventEmitter.removeAllListeners('FaceLiveness_Error'); moduleEventEmitter.removeAllListeners('FaceLiveness_Cancelled'); moduleEventEmitter.removeAllListeners('FaceLiveness_Loading'); moduleEventEmitter.removeAllListeners('FaceLiveness_Loaded'); }; }, []); return { result: response.result, error: response.error, failure: response.failure, cancelled: response.cancelled, isLoading: response.isLoading, }; }; /** * @deprecated This package is deprecated since version 4.0.0 and will no longer receive updates. * Please use @caf.io/react-native-face-liveness instead. * See https://docs.caf.io/caf-sdk/react-native/getting-started-with-the-sdk */ const startFaceLiveness = (mobileToken: string, personId: string) => { module.startFaceLiveness(mobileToken, personId, responseFormattedOptions); }; export { startFaceLiveness, useFaceLiveness };