From 2c69636a8ab9087246ac844eb66f47a9532028e7 Mon Sep 17 00:00:00 2001 From: tjs Date: Wed, 14 Sep 2022 15:38:43 -0400 Subject: [PATCH] refactor swap token chart --- components/swap/SwapTokenChart.tsx | 94 +++++++++++++++++------------- 1 file changed, 52 insertions(+), 42 deletions(-) diff --git a/components/swap/SwapTokenChart.tsx b/components/swap/SwapTokenChart.tsx index fd968bdc..54ddfece 100644 --- a/components/swap/SwapTokenChart.tsx +++ b/components/swap/SwapTokenChart.tsx @@ -14,6 +14,7 @@ import { YAxis, Tooltip, ResponsiveContainer, + Text, } from 'recharts' import FlipNumbers from 'react-flip-numbers' @@ -25,6 +26,7 @@ import { COLORS } from '../../styles/colors' import { useTheme } from 'next-themes' import PercentageChange from '../shared/PercentageChange' import ChartRangeButtons from '../shared/ChartRangeButtons' +import { useViewport } from 'hooks/useViewport' dayjs.extend(relativeTime) @@ -81,6 +83,43 @@ const fetchTokenInfo = async (tokenId: string) => { return data } +const CustomizedLabel = ({ + chartData, + x, + y, + value, +}: { + chartData: any[] + x?: number + y?: string | number + value?: number +}) => { + const { width } = useViewport() + const { theme } = useTheme() + const [min, max] = useMemo(() => { + if (chartData.length) { + const prices = chartData.map((d: any) => d.price) + return [Math.min(...prices), Math.max(...prices)] + } + return ['', ''] + }, [chartData]) + + if (value === min || value === max) { + return ( + width / 3 ? 'end' : 'start'} + > + {formatFixedDecimals(value)} + + ) + } else return
+} + const SwapTokenChart: FunctionComponent = ({ inputTokenId, outputTokenId, @@ -95,32 +134,6 @@ const SwapTokenChart: FunctionComponent = ({ const [daysToShow, setDaysToShow] = useState(1) const { theme } = useTheme() - const [min, max] = useMemo(() => { - if (chartData.length) { - const prices = chartData.map((d: any) => d.price) - return [Math.min(...prices), Math.max(...prices)] - } - return ['', ''] - }, [chartData]) - - const CustomizedLabel = (props: any) => { - const { x, y, value } = props - if (value === min || value === max) { - return ( - - {formatFixedDecimals(value)} - - ) - } else return
- } - const handleMouseMove = (coords: any) => { if (coords.activePayload) { setMouseData(coords.activePayload[0].payload) @@ -143,11 +156,11 @@ const SwapTokenChart: FunctionComponent = ({ } }, [inputTokenId, outputTokenId]) - const handleFlipChart = useCallback(() => { - if (!baseTokenId || !quoteTokenId) return - setBaseTokenId(quoteTokenId) - setQuoteTokenId(baseTokenId) - }, [baseTokenId, quoteTokenId]) + // const handleFlipChart = useCallback(() => { + // if (!baseTokenId || !quoteTokenId) return + // setBaseTokenId(quoteTokenId) + // setQuoteTokenId(baseTokenId) + // }, [baseTokenId, quoteTokenId]) // Use ohlc data const getChartData = useCallback(async () => { @@ -215,7 +228,7 @@ const SwapTokenChart: FunctionComponent = ({ } return ( - + {loadChartData ? ( <> @@ -295,7 +308,7 @@ const SwapTokenChart: FunctionComponent = ({ )}
-
+
= ({ onChange={(v) => setDaysToShow(v)} />
-
- +
+ = ({ ? COLORS.GREEN[theme] : COLORS.RED[theme] } - stopOpacity={0.15} + stopOpacity={0.25} /> = ({ } strokeWidth={1.5} fill="url(#gradientArea)" - label={} - /> - } /> +