Skip to content

Commit

Permalink
fix: ignore old network requests
Browse files Browse the repository at this point in the history
  • Loading branch information
renanvalentin committed Feb 29, 2024
1 parent cff8659 commit 1ed1e6e
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/ui/app/pages/send.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ const AddressPopup = ({
accounts: {},
recentAddress: null,
});
const latestAddressSearchId = React.useRef(0);
const init = async () => {
const currentAccount = await getCurrentAccount();
const accounts = await getAccounts();
Expand Down Expand Up @@ -942,27 +943,29 @@ const AddressPopup = ({

if (isHandle) {
const handle = e.target.value;

const resolvedAddress = await getAdaHandle(handle.slice(1));
if (handle.length > 1 && (await isValidAddress(resolvedAddress))) {
addr = {
result: resolvedAddress,
display: e.target.value,
display: handle,
};
} else {
addr = {
result: '',
display: e.target.value,
display: handle,
error: '$handle not found',
};
}
} else if (isM1) {
const value = e.target.value;
const { isAllowed, ada, current_address, protocolMagic, assets, ttl } =
await getMilkomedaData(e.target.value);
await getMilkomedaData(value);

if (!isAllowed || !isValidEthAddress(e.target.value)) {
if (!isAllowed || !isValidEthAddress(value)) {
addr = {
result: '',
display: e.target.value,
display: value,
isM1: true,
ada,
ttl,
Expand All @@ -973,7 +976,7 @@ const AddressPopup = ({
} else {
addr = {
result: current_address,
display: e.target.value,
display: value,
isM1: true,
ada,
ttl,
Expand All @@ -982,11 +985,12 @@ const AddressPopup = ({
};
}
}

return addr;
};

const handleInputDebounced = useConstant(() =>
debouncePromise(handleInput, 300)
debouncePromise(handleInput, 0)
);

React.useEffect(() => {
Expand Down Expand Up @@ -1034,8 +1038,16 @@ const AddressPopup = ({
fontSize="xs"
placeholder="Address, $handle or Milkomeda"
onInput={async (e) => {
const currentAddressSearchId = latestAddressSearchId.current + 1;
latestAddressSearchId.current = currentAddressSearchId;

setAddress({ display: e.target.value });
const addr = await handleInputDebounced(e);

if (currentAddressSearchId < latestAddressSearchId.current) {
return;
}

if (addr.isM1) removeAllAssets();
triggerTxUpdate(() => setAddress(addr));
onClose();
Expand Down

0 comments on commit 1ed1e6e

Please sign in to comment.