|
| 1 | +import { |
| 2 | + Button, |
| 3 | + Icons, |
| 4 | + Text, |
| 5 | + TouchableOpacity, |
| 6 | + useTheme, |
| 7 | + View |
| 8 | +} from '@avalabs/k2-alpine' |
| 9 | +import { showSnackbar } from 'common/utils/toast' |
| 10 | +import React, { useCallback } from 'react' |
| 11 | +import useAddCustomToken from 'common/hooks/useAddCustomToken' |
| 12 | +import { LocalTokenWithBalance } from 'store/balance' |
| 13 | +import { useRouter } from 'expo-router' |
| 14 | +import { LogoWithNetwork } from 'features/portfolio/assets/components/LogoWithNetwork' |
| 15 | +import { LoadingState } from 'common/components/LoadingState' |
| 16 | +import { ScrollScreen } from 'common/components/ScrollScreen' |
| 17 | +import { TokenLogo } from 'common/components/TokenLogo' |
| 18 | +import { TextInput } from 'react-native' |
| 19 | +import { useSelectedNetwork } from '../store' |
| 20 | + |
| 21 | +export const AddCustomTokenScreen = (): JSX.Element => { |
| 22 | + const { |
| 23 | + theme: { colors } |
| 24 | + } = useTheme() |
| 25 | + const [selectedNetwork, setSelectedNetwork] = useSelectedNetwork() |
| 26 | + const { canGoBack, back, navigate } = useRouter() |
| 27 | + |
| 28 | + const showSuccess = useCallback(() => { |
| 29 | + showSnackbar('Added!') |
| 30 | + setSelectedNetwork(undefined) |
| 31 | + canGoBack() && back() |
| 32 | + }, [setSelectedNetwork, canGoBack, back]) |
| 33 | + |
| 34 | + const { |
| 35 | + tokenAddress, |
| 36 | + setTokenAddress, |
| 37 | + errorMessage, |
| 38 | + token, |
| 39 | + addCustomToken, |
| 40 | + isLoading |
| 41 | + } = useAddCustomToken(showSuccess) |
| 42 | + |
| 43 | + // only enable button if we have token and no error message |
| 44 | + const disabled = !!(errorMessage || !token || isLoading) |
| 45 | + |
| 46 | + const goToScanQrCode = useCallback((): void => { |
| 47 | + // @ts-ignore TODO: make routes typesafe |
| 48 | + navigate('/tokenManagement/scanQrCode') |
| 49 | + }, [navigate]) |
| 50 | + |
| 51 | + const goToSelectNetwork = useCallback((): void => { |
| 52 | + // @ts-ignore TODO: make routes typesafe |
| 53 | + navigate('/selectCustomTokenNetwork') |
| 54 | + }, [navigate]) |
| 55 | + |
| 56 | + const renderToken = useCallback((): JSX.Element | undefined => { |
| 57 | + if (isLoading) { |
| 58 | + return <LoadingState sx={{ flex: 1 }} /> |
| 59 | + } |
| 60 | + |
| 61 | + if (!token) { |
| 62 | + return undefined |
| 63 | + } |
| 64 | + |
| 65 | + return ( |
| 66 | + <View sx={{ marginTop: 32 }}> |
| 67 | + <View |
| 68 | + style={{ justifyContent: 'center', alignItems: 'center', gap: 16 }}> |
| 69 | + <LogoWithNetwork |
| 70 | + token={token as LocalTokenWithBalance} |
| 71 | + outerBorderColor={colors.$surfacePrimary} |
| 72 | + /> |
| 73 | + <Text variant="heading6">{token.name}</Text> |
| 74 | + </View> |
| 75 | + <Button |
| 76 | + disabled={disabled} |
| 77 | + size="large" |
| 78 | + type="primary" |
| 79 | + style={{ margin: 16 }} |
| 80 | + onPress={addCustomToken}> |
| 81 | + Add |
| 82 | + </Button> |
| 83 | + </View> |
| 84 | + ) |
| 85 | + }, [token, colors.$surfacePrimary, addCustomToken, disabled, isLoading]) |
| 86 | + |
| 87 | + const renderTokenAddress = useCallback(() => { |
| 88 | + return ( |
| 89 | + <> |
| 90 | + <View |
| 91 | + sx={{ |
| 92 | + backgroundColor: colors.$surfaceSecondary, |
| 93 | + borderRadius: 12, |
| 94 | + padding: 16, |
| 95 | + flexDirection: 'row', |
| 96 | + justifyContent: 'space-between' |
| 97 | + }}> |
| 98 | + <View sx={{ width: '90%' }}> |
| 99 | + <Text |
| 100 | + variant="body2" |
| 101 | + sx={{ |
| 102 | + fontSize: 11, |
| 103 | + lineHeight: 14, |
| 104 | + color: colors.$textSecondary |
| 105 | + }}> |
| 106 | + Token contract address |
| 107 | + </Text> |
| 108 | + <TextInput |
| 109 | + onChangeText={setTokenAddress} |
| 110 | + numberOfLines={2} |
| 111 | + multiline |
| 112 | + value={tokenAddress} |
| 113 | + style={{ |
| 114 | + color: colors.$textPrimary, |
| 115 | + fontSize: 15, |
| 116 | + lineHeight: 20 |
| 117 | + }} |
| 118 | + /> |
| 119 | + </View> |
| 120 | + <TouchableOpacity onPress={goToScanQrCode} hitSlop={16}> |
| 121 | + <Icons.Custom.QRCodeScanner |
| 122 | + color={colors.$textPrimary} |
| 123 | + width={20} |
| 124 | + height={20} |
| 125 | + /> |
| 126 | + </TouchableOpacity> |
| 127 | + </View> |
| 128 | + |
| 129 | + {errorMessage.length > 0 && ( |
| 130 | + <View |
| 131 | + sx={{ |
| 132 | + flexDirection: 'row', |
| 133 | + alignItems: 'center' |
| 134 | + }}> |
| 135 | + <Icons.Action.Info color={colors.$textDanger} /> |
| 136 | + <Text |
| 137 | + variant="subtitle1" |
| 138 | + sx={{ color: colors.$textDanger, marginLeft: 8 }}> |
| 139 | + {errorMessage} |
| 140 | + </Text> |
| 141 | + </View> |
| 142 | + )} |
| 143 | + </> |
| 144 | + ) |
| 145 | + }, [ |
| 146 | + colors.$surfaceSecondary, |
| 147 | + colors.$textSecondary, |
| 148 | + colors.$textPrimary, |
| 149 | + colors.$textDanger, |
| 150 | + setTokenAddress, |
| 151 | + tokenAddress, |
| 152 | + goToScanQrCode, |
| 153 | + errorMessage |
| 154 | + ]) |
| 155 | + |
| 156 | + const renderNetwork = useCallback((): JSX.Element => { |
| 157 | + return ( |
| 158 | + <TouchableOpacity |
| 159 | + onPress={goToSelectNetwork} |
| 160 | + sx={{ |
| 161 | + backgroundColor: colors.$surfaceSecondary, |
| 162 | + paddingHorizontal: 16, |
| 163 | + paddingVertical: 10, |
| 164 | + justifyContent: 'space-between', |
| 165 | + flexDirection: 'row', |
| 166 | + borderRadius: 12, |
| 167 | + alignItems: 'center' |
| 168 | + }}> |
| 169 | + <Text variant="body2" sx={{ fontSize: 16, lineHeight: 22 }}> |
| 170 | + Network |
| 171 | + </Text> |
| 172 | + {selectedNetwork ? ( |
| 173 | + <View sx={{ flexDirection: 'row', gap: 8, alignItems: 'center' }}> |
| 174 | + <TokenLogo logoUri={selectedNetwork.logoUri} size={24} /> |
| 175 | + <Text |
| 176 | + variant="body2" |
| 177 | + sx={{ |
| 178 | + fontSize: 16, |
| 179 | + lineHeight: 22, |
| 180 | + color: colors.$textSecondary |
| 181 | + }}> |
| 182 | + {selectedNetwork.chainName} |
| 183 | + </Text> |
| 184 | + <View sx={{ marginHorizontal: 8 }}> |
| 185 | + <Icons.Navigation.ChevronRightV2 /> |
| 186 | + </View> |
| 187 | + </View> |
| 188 | + ) : ( |
| 189 | + <View sx={{ flexDirection: 'row', gap: 8, alignItems: 'center' }}> |
| 190 | + <Text |
| 191 | + variant="body2" |
| 192 | + sx={{ |
| 193 | + fontSize: 16, |
| 194 | + lineHeight: 22, |
| 195 | + color: colors.$textSecondary |
| 196 | + }}> |
| 197 | + Select |
| 198 | + </Text> |
| 199 | + <View sx={{ marginHorizontal: 8 }}> |
| 200 | + <Icons.Navigation.ChevronRightV2 /> |
| 201 | + </View> |
| 202 | + </View> |
| 203 | + )} |
| 204 | + </TouchableOpacity> |
| 205 | + ) |
| 206 | + }, [ |
| 207 | + colors.$surfaceSecondary, |
| 208 | + colors.$textSecondary, |
| 209 | + goToSelectNetwork, |
| 210 | + selectedNetwork |
| 211 | + ]) |
| 212 | + |
| 213 | + return ( |
| 214 | + <ScrollScreen |
| 215 | + title={`Add a custom\ntoken`} |
| 216 | + contentContainerStyle={{ padding: 16 }} |
| 217 | + shouldAvoidKeyboard |
| 218 | + isModal> |
| 219 | + <View sx={{ gap: 10, marginTop: 24 }}> |
| 220 | + {renderNetwork()} |
| 221 | + {selectedNetwork && renderTokenAddress()} |
| 222 | + {renderToken()} |
| 223 | + </View> |
| 224 | + </ScrollScreen> |
| 225 | + ) |
| 226 | +} |
0 commit comments