From c7187748d5d8e4a7e9a121c7fba62ef089d66ddd Mon Sep 17 00:00:00 2001 From: saml33 Date: Fri, 13 Jan 2023 15:26:07 +1100 Subject: [PATCH] allow flip swap chart prices --- apis/coingecko.ts | 8 +++-- components/swap/SwapTokenChart.tsx | 56 +++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/apis/coingecko.ts b/apis/coingecko.ts index 428e9be0..4a79981d 100644 --- a/apis/coingecko.ts +++ b/apis/coingecko.ts @@ -33,12 +33,14 @@ export const fetchChartData = async ( const found = a.find((price: any) => price.time === c[0]) if (found) { if (['usd-coin', 'tether'].includes(quoteTokenId)) { - found.price = found.inputPrice / c[4] + found.price = found.p1 / c[4] + found.p2 = c[4] } else { - found.price = c[4] / found.inputPrice + found.price = c[4] / found.p1 + found.p2 = c[4] } } else { - a.push({ time: c[0], inputPrice: c[4] }) + a.push({ time: c[0], p1: c[4] }) } return a }, []) diff --git a/components/swap/SwapTokenChart.tsx b/components/swap/SwapTokenChart.tsx index b2aa4b3f..f01dd923 100644 --- a/components/swap/SwapTokenChart.tsx +++ b/components/swap/SwapTokenChart.tsx @@ -28,10 +28,17 @@ import useLocalStorageState from 'hooks/useLocalStorageState' import { ANIMATION_SETTINGS_KEY } from 'utils/constants' import { INITIAL_ANIMATION_SETTINGS } from '@components/settings/AnimationSettings' import { useTranslation } from 'next-i18next' -import { NoSymbolIcon } from '@heroicons/react/20/solid' +import { ArrowsRightLeftIcon, NoSymbolIcon } from '@heroicons/react/20/solid' dayjs.extend(relativeTime) +interface ChartDataItem { + p1: number + p2: number + price: number + time: number +} + const CustomizedLabel = ({ chartData, x, @@ -85,6 +92,7 @@ const SwapTokenChart = () => { const [quoteTokenId, setQuoteTokenId] = useState(outputCoingeckoId) const [mouseData, setMouseData] = useState(null) const [daysToShow, setDaysToShow] = useState('1') + const [flipPrices, setFlipPrices] = useState(false) const { theme } = useTheme() const [animationSettings] = useLocalStorageState( ANIMATION_SETTINGS_KEY, @@ -100,7 +108,18 @@ const SwapTokenChart = () => { enabled: !!baseTokenId && !!quoteTokenId, } ) - const chartData = chartDataQuery.data + + const chartData = useMemo(() => { + if (!chartDataQuery?.data?.length) return [] + if (!flipPrices) { + return chartDataQuery.data + } else { + return chartDataQuery.data.map((d: ChartDataItem) => { + const price = d.p1 / d.p2 === d.price ? d.p2 / d.p1 : d.p1 / d.p2 + return { ...d, price: price } + }) + } + }, [flipPrices, chartDataQuery]) const handleMouseMove = (coords: any) => { if (coords.activePayload) { @@ -149,6 +168,19 @@ const SwapTokenChart = () => { return 0 } + const swapMarketName = useMemo(() => { + if (!inputBank || !outputBank) return '' + const inputSymbol = formatTokenSymbol(inputBank.name?.toUpperCase()) + const outputSymbol = formatTokenSymbol(outputBank.name?.toUpperCase()) + return ['usd-coin', 'tether'].includes(inputCoingeckoId || '') + ? !flipPrices + ? `${outputSymbol}/${inputSymbol}` + : `${inputSymbol}/${outputSymbol}` + : !flipPrices + ? `${inputSymbol}/${outputSymbol}` + : `${outputSymbol}/${inputSymbol}` + }, [flipPrices, inputBank, inputCoingeckoId, outputBank]) + return ( {chartDataQuery?.isLoading || chartDataQuery.isFetching ? ( @@ -169,23 +201,13 @@ const SwapTokenChart = () => {
{inputBank && outputBank ? (
-

- {['usd-coin', 'tether'].includes(inputCoingeckoId || '') - ? `${formatTokenSymbol( - outputBank?.name?.toUpperCase() - )}/${inputBank?.name?.toUpperCase()}` - : `${formatTokenSymbol( - inputBank?.name?.toUpperCase() - )}/${formatTokenSymbol( - outputBank?.name?.toUpperCase() - )}`} -

- {/*
{swapMarketName}

+
setFlipPrices(!flipPrices)} > - -
*/} + +
) : null} {mouseData ? (