Created
June 5, 2025 18:52
-
-
Save GeDiez/1cb27241b789fe8d87c24c5399cec91d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
useMemo, | |
useState, | |
useEffect, | |
useCallback, | |
} from 'react'; | |
import { | |
isDevelopment, | |
getMetaMapConfig, | |
} from '@config'; | |
const { flowId, clientId } = getMetaMapConfig(); | |
export const useMetamap = ({ | |
metaData, | |
onUserFinish, | |
} = {}) => { | |
const [isScriptLoaded, setIsScriptLoaded] = useState(false); | |
const [isLoading, setIsLoading] = useState(false); | |
const verification = useMemo(() => { | |
if (!isScriptLoaded) return null; | |
const configuration = { | |
flowId, | |
clientId, | |
debug: isDevelopment, | |
metadata: metaData ? JSON.stringify(metaData) : undefined, | |
}; | |
const instance = new window.MetamapVerification(configuration); | |
return instance; | |
}, [metaData, isScriptLoaded]); | |
const onOpenWidget = useCallback(() => { | |
setIsLoading(true); | |
verification.start(); | |
}, [verification]); | |
useEffect(() => { | |
if (!verification) return; | |
if (onUserFinish) verification.on('metamap:userFinishedSdk', onUserFinish); | |
}, [onUserFinish, verification]); | |
useEffect(() => { | |
if (!verification) return; | |
verification.on('metamap:loaded', () => setIsLoading(false)); | |
}, [verification]); | |
useEffect(() => { | |
// Since script with onLoad prevents the script to be loaded | |
// we need to check if the script is loaded by polling | |
const interval = setInterval(() => { | |
if (window.MetamapVerification) { | |
setIsScriptLoaded(true); | |
clearInterval(interval); | |
} | |
}, 100); | |
return () => clearInterval(interval); | |
}, []); | |
return { | |
isLoading, | |
onOpenWidget, | |
setIsScriptLoaded, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment