refactor swap token chart
This commit is contained in:
parent
9780a94ce1
commit
2c69636a8a
|
@ -14,6 +14,7 @@ import {
|
||||||
YAxis,
|
YAxis,
|
||||||
Tooltip,
|
Tooltip,
|
||||||
ResponsiveContainer,
|
ResponsiveContainer,
|
||||||
|
Text,
|
||||||
} from 'recharts'
|
} from 'recharts'
|
||||||
import FlipNumbers from 'react-flip-numbers'
|
import FlipNumbers from 'react-flip-numbers'
|
||||||
|
|
||||||
|
@ -25,6 +26,7 @@ import { COLORS } from '../../styles/colors'
|
||||||
import { useTheme } from 'next-themes'
|
import { useTheme } from 'next-themes'
|
||||||
import PercentageChange from '../shared/PercentageChange'
|
import PercentageChange from '../shared/PercentageChange'
|
||||||
import ChartRangeButtons from '../shared/ChartRangeButtons'
|
import ChartRangeButtons from '../shared/ChartRangeButtons'
|
||||||
|
import { useViewport } from 'hooks/useViewport'
|
||||||
|
|
||||||
dayjs.extend(relativeTime)
|
dayjs.extend(relativeTime)
|
||||||
|
|
||||||
|
@ -81,6 +83,43 @@ const fetchTokenInfo = async (tokenId: string) => {
|
||||||
return data
|
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 (
|
||||||
|
<Text
|
||||||
|
x={x}
|
||||||
|
y={y}
|
||||||
|
dy={value === min ? 16 : -8}
|
||||||
|
fill={theme === 'Light' ? 'rgba(0,0,0,0.6)' : 'rgba(255,255,255,0.7)'}
|
||||||
|
fontSize={10}
|
||||||
|
textAnchor={x && y && x > width / 3 ? 'end' : 'start'}
|
||||||
|
>
|
||||||
|
{formatFixedDecimals(value)}
|
||||||
|
</Text>
|
||||||
|
)
|
||||||
|
} else return <div />
|
||||||
|
}
|
||||||
|
|
||||||
const SwapTokenChart: FunctionComponent<SwapTokenChartProps> = ({
|
const SwapTokenChart: FunctionComponent<SwapTokenChartProps> = ({
|
||||||
inputTokenId,
|
inputTokenId,
|
||||||
outputTokenId,
|
outputTokenId,
|
||||||
|
@ -95,32 +134,6 @@ const SwapTokenChart: FunctionComponent<SwapTokenChartProps> = ({
|
||||||
const [daysToShow, setDaysToShow] = useState(1)
|
const [daysToShow, setDaysToShow] = useState(1)
|
||||||
const { theme } = useTheme()
|
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 (
|
|
||||||
<text
|
|
||||||
x={x}
|
|
||||||
y={y}
|
|
||||||
dy={value === min ? 16 : -8}
|
|
||||||
fill={theme === 'Light' ? 'rgba(0,0,0,0.6)' : 'rgba(255,255,255,0.7)'}
|
|
||||||
fontSize={10}
|
|
||||||
textAnchor="middle"
|
|
||||||
>
|
|
||||||
{formatFixedDecimals(value)}
|
|
||||||
</text>
|
|
||||||
)
|
|
||||||
} else return <div />
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleMouseMove = (coords: any) => {
|
const handleMouseMove = (coords: any) => {
|
||||||
if (coords.activePayload) {
|
if (coords.activePayload) {
|
||||||
setMouseData(coords.activePayload[0].payload)
|
setMouseData(coords.activePayload[0].payload)
|
||||||
|
@ -143,11 +156,11 @@ const SwapTokenChart: FunctionComponent<SwapTokenChartProps> = ({
|
||||||
}
|
}
|
||||||
}, [inputTokenId, outputTokenId])
|
}, [inputTokenId, outputTokenId])
|
||||||
|
|
||||||
const handleFlipChart = useCallback(() => {
|
// const handleFlipChart = useCallback(() => {
|
||||||
if (!baseTokenId || !quoteTokenId) return
|
// if (!baseTokenId || !quoteTokenId) return
|
||||||
setBaseTokenId(quoteTokenId)
|
// setBaseTokenId(quoteTokenId)
|
||||||
setQuoteTokenId(baseTokenId)
|
// setQuoteTokenId(baseTokenId)
|
||||||
}, [baseTokenId, quoteTokenId])
|
// }, [baseTokenId, quoteTokenId])
|
||||||
|
|
||||||
// Use ohlc data
|
// Use ohlc data
|
||||||
const getChartData = useCallback(async () => {
|
const getChartData = useCallback(async () => {
|
||||||
|
@ -215,7 +228,7 @@ const SwapTokenChart: FunctionComponent<SwapTokenChartProps> = ({
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ContentBox hideBorder hidePadding className="h-full">
|
<ContentBox hideBorder className="h-full">
|
||||||
{loadChartData ? (
|
{loadChartData ? (
|
||||||
<>
|
<>
|
||||||
<SheenLoader className="w-[148px] rounded-md">
|
<SheenLoader className="w-[148px] rounded-md">
|
||||||
|
@ -295,7 +308,7 @@ const SwapTokenChart: FunctionComponent<SwapTokenChartProps> = ({
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="mt-2 h-28 w-1/2 md:h-72 md:w-auto">
|
<div className="mt-2 h-28 w-1/2 md:h-80 md:w-auto">
|
||||||
<div className="-mb-2 flex justify-end md:absolute md:-top-1 md:right-0">
|
<div className="-mb-2 flex justify-end md:absolute md:-top-1 md:right-0">
|
||||||
<ChartRangeButtons
|
<ChartRangeButtons
|
||||||
activeValue={daysToShow}
|
activeValue={daysToShow}
|
||||||
|
@ -304,12 +317,13 @@ const SwapTokenChart: FunctionComponent<SwapTokenChartProps> = ({
|
||||||
onChange={(v) => setDaysToShow(v)}
|
onChange={(v) => setDaysToShow(v)}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="-mx-6 h-full">
|
<div className="mt-4 h-full md:-mx-5">
|
||||||
<ResponsiveContainer width="100%" height="100%">
|
<ResponsiveContainer>
|
||||||
<AreaChart
|
<AreaChart
|
||||||
data={chartData}
|
data={chartData}
|
||||||
onMouseMove={handleMouseMove}
|
onMouseMove={handleMouseMove}
|
||||||
onMouseLeave={handleMouseLeave}
|
onMouseLeave={handleMouseLeave}
|
||||||
|
margin={{ top: 0, right: 0, bottom: 0, left: 0 }}
|
||||||
>
|
>
|
||||||
<Tooltip
|
<Tooltip
|
||||||
cursor={{
|
cursor={{
|
||||||
|
@ -332,7 +346,7 @@ const SwapTokenChart: FunctionComponent<SwapTokenChartProps> = ({
|
||||||
? COLORS.GREEN[theme]
|
? COLORS.GREEN[theme]
|
||||||
: COLORS.RED[theme]
|
: COLORS.RED[theme]
|
||||||
}
|
}
|
||||||
stopOpacity={0.15}
|
stopOpacity={0.25}
|
||||||
/>
|
/>
|
||||||
<stop
|
<stop
|
||||||
offset="99%"
|
offset="99%"
|
||||||
|
@ -356,13 +370,9 @@ const SwapTokenChart: FunctionComponent<SwapTokenChartProps> = ({
|
||||||
}
|
}
|
||||||
strokeWidth={1.5}
|
strokeWidth={1.5}
|
||||||
fill="url(#gradientArea)"
|
fill="url(#gradientArea)"
|
||||||
label={<CustomizedLabel />}
|
label={<CustomizedLabel chartData={chartData} />}
|
||||||
/>
|
|
||||||
<XAxis
|
|
||||||
dataKey="time"
|
|
||||||
hide
|
|
||||||
padding={{ left: 20, right: 20 }}
|
|
||||||
/>
|
/>
|
||||||
|
<XAxis dataKey="time" hide padding={{ left: 0, right: 0 }} />
|
||||||
<YAxis
|
<YAxis
|
||||||
dataKey="price"
|
dataKey="price"
|
||||||
type="number"
|
type="number"
|
||||||
|
|
Loading…
Reference in New Issue