@@ -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,10 +191,72 @@ 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
+
224
+ const renderCustomTokenTemplateRows = ( ) => {
225
+ return customTokenTemplate
226
+ . sort ( ( a , b ) => ( a . key === 'contractAddress' ? - 1 : 1 ) )
227
+ . map ( item => {
228
+ if ( item . type === 'nativeAmount' ) return null
229
+ return (
230
+ < FilledTextInput
231
+ key = { item . key }
232
+ aroundRem = { 0.5 }
233
+ autoCapitalize = "none"
234
+ autoCorrect = { false }
235
+ autoFocus = { false }
236
+ placeholder = { translateDescription ( item . displayName ) }
237
+ keyboardType = { item . type === 'number' ? 'numeric' : 'default' }
238
+ value = { location . get ( item . key ) ?? '' }
239
+ onChangeText = { value => {
240
+ setLocation ( location => {
241
+ const out = new Map ( location )
242
+ out . set ( item . key , value . replace ( / \s / g, '' ) )
243
+ return out
244
+ } )
245
+
246
+ if ( item . key === 'contractAddress' ) {
247
+ autoCompleteToken ( value ) . catch ( ( ) => { } )
248
+ }
249
+ } }
250
+ />
251
+ )
252
+ } )
253
+ }
254
+
190
255
return (
191
256
< SceneWrapper avoidKeyboard >
192
257
< SceneHeader title = { tokenId == null ? lstrings . title_add_token : lstrings . title_edit_token } underline />
193
258
< ScrollView style = { styles . scroll } contentContainerStyle = { styles . scrollContainer } scrollIndicatorInsets = { SCROLL_INDICATOR_INSET_FIX } >
259
+ { renderCustomTokenTemplateRows ( ) }
194
260
< FilledTextInput
195
261
aroundRem = { 0.5 }
196
262
autoCapitalize = "characters"
@@ -209,28 +275,6 @@ function EditTokenSceneComponent(props: Props) {
209
275
value = { displayName }
210
276
onChangeText = { setDisplayName }
211
277
/>
212
- { customTokenTemplate . map ( item => {
213
- if ( item . type === 'nativeAmount' ) return null
214
- return (
215
- < FilledTextInput
216
- key = { item . key }
217
- aroundRem = { 0.5 }
218
- autoCapitalize = "none"
219
- autoCorrect = { false }
220
- autoFocus = { false }
221
- placeholder = { translateDescription ( item . displayName ) }
222
- keyboardType = { item . type === 'number' ? 'numeric' : 'default' }
223
- value = { location . get ( item . key ) ?? '' }
224
- onChangeText = { value =>
225
- setLocation ( location => {
226
- const out = new Map ( location )
227
- out . set ( item . key , value . replace ( / \s / g, '' ) )
228
- return out
229
- } )
230
- }
231
- />
232
- )
233
- } ) }
234
278
< FilledTextInput
235
279
aroundRem = { 0.5 }
236
280
autoCorrect = { false }
0 commit comments