import { getVersionUpgrade , minVersionBump , VersionUpgrade } from '@uniswap/token-lists' ; import { OPTIMISM_LIST , UNSUPPORTED_LIST_URLS } from 'constants/lists' ; import { useCallback , useEffect } from 'react' ; import { useAllLists } from 'state/lists/hooks' ; import { useFetchListCallback } from '../../hooks/useFetchListCallback' ; import useInterval from '../../hooks/useInterval' ; import useIsWindowVisible from '../../hooks/useIsWindowVisible' ; import { acceptListUpdate , enableList } from './actions' ; import { useActiveListUrls } from './hooks' ; import { useAppDispatch } from 'hooks/useAppDispatch' ; export default function Updater ( ) { const dispatch = useAppDispatch ( ) ; const isWindowVisible = useIsWindowVisible ( ) ; const lists = useAllLists ( ) ; const activeListUrls = useActiveListUrls ( ) ; const fetchList = useFetchListCallback ( ) ; const fetchAllListsCallback = useCallback ( ( ) => { if ( ! isWindowVisible ) return ; Object . keys ( lists ) . forEach ( ( url ) => fetchList ( url ) . catch ( ( error ) => console . debug ( 'interval list fetching error' , error ) ) ) ; } , [ fetchList , isWindowVisible , lists ] ) ; dispatch ( enableList ( OPTIMISM_LIST ) ) ; useInterval ( fetchAllListsCallback , 1000 * 60 * 10 ) ; useEffect ( ( ) => { Object . keys ( lists ) . forEach ( ( listUrl ) => { const list = lists [ listUrl ] ; if ( ! list . current && ! list . loadingRequestId && ! list . error ) { fetchList ( listUrl ) . catch ( ( error ) => console . debug ( 'list added fetching error' , error ) ) ; } } ) ; } , [ dispatch , fetchList , lists ] ) ; useEffect ( ( ) => { UNSUPPORTED_LIST_URLS . forEach ( ( listUrl ) => { const list = lists [ listUrl ] ; if ( ! list || ( ! list . current && ! list . loadingRequestId && ! list . error ) ) { fetchList ( listUrl ) . catch ( ( error ) => console . debug ( 'list added fetching error' , error ) ) ; } } ) ; } , [ dispatch , fetchList , lists ] ) ; useEffect ( ( ) => { Object . keys ( lists ) . forEach ( ( listUrl ) => { const list = lists [ listUrl ] ; if ( list . current && list . pendingUpdate ) { const bump = getVersionUpgrade ( list . current . version , list . pendingUpdate . version ) ; switch ( bump ) { case VersionUpgrade . NONE : throw new Error ( 'unexpected no version bump' ) ; case VersionUpgrade . PATCH : case VersionUpgrade . MINOR : const min = minVersionBump ( list . current . tokens , list . pendingUpdate . tokens ) ; if ( bump >= min ) { dispatch ( acceptListUpdate ( listUrl ) ) ; } else { console . error ( `List at url ${ listUrl } could not automatically update because the version bump was only PATCH/MINOR while the update had breaking changes and should have been MAJOR` ) ; } break ; case VersionUpgrade . MAJOR : dispatch ( acceptListUpdate ( listUrl ) ) ; } } } ) ; } , [ dispatch , lists , activeListUrls ] ) ; return null ; }