Commit baf00f20 authored by Vignesh Mohankumar's avatar Vignesh Mohankumar Committed by GitHub

feat: use filterTimeAtom for PriceChart (#4323)

* feat: use filterTimeAtom

* comment
parent 1309b1f1
...@@ -3,10 +3,12 @@ import { localPoint } from '@visx/event' ...@@ -3,10 +3,12 @@ import { localPoint } from '@visx/event'
import { EventType } from '@visx/event/lib/types' import { EventType } from '@visx/event/lib/types'
import { GlyphCircle } from '@visx/glyph' import { GlyphCircle } from '@visx/glyph'
import { Line } from '@visx/shape' import { Line } from '@visx/shape'
import { filterTimeAtom } from 'components/Explore/state'
import { bisect, curveBasis, NumberValue, scaleLinear } from 'd3' import { bisect, curveBasis, NumberValue, scaleLinear } from 'd3'
import { useActiveLocale } from 'hooks/useActiveLocale' import { useActiveLocale } from 'hooks/useActiveLocale'
import useTheme from 'hooks/useTheme' import useTheme from 'hooks/useTheme'
import { TimePeriod } from 'hooks/useTopTokens' import { TimePeriod } from 'hooks/useTopTokens'
import { useAtom } from 'jotai'
import { useCallback, useState } from 'react' import { useCallback, useState } from 'react'
import { ArrowDownRight, ArrowUpRight } from 'react-feather' import { ArrowDownRight, ArrowUpRight } from 'react-feather'
import styled from 'styled-components/macro' import styled from 'styled-components/macro'
...@@ -23,6 +25,7 @@ import { ...@@ -23,6 +25,7 @@ import {
import data from './data.json' import data from './data.json'
import LineChart from './LineChart' import LineChart from './LineChart'
// TODO: This should be combined with the logic in TimeSelector.
const TIME_DISPLAYS: [TimePeriod, string][] = [ const TIME_DISPLAYS: [TimePeriod, string][] = [
[TimePeriod.hour, '1H'], [TimePeriod.hour, '1H'],
[TimePeriod.day, '1D'], [TimePeriod.day, '1D'],
...@@ -110,10 +113,10 @@ function getTicks(startTimestamp: number, endTimestamp: number, numTicks = 5) { ...@@ -110,10 +113,10 @@ function getTicks(startTimestamp: number, endTimestamp: number, numTicks = 5) {
function tickFormat( function tickFormat(
startTimestamp: number, startTimestamp: number,
endTimestamp: number, endTimestamp: number,
activeTimePeriod: TimePeriod, timePeriod: TimePeriod,
locale: string locale: string
): [TickFormatter<NumberValue>, (v: number) => string, number[]] { ): [TickFormatter<NumberValue>, (v: number) => string, number[]] {
switch (activeTimePeriod) { switch (timePeriod) {
case TimePeriod.hour: case TimePeriod.hour:
return [hourFormatter(locale), dayHourFormatter(locale), getTicks(startTimestamp, endTimestamp)] return [hourFormatter(locale), dayHourFormatter(locale), getTicks(startTimestamp, endTimestamp)]
case TimePeriod.day: case TimePeriod.day:
...@@ -139,12 +142,12 @@ interface PriceChartProps { ...@@ -139,12 +142,12 @@ interface PriceChartProps {
} }
export function PriceChart({ width, height }: PriceChartProps) { export function PriceChart({ width, height }: PriceChartProps) {
const [activeTimePeriod, setTimePeriod] = useState(TimePeriod.hour) const [timePeriod, setTimePeriod] = useAtom(filterTimeAtom)
const locale = useActiveLocale() const locale = useActiveLocale()
const theme = useTheme() const theme = useTheme()
/* TODO: Implement API calls & cache to use here */ /* TODO: Implement API calls & cache to use here */
const pricePoints = data[activeTimePeriod] const pricePoints = data[timePeriod]
const startingPrice = pricePoints[0] const startingPrice = pricePoints[0]
const endingPrice = pricePoints[pricePoints.length - 1] const endingPrice = pricePoints[pricePoints.length - 1]
const initialState = { pricePoint: endingPrice, xCoordinate: null } const initialState = { pricePoint: endingPrice, xCoordinate: null }
...@@ -187,7 +190,7 @@ export function PriceChart({ width, height }: PriceChartProps) { ...@@ -187,7 +190,7 @@ export function PriceChart({ width, height }: PriceChartProps) {
const [tickFormatter, crosshairDateFormatter, ticks] = tickFormat( const [tickFormatter, crosshairDateFormatter, ticks] = tickFormat(
startingPrice.timestamp, startingPrice.timestamp,
endingPrice.timestamp, endingPrice.timestamp,
activeTimePeriod, timePeriod,
locale locale
) )
const [delta, arrow] = getDelta(startingPrice.value, selected.pricePoint.value) const [delta, arrow] = getDelta(startingPrice.value, selected.pricePoint.value)
...@@ -209,7 +212,7 @@ export function PriceChart({ width, height }: PriceChartProps) { ...@@ -209,7 +212,7 @@ export function PriceChart({ width, height }: PriceChartProps) {
getY={(p: PricePoint) => rdScale(p.value)} getY={(p: PricePoint) => rdScale(p.value)}
marginTop={margin.top} marginTop={margin.top}
/* Default curve doesn't look good for the ALL chart */ /* Default curve doesn't look good for the ALL chart */
curve={activeTimePeriod === TimePeriod.all ? curveBasis : undefined} curve={timePeriod === TimePeriod.all ? curveBasis : undefined}
strokeWidth={2} strokeWidth={2}
width={graphWidth} width={graphWidth}
height={graphHeight} height={graphHeight}
...@@ -273,7 +276,7 @@ export function PriceChart({ width, height }: PriceChartProps) { ...@@ -273,7 +276,7 @@ export function PriceChart({ width, height }: PriceChartProps) {
</LineChart> </LineChart>
<TimeOptionsContainer> <TimeOptionsContainer>
{TIME_DISPLAYS.map(([value, display]) => ( {TIME_DISPLAYS.map(([value, display]) => (
<TimeButton key={display} active={activeTimePeriod === value} onClick={() => setTimePeriod(value)}> <TimeButton key={display} active={timePeriod === value} onClick={() => setTimePeriod(value)}>
{display} {display}
</TimeButton> </TimeButton>
))} ))}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment