diff --git a/apps/cowswap-frontend/src/modules/tokensList/containers/AutoImportTokens/index.tsx b/apps/cowswap-frontend/src/modules/tokensList/containers/AutoImportTokens/index.tsx index 7f2cd088fc..589754849c 100644 --- a/apps/cowswap-frontend/src/modules/tokensList/containers/AutoImportTokens/index.tsx +++ b/apps/cowswap-frontend/src/modules/tokensList/containers/AutoImportTokens/index.tsx @@ -1,7 +1,7 @@ import { useEffect, useMemo, useState } from 'react' import { isTruthy } from '@cowprotocol/common-utils' -import { useImportTokenCallback, useSearchNonExistentToken } from '@cowprotocol/tokens' +import { useAddUserToken, useSearchNonExistentToken } from '@cowprotocol/tokens' import { Nullish } from 'types' @@ -17,7 +17,7 @@ export interface AutoImportTokensProps { export function AutoImportTokens({ inputToken, outputToken }: AutoImportTokensProps) { const [isModalOpen, setIsModalOpen] = useState(false) - const importTokenCallback = useImportTokenCallback() + const importTokenCallback = useAddUserToken() const foundInputToken = useSearchNonExistentToken(inputToken || null) const foundOutputToken = useSearchNonExistentToken(outputToken || null) diff --git a/apps/cowswap-frontend/src/modules/tokensList/containers/ManageLists/index.cosmos.tsx b/apps/cowswap-frontend/src/modules/tokensList/containers/ManageLists/index.cosmos.tsx index 1c51ec5c0f..c18576bc92 100644 --- a/apps/cowswap-frontend/src/modules/tokensList/containers/ManageLists/index.cosmos.tsx +++ b/apps/cowswap-frontend/src/modules/tokensList/containers/ManageLists/index.cosmos.tsx @@ -7,6 +7,9 @@ import { importListsMock, listsMock } from '../../mocks' import { ManageLists } from './index' const Wrapper = styled.div` + max-height: 90vh; + margin: 20px auto; + display: flex; width: 450px; background: var(${UI.COLOR_CONTAINER_BG_01}); ` diff --git a/apps/cowswap-frontend/src/modules/tokensList/containers/ManageLists/index.tsx b/apps/cowswap-frontend/src/modules/tokensList/containers/ManageLists/index.tsx index 4f738515ab..a4d6887582 100644 --- a/apps/cowswap-frontend/src/modules/tokensList/containers/ManageLists/index.tsx +++ b/apps/cowswap-frontend/src/modules/tokensList/containers/ManageLists/index.tsx @@ -1,12 +1,6 @@ import { useMemo } from 'react' -import { - ListSearchResponse, - ListState, - useListsEnabledState, - useRemoveTokenList, - useToggleListCallback, -} from '@cowprotocol/tokens' +import { ListSearchResponse, ListState, useListsEnabledState, useRemoveList, useToggleList } from '@cowprotocol/tokens' import * as styledEl from './styled' @@ -30,8 +24,8 @@ export function ManageLists(props: ManageListsProps) { const activeTokenListsIds = useListsEnabledState() const addListImport = useAddListImport() - const removeCustomTokenLists = useRemoveTokenList() - const toggleList = useToggleListCallback() + const removeList = useRemoveList() + const toggleList = useToggleList() const { source, listToImport } = useListSearchResponse(listSearchResponse) @@ -55,7 +49,7 @@ export function ManageLists(props: ManageListsProps) { key={list.id} list={list} enabled={!!activeTokenListsIds[list.id]} - removeList={removeCustomTokenLists} + removeList={removeList} toggleList={toggleList} /> ))} diff --git a/apps/cowswap-frontend/src/modules/tokensList/containers/ManageListsAndTokens/index.cosmos.tsx b/apps/cowswap-frontend/src/modules/tokensList/containers/ManageListsAndTokens/index.cosmos.tsx index 7c56f19572..2d6eced752 100644 --- a/apps/cowswap-frontend/src/modules/tokensList/containers/ManageListsAndTokens/index.cosmos.tsx +++ b/apps/cowswap-frontend/src/modules/tokensList/containers/ManageListsAndTokens/index.cosmos.tsx @@ -5,6 +5,9 @@ import { customTokensMock, listsMock } from '../../mocks' import { ManageListsAndTokens } from './index' const Wrapper = styled.div` + max-height: 90vh; + margin: 20px auto; + display: flex; width: 450px; ` diff --git a/apps/cowswap-frontend/src/modules/tokensList/containers/ManageTokens/index.tsx b/apps/cowswap-frontend/src/modules/tokensList/containers/ManageTokens/index.tsx index 335193cfda..c91cd63a5f 100644 --- a/apps/cowswap-frontend/src/modules/tokensList/containers/ManageTokens/index.tsx +++ b/apps/cowswap-frontend/src/modules/tokensList/containers/ManageTokens/index.tsx @@ -2,7 +2,7 @@ import { useMemo } from 'react' import { TokenWithLogo } from '@cowprotocol/common-const' import { ExplorerDataType, getExplorerLink } from '@cowprotocol/common-utils' -import { TokenLogo, TokenSearchResponse, useRemoveTokenCallback, useResetUserTokensCallback } from '@cowprotocol/tokens' +import { TokenLogo, TokenSearchResponse, useRemoveUserToken, useResetUserTokens } from '@cowprotocol/tokens' import { TokenSymbol } from '@cowprotocol/ui' import { ExternalLink, Trash } from 'react-feather' @@ -30,8 +30,8 @@ export function ManageTokens(props: ManageTokensProps) { const { tokens, tokenSearchResponse } = props const addTokenImportCallback = useAddTokenImportCallback() - const removeTokenCallback = useRemoveTokenCallback() - const resetUserTokensCallback = useResetUserTokensCallback() + const removeTokenCallback = useRemoveUserToken() + const resetUserTokensCallback = useResetUserTokens() const { inactiveListsResult, blockchainResult, activeListsResult, externalApiResult } = tokenSearchResponse diff --git a/apps/cowswap-frontend/src/modules/tokensList/containers/SelectTokenWidget/index.tsx b/apps/cowswap-frontend/src/modules/tokensList/containers/SelectTokenWidget/index.tsx index 1114c35210..ac43ba2669 100644 --- a/apps/cowswap-frontend/src/modules/tokensList/containers/SelectTokenWidget/index.tsx +++ b/apps/cowswap-frontend/src/modules/tokensList/containers/SelectTokenWidget/index.tsx @@ -1,26 +1,30 @@ import { useAtomValue, useSetAtom } from 'jotai' import { useCallback, useState } from 'react' +import { addListAnalytics } from '@cowprotocol/analytics' import { TokenWithLogo } from '@cowprotocol/common-const' import { + getTokenListSource, ListState, - useAddCustomTokenLists, + useAddList, useAllListsList, useAllTokens, useFavouriteTokens, - useImportTokenCallback, + useAddUserToken, useUserAddedTokens, + useUnsupportedTokens, } from '@cowprotocol/tokens' import styled from 'styled-components/macro' import { CowModal } from 'common/pure/Modal' +import { useAllTokensBalances } from '../../hooks/useAllTokensBalances' import { ImportListModal } from '../../pure/ImportListModal' import { ImportTokenModal } from '../../pure/ImportTokenModal' +import { SelectTokenModal } from '../../pure/SelectTokenModal' import { selectTokenWidgetAtom, updateSelectTokenWidgetAtom } from '../../state/selectTokenWidgetAtom' import { ManageListsAndTokens } from '../ManageListsAndTokens' -import { SelectTokenModal } from '../SelectTokenModal' const Wrapper = styled.div` width: 100%; @@ -30,22 +34,21 @@ const Wrapper = styled.div` } ` -// TODO: remove mock -const balancesMock = {} - export function SelectTokenWidget() { const { open, onSelectToken, tokenToImport, listToImport, selectedToken } = useAtomValue(selectTokenWidgetAtom) const [isManageWidgetOpen, setIsManageWidgetOpen] = useState(false) const updateSelectTokenWidget = useSetAtom(updateSelectTokenWidgetAtom) - const addCustomTokenLists = useAddCustomTokenLists() - const importTokenCallback = useImportTokenCallback() + const addCustomTokenLists = useAddList() + const importTokenCallback = useAddUserToken() const allTokens = useAllTokens() const favouriteTokens = useFavouriteTokens() const userAddedTokens = useUserAddedTokens() const allTokenLists = useAllListsList() + const [balances, balancesLoading] = useAllTokensBalances() + const unsupportedTokens = useUnsupportedTokens() const closeTokenSelectWidget = useCallback(() => { updateSelectTokenWidget({ @@ -76,6 +79,7 @@ export function SelectTokenWidget() { const importListAndBack = (list: ListState) => { addCustomTokenLists(list) updateSelectTokenWidget({ listToImport: undefined }) + addListAnalytics('Success', getTokenListSource(list.source)) } if (!onSelectToken) return null @@ -119,10 +123,12 @@ export function SelectTokenWidget() { return ( setIsManageWidgetOpen(true)} diff --git a/apps/cowswap-frontend/src/modules/tokensList/hooks/useAddListImport.ts b/apps/cowswap-frontend/src/modules/tokensList/hooks/useAddListImport.ts index e643add5af..f07ba5bcf2 100644 --- a/apps/cowswap-frontend/src/modules/tokensList/hooks/useAddListImport.ts +++ b/apps/cowswap-frontend/src/modules/tokensList/hooks/useAddListImport.ts @@ -1,4 +1,4 @@ -import { useSetAtom } from 'jotai/index' +import { useSetAtom } from 'jotai' import { useCallback } from 'react' import { ListState } from '@cowprotocol/tokens' diff --git a/apps/cowswap-frontend/src/modules/tokensList/hooks/useAddTokenImportCallback.ts b/apps/cowswap-frontend/src/modules/tokensList/hooks/useAddTokenImportCallback.ts index d4be213851..e2e320c968 100644 --- a/apps/cowswap-frontend/src/modules/tokensList/hooks/useAddTokenImportCallback.ts +++ b/apps/cowswap-frontend/src/modules/tokensList/hooks/useAddTokenImportCallback.ts @@ -1,4 +1,4 @@ -import { useSetAtom } from 'jotai/index' +import { useSetAtom } from 'jotai' import { useCallback } from 'react' import { TokenWithLogo } from '@cowprotocol/common-const' diff --git a/apps/cowswap-frontend/src/modules/tokensList/pure/ListItem/index.tsx b/apps/cowswap-frontend/src/modules/tokensList/pure/ListItem/index.tsx index 2fa71f4657..ed935df2d8 100644 --- a/apps/cowswap-frontend/src/modules/tokensList/pure/ListItem/index.tsx +++ b/apps/cowswap-frontend/src/modules/tokensList/pure/ListItem/index.tsx @@ -14,8 +14,8 @@ import { TokenListDetails } from '../TokenListDetails' export interface TokenListItemProps { list: ListState enabled: boolean - toggleList(id: string): void - removeList(id: string): void + toggleList(list: ListState, enabled: boolean): void + removeList(list: ListState): void } export function ListItem(props: TokenListItemProps) { @@ -24,7 +24,7 @@ export function ListItem(props: TokenListItemProps) { const [isActive, setIsActive] = useState(enabled) const toggle = () => { - toggleList(list.id) + toggleList(list, enabled) setIsActive((state) => !state) } @@ -50,7 +50,7 @@ export function ListItem(props: TokenListItemProps) { - removeList(list.id)}> + removeList(list)}> Remove list diff --git a/apps/cowswap-frontend/src/modules/tokensList/containers/SelectTokenModal/index.cosmos.tsx b/apps/cowswap-frontend/src/modules/tokensList/pure/SelectTokenModal/index.cosmos.tsx similarity index 73% rename from apps/cowswap-frontend/src/modules/tokensList/containers/SelectTokenModal/index.cosmos.tsx rename to apps/cowswap-frontend/src/modules/tokensList/pure/SelectTokenModal/index.cosmos.tsx index fa8976af10..42aca59f5c 100644 --- a/apps/cowswap-frontend/src/modules/tokensList/containers/SelectTokenModal/index.cosmos.tsx +++ b/apps/cowswap-frontend/src/modules/tokensList/pure/SelectTokenModal/index.cosmos.tsx @@ -1,32 +1,43 @@ import { getRandomInt } from '@cowprotocol/common-utils' import { CurrencyAmount } from '@uniswap/sdk-core' -import { Currency } from '@uniswap/sdk-core' import styled from 'styled-components/macro' +import { TokenAmounts } from 'modules/tokens' + import { allTokensMock, favouriteTokensMock } from '../../mocks' import { SelectTokenModal, SelectTokenModalProps } from './index' const Wrapper = styled.div` + max-height: 90vh; + margin: 20px auto; + display: flex; width: 450px; ` +const unsupportedTokens = {} + const selectedToken = favouriteTokensMock[0].address -const balances = allTokensMock.reduce<{ [key: string]: CurrencyAmount }>((acc, token) => { - acc[token.address.toLowerCase()] = CurrencyAmount.fromRawAmount( - token, - getRandomInt(20_000, 120_000_000) * 10 ** token.decimals - ) +const balances = allTokensMock.reduce((acc, token) => { + acc[token.address] = { + value: CurrencyAmount.fromRawAmount(token, getRandomInt(20_000, 120_000_000) * 10 ** token.decimals), + loading: false, + syncing: false, + valid: true, + error: false, + } return acc }, {}) const defaultProps: SelectTokenModalProps = { + unsupportedTokens, allTokens: allTokensMock, favouriteTokens: favouriteTokensMock, balances, + balancesLoading: false, selectedToken, onSelectToken() { console.log('onSelectToken') @@ -52,7 +63,7 @@ const Fixtures = { ), NoTokenFound: ( - + ), searchFromInactiveLists: ( diff --git a/apps/cowswap-frontend/src/modules/tokensList/containers/SelectTokenModal/index.tsx b/apps/cowswap-frontend/src/modules/tokensList/pure/SelectTokenModal/index.tsx similarity index 80% rename from apps/cowswap-frontend/src/modules/tokensList/containers/SelectTokenModal/index.tsx rename to apps/cowswap-frontend/src/modules/tokensList/pure/SelectTokenModal/index.tsx index b8b5d72c91..c6d73ddc52 100644 --- a/apps/cowswap-frontend/src/modules/tokensList/containers/SelectTokenModal/index.tsx +++ b/apps/cowswap-frontend/src/modules/tokensList/pure/SelectTokenModal/index.tsx @@ -1,24 +1,25 @@ import { useState } from 'react' import { TokenWithLogo } from '@cowprotocol/common-const' -import { useUnsupportedTokens } from '@cowprotocol/tokens' -import { Currency, CurrencyAmount } from '@uniswap/sdk-core' +import { UnsupportedTokensState } from '@cowprotocol/tokens' import { Edit, X } from 'react-feather' import * as styledEl from './styled' -import { useAllTokensBalances } from '../../hooks/useAllTokensBalances' -import { IconButton } from '../../pure/commonElements' -import { FavouriteTokensList } from '../../pure/FavouriteTokensList' -import { TokensVirtualList } from '../../pure/TokensVirtualList' +import { TokenAmounts } from '../../../tokens' +import { TokenSearchResults } from '../../containers/TokenSearchResults' import { SelectTokenContext } from '../../types' -import { TokenSearchResults } from '../TokenSearchResults' +import { IconButton } from '../commonElements' +import { FavouriteTokensList } from '../FavouriteTokensList' +import { TokensVirtualList } from '../TokensVirtualList' export interface SelectTokenModalProps { allTokens: TokenWithLogo[] favouriteTokens: TokenWithLogo[] - balances: { [key: string]: CurrencyAmount } + balances: TokenAmounts + unsupportedTokens: UnsupportedTokensState + balancesLoading: boolean selectedToken?: string onSelectToken(token: TokenWithLogo): void defaultInputValue?: string @@ -34,6 +35,9 @@ export function SelectTokenModal(props: SelectTokenModalProps) { favouriteTokens, allTokens, selectedToken, + balances, + balancesLoading, + unsupportedTokens, onSelectToken, onDismiss, onOpenManageWidget, @@ -42,10 +46,6 @@ export function SelectTokenModal(props: SelectTokenModalProps) { const [inputValue, setInputValue] = useState(defaultInputValue) const [isEnterPressed, setIsEnterPressed] = useState(false) - const unsupportedTokens = useUnsupportedTokens() - - const [balances, balancesLoading] = useAllTokensBalances() - const handleEnterPress = () => { setIsEnterPressed(true) setTimeout(() => setIsEnterPressed(false), 100) diff --git a/apps/cowswap-frontend/src/modules/tokensList/containers/SelectTokenModal/styled.ts b/apps/cowswap-frontend/src/modules/tokensList/pure/SelectTokenModal/styled.ts similarity index 95% rename from apps/cowswap-frontend/src/modules/tokensList/containers/SelectTokenModal/styled.ts rename to apps/cowswap-frontend/src/modules/tokensList/pure/SelectTokenModal/styled.ts index 8ad976c82a..980c6c1c47 100644 --- a/apps/cowswap-frontend/src/modules/tokensList/containers/SelectTokenModal/styled.ts +++ b/apps/cowswap-frontend/src/modules/tokensList/pure/SelectTokenModal/styled.ts @@ -2,7 +2,7 @@ import styled from 'styled-components/macro' import { UI } from 'common/constants/theme' -import { blankButtonMixin } from '../../pure/commonElements' +import { blankButtonMixin } from '../commonElements' export const Wrapper = styled.div` display: flex; diff --git a/libs/tokens/src/hooks/lists/useAddList.ts b/libs/tokens/src/hooks/lists/useAddList.ts new file mode 100644 index 0000000000..a8fddaa3d7 --- /dev/null +++ b/libs/tokens/src/hooks/lists/useAddList.ts @@ -0,0 +1,6 @@ +import { useSetAtom } from 'jotai' +import { addListAtom } from '../../state/tokenLists/tokenListsActionsAtom' + +export function useAddList() { + return useSetAtom(addListAtom) +} diff --git a/libs/tokens/src/hooks/useAllListsList.ts b/libs/tokens/src/hooks/lists/useAllListsList.ts similarity index 52% rename from libs/tokens/src/hooks/useAllListsList.ts rename to libs/tokens/src/hooks/lists/useAllListsList.ts index ee51e172fd..8bf8c36228 100644 --- a/libs/tokens/src/hooks/useAllListsList.ts +++ b/libs/tokens/src/hooks/lists/useAllListsList.ts @@ -1,6 +1,6 @@ import { useAtomValue } from 'jotai' -import { listsStatesListAtom } from '../state/tokenLists/tokenListsStateAtom' -import { ListState } from '../types' +import { listsStatesListAtom } from '../../state/tokenLists/tokenListsStateAtom' +import { ListState } from '../../types' export function useAllListsList(): ListState[] { return useAtomValue(listsStatesListAtom) diff --git a/libs/tokens/src/hooks/useListsEnabledState.ts b/libs/tokens/src/hooks/lists/useListsEnabledState.ts similarity index 52% rename from libs/tokens/src/hooks/useListsEnabledState.ts rename to libs/tokens/src/hooks/lists/useListsEnabledState.ts index 305fb01967..937b0b078c 100644 --- a/libs/tokens/src/hooks/useListsEnabledState.ts +++ b/libs/tokens/src/hooks/lists/useListsEnabledState.ts @@ -1,6 +1,6 @@ import { useAtomValue } from 'jotai' -import { listsEnabledStateAtom } from '../state/tokenLists/tokenListsStateAtom' -import { ListsEnabledState } from '../types' +import { listsEnabledStateAtom } from '../../state/tokenLists/tokenListsStateAtom' +import { ListsEnabledState } from '../../types' export function useListsEnabledState(): ListsEnabledState { return useAtomValue(listsEnabledStateAtom) diff --git a/libs/tokens/src/hooks/lists/useRemoveList.ts b/libs/tokens/src/hooks/lists/useRemoveList.ts new file mode 100644 index 0000000000..a91a39ba5d --- /dev/null +++ b/libs/tokens/src/hooks/lists/useRemoveList.ts @@ -0,0 +1,15 @@ +import { useSetAtom } from 'jotai' +import { removeListAnalytics } from '@cowprotocol/analytics' + +import { removeListAtom } from '../../state/tokenLists/tokenListsActionsAtom' +import { ListState } from '../../types' +import { getTokenListSource } from '../../utils/getTokenListSource' + +export function useRemoveList() { + const removeList = useSetAtom(removeListAtom) + + return (list: ListState) => { + removeList(list.id) + removeListAnalytics('Confirm', getTokenListSource(list.source)) + } +} diff --git a/libs/tokens/src/hooks/useSearchList.ts b/libs/tokens/src/hooks/lists/useSearchList.ts similarity index 83% rename from libs/tokens/src/hooks/useSearchList.ts rename to libs/tokens/src/hooks/lists/useSearchList.ts index 6f5f9c68d3..57e28bac4e 100644 --- a/libs/tokens/src/hooks/useSearchList.ts +++ b/libs/tokens/src/hooks/lists/useSearchList.ts @@ -1,11 +1,11 @@ import useSWR, { SWRResponse } from 'swr' -import { fetchTokenList } from '../services/fetchTokenList' +import { fetchTokenList } from '../../services/fetchTokenList' import { parseENSAddress } from '@cowprotocol/common-utils' import { useAtomValue } from 'jotai' -import { allListsSourcesAtom, listsStatesMapAtom } from '../state/tokenLists/tokenListsStateAtom' +import { allListsSourcesAtom, listsStatesMapAtom } from '../../state/tokenLists/tokenListsStateAtom' import { useMemo } from 'react' -import { getIsTokenListWithUrl } from '../utils/getIsTokenListWithUrl' -import { ListState } from '../types' +import { getIsTokenListWithUrl } from '../../utils/getIsTokenListWithUrl' +import { ListState } from '../../types' export type ListSearchResponse = | { diff --git a/libs/tokens/src/hooks/lists/useToggleList.ts b/libs/tokens/src/hooks/lists/useToggleList.ts new file mode 100644 index 0000000000..66f0908f04 --- /dev/null +++ b/libs/tokens/src/hooks/lists/useToggleList.ts @@ -0,0 +1,14 @@ +import { useSetAtom } from 'jotai' +import { toggleListAtom } from '../../state/tokenLists/tokenListsActionsAtom' +import { toggleListAnalytics } from '@cowprotocol/analytics' +import { ListState } from '../../types' +import { getTokenListSource } from '../../utils/getTokenListSource' + +export function useToggleList() { + const toggleList = useSetAtom(toggleListAtom) + + return (list: ListState, enabled: boolean) => { + toggleList(list.id) + toggleListAnalytics(enabled, getTokenListSource(list.source)) + } +} diff --git a/libs/tokens/src/hooks/useFavouriteTokens.ts b/libs/tokens/src/hooks/tokens/favourite/useFavouriteTokens.ts similarity index 70% rename from libs/tokens/src/hooks/useFavouriteTokens.ts rename to libs/tokens/src/hooks/tokens/favourite/useFavouriteTokens.ts index 4ffae1d333..1143919f11 100644 --- a/libs/tokens/src/hooks/useFavouriteTokens.ts +++ b/libs/tokens/src/hooks/tokens/favourite/useFavouriteTokens.ts @@ -1,5 +1,5 @@ import { useAtomValue } from 'jotai' -import { favouriteTokensListAtom } from '../state/tokens/favouriteTokensAtom' +import { favouriteTokensListAtom } from '../../../state/tokens/favouriteTokensAtom' import { TokenWithLogo } from '@cowprotocol/common-const' export function useFavouriteTokens(): TokenWithLogo[] { diff --git a/libs/tokens/src/hooks/useResetFavouriteTokens.ts b/libs/tokens/src/hooks/tokens/favourite/useResetFavouriteTokens.ts similarity index 60% rename from libs/tokens/src/hooks/useResetFavouriteTokens.ts rename to libs/tokens/src/hooks/tokens/favourite/useResetFavouriteTokens.ts index 0c085cd2bf..6b64480894 100644 --- a/libs/tokens/src/hooks/useResetFavouriteTokens.ts +++ b/libs/tokens/src/hooks/tokens/favourite/useResetFavouriteTokens.ts @@ -1,5 +1,5 @@ import { useSetAtom } from 'jotai' -import { resetFavouriteTokensAtom } from '../state/tokens/favouriteTokensAtom' +import { resetFavouriteTokensAtom } from '../../../state/tokens/favouriteTokensAtom' export function useResetFavouriteTokens() { return useSetAtom(resetFavouriteTokensAtom) diff --git a/libs/tokens/src/hooks/useToggleFavouriteToken.ts b/libs/tokens/src/hooks/tokens/favourite/useToggleFavouriteToken.ts similarity index 60% rename from libs/tokens/src/hooks/useToggleFavouriteToken.ts rename to libs/tokens/src/hooks/tokens/favourite/useToggleFavouriteToken.ts index beb5bbd103..e0c389f896 100644 --- a/libs/tokens/src/hooks/useToggleFavouriteToken.ts +++ b/libs/tokens/src/hooks/tokens/favourite/useToggleFavouriteToken.ts @@ -1,5 +1,5 @@ import { useSetAtom } from 'jotai' -import { toggleFavouriteTokenAtom } from '../state/tokens/favouriteTokensAtom' +import { toggleFavouriteTokenAtom } from '../../../state/tokens/favouriteTokensAtom' export function useToggleFavouriteToken() { return useSetAtom(toggleFavouriteTokenAtom) diff --git a/libs/tokens/src/hooks/useAddUnsupportedToken.ts b/libs/tokens/src/hooks/tokens/unsupported/useAddUnsupportedToken.ts similarity index 59% rename from libs/tokens/src/hooks/useAddUnsupportedToken.ts rename to libs/tokens/src/hooks/tokens/unsupported/useAddUnsupportedToken.ts index f3247cbb4f..1becb6a336 100644 --- a/libs/tokens/src/hooks/useAddUnsupportedToken.ts +++ b/libs/tokens/src/hooks/tokens/unsupported/useAddUnsupportedToken.ts @@ -1,5 +1,5 @@ import { useSetAtom } from 'jotai' -import { addUnsupportedTokenAtom } from '../state/tokens/unsupportedTokensAtom' +import { addUnsupportedTokenAtom } from '../../../state/tokens/unsupportedTokensAtom' export function useAddUnsupportedToken() { return useSetAtom(addUnsupportedTokenAtom) diff --git a/libs/tokens/src/hooks/useIsTradeUnsupported.ts b/libs/tokens/src/hooks/tokens/unsupported/useIsTradeUnsupported.ts similarity index 100% rename from libs/tokens/src/hooks/useIsTradeUnsupported.ts rename to libs/tokens/src/hooks/tokens/unsupported/useIsTradeUnsupported.ts diff --git a/libs/tokens/src/hooks/useIsUnsupportedToken.ts b/libs/tokens/src/hooks/tokens/unsupported/useIsUnsupportedToken.ts similarity index 100% rename from libs/tokens/src/hooks/useIsUnsupportedToken.ts rename to libs/tokens/src/hooks/tokens/unsupported/useIsUnsupportedToken.ts diff --git a/libs/tokens/src/hooks/useIsUnsupportedTokens.ts b/libs/tokens/src/hooks/tokens/unsupported/useIsUnsupportedTokens.ts similarity index 100% rename from libs/tokens/src/hooks/useIsUnsupportedTokens.ts rename to libs/tokens/src/hooks/tokens/unsupported/useIsUnsupportedTokens.ts diff --git a/libs/tokens/src/hooks/useRemoveUnsupportedToken.ts b/libs/tokens/src/hooks/tokens/unsupported/useRemoveUnsupportedToken.ts similarity index 59% rename from libs/tokens/src/hooks/useRemoveUnsupportedToken.ts rename to libs/tokens/src/hooks/tokens/unsupported/useRemoveUnsupportedToken.ts index 3f0f8a1fa8..373bc112d0 100644 --- a/libs/tokens/src/hooks/useRemoveUnsupportedToken.ts +++ b/libs/tokens/src/hooks/tokens/unsupported/useRemoveUnsupportedToken.ts @@ -1,5 +1,5 @@ import { useSetAtom } from 'jotai' -import { removeUnsupportedTokenAtom } from '../state/tokens/unsupportedTokensAtom' +import { removeUnsupportedTokenAtom } from '../../../state/tokens/unsupportedTokensAtom' export function useRemoveUnsupportedToken() { return useSetAtom(removeUnsupportedTokenAtom) diff --git a/libs/tokens/src/hooks/useUnsupportedTokens.ts b/libs/tokens/src/hooks/tokens/unsupported/useUnsupportedTokens.ts similarity index 59% rename from libs/tokens/src/hooks/useUnsupportedTokens.ts rename to libs/tokens/src/hooks/tokens/unsupported/useUnsupportedTokens.ts index de9fa8ce26..319109d5d9 100644 --- a/libs/tokens/src/hooks/useUnsupportedTokens.ts +++ b/libs/tokens/src/hooks/tokens/unsupported/useUnsupportedTokens.ts @@ -1,5 +1,5 @@ import { useAtomValue } from 'jotai' -import { currentUnsupportedTokensAtom } from '../state/tokens/unsupportedTokensAtom' +import { currentUnsupportedTokensAtom } from '../../../state/tokens/unsupportedTokensAtom' export function useUnsupportedTokens() { return useAtomValue(currentUnsupportedTokensAtom) diff --git a/libs/tokens/src/hooks/useAllTokens.ts b/libs/tokens/src/hooks/tokens/useAllTokens.ts similarity index 73% rename from libs/tokens/src/hooks/useAllTokens.ts rename to libs/tokens/src/hooks/tokens/useAllTokens.ts index acc858c0e3..4a03ee6551 100644 --- a/libs/tokens/src/hooks/useAllTokens.ts +++ b/libs/tokens/src/hooks/tokens/useAllTokens.ts @@ -1,5 +1,5 @@ import { useAtomValue } from 'jotai' -import { activeTokensAtom } from '../state/tokens/tokensAtom' +import { activeTokensAtom } from '../../state/tokens/allTokensAtom' import { TokenWithLogo } from '@cowprotocol/common-const' diff --git a/libs/tokens/src/hooks/useAreThereTokensWithSameSymbol.ts b/libs/tokens/src/hooks/tokens/useAreThereTokensWithSameSymbol.ts similarity index 89% rename from libs/tokens/src/hooks/useAreThereTokensWithSameSymbol.ts rename to libs/tokens/src/hooks/tokens/useAreThereTokensWithSameSymbol.ts index b61c1e2171..b014d95095 100644 --- a/libs/tokens/src/hooks/useAreThereTokensWithSameSymbol.ts +++ b/libs/tokens/src/hooks/tokens/useAreThereTokensWithSameSymbol.ts @@ -2,7 +2,7 @@ import { useAtomValue } from 'jotai' import { useCallback } from 'react' import { isAddress } from '@cowprotocol/common-utils' -import { tokensBySymbolAtom } from '../state/tokens/tokensAtom' +import { tokensBySymbolAtom } from '../../state/tokens/allTokensAtom' export function useAreThereTokensWithSameSymbol(): (tokenAddressOrSymbol: string | null | undefined) => boolean { const tokensBySymbol = useAtomValue(tokensBySymbolAtom) diff --git a/libs/tokens/src/hooks/useSearchNonExistentToken.ts b/libs/tokens/src/hooks/tokens/useSearchNonExistentToken.ts similarity index 93% rename from libs/tokens/src/hooks/useSearchNonExistentToken.ts rename to libs/tokens/src/hooks/tokens/useSearchNonExistentToken.ts index 883470624a..b592066ed6 100644 --- a/libs/tokens/src/hooks/useSearchNonExistentToken.ts +++ b/libs/tokens/src/hooks/tokens/useSearchNonExistentToken.ts @@ -4,7 +4,7 @@ import { useMemo } from 'react' import { TokenWithLogo } from '@cowprotocol/common-const' import { isAddress, isTruthy } from '@cowprotocol/common-utils' -import { tokenListsUpdatingAtom } from '../state/tokenLists/tokenListsStateAtom' +import { tokenListsUpdatingAtom } from '../../state/tokenLists/tokenListsStateAtom' import { useTokensByAddressMap } from './useTokensByAddressMap' import { useSearchToken } from './useSearchToken' diff --git a/libs/tokens/src/hooks/useSearchToken.ts b/libs/tokens/src/hooks/tokens/useSearchToken.ts similarity index 90% rename from libs/tokens/src/hooks/useSearchToken.ts rename to libs/tokens/src/hooks/tokens/useSearchToken.ts index 71c2a5377f..0cf4073bbb 100644 --- a/libs/tokens/src/hooks/useSearchToken.ts +++ b/libs/tokens/src/hooks/tokens/useSearchToken.ts @@ -1,18 +1,18 @@ import { useMemo } from 'react' import { useAtomValue } from 'jotai' -import { activeTokensAtom, inactiveTokensAtom } from '../state/tokens/tokensAtom' +import { activeTokensAtom, inactiveTokensAtom } from '../../state/tokens/allTokensAtom' import { useDebounce } from '@cowprotocol/common-hooks' import { useWeb3React } from '@web3-react/core' import { isAddress } from '@cowprotocol/common-utils' import ms from 'ms.macro' -import { getTokenSearchFilter } from '../utils/getTokenSearchFilter' +import { getTokenSearchFilter } from '../../utils/getTokenSearchFilter' import useSWR from 'swr' -import { searchTokensInApi } from '../services/searchTokensInApi' +import { searchTokensInApi } from '../../services/searchTokensInApi' import { TokenWithLogo } from '@cowprotocol/common-const' -import { environmentAtom } from '../state/environmentAtom' -import { parseTokensFromApi } from '../utils/parseTokensFromApi' -import { fetchTokenFromBlockchain } from '../utils/fetchTokenFromBlockchain' -import { tokenWithLogoFromToken } from '../utils/tokenWithLogoFromToken' +import { environmentAtom } from '../../state/environmentAtom' +import { parseTokensFromApi } from '../../utils/parseTokensFromApi' +import { fetchTokenFromBlockchain } from '../../utils/fetchTokenFromBlockchain' +import { tokenWithLogoFromToken } from '../../utils/tokenWithLogoFromToken' const IN_LISTS_DEBOUNCE_TIME = ms`100ms` const IN_EXTERNALS_DEBOUNCE_TIME = ms`1s` diff --git a/libs/tokens/src/hooks/useTokenBySymbolOrAddress.ts b/libs/tokens/src/hooks/tokens/useTokenBySymbolOrAddress.ts similarity index 89% rename from libs/tokens/src/hooks/useTokenBySymbolOrAddress.ts rename to libs/tokens/src/hooks/tokens/useTokenBySymbolOrAddress.ts index 9af4c6d357..efd4a9277c 100644 --- a/libs/tokens/src/hooks/useTokenBySymbolOrAddress.ts +++ b/libs/tokens/src/hooks/tokens/useTokenBySymbolOrAddress.ts @@ -2,7 +2,7 @@ import { useAtomValue } from 'jotai' import { useMemo } from 'react' import { TokenWithLogo } from '@cowprotocol/common-const' -import { tokensByAddressAtom, tokensBySymbolAtom } from '../state/tokens/tokensAtom' +import { tokensByAddressAtom, tokensBySymbolAtom } from '../../state/tokens/allTokensAtom' export function useTokenBySymbolOrAddress(symbolOrAddress?: string | null): TokenWithLogo | null { const tokensByAddress = useAtomValue(tokensByAddressAtom) diff --git a/libs/tokens/src/hooks/useTokensByAddressMap.ts b/libs/tokens/src/hooks/tokens/useTokensByAddressMap.ts similarity index 61% rename from libs/tokens/src/hooks/useTokensByAddressMap.ts rename to libs/tokens/src/hooks/tokens/useTokensByAddressMap.ts index 35df04953f..3aa9fac26f 100644 --- a/libs/tokens/src/hooks/useTokensByAddressMap.ts +++ b/libs/tokens/src/hooks/tokens/useTokensByAddressMap.ts @@ -1,4 +1,4 @@ -import { TokensByAddress, tokensByAddressAtom } from '../state/tokens/tokensAtom' +import { TokensByAddress, tokensByAddressAtom } from '../../state/tokens/allTokensAtom' import { useAtomValue } from 'jotai' export function useTokensByAddressMap(): TokensByAddress { diff --git a/libs/tokens/src/hooks/tokens/userAdded/useAddUserToken.ts b/libs/tokens/src/hooks/tokens/userAdded/useAddUserToken.ts new file mode 100644 index 0000000000..10c74d0046 --- /dev/null +++ b/libs/tokens/src/hooks/tokens/userAdded/useAddUserToken.ts @@ -0,0 +1,7 @@ +import { TokenWithLogo } from '@cowprotocol/common-const' +import { useSetAtom } from 'jotai' +import { addUserTokenAtom } from '../../../state/tokens/userAddedTokensAtom' + +export function useAddUserToken(): (tokens: TokenWithLogo[]) => void { + return useSetAtom(addUserTokenAtom) +} diff --git a/libs/tokens/src/hooks/useRemoveTokenCallback.ts b/libs/tokens/src/hooks/tokens/userAdded/useRemoveUserToken.ts similarity index 59% rename from libs/tokens/src/hooks/useRemoveTokenCallback.ts rename to libs/tokens/src/hooks/tokens/userAdded/useRemoveUserToken.ts index 37846b6e92..27841bf081 100644 --- a/libs/tokens/src/hooks/useRemoveTokenCallback.ts +++ b/libs/tokens/src/hooks/tokens/userAdded/useRemoveUserToken.ts @@ -1,8 +1,8 @@ import { TokenWithLogo } from '@cowprotocol/common-const' import { useSetAtom } from 'jotai' -import { removeUserTokenAtom } from '../state/tokens/userAddedTokensAtom' +import { removeUserTokenAtom } from '../../../state/tokens/userAddedTokensAtom' -export function useRemoveTokenCallback(): (token: TokenWithLogo) => void { +export function useRemoveUserToken(): (token: TokenWithLogo) => void { const removeUserToken = useSetAtom(removeUserTokenAtom) return (token: TokenWithLogo) => { diff --git a/libs/tokens/src/hooks/tokens/userAdded/useResetUserTokens.ts b/libs/tokens/src/hooks/tokens/userAdded/useResetUserTokens.ts new file mode 100644 index 0000000000..41c3f50213 --- /dev/null +++ b/libs/tokens/src/hooks/tokens/userAdded/useResetUserTokens.ts @@ -0,0 +1,6 @@ +import { useSetAtom } from 'jotai' +import { resetUserTokensAtom } from '../../../state/tokens/userAddedTokensAtom' + +export function useResetUserTokens(): () => void { + return useSetAtom(resetUserTokensAtom) +} diff --git a/libs/tokens/src/hooks/useUserAddedTokens.ts b/libs/tokens/src/hooks/tokens/userAdded/useUserAddedTokens.ts similarity index 70% rename from libs/tokens/src/hooks/useUserAddedTokens.ts rename to libs/tokens/src/hooks/tokens/userAdded/useUserAddedTokens.ts index 2ac246db2d..4ee33973e7 100644 --- a/libs/tokens/src/hooks/useUserAddedTokens.ts +++ b/libs/tokens/src/hooks/tokens/userAdded/useUserAddedTokens.ts @@ -1,5 +1,5 @@ import { useAtomValue } from 'jotai' -import { userAddedTokensListAtom } from '../state/tokens/userAddedTokensAtom' +import { userAddedTokensListAtom } from '../../../state/tokens/userAddedTokensAtom' import { TokenWithLogo } from '@cowprotocol/common-const' export function useUserAddedTokens(): TokenWithLogo[] { diff --git a/libs/tokens/src/hooks/useActiveTokenListsIds.ts b/libs/tokens/src/hooks/useActiveTokenListsIds.ts deleted file mode 100644 index 72aa43cd7e..0000000000 --- a/libs/tokens/src/hooks/useActiveTokenListsIds.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { useAtomValue } from 'jotai' -import { activeTokenListsMapAtom } from '../state/tokenLists/tokenListsStateAtom' - -export function useActiveTokenListsIds() { - return useAtomValue(activeTokenListsMapAtom) -} diff --git a/libs/tokens/src/hooks/useAddCustomTokenLists.ts b/libs/tokens/src/hooks/useAddCustomTokenLists.ts deleted file mode 100644 index 30f6c4607a..0000000000 --- a/libs/tokens/src/hooks/useAddCustomTokenLists.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { useSetAtom } from 'jotai' -import { addListAtom } from '../state/tokenLists/tokenListsActionsAtom' - -export function useAddCustomTokenLists() { - return useSetAtom(addListAtom) -} diff --git a/libs/tokens/src/hooks/useAllTokenListsInfo.ts b/libs/tokens/src/hooks/useAllTokenListsInfo.ts deleted file mode 100644 index 003af0d1c0..0000000000 --- a/libs/tokens/src/hooks/useAllTokenListsInfo.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { useAtomValue } from 'jotai' -import { allTokenListsInfoAtom } from '../state/tokenLists/tokenListsStateAtom' -import { TokenListInfo } from '../types' - -export function useAllTokenListsInfo(): TokenListInfo[] { - return useAtomValue(allTokenListsInfoAtom) -} diff --git a/libs/tokens/src/hooks/useImportTokenCallback.ts b/libs/tokens/src/hooks/useImportTokenCallback.ts deleted file mode 100644 index 269bbe52a5..0000000000 --- a/libs/tokens/src/hooks/useImportTokenCallback.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { TokenWithLogo } from '@cowprotocol/common-const' -import { useSetAtom } from 'jotai' -import { addUserTokenAtom } from '../state/tokens/userAddedTokensAtom' - -export function useImportTokenCallback(): (tokens: TokenWithLogo[]) => void { - return useSetAtom(addUserTokenAtom) -} diff --git a/libs/tokens/src/hooks/useRemoveTokenList.ts b/libs/tokens/src/hooks/useRemoveTokenList.ts deleted file mode 100644 index 8d2cc956e6..0000000000 --- a/libs/tokens/src/hooks/useRemoveTokenList.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { useSetAtom } from 'jotai' -import { removeListAtom } from '../state/tokenLists/tokenListsActionsAtom' - -export function useRemoveTokenList() { - return useSetAtom(removeListAtom) -} diff --git a/libs/tokens/src/hooks/useResetUserTokensCallback.ts b/libs/tokens/src/hooks/useResetUserTokensCallback.ts deleted file mode 100644 index 38e51ceec6..0000000000 --- a/libs/tokens/src/hooks/useResetUserTokensCallback.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { useSetAtom } from 'jotai' -import { resetUserTokenAtom } from '../state/tokens/userAddedTokensAtom' - -export function useResetUserTokensCallback(): () => void { - return useSetAtom(resetUserTokenAtom) -} diff --git a/libs/tokens/src/hooks/useToggleListCallback.ts b/libs/tokens/src/hooks/useToggleListCallback.ts deleted file mode 100644 index 6180557536..0000000000 --- a/libs/tokens/src/hooks/useToggleListCallback.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { useSetAtom } from 'jotai' -import { toggleListAtom } from '../state/tokenLists/tokenListsActionsAtom' - -export function useToggleListCallback() { - return useSetAtom(toggleListAtom) -} diff --git a/libs/tokens/src/index.ts b/libs/tokens/src/index.ts index 90d6ddca27..3b9215328c 100644 --- a/libs/tokens/src/index.ts +++ b/libs/tokens/src/index.ts @@ -6,37 +6,38 @@ export { TokenLogo } from './pure/TokenLogo' // Types export * from './types' -export type { TokensByAddress, TokensBySymbol } from './state/tokens/tokensAtom' -export type { ListSearchResponse } from './hooks/useSearchList' -export type { TokenSearchResponse } from './hooks/useSearchToken' +export type { TokensByAddress, TokensBySymbol } from './state/tokens/allTokensAtom' +export type { ListSearchResponse } from './hooks/lists/useSearchList' +export type { TokenSearchResponse } from './hooks/tokens/useSearchToken' // Hooks -export { useAllListsList } from './hooks/useAllListsList' -export { useAddCustomTokenLists } from './hooks/useAddCustomTokenLists' -export { useAllTokens } from './hooks/useAllTokens' -export { useFavouriteTokens } from './hooks/useFavouriteTokens' -export { useUserAddedTokens } from './hooks/useUserAddedTokens' -export { useImportTokenCallback } from './hooks/useImportTokenCallback' -export { useRemoveTokenCallback } from './hooks/useRemoveTokenCallback' -export { useResetUserTokensCallback } from './hooks/useResetUserTokensCallback' -export { useRemoveTokenList } from './hooks/useRemoveTokenList' -export { useToggleListCallback } from './hooks/useToggleListCallback' -export { useListsEnabledState } from './hooks/useListsEnabledState' -export { useAddUnsupportedToken } from './hooks/useAddUnsupportedToken' -export { useRemoveUnsupportedToken } from './hooks/useRemoveUnsupportedToken' -export { useUnsupportedTokens } from './hooks/useUnsupportedTokens' -export { useIsTradeUnsupported } from './hooks/useIsTradeUnsupported' -export { useIsUnsupportedToken } from './hooks/useIsUnsupportedToken' -export { useIsUnsupportedTokens } from './hooks/useIsUnsupportedTokens' -export { useResetFavouriteTokens } from './hooks/useResetFavouriteTokens' -export { useToggleFavouriteToken } from './hooks/useToggleFavouriteToken' -export { useTokensByAddressMap } from './hooks/useTokensByAddressMap' -export { useTokenBySymbolOrAddress } from './hooks/useTokenBySymbolOrAddress' -export { useAreThereTokensWithSameSymbol } from './hooks/useAreThereTokensWithSameSymbol' -export { useSearchList } from './hooks/useSearchList' -export { useSearchToken } from './hooks/useSearchToken' -export { useSearchNonExistentToken } from './hooks/useSearchNonExistentToken' +export { useAllListsList } from './hooks/lists/useAllListsList' +export { useAddList } from './hooks/lists/useAddList' +export { useAllTokens } from './hooks/tokens/useAllTokens' +export { useFavouriteTokens } from './hooks/tokens/favourite/useFavouriteTokens' +export { useUserAddedTokens } from './hooks/tokens/userAdded/useUserAddedTokens' +export { useAddUserToken } from './hooks/tokens/userAdded/useAddUserToken' +export { useRemoveUserToken } from './hooks/tokens/userAdded/useRemoveUserToken' +export { useResetUserTokens } from './hooks/tokens/userAdded/useResetUserTokens' +export { useRemoveList } from './hooks/lists/useRemoveList' +export { useToggleList } from './hooks/lists/useToggleList' +export { useListsEnabledState } from './hooks/lists/useListsEnabledState' +export { useAddUnsupportedToken } from './hooks/tokens/unsupported/useAddUnsupportedToken' +export { useRemoveUnsupportedToken } from './hooks/tokens/unsupported/useRemoveUnsupportedToken' +export { useUnsupportedTokens } from './hooks/tokens/unsupported/useUnsupportedTokens' +export { useIsTradeUnsupported } from './hooks/tokens/unsupported/useIsTradeUnsupported' +export { useIsUnsupportedToken } from './hooks/tokens/unsupported/useIsUnsupportedToken' +export { useIsUnsupportedTokens } from './hooks/tokens/unsupported/useIsUnsupportedTokens' +export { useResetFavouriteTokens } from './hooks/tokens/favourite/useResetFavouriteTokens' +export { useToggleFavouriteToken } from './hooks/tokens/favourite/useToggleFavouriteToken' +export { useTokensByAddressMap } from './hooks/tokens/useTokensByAddressMap' +export { useTokenBySymbolOrAddress } from './hooks/tokens/useTokenBySymbolOrAddress' +export { useAreThereTokensWithSameSymbol } from './hooks/tokens/useAreThereTokensWithSameSymbol' +export { useSearchList } from './hooks/lists/useSearchList' +export { useSearchToken } from './hooks/tokens/useSearchToken' +export { useSearchNonExistentToken } from './hooks/tokens/useSearchNonExistentToken' // Utils export { getTokenListViewLink } from './utils/getTokenListViewLink' export { getTokenLogoUrls } from './utils/getTokenLogoUrls' +export { getTokenListSource } from './utils/getTokenListSource' diff --git a/libs/tokens/src/state/tokenLists/tokenListsActionsAtom.ts b/libs/tokens/src/state/tokenLists/tokenListsActionsAtom.ts index a1115d1849..e4c6e40427 100644 --- a/libs/tokens/src/state/tokenLists/tokenListsActionsAtom.ts +++ b/libs/tokens/src/state/tokenLists/tokenListsActionsAtom.ts @@ -69,6 +69,8 @@ export const toggleListAtom = atom(null, (get, set, id: string) => { const listsEnabledState = get(listsEnabledStateAtom) const states = get(listsStatesMapAtom) + if (!states[id]) return + const list = { ...states[id] } list.isEnabled = !listsEnabledState[id] diff --git a/libs/tokens/src/state/tokens/tokensAtom.ts b/libs/tokens/src/state/tokens/allTokensAtom.ts similarity index 100% rename from libs/tokens/src/state/tokens/tokensAtom.ts rename to libs/tokens/src/state/tokens/allTokensAtom.ts diff --git a/libs/tokens/src/state/tokens/unsupportedTokensAtom.ts b/libs/tokens/src/state/tokens/unsupportedTokensAtom.ts index 8136fc7343..b7e9f208e8 100644 --- a/libs/tokens/src/state/tokens/unsupportedTokensAtom.ts +++ b/libs/tokens/src/state/tokens/unsupportedTokensAtom.ts @@ -2,14 +2,16 @@ import { atomWithStorage } from 'jotai/utils' import { SupportedChainId } from '@cowprotocol/cow-sdk' import { atom } from 'jotai' import { environmentAtom } from '../environmentAtom' - -export const unsupportedTokensAtom = atomWithStorage< - Record ->('unsupportedTokensAtom:v1', { - [SupportedChainId.MAINNET]: {}, - [SupportedChainId.GNOSIS_CHAIN]: {}, - [SupportedChainId.GOERLI]: {}, -}) +import { UnsupportedTokensState } from '../../types' + +export const unsupportedTokensAtom = atomWithStorage>( + 'unsupportedTokensAtom:v1', + { + [SupportedChainId.MAINNET]: {}, + [SupportedChainId.GNOSIS_CHAIN]: {}, + [SupportedChainId.GOERLI]: {}, + } +) export const currentUnsupportedTokensAtom = atom((get) => { const { chainId } = get(environmentAtom) diff --git a/libs/tokens/src/state/tokens/userAddedTokensAtom.ts b/libs/tokens/src/state/tokens/userAddedTokensAtom.ts index 9e147bdab4..a6ad0a73ee 100644 --- a/libs/tokens/src/state/tokens/userAddedTokensAtom.ts +++ b/libs/tokens/src/state/tokens/userAddedTokensAtom.ts @@ -49,7 +49,7 @@ export const removeUserTokenAtom = atom(null, (get, set, token: TokenWithLogo) = }) }) -export const resetUserTokenAtom = atom(null, (get, set) => { +export const resetUserTokensAtom = atom(null, (get, set) => { const { chainId } = get(environmentAtom) const userAddedTokensState = get(userAddedTokensAtom) diff --git a/libs/tokens/src/types.ts b/libs/tokens/src/types.ts index d8152acacb..b3f62d6753 100644 --- a/libs/tokens/src/types.ts +++ b/libs/tokens/src/types.ts @@ -23,6 +23,8 @@ export type ListsSourcesByNetwork = Record[]) return acc }, []) } - -// Spread tokens from fetched lists to active and inactive tokens lists -export function parseTokenListResults(chainId: SupportedChainId, fetchedTokens: ListState[]): TokensAndListsUpdate { - return fetchedTokens.reduce( - (acc, val) => { - const listId = val.id - - acc.lists[listId] = val - - return acc - }, - { activeTokens: {}, inactiveTokens: {}, lists: {} } - ) -} - -// Move tokens from active to inactive and vice versa -export function updateTokensLists( - tokensState: TokensState, - activeTokensListsMap: ListsEnabledState -): TokensState | null { - const { activeTokens, inactiveTokens } = tokensState - - const activeLists = Object.keys(activeTokens) - const inactiveLists = Object.keys(inactiveTokens) - - const areListsEmpty = !activeLists.length && !inactiveLists.length - - if (areListsEmpty) return null - - const noUpdates = Object.keys(activeTokensListsMap).every((listId) => { - const isListEnabled = activeTokensListsMap[listId] - - if (typeof isListEnabled === 'undefined') return true - - return !!(isListEnabled ? activeTokens[listId] : inactiveTokens[listId]) - }) - - if (noUpdates) return null - - return [...activeLists, ...inactiveLists].reduce( - (state, listId) => { - const isListEnabled = activeTokensListsMap[listId] - - if (isListEnabled) { - // If list is enabled, move all tokens from inactive to active - if (!state.activeTokens[listId]) { - state.activeTokens[listId] = inactiveTokens[listId] - } - - delete state.inactiveTokens[listId] - } else { - // If list is disabled, move all tokens from active to inactive - if (!state.inactiveTokens[listId]) { - state.inactiveTokens[listId] = activeTokens[listId] - } - - delete state.activeTokens[listId] - } - - return state - }, - { ...tokensState } - ) -} diff --git a/libs/tokens/src/utils/buildTokenListInfo.ts b/libs/tokens/src/utils/buildTokenListInfo.ts deleted file mode 100644 index 8abcbe4f8e..0000000000 --- a/libs/tokens/src/utils/buildTokenListInfo.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { TokenListInfo } from '../types' -import { TokenListResult } from '../services/fetchTokenList' - -export function buildTokenListInfo(val: TokenListResult): TokenListInfo { - const { major, minor, patch } = val.list.version - - return { - id: val.id, - source: val.source, - name: val.list.name, - timestamp: val.list.timestamp, - version: `v${major}.${minor}.${patch}`, - logoUrl: val.list.logoURI, - tokensCount: val.list.tokens.length, - priority: val.priority, - } -} diff --git a/libs/tokens/src/utils/getTokenListSource.ts b/libs/tokens/src/utils/getTokenListSource.ts new file mode 100644 index 0000000000..98de60e795 --- /dev/null +++ b/libs/tokens/src/utils/getTokenListSource.ts @@ -0,0 +1,5 @@ +import { ListResource } from '../types' + +export function getTokenListSource(source: ListResource): string { + return 'ensName' in source ? source.ensName : source.url +} diff --git a/libs/tokens/src/utils/getTokenListViewLink.ts b/libs/tokens/src/utils/getTokenListViewLink.ts index 9b47d117b8..094497d888 100644 --- a/libs/tokens/src/utils/getTokenListViewLink.ts +++ b/libs/tokens/src/utils/getTokenListViewLink.ts @@ -1,7 +1,6 @@ import { ListResource } from '../types' +import { getTokenListSource } from './getTokenListSource' export function getTokenListViewLink(source: ListResource): string { - const url = 'ensName' in source ? source.ensName : source.url - - return `https://tokenlists.org/token-list?url=${url}` + return `https://tokenlists.org/token-list?url=${getTokenListSource(source)}` }