Skip to content

Commit a739dd6

Browse files
committed
Add auto-complete to EditTokenScene
1 parent 3534bdc commit a739dd6

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- added: Add eCash.
66
- added: Thorchain TCY stake and unstake
77
- added: Add Fantom to Sonic bridge
8+
- added: Use `getTokenDetails` to auto-complete fields in `EditTokenScene`
89
- added: Toast notifications for PIN changes.
910
- added: Scam warning to camera access request modal
1011
- changed: Auto launch QR scanner for multi-out payments if previously used

src/components/scenes/EditTokenScene.tsx

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ function EditTokenSceneComponent(props: Props) {
7070
return out
7171
})
7272

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+
7377
const handleDelete = useHandler(async () => {
7478
if (tokenId == null) return
7579
await Airship.show<'ok' | 'cancel' | undefined>(bridge => (
@@ -187,6 +191,36 @@ function EditTokenSceneComponent(props: Props) {
187191
}
188192
})
189193

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+
190224
const renderCustomTokenTemplateRows = () => {
191225
return customTokenTemplate
192226
.sort((a, b) => (a.key === 'contractAddress' ? -1 : 1))
@@ -202,13 +236,17 @@ function EditTokenSceneComponent(props: Props) {
202236
placeholder={translateDescription(item.displayName)}
203237
keyboardType={item.type === 'number' ? 'numeric' : 'default'}
204238
value={location.get(item.key) ?? ''}
205-
onChangeText={value =>
239+
onChangeText={value => {
206240
setLocation(location => {
207241
const out = new Map(location)
208242
out.set(item.key, value.replace(/\s/g, ''))
209243
return out
210244
})
211-
}
245+
246+
if (item.key === 'contractAddress') {
247+
autoCompleteToken(value).catch(() => {})
248+
}
249+
}}
212250
/>
213251
)
214252
})

0 commit comments

Comments
 (0)