Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoYhun committed Jul 27, 2023
1 parent f15b23a commit 1925b94
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 51 deletions.
5 changes: 1 addition & 4 deletions src/components/SearchModal/CurrencyList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,6 @@ export function CurrencyRow({
return false
}

// if (isTokenNative(currency, currency.chainId)) {
// return !!favoriteTokens.includeNativeToken
// }

if (currency.isToken) {
const addr = (currency as Token).address ?? ''
const addresses = favoriteTokens ?? []
Expand All @@ -148,6 +144,7 @@ export function CurrencyRow({

return false
})()

const balanceComponent = hideBalance ? (
'******'
) : currencyBalance ? (
Expand Down
3 changes: 1 addition & 2 deletions src/components/SearchModal/CurrencySearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ const ButtonClear = styled.div`
gap: 5px;
cursor: pointer;
`
// const MAX_FAVORITE_PAIR = 12

interface CurrencySearchProps {
isOpen: boolean
Expand Down Expand Up @@ -169,7 +168,6 @@ export function CurrencySearch({
const tokenComparator = useTokenComparator(false, customChainId)

const [commonTokens, setCommonTokens] = useState<(Token | Currency)[]>([])
console.log('🚀 ~ file: CurrencySearch.tsx:172 ~ commonTokens:', commonTokens)
const [loadingCommon, setLoadingCommon] = useState(true)

const tokenImportsFiltered = useMemo(() => {
Expand Down Expand Up @@ -288,6 +286,7 @@ export function CurrencySearch({

if (addressesToFetch.length) {
const tokens = await fetchListTokenByAddresses(addressesToFetch, chainId)
// Sort the returned token list to match the order of the passed address list
result = result.concat(
tokens.sort((x, y) => {
return addressesToFetch.indexOf(x.wrapped.address) - addressesToFetch.indexOf(y.wrapped.address)
Expand Down
23 changes: 16 additions & 7 deletions src/state/user/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ import {
import { CROSS_CHAIN_SETTING_DEFAULT, CrossChainSetting, VIEW_MODE } from 'state/user/reducer'
import { isAddress, isChristmasTime } from 'utils'

const MAX_FAVORITE_LIMIT = 12

function serializeToken(token: Token | WrappedTokenInfo): SerializedToken {
return {
chainId: token.chainId,
Expand Down Expand Up @@ -404,20 +406,27 @@ export const useUserFavoriteTokens = (chainId: ChainId) => {

const favoriteTokens = useMemo(() => {
if (!chainId || !defaultTokens) return undefined
const favoritedToken = favoriteTokensByChainId?.[chainId]
const favoritedTokenAddresses = favoritedToken
? defaultTokens
.filter(address => favoritedToken[address] !== false)
.concat(Object.keys(favoritedToken).filter(address => favoritedToken[address]))
: []
const favoritedTokens = favoriteTokensByChainId?.[chainId] || {}
const favoritedTokenAddresses = defaultTokens
.filter(address => favoritedTokens[address] !== false)
.concat(Object.keys(favoritedTokens).filter(address => favoritedTokens[address]))

return [...new Set(favoritedTokenAddresses)]
}, [chainId, favoriteTokensByChainId, defaultTokens])

const toggleFavoriteToken = useCallback(
(payload: ToggleFavoriteTokenPayload) => {
// Is adding favorite and reached max limit
if (
favoriteTokens &&
favoriteTokens?.indexOf(payload.address) < 0 &&
favoriteTokens.length >= MAX_FAVORITE_LIMIT
) {
return
}
dispatch(toggleFavoriteTokenAction(payload))
},
[dispatch],
[dispatch, favoriteTokens],
)

return { favoriteTokens, toggleFavoriteToken }
Expand Down
51 changes: 13 additions & 38 deletions src/state/user/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,14 @@ interface UserState {
}
>
>
// favoriteTokensByChainIdv2: Partial<
// Record<
// ChainId,
// {
// [address: string]: boolean
// }
// >
// >
favoriteTokensByChainIdv2: any
favoriteTokensByChainIdv2: Partial<
Record<
ChainId,
{
[address: string]: boolean
}
>
>
readonly chainId: ChainId
acceptedTermVersion: number | null
viewMode: VIEW_MODE
Expand Down Expand Up @@ -323,37 +322,13 @@ export default createReducer(initialState, builder =>
state.favoriteTokensByChainId = undefined
}

if (!state.favoriteTokensByChainIdv2[chainId]) {
state.favoriteTokensByChainIdv2[chainId] = {}
}

state.favoriteTokensByChainIdv2[chainId][address] = !state.favoriteTokensByChainIdv2[chainId][address]

// if (!state.favoriteTokensByChainId) {
// state.favoriteTokensByChainId = {}
// }
let favoriteTokens = state.favoriteTokensByChainIdv2[chainId]

// let favoriteTokens = state.favoriteTokensByChainId[chainId]
// if (!favoriteTokens) {
// favoriteTokens = defaultCommonTokens
// state.favoriteTokensByChainId[chainId] = favoriteTokens
// }

// if (isNative) {
// const previousValue = favoriteTokens.includeNativeToken
// favoriteTokens.includeNativeToken = !previousValue
// return
// }
if (!favoriteTokens) {
favoriteTokens = {}
}

// if (address) {
// // this is intentionally added, to remove compiler error
// const index = favoriteTokens.addresses.findIndex(addr => addr === address)
// if (index === -1) {
// favoriteTokens.addresses.push(address)
// return
// }
// favoriteTokens.addresses.splice(index, 1)
// }
favoriteTokens[address] = !favoriteTokens[address]
})
.addCase(updateChainId, (state, { payload: chainId }) => {
state.chainId = chainId
Expand Down

0 comments on commit 1925b94

Please sign in to comment.