1616
1717import { ChangeEvent , useCallback , useState , useEffect } from 'react' ;
1818import { useTranslation } from 'react-i18next' ;
19+ import { alertUser } from '@/components/alert/Alert' ;
1920import { TOption } from '@/components/dropdown/dropdown' ;
2021import { InputWithDropdown } from '@/components/forms/input-with-dropdown' ;
2122import * as accountApi from '@/api/account' ;
2223import { getReceiveAddressList , IAccount } from '@/api/account' ;
2324import { statusChanged , syncdone } from '@/api/accountsync' ;
25+ import { connectKeystore , getKeystoreFeatures } from '@/api/keystores' ;
2426import { unsubscribe } from '@/utils/subscriptions' ;
2527import { TUnsubscribe } from '@/utils/transport-common' ;
2628import { useMountedRef } from '@/hooks/mount' ;
2729import { useMediaQuery } from '@/hooks/mediaquery' ;
30+ import { FirmwareUpgradeRequiredDialog } from '@/components/dialog/firmware-upgrade-required-dialog' ;
2831import { SpinnerRingAnimated } from '@/components/spinner/SpinnerAnimation' ;
2932import { Logo } from '@/components/icon' ;
3033import receiverStyles from './receiver-address-input.module.css' ;
@@ -73,6 +76,7 @@ export const ReceiverAddressWrapper = ({
7376 children,
7477} : TReceiverAddressWrapperProps ) => {
7578 const { t } = useTranslation ( ) ;
79+ const [ showFirmwareUpgradeDialog , setShowFirmwareUpgradeDialog ] = useState ( false ) ;
7680 const mounted = useMountedRef ( ) ;
7781 const isMobile = useMediaQuery ( '(max-width: 768px)' ) ;
7882 const [ selectedAccount , setSelectedAccount ] = useState < TOption < IAccount | null > | null > ( null ) ;
@@ -88,11 +92,34 @@ export const ReceiverAddressWrapper = ({
8892 } ;
8993 } ) : [ ] ;
9094
95+ const checkFirmwareSupport = useCallback ( async ( selectedAccount : accountApi . IAccount ) => {
96+ const rootFingerprint = selectedAccount . keystore . rootFingerprint ;
97+ const connectResult = await connectKeystore ( rootFingerprint ) ;
98+ if ( ! connectResult . success ) {
99+ return false ;
100+ }
101+ const featuresResult = await getKeystoreFeatures ( rootFingerprint ) ;
102+ if ( ! featuresResult . success ) {
103+ alertUser ( featuresResult . errorMessage || t ( 'genericError' ) ) ;
104+ return false ;
105+ }
106+ if ( ! featuresResult . features ?. supportsSendToSelf ) {
107+ setShowFirmwareUpgradeDialog ( true ) ;
108+ return false ;
109+ }
110+ return true ;
111+ } , [ t ] ) ;
112+
91113 const handleSendToAccount = useCallback ( async ( selectedOption : TAccountOption ) => {
92114 if ( selectedOption . value === null || selectedOption . disabled ) {
93115 return ;
94116 }
95117 const selectedAccountValue = selectedOption . value ;
118+
119+ const supported = await checkFirmwareSupport ( selectedAccountValue ) ;
120+ if ( ! supported ) {
121+ return ;
122+ }
96123 setSelectedAccount ( selectedOption ) ;
97124 try {
98125 const receiveAddresses = await getReceiveAddressList ( selectedAccountValue . code ) ( ) ;
@@ -104,7 +131,7 @@ export const ReceiverAddressWrapper = ({
104131 } catch ( e ) {
105132 console . error ( e ) ;
106133 }
107- } , [ onInputChange , onAccountChange ] ) ;
134+ } , [ onInputChange , onAccountChange , checkFirmwareSupport ] ) ;
108135
109136 const handleReset = useCallback ( ( ) => {
110137 setSelectedAccount ( null ) ;
@@ -141,34 +168,40 @@ export const ReceiverAddressWrapper = ({
141168 } , [ accounts , checkAccountStatus ] ) ;
142169
143170 return (
144- < InputWithDropdown
145- id = "recipientAddress"
146- label = { t ( 'send.address.label' ) }
147- error = { error }
148- align = "left"
149- placeholder = { t ( 'send.address.placeholder' ) }
150- onInput = { ( e : ChangeEvent < HTMLInputElement > ) => onInputChange ( e . target . value ) }
151- value = { recipientAddress }
152- disabled = { selectedAccount !== null }
153- autoFocus = { ! isMobile }
154- dropdownOptions = { accountOptions }
155- dropdownValue = { selectedAccount }
156- onDropdownChange = { ( selected ) => {
157- if ( selected && selected . value !== null && ! ( selected as TAccountOption ) . disabled ) {
158- handleSendToAccount ( selected as TAccountOption ) ;
159- }
160- } }
161- dropdownPlaceholder = { t ( 'send.sendToAccount.placeholder' ) }
162- dropdownTitle = { t ( 'send.sendToAccount.title' ) }
163- renderOptions = { ( e , isSelectedValue ) => < AccountOption option = { e } isSelectedValue = { isSelectedValue } /> }
164- isOptionDisabled = { ( option ) => ( option as TAccountOption ) . disabled || false }
165- labelSection = { selectedAccount ? (
166- < span role = "button" id = "sendToSelf" className = { receiverStyles . action } onClick = { handleReset } >
167- { t ( 'generic.reset' ) }
168- </ span >
169- ) : undefined }
170- >
171- { children }
172- </ InputWithDropdown >
171+ < >
172+ < InputWithDropdown
173+ id = "recipientAddress"
174+ label = { t ( 'send.address.label' ) }
175+ error = { error }
176+ align = "left"
177+ placeholder = { t ( 'send.address.placeholder' ) }
178+ onInput = { ( e : ChangeEvent < HTMLInputElement > ) => onInputChange ( e . target . value ) }
179+ value = { recipientAddress }
180+ disabled = { selectedAccount !== null }
181+ autoFocus = { ! isMobile }
182+ dropdownOptions = { accountOptions }
183+ dropdownValue = { selectedAccount }
184+ onDropdownChange = { ( selected ) => {
185+ if ( selected && selected . value !== null && ! ( selected as TAccountOption ) . disabled ) {
186+ handleSendToAccount ( selected as TAccountOption ) ;
187+ }
188+ } }
189+ dropdownPlaceholder = { t ( 'send.sendToAccount.placeholder' ) }
190+ dropdownTitle = { t ( 'send.sendToAccount.title' ) }
191+ renderOptions = { ( e , isSelectedValue ) => < AccountOption option = { e } isSelectedValue = { isSelectedValue } /> }
192+ isOptionDisabled = { ( option ) => ( option as TAccountOption ) . disabled || false }
193+ labelSection = { selectedAccount ? (
194+ < span role = "button" id = "sendToSelf" className = { receiverStyles . action } onClick = { handleReset } >
195+ { t ( 'generic.reset' ) }
196+ </ span >
197+ ) : undefined }
198+ >
199+ { children }
200+ </ InputWithDropdown >
201+ < FirmwareUpgradeRequiredDialog
202+ open = { showFirmwareUpgradeDialog }
203+ onClose = { ( ) => setShowFirmwareUpgradeDialog ( false ) }
204+ />
205+ </ >
173206 ) ;
174207} ;
0 commit comments