diff --git a/common/components/ChainItem.tsx b/common/components/ChainItem.tsx index 954eebb..6e6424a 100644 --- a/common/components/ChainItem.tsx +++ b/common/components/ChainItem.tsx @@ -14,7 +14,8 @@ export const ChainItem: React.FC = ({ chain }) => { <>
- {chain.nativeCurrency?.symbol ?? ''} + {/* {chain.nativeCurrency?.symbol ?? ''} */} + {chain.chain} {chain.network} diff --git a/common/components/SearchRecommend.tsx b/common/components/SearchRecommend.tsx new file mode 100644 index 0000000..a0e2bed --- /dev/null +++ b/common/components/SearchRecommend.tsx @@ -0,0 +1,59 @@ +import { Tag, Grid } from '@geist-ui/react' +import { useEffect, useMemo, useState } from 'react' +import { EVM_BOX_PERSIST } from '../constants' +import { useChain } from '../hooks/useChain' + +const ChainIdMap = { + BSC: '56', + HECO: '128', + xDAI: '100', + FTM: '250', +} +const DEFAULT_RECOMMEND = [ + ChainIdMap.BSC, + ChainIdMap.FTM, + ChainIdMap.HECO, + // ChainIdMap.xDAI, +] + +export const useRecommend = (chains: Chain[]): Chain[] => { + const [recommends, setRecommends] = useState>(DEFAULT_RECOMMEND) + const recommendChains = useMemo(() => { + return recommends.map(item => chains.find(chain => String(chain.chainId) === item)).filter(Boolean) + }, [recommends]) + + useEffect(() => { + const prevSelect = window.localStorage.getItem(EVM_BOX_PERSIST) || '' + + setRecommends([...prevSelect.split(','), ...DEFAULT_RECOMMEND]) + }, []) + + return recommendChains as Chain[] +} + +export const SearchRecommend: React.FC<{ chains: Chain[] }> = ({ chains }) => { + const recommends = useRecommend(chains) + const [, addEthChain] = useChain() + + return ( +
+ + + {recommends.length > DEFAULT_RECOMMEND.length ? 'Recent' : 'HOT'}: + + { + recommends.filter(Boolean).splice(0, 2).map((chain: Chain) => ( + + addEthChain(chain)} >{chain.chain}({chain.network}) + ), + ) + } + + +
+ ) +} diff --git a/common/components/index.ts b/common/components/index.ts index 5822eab..e417c17 100644 --- a/common/components/index.ts +++ b/common/components/index.ts @@ -1,2 +1,3 @@ export * from './ChainItem' export * from './GithubCorner' +export * from './SearchRecommend' diff --git a/common/constants.ts b/common/constants.ts new file mode 100644 index 0000000..4fec783 --- /dev/null +++ b/common/constants.ts @@ -0,0 +1 @@ +export const EVM_BOX_PERSIST = 'evm_box_persist' diff --git a/common/hooks/useChain.tsx b/common/hooks/useChain.tsx index a9995a5..64aeaef 100644 --- a/common/hooks/useChain.tsx +++ b/common/hooks/useChain.tsx @@ -1,6 +1,7 @@ import { useToasts } from '@geist-ui/react' import { utils } from 'ethers' import { useEffect, useState } from 'react' +import { EVM_BOX_PERSIST } from '../constants' import { useDApp } from './useDApp' const { hexValue } = utils @@ -34,6 +35,9 @@ export const useChain = (): [number | undefined, (chain: Chain) => void] => { setToast({ text: 'add network successfully!', }) + const prev = window.localStorage.getItem(EVM_BOX_PERSIST) + const persist = [chain.chainId, prev].filter(Boolean).join(',') + window.localStorage.setItem(EVM_BOX_PERSIST, persist) }).catch((e: Error) => { setToast({ text: 'add network failed: ' + e.message, diff --git a/pages/index.tsx b/pages/index.tsx index db6d747..3271604 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -8,7 +8,7 @@ import { Search } from '@geist-ui/react-icons' import { GetStaticProps } from 'next' import { FormEventHandler, useState } from 'react' import debounce from 'lodash/debounce' -import { GithubCorner, ChainItem } from '../common/components' +import { GithubCorner, ChainItem, SearchRecommend } from '../common/components' interface HomeProps { chains: Chain[] @@ -16,6 +16,7 @@ interface HomeProps { export const Home: React.FC = ({ chains }) => { const [filter, setFilter] = useState(chains) + const [searchFocused, setSearchFocused] = useState(false) const searchNetwork: FormEventHandler = (e) => { const searchContent = (e.target as HTMLInputElement).value.trim() @@ -50,7 +51,16 @@ export const Home: React.FC = ({ chains }) => {

EVM Box

Use Your favorite EVM Compatible Network

- } placeholder="Search Network" onChange={onSearch} clearable /> + } + onFocus={() => setSearchFocused(true)} + onBlur={() => setSearchFocused(false)} + onChange={onSearch} + clearable + /> + {!searchFocused && }