-
-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(suite-native): fee options animated design
- Loading branch information
Showing
9 changed files
with
327 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,169 @@ | ||
import { useContext } from 'react'; | ||
import { TouchableOpacity } from 'react-native'; | ||
import { Pressable } from 'react-native'; | ||
import { useSelector } from 'react-redux'; | ||
import Animated, { | ||
interpolateColor, | ||
useAnimatedStyle, | ||
useDerivedValue, | ||
withTiming, | ||
} from 'react-native-reanimated'; | ||
|
||
import { NetworkSymbol } from '@suite-common/wallet-config'; | ||
import { getNetworkType, NetworkSymbol } from '@suite-common/wallet-config'; | ||
import { GeneralPrecomposedTransactionFinal } from '@suite-common/wallet-types'; | ||
import { Text, HStack, VStack, Radio } from '@suite-native/atoms'; | ||
import { Text, HStack, VStack, Radio, Box } from '@suite-native/atoms'; | ||
import { CryptoToFiatAmountFormatter, CryptoAmountFormatter } from '@suite-native/formatters'; | ||
import { FormContext } from '@suite-native/forms'; | ||
import { TxKeyPath, Translation } from '@suite-native/intl'; | ||
import { FeesRootState, selectNetworkFeeLevelTimeEstimate } from '@suite-common/wallet-core'; | ||
import { | ||
FeesRootState, | ||
selectNetworkFeeLevelFeePerUnit, | ||
selectNetworkFeeLevelTimeEstimate, | ||
} from '@suite-common/wallet-core'; | ||
import { Color } from '@trezor/theme'; | ||
import { prepareNativeStyle, useNativeStyles } from '@trezor/styles'; | ||
import { getFeeUnits } from '@suite-common/wallet-utils'; | ||
|
||
import { SendFeesFormValues } from '../sendFeesFormSchema'; | ||
import { NativeSupportedFeeLevel } from '../types'; | ||
import { FeeOptionErrorMessage } from './FeeOptionErrorMessage'; | ||
|
||
const feeLabelsMap = { | ||
economy: { | ||
label: 'moduleSend.fees.levels.low.label', | ||
timeEstimate: 'moduleSend.fees.levels.low.timeEstimate', | ||
}, | ||
normal: { | ||
label: 'moduleSend.fees.levels.medium.label', | ||
timeEstimate: 'moduleSend.fees.levels.medium.timeEstimate', | ||
}, | ||
high: { | ||
label: 'moduleSend.fees.levels.high.label', | ||
timeEstimate: 'moduleSend.fees.levels.high.timeEstimate', | ||
}, | ||
} as const satisfies Record<NativeSupportedFeeLevel, { label: TxKeyPath; timeEstimate: TxKeyPath }>; | ||
economy: 'moduleSend.fees.levels.low', | ||
normal: 'moduleSend.fees.levels.medium', | ||
high: 'moduleSend.fees.levels.high', | ||
} as const satisfies Record<NativeSupportedFeeLevel, TxKeyPath>; | ||
|
||
const wrapperStyle = prepareNativeStyle(utils => ({ | ||
overflow: 'hidden', | ||
borderRadius: utils.borders.radii.medium, | ||
borderWidth: utils.borders.widths.large, | ||
backgroundColor: utils.colors.backgroundSurfaceElevation1, | ||
borderColor: utils.colors.backgroundSurfaceElevation0, | ||
})); | ||
|
||
const valuesWrapperStyle = prepareNativeStyle(utils => ({ | ||
padding: utils.spacings.medium, | ||
})); | ||
|
||
export const FeeOption = ({ | ||
feeKey, | ||
feeLevel, | ||
networkSymbol, | ||
transactionBytes, | ||
}: { | ||
feeKey: SendFeesFormValues['feeLevel']; | ||
feeLevel: GeneralPrecomposedTransactionFinal; | ||
networkSymbol: NetworkSymbol; | ||
transactionBytes: number; | ||
}) => { | ||
const { utils } = useNativeStyles(); | ||
const { applyStyle } = useNativeStyles(); | ||
const { watch, setValue } = useContext(FormContext); | ||
const selectedLevel = watch('feeLevel'); | ||
|
||
const feeTimeEstimate = useSelector((state: FeesRootState) => | ||
selectNetworkFeeLevelTimeEstimate(state, feeKey, networkSymbol), | ||
); | ||
|
||
const isChecked = selectedLevel === feeKey; | ||
const feePerUnit = useSelector((state: FeesRootState) => | ||
selectNetworkFeeLevelFeePerUnit(state, feeKey, networkSymbol), | ||
); | ||
|
||
const { label } = feeLabelsMap[feeKey]; | ||
const isErrorFee = feeLevel.type !== 'final'; | ||
|
||
const handleSelectFeeLevel = () => { | ||
setValue('feeLevel', feeKey, { | ||
shouldValidate: true, | ||
}); | ||
}; | ||
|
||
const selectedLevel = watch('feeLevel'); | ||
const isChecked = selectedLevel === feeKey; | ||
|
||
const highlightColor: Color = isErrorFee | ||
? 'backgroundAlertRedBold' | ||
: 'backgroundSecondaryDefault'; | ||
|
||
const borderAnimationValue = useDerivedValue( | ||
() => (isChecked ? withTiming(1) : withTiming(0)), | ||
[isChecked], | ||
); | ||
|
||
const animatedCardStyle = useAnimatedStyle( | ||
() => ({ | ||
borderColor: interpolateColor( | ||
borderAnimationValue.value, | ||
[0, 1], | ||
[utils.colors.backgroundSurfaceElevation0, utils.colors[highlightColor]], | ||
), | ||
}), | ||
[borderAnimationValue, highlightColor], | ||
); | ||
|
||
const label = feeLabelsMap[feeKey]; | ||
const networkType = getNetworkType(networkSymbol); | ||
const feeUnits = getFeeUnits(networkType); | ||
const formattedFeePerUnit = `${feePerUnit} ${feeUnits}`; | ||
|
||
// If trezor-connect was not able to compose the fee level, we have to mock its value. | ||
const mockedFee = transactionBytes * Number(feePerUnit); | ||
const fee = isErrorFee ? mockedFee.toString() : feeLevel.fee; | ||
|
||
return ( | ||
<TouchableOpacity onPress={handleSelectFeeLevel}> | ||
<HStack spacing="large" justifyContent="space-between" flex={1} alignItems="center"> | ||
<VStack alignItems="flex-start" spacing="extraSmall"> | ||
<Text variant="highlight"> | ||
<Translation id={label} /> | ||
</Text> | ||
<Text variant="hint" color="textSubdued"> | ||
{feeTimeEstimate} | ||
</Text> | ||
</VStack> | ||
<VStack flex={1} alignItems="flex-end" spacing="extraSmall"> | ||
<CryptoToFiatAmountFormatter | ||
variant="body" | ||
color="textDefault" | ||
value={feeLevel.fee} | ||
network={networkSymbol} | ||
/> | ||
<CryptoAmountFormatter | ||
variant="hint" | ||
color="textSubdued" | ||
value={feeLevel.fee} | ||
network={networkSymbol} | ||
isBalance={false} | ||
/> | ||
</VStack> | ||
<Radio | ||
isChecked={isChecked} | ||
value={feeKey} | ||
onPress={handleSelectFeeLevel} | ||
testID={`@send/fees-level-${feeKey}`} | ||
/> | ||
</HStack> | ||
</TouchableOpacity> | ||
<Pressable onPress={handleSelectFeeLevel}> | ||
<Animated.View | ||
style={[ | ||
applyStyle(wrapperStyle, { borderColor: highlightColor }), | ||
animatedCardStyle, | ||
]} | ||
> | ||
<Box style={applyStyle(valuesWrapperStyle)}> | ||
<HStack | ||
spacing="large" | ||
justifyContent="space-between" | ||
flex={1} | ||
alignItems="center" | ||
> | ||
<VStack alignItems="flex-start" spacing="extraSmall"> | ||
<Box alignItems="center" flexDirection="row"> | ||
<Text variant="highlight"> | ||
<Translation id={label} /> | ||
{' • '} | ||
</Text> | ||
<Text variant="hint" color="textSubdued"> | ||
{formattedFeePerUnit} | ||
</Text> | ||
</Box> | ||
<Text variant="hint" color="textSubdued"> | ||
{`~ ${feeTimeEstimate}`} | ||
</Text> | ||
</VStack> | ||
<VStack flex={1} alignItems="flex-end" spacing="extraSmall"> | ||
<CryptoToFiatAmountFormatter | ||
variant="body" | ||
color="textDefault" | ||
value={fee} | ||
network={networkSymbol} | ||
/> | ||
<CryptoAmountFormatter | ||
variant="hint" | ||
color="textSubdued" | ||
value={fee} | ||
network={networkSymbol} | ||
isBalance={false} | ||
/> | ||
</VStack> | ||
<Radio | ||
isChecked={isChecked} | ||
value={feeKey} | ||
activeColor={isErrorFee ? 'iconAlertRed' : 'backgroundPrimaryDefault'} | ||
onPress={handleSelectFeeLevel} | ||
testID={`@send/fees-level-${feeKey}`} | ||
/> | ||
</HStack> | ||
</Box> | ||
|
||
{isErrorFee && <FeeOptionErrorMessage isVisible={isChecked} />} | ||
</Animated.View> | ||
</Pressable> | ||
); | ||
}; |
Oops, something went wrong.