import { useState , useEffect } from 'react' ; import { useSubscription } from '@apollo/react-hooks' ; import { UNIQUE_CARS } from './gql' ; export function useUniqueCars ( ) { const { data } = useSubscription ( UNIQUE_CARS ) ; const uniqueCars = data && data . UNIQUE_CARS ; const [ cars , setCars ] = useState < { [ key ] : boolean ; } > ( { } ) ; const [ count , setCount ] = useState < number > ( 0 ) ; useEffect ( ( ) => { if ( uniqueCars ) { setCars ( prev => { prev [ uniqueCars . ID ] = true ; setCount ( Object . keys ( prev ) . length ) ; return prev ; } ) ; } } , [ uniqueCars ] ) ; return { cars , count } ; }