|
| 1 | +import { PaneSkeleton } from '#/skeleton/PaneSkeleton'; |
| 2 | +import { withSuspense } from '#/skeleton/withSuspense'; |
| 3 | +import { zUAddress } from '~/lib/zod'; |
| 4 | +import { AccountParams } from '../_layout'; |
| 5 | +import { useLocalParams } from '~/hooks/useLocalParams'; |
| 6 | +import { graphql } from 'relay-runtime'; |
| 7 | +import { useState } from 'react'; |
| 8 | +import Decimal from 'decimal.js'; |
| 9 | +import { z } from 'zod'; |
| 10 | +import { Pane } from '#/layout/Pane'; |
| 11 | +import { TokenAmountInput } from '#/token/TokenAmountInput'; |
| 12 | +import { useLazyQuery } from '~/api'; |
| 13 | +import { send_SendScreen2Query } from '~/api/__generated__/send_SendScreen2Query.graphql'; |
| 14 | +import { useSelectedToken } from '~/hooks/useSelectToken'; |
| 15 | +import { asChain } from 'lib'; |
| 16 | +import { Scrollable } from '#/Scrollable'; |
| 17 | +import { Appbar } from '#/Appbar/Appbar'; |
| 18 | +import { View } from 'react-native'; |
| 19 | +import { createStyles, useStyles } from '@theme/styles'; |
| 20 | +import { SendMode, SendModeChips } from '#/send/SendModeChips'; |
| 21 | +import { ItemList } from '#/layout/ItemList'; |
| 22 | +import { SendAccount } from '#/send/SendAccount'; |
| 23 | +import { SendTo } from '#/send/SendTo'; |
| 24 | +import { match } from 'ts-pattern'; |
| 25 | +import { TransferMode } from '#/send/TransferMode'; |
| 26 | +import { TransferFromMode } from '#/send/TransferFromMode'; |
| 27 | +import { Text } from 'react-native-paper'; |
| 28 | + |
| 29 | +const Query = graphql` |
| 30 | + query send_SendScreen2Query($account: UAddress!, $token: UAddress!) { |
| 31 | + token(address: $token) @required(action: THROW) { |
| 32 | + id |
| 33 | + address |
| 34 | + decimals |
| 35 | + balance(input: { account: $account }) |
| 36 | + price { |
| 37 | + usd |
| 38 | + } |
| 39 | + ...TokenAmountInput_token |
| 40 | + ...SendAccount_token @arguments(account: $account) |
| 41 | + ...TransferMode_token |
| 42 | + ...TransferFromMode_token |
| 43 | + } |
| 44 | +
|
| 45 | + account(address: $account) @required(action: THROW) { |
| 46 | + address |
| 47 | + ...useProposeTransaction_account |
| 48 | + ...SendAccount_account |
| 49 | + ...TransferMode_account |
| 50 | + ...TransferFromMode_account |
| 51 | + } |
| 52 | + } |
| 53 | +`; |
| 54 | + |
| 55 | +export const SendScreenParams = AccountParams.extend({ |
| 56 | + to: zUAddress().optional(), |
| 57 | +}); |
| 58 | +export type SendScreenParams = z.infer<typeof SendScreenParams>; |
| 59 | + |
| 60 | +function SendScreen() { |
| 61 | + const params = useLocalParams(SendScreenParams); |
| 62 | + const { styles } = useStyles(stylesheet); |
| 63 | + const chain = asChain(params.account); |
| 64 | + |
| 65 | + const { token, account } = useLazyQuery<send_SendScreen2Query>(Query, { |
| 66 | + account: params.account, |
| 67 | + token: useSelectedToken(chain), |
| 68 | + }); |
| 69 | + |
| 70 | + const [amount, setAmount] = useState<Decimal>(new Decimal(0)); |
| 71 | + const [mode, setMode] = useState<SendMode>('transfer'); |
| 72 | + const [to, setTo] = useState(params.to); |
| 73 | + |
| 74 | + const warning = amount.gt(token.balance) && 'Insufficient balance'; |
| 75 | + |
| 76 | + return ( |
| 77 | + <Pane flex padding={false}> |
| 78 | + <Scrollable contentContainerStyle={styles.container}> |
| 79 | + <Appbar noPadding /> |
| 80 | + <View style={styles.inputContainer}> |
| 81 | + <TokenAmountInput |
| 82 | + account={account.address} |
| 83 | + amount={amount} |
| 84 | + onChange={setAmount} |
| 85 | + token={token} |
| 86 | + /> |
| 87 | + <Text variant="headlineMedium" style={styles.warning}> |
| 88 | + {warning} |
| 89 | + </Text> |
| 90 | + </View> |
| 91 | + |
| 92 | + <View style={styles.sendModeChipsContainer}> |
| 93 | + <SendModeChips mode={mode} onChange={setMode} /> |
| 94 | + </View> |
| 95 | + |
| 96 | + <ItemList> |
| 97 | + <SendAccount account={account} token={token} style={styles.item} /> |
| 98 | + <SendTo chain={chain} to={to} onChange={setTo} containerStyle={styles.item} /> |
| 99 | + </ItemList> |
| 100 | + |
| 101 | + {match(mode) |
| 102 | + .with('transfer', () => ( |
| 103 | + <TransferMode account={account} token={token} to={to} amount={amount} /> |
| 104 | + )) |
| 105 | + .with('transferFrom', () => ( |
| 106 | + <TransferFromMode account={account} token={token} to={to} amount={amount} /> |
| 107 | + )) |
| 108 | + .exhaustive()} |
| 109 | + </Scrollable> |
| 110 | + </Pane> |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +const stylesheet = createStyles(({ colors, padding, negativeMargin }) => ({ |
| 115 | + container: { |
| 116 | + gap: 8, |
| 117 | + paddingHorizontal: padding, |
| 118 | + }, |
| 119 | + inputContainer: { |
| 120 | + marginVertical: 16, |
| 121 | + }, |
| 122 | + sendModeChipsContainer: { |
| 123 | + marginHorizontal: negativeMargin, |
| 124 | + }, |
| 125 | + item: { |
| 126 | + backgroundColor: colors.surface, |
| 127 | + }, |
| 128 | + warning: { |
| 129 | + color: colors.warning, |
| 130 | + }, |
| 131 | +})); |
| 132 | + |
| 133 | +export default withSuspense(SendScreen, <PaneSkeleton />); |
| 134 | + |
| 135 | +export { ErrorBoundary } from '#/ErrorBoundary'; |
0 commit comments