|
| 1 | +import { withSuspense } from '#/skeleton/withSuspense'; |
| 2 | +import { zUuid } from '~/lib/zod'; |
| 3 | +import { AccountParams } from '../../../_layout'; |
| 4 | +import { useLocalParams } from '~/hooks/useLocalParams'; |
| 5 | +import { Pane } from '#/layout/Pane'; |
| 6 | +import { graphql } from 'relay-runtime'; |
| 7 | +import { useLazyQuery } from '~/api'; |
| 8 | +import { Id_TransferScreenQuery } from '~/api/__generated__/Id_TransferScreenQuery.graphql'; |
| 9 | +import { Scrollable } from '#/Scrollable'; |
| 10 | +import { TokenIcon } from '#/token/TokenIcon'; |
| 11 | +import { OverlayIcon } from '#/layout/OverlayIcon'; |
| 12 | +import { ContactsOutlineIcon, ReceiveIcon, ShareIcon, WebIcon } from '@theme/icons'; |
| 13 | +import { View } from 'react-native'; |
| 14 | +import { ICON_SIZE } from '@theme/paper'; |
| 15 | +import { Text } from 'react-native-paper'; |
| 16 | +import { createStyles, useStyles } from '@theme/styles'; |
| 17 | +import { Timestamp } from '#/format/Timestamp'; |
| 18 | +import { Appbar } from '#/Appbar/Appbar'; |
| 19 | +import { TokenAmount } from '#/token/TokenAmount'; |
| 20 | +import { FiatValue } from '#/FiatValue'; |
| 21 | +import { ItemList } from '#/layout/ItemList'; |
| 22 | +import { ListItem } from '#/list/ListItem'; |
| 23 | +import { AddressIcon } from '#/Identicon/AddressIcon'; |
| 24 | +import { Chip } from '#/Chip'; |
| 25 | +import { SelectableAddress } from '#/address/SelectableAddress'; |
| 26 | +import { asChain, asUAddress } from 'lib'; |
| 27 | +import { Link, useRouter } from 'expo-router'; |
| 28 | +import Decimal from 'decimal.js'; |
| 29 | +import { Actions } from '#/layout/Actions'; |
| 30 | +import { Button } from '#/Button'; |
| 31 | +import { share } from '~/lib/share'; |
| 32 | +import { CHAINS } from 'chains'; |
| 33 | +import { PaneSkeleton } from '#/skeleton/PaneSkeleton'; |
| 34 | + |
| 35 | +const Query = graphql` |
| 36 | + query Id_TransferScreenQuery($id: ID!, $account: UAddress!) { |
| 37 | + transfer(id: $id) @required(action: THROW) { |
| 38 | + id |
| 39 | + tokenAddress |
| 40 | + timestamp |
| 41 | + from |
| 42 | + amount |
| 43 | + value |
| 44 | + systxHash |
| 45 | + token { |
| 46 | + ...TokenIcon_token |
| 47 | + ...TokenAmount_token |
| 48 | + balance(input: { account: $account }) |
| 49 | + price { |
| 50 | + id |
| 51 | + usd |
| 52 | + } |
| 53 | + } |
| 54 | + account { |
| 55 | + id |
| 56 | + address |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +`; |
| 61 | + |
| 62 | +const TransferScreenParams = AccountParams.extend({ id: zUuid() }); |
| 63 | + |
| 64 | +function TransferScreen() { |
| 65 | + const { id, account } = useLocalParams(TransferScreenParams); |
| 66 | + const { styles } = useStyles(stylesheet); |
| 67 | + const router = useRouter(); |
| 68 | + |
| 69 | + const { transfer: t } = useLazyQuery<Id_TransferScreenQuery>(Query, { id, account }); |
| 70 | + const chain = asChain(t.tokenAddress); |
| 71 | + const from = asUAddress(t.from, chain); |
| 72 | + |
| 73 | + const blockExplorer = CHAINS[chain].blockExplorers?.default; |
| 74 | + const explorerUrl = blockExplorer && `${blockExplorer.url}/tx/${t.systxHash}`; |
| 75 | + |
| 76 | + return ( |
| 77 | + <Pane flex> |
| 78 | + <Scrollable> |
| 79 | + <Appbar mode="small" /> |
| 80 | + <View style={styles.centered}> |
| 81 | + <View style={styles.icon}> |
| 82 | + <TokenIcon token={t.token} size={ICON_SIZE.extraLarge} /> |
| 83 | + <OverlayIcon icon={ReceiveIcon} parentSize={ICON_SIZE.extraLarge} /> |
| 84 | + </View> |
| 85 | + |
| 86 | + <Text variant="titleLarge" style={styles.received}> |
| 87 | + Received <Timestamp timestamp={t.timestamp} /> |
| 88 | + </Text> |
| 89 | + |
| 90 | + <Text variant="headlineMedium"> |
| 91 | + {/* TODO: generic token resolution */} |
| 92 | + {t.token && <TokenAmount token={t.token} amount={t.amount} />} |
| 93 | + |
| 94 | + <Text style={styles.value}> |
| 95 | + {' ('} |
| 96 | + <FiatValue value={t.amount} /> |
| 97 | + {')'} |
| 98 | + </Text> |
| 99 | + </Text> |
| 100 | + </View> |
| 101 | + |
| 102 | + <ItemList> |
| 103 | + <ListItem |
| 104 | + leading={<AddressIcon address={from} />} |
| 105 | + overline="From" |
| 106 | + headline={<SelectableAddress address={from} />} |
| 107 | + trailing={ |
| 108 | + <Chip |
| 109 | + mode="outlined" |
| 110 | + icon={(props) => ( |
| 111 | + <ContactsOutlineIcon {...props} color={styles.contactChip.color} /> |
| 112 | + )} |
| 113 | + onPress={() => |
| 114 | + router.push({ pathname: `/(nav)/contacts/[address]`, params: { address: from } }) |
| 115 | + } |
| 116 | + > |
| 117 | + Contact |
| 118 | + </Chip> |
| 119 | + } |
| 120 | + containerStyle={styles.item} |
| 121 | + /> |
| 122 | + |
| 123 | + <ListItem |
| 124 | + leading={<AddressIcon address={t.account.address} />} |
| 125 | + overline="Account" |
| 126 | + headline={<SelectableAddress address={t.account.address} />} |
| 127 | + containerStyle={styles.item} |
| 128 | + trailing={({ Text }) => |
| 129 | + t.token && ( |
| 130 | + <View style={styles.balanceContainer}> |
| 131 | + <Text> |
| 132 | + <TokenAmount token={t.token} amount={t.token.balance} /> |
| 133 | + </Text> |
| 134 | + |
| 135 | + {t.token.price && ( |
| 136 | + <Text> |
| 137 | + <FiatValue value={new Decimal(t.token.balance).mul(t.token.price.usd)} /> |
| 138 | + </Text> |
| 139 | + )} |
| 140 | + </View> |
| 141 | + ) |
| 142 | + } |
| 143 | + /> |
| 144 | + </ItemList> |
| 145 | + |
| 146 | + {explorerUrl && ( |
| 147 | + <Actions horizontal style={styles.actions}> |
| 148 | + <Link href={explorerUrl} asChild> |
| 149 | + <Button mode="contained-tonal" icon={WebIcon}> |
| 150 | + Explorer |
| 151 | + </Button> |
| 152 | + </Link> |
| 153 | + |
| 154 | + <Button |
| 155 | + mode="contained-tonal" |
| 156 | + icon={ShareIcon} |
| 157 | + onPress={() => share({ url: explorerUrl })} |
| 158 | + > |
| 159 | + Share |
| 160 | + </Button> |
| 161 | + </Actions> |
| 162 | + )} |
| 163 | + </Scrollable> |
| 164 | + </Pane> |
| 165 | + ); |
| 166 | +} |
| 167 | + |
| 168 | +const stylesheet = createStyles(({ colors }) => ({ |
| 169 | + centered: { |
| 170 | + alignItems: 'center', |
| 171 | + marginBottom: 16, |
| 172 | + }, |
| 173 | + icon: { |
| 174 | + marginBottom: 16, |
| 175 | + }, |
| 176 | + received: { |
| 177 | + color: colors.onSurfaceVariant, |
| 178 | + }, |
| 179 | + value: { |
| 180 | + color: colors.tertiary, |
| 181 | + }, |
| 182 | + item: { |
| 183 | + backgroundColor: colors.surface, |
| 184 | + }, |
| 185 | + balanceContainer: { |
| 186 | + flexDirection: 'column', |
| 187 | + alignItems: 'flex-end', |
| 188 | + }, |
| 189 | + contactChip: { |
| 190 | + color: colors.onSurface, |
| 191 | + }, |
| 192 | + actions: { |
| 193 | + marginVertical: 8, |
| 194 | + }, |
| 195 | +})); |
| 196 | + |
| 197 | +export default withSuspense(TransferScreen, <PaneSkeleton />); |
| 198 | + |
| 199 | +export { ErrorBoundary } from '#/ErrorBoundary'; |
0 commit comments