From 52e4f219f7839d28cb165459595ba18e00e0e0a7 Mon Sep 17 00:00:00 2001 From: muzam1l Date: Fri, 17 Mar 2023 16:49:00 +0530 Subject: [PATCH] Transaction amount UI. --- components/Transaction/ui.tsx | 127 ++++++++++++++++++++++++++++++++-- content/transactions.json | 16 ++--- state/transactions.ts | 73 +++++++++++-------- utils/helpers.ts | 14 ++++ utils/schema.ts | 15 ++-- 5 files changed, 196 insertions(+), 49 deletions(-) diff --git a/components/Transaction/ui.tsx b/components/Transaction/ui.tsx index 48773fd..71eb44e 100644 --- a/components/Transaction/ui.tsx +++ b/components/Transaction/ui.tsx @@ -15,11 +15,12 @@ import { import { useSnapshot } from 'valtio' import state from '../../state' import { streamState } from '../DebugStream' -import { Button } from '..' +import { Box, Button } from '..' import Textarea from '../Textarea' import { getFlags } from '../../state/constants/flags' import { Plus, Trash } from 'phosphor-react' import AccountSequence from '../Sequence' +import { typeIs } from '../../utils/helpers' interface UIProps { setState: (pTx?: Partial | undefined) => TransactionState | undefined @@ -87,6 +88,22 @@ export const TxUI: FC = ({ [setState, txFields] ) + const setRawField = useCallback( + (field: keyof TxFields, type: string, value: any) => { + // TODO $type should be a narrowed type + setState({ + txFields: { + ...txFields, + [field]: { + $type: type, + $value: value + } + } + }) + }, + [setState, txFields] + ) + const handleEstimateFee = useCallback( async (state?: TransactionState, silent?: boolean) => { setFeeLoading(true) @@ -134,6 +151,16 @@ export const TxUI: FC = ({ } const otherFields = Object.keys(txFields).filter(k => !richFields.includes(k)) as [keyof TxFields] + const amountOptions = [ + { label: 'XRP', value: 'xrp' }, + { label: 'Token', value: 'token' } + ] as const + + const defaultTokenAmount = { + value: '0', + currency: '', + issuer: '' + } return ( = ({ let _value = txFields[field] let value: string | undefined - if (typeof _value === 'object') { - if (_value.$type === 'json' && typeof _value.$value === 'object') { + if (typeIs(_value, 'object')) { + if (_value.$type === 'json' && typeIs(_value.$value, ['object', 'array'])) { value = JSON.stringify(_value.$value, null, 2) } else { - value = _value.$value.toString() + value = _value.$value?.toString() } } else { value = _value?.toString() } - const isXrp = typeof _value === 'object' && _value.$type === 'xrp' + const isXrpAmount = typeIs(_value, 'object') && _value.$type === 'amount.xrp' + const isTokenAmount = typeIs(_value, 'object') && _value.$type === 'amount.token' const isJson = typeof _value === 'object' && _value.$type === 'json' const isFee = field === 'Fee' let rows = isJson ? (value?.match(/\n/gm)?.length || 0) + 1 : undefined if (rows && rows > 5) rows = 5 + let tokenAmount = defaultTokenAmount + if (isTokenAmount && typeIs(_value, 'object') && typeIs(_value.$value, 'object')) { + tokenAmount = { + value: _value.$value.value, + currency: _value.$value.currency, + issuer: _value.$value.issuer + } + } + + if (isXrpAmount || isTokenAmount) { + return ( + + + {isTokenAmount ? ( + + + setRawField(field, 'amount.token', { + ...tokenAmount, + issuer: e.target.value + }) + } + /> + { + setRawField(field, 'amount.token', { + ...tokenAmount, + currency: e.target.value + }) + }} + /> + { + setRawField(field, 'amount.token', { + ...tokenAmount, + value: e.target.value + }) + }} + /> + + ) : ( + handleSetField(field, e.target.value)} + onKeyPress={e => { + if (e.key === '.' || e.key === ',') { + e.preventDefault() + } + }} + /> + )} + +