import { Token } from '@uniswap/sdk-core' ; import { useCallback , useMemo } from 'react' ; import { shallowEqual , useDispatch , useSelector } from 'react-redux' ; import { useActiveWeb3React } from '../../hooks' ; import { AppDispatch , AppState } from '../index' ; import { addSerializedToken , removeSerializedToken , SerializedToken , updateUserDarkMode , toggleURLWarning , addSavedToken , addSavedPool , } from './actions' ; import { useAppSelector } from 'hooks/useAppDispatch' ; function serializeToken ( token ) { return { chainId : token . chainId , address : token . address , decimals : token . decimals , symbol : token . symbol , name : token . name , } ; } function deserializeToken ( serializedToken ) { return new Token ( serializedToken . chainId , serializedToken . address , serializedToken . decimals , serializedToken . symbol , serializedToken . name ) ; } export function useIsDarkMode ( ) { const { userDarkMode , matchesDarkMode } = useSelector < AppState , { userDarkMode : boolean | null ; matchesDarkMode : boolean ; } > ( ( { user : { matchesDarkMode , userDarkMode } } ) => ( { userDarkMode , matchesDarkMode , } ) , shallowEqual ) ; return userDarkMode === null ? matchesDarkMode : userDarkMode ; } export function useDarkModeManager ( ) { const dispatch = useDispatch < AppDispatch > ( ) ; const darkMode = true ; const toggleSetDarkMode = useCallback ( ( ) => { dispatch ( updateUserDarkMode ( { userDarkMode : ! darkMode } ) ) ; } , [ darkMode , dispatch ] ) ; return [ darkMode , toggleSetDarkMode ] ; } export function useAddUserToken ( ) { const dispatch = useDispatch < AppDispatch > ( ) ; return useCallback ( ( token ) => { dispatch ( addSerializedToken ( { serializedToken : serializeToken ( token ) } ) ) ; } , [ dispatch ] ) ; } export function useSavedTokens ( ) { const dispatch = useDispatch < AppDispatch > ( ) ; const savedTokens = useSelector ( ( state ) => state . user . savedTokens ) ; const updatedSavedTokens = useCallback ( ( address ) => { dispatch ( addSavedToken ( { address } ) ) ; } , [ dispatch ] ) ; return [ savedTokens ?? [ ] , updatedSavedTokens ] ; } export function useSavedPools ( ) { const dispatch = useDispatch < AppDispatch > ( ) ; const savedPools = useSelector ( ( state ) => state . user . savedPools ) ; const updateSavedPools = useCallback ( ( address ) => { dispatch ( addSavedPool ( { address } ) ) ; } , [ dispatch ] ) ; return [ savedPools ?? [ ] , updateSavedPools ] ; } export function useRemoveUserAddedToken ( ) { const dispatch = useDispatch < AppDispatch > ( ) ; return useCallback ( ( chainId , address ) => { dispatch ( removeSerializedToken ( { chainId , address } ) ) ; } , [ dispatch ] ) ; } export function useUserAddedTokens ( ) { const { chainId } = useActiveWeb3React ( ) ; const serializedTokensMap = useAppSelector ( ( { user : { tokens } } ) => tokens ) ; return useMemo ( ( ) => { if ( ! chainId ) return [ ] ; return Object . values ( serializedTokensMap ?. [ chainId ] ?? { } ) . map ( deserializeToken ) ; } , [ serializedTokensMap , chainId ] ) ; } export function useURLWarningVisible ( ) { return useSelector ( ( state ) => state . user . URLWarningVisible ) ; } export function useURLWarningToggle ( ) { const dispatch = useDispatch ( ) ; return useCallback ( ( ) => dispatch ( toggleURLWarning ( ) ) , [ dispatch ] ) ; }