import { useMutation } from 'react-query' ; import { useContext , useEffect , useState } from 'react' ; import { ISubscriberJwt } from '@notifire/shared' ; import { initializeSession } from '../api/initialize-session' ; import { applyToken } from '../store/use-auth-controller' ; import { AuthContext , IAuthContext } from '../store/auth.context' ; import { postUsageLog } from '../api/usage' ; interface IUserInfo { firstName : string ; lastName : string ; email : string ; phone : string ; } export function useInitialization ( ) { const { setToken , setUser } = useContext < IAuthContext > ( AuthContext ) ; const [ initialized , setInitialized ] = useState < boolean > ( false ) ; const { mutateAsync } = useMutation < { token : string ; profile : ISubscriberJwt ; } , never , { clientId : string ; userId : string ; userInfo : IUserInfo ; } > ( ( params ) => initializeSession ( params . clientId , params . userId , params . userInfo ) ) ; useEffect ( ( ) => { const handler = async ( event ) => { try { if ( event . data . type === 'INIT_IFRAME' ) { if ( 'parentIFrame' in window ) { ( window as any ) . parentIFrame . autoResize ( true ) ; } const payload = event . data . value ; const response = await mutateAsync ( { clientId : payload . clientId , userId : payload . data . $user_id , userInfo : { firstName : payload . data . $first_name , lastName : payload . data . $last_name , email : payload . data . $email , phone : payload . data . $phone , } , } ) ; applyToken ( response . token ) ; setUser ( response . profile ) ; setToken ( response . token ) ; setInitialized ( true ) ; postUsageLog ( 'Load Widget' , { } ) ; } else if ( event . data . type === 'SHOW_WIDGET' ) { postUsageLog ( 'Open Widget' , { } ) ; } } catch ( e ) { console . error ( e ) ; } } ; if ( process . env . NODE_ENV === 'test' ) { ( window as any ) . initHandler = handler ; } window . addEventListener ( 'message' , handler ) ; return ( ) => window . removeEventListener ( 'message' , handler ) ; } , [ ] ) ; return { initialized , } ; }