import { useCallback } from 'react' ; import { nanoid } from 'nanoid' ; import { useDispatch , actions } from '../store' ; import useGraphQL from './useGraphQL' ; const useTwilio = ( ) => { const dispatch = useDispatch ( ) ; const { loading , request } = useGraphQL ( ) ; const getTwilioToken = useCallback ( async ( room ) => { let identity = '' ; try { identity = localStorage . getItem ( 'identity' ) || nanoid ( ) ; localStorage . setItem ( 'identity' , identity ) ; } catch ( e ) { } const { response , error } = await request ( { query : `query {
          room {
            get (room: "${ room }", identity: "${ identity }") {
              token
            }
          }
        }` } ) ; if ( error ) return { error } ; dispatch ( actions . set ( { twilio : response . data . room . get } ) ) ; return { response } ; } , [ request , dispatch ] ) ; return { loading , getTwilioToken } ; } ; export default useTwilio ;