Skip to content

Commit

Permalink
fix: display same tokens warning
Browse files Browse the repository at this point in the history
  • Loading branch information
mikasackermn committed Nov 13, 2024
1 parent 346c93e commit de5524d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
12 changes: 4 additions & 8 deletions widget/embedded/src/hooks/useSwapInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useCallback, useEffect, useRef, useState } from 'react';
import { useAppStore } from '../store/AppStore';
import { useQuoteStore } from '../store/quote';
import { useWalletsStore } from '../store/wallets';
import { QuoteErrorType, QuoteWarningType } from '../types';
import { QuoteErrorType } from '../types';
import { debounce } from '../utils/common';
import { isPositiveNumber } from '../utils/numbers';
import {
Expand Down Expand Up @@ -86,7 +86,9 @@ export function useSwapInput({
const userSlippage = customSlippage ?? slippage;
const tokensValueInvalid = !fromToken || !toToken;
const shouldSkipRequest =
tokensValueInvalid || !isPositiveNumber(inputAmount);
tokensValueInvalid ||
areTokensEqual(fromToken, toToken) ||
!isPositiveNumber(inputAmount);

const resetState = (loading: boolean) => {
setLoading(loading);
Expand All @@ -105,12 +107,6 @@ export function useSwapInput({
affiliatePercent,
affiliateWallets,
} = params;
if (areTokensEqual(fromToken, toToken)) {
updateQuotePartialState('warning', {
type: QuoteWarningType.SAME_TOKENS,
});
return;
}
if (!loading) {
resetState(true);
}
Expand Down
17 changes: 13 additions & 4 deletions widget/embedded/src/pages/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SelectedQuote } from '../types';
import type { SameTokensWarning, SelectedQuote } from '../types';

import { i18n } from '@lingui/core';
import { Button, Divider, styled, WarningIcon } from '@rango-dev/ui';
Expand All @@ -19,10 +19,11 @@ import { useAppStore } from '../store/AppStore';
import { useQuoteStore } from '../store/quote';
import { useUiStore } from '../store/ui';
import { useWalletsStore } from '../store/wallets';
import { UiEventTypes } from '../types';
import { QuoteWarningType, UiEventTypes } from '../types';
import { isVariantExpandable } from '../utils/configs';
import { emitPreventableEvent } from '../utils/events';
import { getSwapButtonState, isTokensIdentical } from '../utils/swap';
import { areTokensEqual } from '../utils/wallets';

const MainContainer = styled('div', {
display: 'flex',
Expand Down Expand Up @@ -88,9 +89,16 @@ export function Home() {

const fetchingQuote = hasInputs && fetchMetaStatus === 'success' && loading;

const sameTokensWarning: SameTokensWarning | null =
areTokensEqual(fromToken, toToken) && fetchMetaStatus === 'success'
? {
type: QuoteWarningType.SAME_TOKENS,
}
: null;

const hasValidQuotes =
!isExpandable || (isExpandable && quotes?.results.length);
const hasWarningOrError = quoteWarning || quoteError;
const hasWarningOrError = quoteWarning || quoteError || sameTokensWarning;
const showMessages = hasValidQuotes && hasWarningOrError;

useEffect(() => {
Expand Down Expand Up @@ -123,6 +131,7 @@ export function Home() {
setSelectedQuote(quote);
}
};

return (
<MainContainer>
<Layout
Expand Down Expand Up @@ -207,7 +216,7 @@ export function Home() {
<>
<Divider size="10" />
<QuoteWarningsAndErrors
warning={quoteWarning}
warning={quoteWarning || sameTokensWarning}
error={quoteError}
couldChangeSettings={true}
refetchQuote={fetchQuote}
Expand Down

0 comments on commit de5524d

Please sign in to comment.