import { ApolloClient , NormalizedCacheObject } from '@apollo/client' ; import { arbitrumBlockClient , arbitrumClient , blockClient , client , optimismClient , optimismBlockClient , polygonBlockClient , polygonClient , } from 'apollo/client' ; import { NetworkInfo , SupportedNetwork } from 'constants/networks' ; import { useCallback , useMemo } from 'react' ; import { useDispatch , useSelector } from 'react-redux' ; import { useActiveWeb3React } from '../../hooks' ; import { AppDispatch , AppState } from '../index' ; import { addPopup , ApplicationModal , PopupContent , removePopup , setOpenModal , updateActiveNetworkVersion , updateSubgraphStatus , } from './actions' ; export function useBlockNumber ( ) { const { chainId } = useActiveWeb3React ( ) ; return useSelector ( ( state ) => state . application . blockNumber [ chainId ?? - 1 ] ) ; } export function useModalOpen ( modal ) { const openModal = useSelector ( ( state ) => state . application . openModal ) ; return openModal === modal ; } export function useToggleModal ( modal ) { const open = useModalOpen ( modal ) ; const dispatch = useDispatch < AppDispatch > ( ) ; return useCallback ( ( ) => dispatch ( setOpenModal ( open ? null : modal ) ) , [ dispatch , modal , open ] ) ; } export function useOpenModal ( modal ) { const dispatch = useDispatch < AppDispatch > ( ) ; return useCallback ( ( ) => dispatch ( setOpenModal ( modal ) ) , [ dispatch , modal ] ) ; } export function useCloseModals ( ) { const dispatch = useDispatch < AppDispatch > ( ) ; return useCallback ( ( ) => dispatch ( setOpenModal ( null ) ) , [ dispatch ] ) ; } export function useWalletModalToggle ( ) { return useToggleModal ( ApplicationModal . WALLET ) ; } export function useToggleSettingsMenu ( ) { return useToggleModal ( ApplicationModal . SETTINGS ) ; } export function useAddPopup ( ) { const dispatch = useDispatch ( ) ; return useCallback ( ( content , key ? ) => { dispatch ( addPopup ( { content , key } ) ) ; } , [ dispatch ] ) ; } export function useRemovePopup ( ) { const dispatch = useDispatch ( ) ; return useCallback ( ( key ) => { dispatch ( removePopup ( { key } ) ) ; } , [ dispatch ] ) ; } export function useActivePopups ( ) { const list = useSelector ( ( state ) => state . application . popupList ) ; return useMemo ( ( ) => list . filter ( ( item ) => item . show ) , [ list ] ) ; } export function useSubgraphStatus ( ) { const dispatch = useDispatch ( ) ; const status = useSelector ( ( state ) => state . application . subgraphStatus ) ; const update = useCallback ( ( available , syncedBlock , headBlock ) => { dispatch ( updateSubgraphStatus ( { available , syncedBlock , headBlock } ) ) ; } , [ dispatch ] ) ; return [ status , update ] ; } export function useActiveNetworkVersion ( ) { const dispatch = useDispatch ( ) ; const activeNetwork = useSelector ( ( state ) => state . application . activeNetworkVersion ) ; const update = useCallback ( ( activeNetworkVersion ) => { dispatch ( updateActiveNetworkVersion ( { activeNetworkVersion } ) ) ; } , [ dispatch ] ) ; return [ activeNetwork , update ] ; } export function useDataClient ( ) { const [ activeNetwork ] = useActiveNetworkVersion ( ) ; switch ( activeNetwork . id ) { case SupportedNetwork . ETHEREUM : return client ; case SupportedNetwork . ARBITRUM : return arbitrumClient ; case SupportedNetwork . OPTIMISM : return optimismClient ; case SupportedNetwork . POLYGON : return polygonClient ; default : return client ; } } export function useBlockClient ( ) { const [ activeNetwork ] = useActiveNetworkVersion ( ) ; switch ( activeNetwork . id ) { case SupportedNetwork . ETHEREUM : return blockClient ; case SupportedNetwork . ARBITRUM : return arbitrumBlockClient ; case SupportedNetwork . OPTIMISM : return optimismBlockClient ; case SupportedNetwork . POLYGON : return polygonBlockClient ; default : return blockClient ; } } export function useClients ( ) { const dataClient = useDataClient ( ) ; const blockClient = useBlockClient ( ) ; return { dataClient , blockClient , } ; }