import { useEffect , useRef , useState } from 'react' ; import { SubscriptionResult } from '@aragon/connect-types' ; import { App , warn } from '@aragon/connect-core' ; import { useConnect } from './use-connect' ; import { hash } from './utils' ; export function createAppHook ( appConnect , connector ? ) { warn ( 'createAppHook is going to be deprecated in the next version. ' + 'Please use useConnect() and the appConnectors prop on <Connect />.' ) ; return function useAppData < T = any > ( app , callback ? , dependencies ? ) { const callbackRef = useRef < Function > ( ( app ) => app ) ; useEffect ( ( ) => { callbackRef . current = callback || ( ( app ) => app ) ; } , [ callback ] ) ; const [ [ connectedApp , appError ] , setApp ] = useState < [ App | null , Error | null ] > ( [ null , null ] ) ; const appHashRef = useRef < string > ( '' ) ; useEffect ( ( ) => { const appHash = hash ( app ) ; if ( appHash === appHashRef . current ) { return ; } appHashRef . current = appHash ; if ( ! app ) { return ; } let cancelled = false ; appConnect ( app , connector ) . then ( ( connectedApp ) => { if ( ! cancelled ) { setApp ( [ connectedApp , null ] ) ; } } ) . catch ( ( err ) => { if ( ! cancelled ) { setApp ( [ null , err ] ) ; } } ) ; return ( ) => { cancelled = true ; } ; } , [ app ] ) ; return useConnect ( ( ) => { if ( appError ) { throw appError ; } return connectedApp ? callbackRef . current ( connectedApp ) : undefined ; } , [ ... ( dependencies || [ ] ) , connectedApp , appError ] ) ; } ; }