@@ -70,6 +70,10 @@ function EditTokenSceneComponent(props: Props) {
70
70
return out
71
71
} )
72
72
73
+ // Keep track of whether we auto-completed a token:
74
+ const [ didAutoCompleteToken , setDidAutoCompleteToken ] = React . useState < boolean > ( false )
75
+ const isAutoCompleteTokenLoading = React . useRef < boolean > ( false )
76
+
73
77
const handleDelete = useHandler ( async ( ) => {
74
78
if ( tokenId == null ) return
75
79
await Airship . show < 'ok' | 'cancel' | undefined > ( bridge => (
@@ -187,6 +191,36 @@ function EditTokenSceneComponent(props: Props) {
187
191
}
188
192
} )
189
193
194
+ const autoCompleteToken = async ( searchString : string ) => {
195
+ if (
196
+ // Ignore autocomplete if it's already loading
197
+ isAutoCompleteTokenLoading . current ||
198
+ // and ggnore autocomplete if the scene was initialized with any of the token details prefilled,
199
+ route . params . currencyCode != null ||
200
+ route . params . displayName != null ||
201
+ route . params . multiplier != null ||
202
+ route . params . networkLocation != null
203
+ ) {
204
+ return
205
+ }
206
+
207
+ isAutoCompleteTokenLoading . current = true
208
+ const [ token ] = await wallet . currencyConfig . getTokenDetails ( { contractAddress : searchString } ) . catch ( ( ) => [ ] )
209
+ isAutoCompleteTokenLoading . current = false
210
+
211
+ if ( token != null ) {
212
+ setCurrencyCode ( token . currencyCode )
213
+ setDisplayName ( token . displayName )
214
+ setDecimalPlaces ( ( token . denominations [ 0 ] . multiplier . length - 1 ) . toString ( ) )
215
+ setDidAutoCompleteToken ( true )
216
+ } else if ( token == null && didAutoCompleteToken ) {
217
+ setCurrencyCode ( '' )
218
+ setDisplayName ( '' )
219
+ setDecimalPlaces ( '18' )
220
+ setDidAutoCompleteToken ( false )
221
+ }
222
+ }
223
+
190
224
const renderCustomTokenTemplateRows = ( ) => {
191
225
return customTokenTemplate
192
226
. sort ( ( a , b ) => ( a . key === 'contractAddress' ? - 1 : 1 ) )
@@ -202,13 +236,17 @@ function EditTokenSceneComponent(props: Props) {
202
236
placeholder = { translateDescription ( item . displayName ) }
203
237
keyboardType = { item . type === 'number' ? 'numeric' : 'default' }
204
238
value = { location . get ( item . key ) ?? '' }
205
- onChangeText = { value =>
239
+ onChangeText = { value => {
206
240
setLocation ( location => {
207
241
const out = new Map ( location )
208
242
out . set ( item . key , value . replace ( / \s / g, '' ) )
209
243
return out
210
244
} )
211
- }
245
+
246
+ if ( item . key === 'contractAddress' ) {
247
+ autoCompleteToken ( value ) . catch ( ( ) => { } )
248
+ }
249
+ } }
212
250
/>
213
251
)
214
252
} )
0 commit comments