chore: refactor TDP time selector (#7600)

* feat: [info] add tdp charts toggle, WIP

* some refactoring

* remove chartType related changes after cherrypick

* nit

* remove setTransition
This commit is contained in:
Kristie Huang 2023-11-16 13:46:25 -05:00 committed by GitHub
parent 2e618fb2aa
commit 90497dc08a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 66 deletions

@ -1,15 +1,24 @@
import { ParentSize } from '@visx/responsive' import { ParentSize } from '@visx/responsive'
import { ChartContainer, LoadingChart } from 'components/Tokens/TokenDetails/Skeleton' import { LoadingChart } from 'components/Tokens/TokenDetails/Skeleton'
import { TokenPriceQuery } from 'graphql/data/TokenPrice' import { TokenPriceQuery } from 'graphql/data/TokenPrice'
import { isPricePoint, PricePoint } from 'graphql/data/util' import { isPricePoint, PricePoint } from 'graphql/data/util'
import { TimePeriod } from 'graphql/data/util'
import { useAtomValue } from 'jotai/utils' import { useAtomValue } from 'jotai/utils'
import { pageTimePeriodAtom } from 'pages/TokenDetails' import { pageTimePeriodAtom } from 'pages/TokenDetails'
import { startTransition, Suspense, useMemo } from 'react' import { Suspense, useMemo } from 'react'
import styled from 'styled-components'
import { PriceChart } from '../../Charts/PriceChart' import { PriceChart } from '../../Charts/PriceChart'
import TimePeriodSelector from './TimeSelector' import TimePeriodSelector from './TimeSelector'
export const ChartContainer = styled.div`
display: flex;
flex-direction: column;
height: 436px;
margin-bottom: 24px;
align-items: flex-start;
width: 100%;
`
function usePriceHistory(tokenPriceData: TokenPriceQuery): PricePoint[] | undefined { function usePriceHistory(tokenPriceData: TokenPriceQuery): PricePoint[] | undefined {
// Appends the current price to the end of the priceHistory array // Appends the current price to the end of the priceHistory array
const priceHistory = useMemo(() => { const priceHistory = useMemo(() => {
@ -25,49 +34,28 @@ function usePriceHistory(tokenPriceData: TokenPriceQuery): PricePoint[] | undefi
return priceHistory return priceHistory
} }
export default function ChartSection({ export default function ChartSection({ tokenPriceQuery }: { tokenPriceQuery?: TokenPriceQuery }) {
tokenPriceQuery,
onChangeTimePeriod,
}: {
tokenPriceQuery?: TokenPriceQuery
onChangeTimePeriod: OnChangeTimePeriod
}) {
if (!tokenPriceQuery) { if (!tokenPriceQuery) {
return <LoadingChart /> return <LoadingChart />
} }
return ( return (
<Suspense fallback={<LoadingChart />}> <Suspense fallback={<LoadingChart />}>
<ChartContainer> <ChartContainer data-testid="chart-container">
<Chart tokenPriceQuery={tokenPriceQuery} onChangeTimePeriod={onChangeTimePeriod} /> <Chart tokenPriceQuery={tokenPriceQuery} />
<TimePeriodSelector />
</ChartContainer> </ChartContainer>
</Suspense> </Suspense>
) )
} }
export type OnChangeTimePeriod = (t: TimePeriod) => void function Chart({ tokenPriceQuery }: { tokenPriceQuery: TokenPriceQuery }) {
function Chart({
tokenPriceQuery,
onChangeTimePeriod,
}: {
tokenPriceQuery: TokenPriceQuery
onChangeTimePeriod: OnChangeTimePeriod
}) {
const prices = usePriceHistory(tokenPriceQuery) const prices = usePriceHistory(tokenPriceQuery)
// Initializes time period to global & maintain separate time period for subsequent changes
const timePeriod = useAtomValue(pageTimePeriodAtom) const timePeriod = useAtomValue(pageTimePeriodAtom)
return ( return (
<ChartContainer data-testid="chart-container">
<ParentSize> <ParentSize>
{({ width }) => <PriceChart prices={prices} width={width} height={392} timePeriod={timePeriod} />} {({ width }) => <PriceChart prices={prices} width={width} height={392} timePeriod={timePeriod} />}
</ParentSize> </ParentSize>
<TimePeriodSelector
currentTimePeriod={timePeriod}
onTimeChange={(t: TimePeriod) => {
startTransition(() => onChangeTimePeriod(t))
}}
/>
</ChartContainer>
) )
} }

@ -11,6 +11,7 @@ import { textFadeIn } from 'theme/styles'
import { LoadingBubble } from '../loading' import { LoadingBubble } from '../loading'
import { AboutContainer, AboutHeader } from './About' import { AboutContainer, AboutHeader } from './About'
import { BreadcrumbNav, BreadcrumbNavLink } from './BreadcrumbNavLink' import { BreadcrumbNav, BreadcrumbNavLink } from './BreadcrumbNavLink'
import { ChartContainer } from './ChartSection'
import { StatPair, StatsWrapper, StatWrapper } from './StatsSection' import { StatPair, StatsWrapper, StatWrapper } from './StatsSection'
const SWAP_COMPONENT_WIDTH = 360 const SWAP_COMPONENT_WIDTH = 360
@ -53,14 +54,6 @@ export const RightPanel = styled.div<{ isInfoTDPEnabled?: boolean }>`
display: flex; display: flex;
} }
` `
export const ChartContainer = styled.div`
display: flex;
flex-direction: column;
height: 436px;
margin-bottom: 24px;
align-items: flex-start;
width: 100%;
`
const LoadingChartContainer = styled.div` const LoadingChartContainer = styled.div`
display: flex; display: flex;
flex-direction: row; flex-direction: row;

@ -1,5 +1,5 @@
import { TimePeriod } from 'graphql/data/util' import { useAtom } from 'jotai'
import { startTransition, useState } from 'react' import { pageTimePeriodAtom } from 'pages/TokenDetails'
import styled from 'styled-components' import styled from 'styled-components'
import { MEDIUM_MEDIA_BREAKPOINT } from '../constants' import { MEDIUM_MEDIA_BREAKPOINT } from '../constants'
@ -46,26 +46,13 @@ const TimeButton = styled.button<{ active: boolean }>`
} }
` `
export default function TimePeriodSelector({ export default function TimePeriodSelector() {
currentTimePeriod, const [timePeriod, setTimePeriod] = useAtom(pageTimePeriodAtom)
onTimeChange,
}: {
currentTimePeriod: TimePeriod
onTimeChange: (t: TimePeriod) => void
}) {
const [timePeriod, setTimePeriod] = useState(currentTimePeriod)
return ( return (
<TimeOptionsWrapper> <TimeOptionsWrapper>
<TimeOptionsContainer> <TimeOptionsContainer>
{ORDERED_TIMES.map((time) => ( {ORDERED_TIMES.map((time) => (
<TimeButton <TimeButton key={DISPLAYS[time]} active={timePeriod === time} onClick={() => setTimePeriod(time)}>
key={DISPLAYS[time]}
active={timePeriod === time}
onClick={() => {
startTransition(() => onTimeChange(time))
setTimePeriod(time)
}}
>
{DISPLAYS[time]} {DISPLAYS[time]}
</TimeButton> </TimeButton>
))} ))}

@ -41,7 +41,6 @@ import { CopyContractAddress } from 'theme/components'
import { isAddress, shortenAddress } from 'utils' import { isAddress, shortenAddress } from 'utils'
import { addressesAreEquivalent } from 'utils/addressesAreEquivalent' import { addressesAreEquivalent } from 'utils/addressesAreEquivalent'
import { OnChangeTimePeriod } from './ChartSection'
import InvalidTokenDetails from './InvalidTokenDetails' import InvalidTokenDetails from './InvalidTokenDetails'
import { TokenDescription } from './TokenDescription' import { TokenDescription } from './TokenDescription'
@ -102,7 +101,6 @@ type TokenDetailsProps = {
chain: InterfaceGqlChain chain: InterfaceGqlChain
tokenQuery: TokenQuery tokenQuery: TokenQuery
tokenPriceQuery?: TokenPriceQuery tokenPriceQuery?: TokenPriceQuery
onChangeTimePeriod: OnChangeTimePeriod
} }
export default function TokenDetails({ export default function TokenDetails({
urlAddress, urlAddress,
@ -110,7 +108,6 @@ export default function TokenDetails({
chain, chain,
tokenQuery, tokenQuery,
tokenPriceQuery, tokenPriceQuery,
onChangeTimePeriod,
}: TokenDetailsProps) { }: TokenDetailsProps) {
if (!urlAddress) { if (!urlAddress) {
throw new Error('Invalid token details route: tokenAddress param is undefined') throw new Error('Invalid token details route: tokenAddress param is undefined')
@ -258,7 +255,7 @@ export default function TokenDetails({
<ShareButton currency={detailedToken} /> <ShareButton currency={detailedToken} />
</TokenActions> </TokenActions>
</TokenInfoContainer> </TokenInfoContainer>
<ChartSection tokenPriceQuery={tokenPriceQuery} onChangeTimePeriod={onChangeTimePeriod} /> <ChartSection tokenPriceQuery={tokenPriceQuery} />
<StatsSection chainId={pageChainId} address={address} tokenQueryData={tokenQueryData} /> <StatsSection chainId={pageChainId} address={address} tokenQueryData={tokenQueryData} />
<Hr /> <Hr />

@ -4,8 +4,7 @@ import { NATIVE_CHAIN_ID } from 'constants/tokens'
import { useTokenPriceQuery, useTokenQuery } from 'graphql/data/__generated__/types-and-hooks' import { useTokenPriceQuery, useTokenQuery } from 'graphql/data/__generated__/types-and-hooks'
import { TimePeriod, toHistoryDuration, validateUrlChainParam } from 'graphql/data/util' import { TimePeriod, toHistoryDuration, validateUrlChainParam } from 'graphql/data/util'
import useParsedQueryString from 'hooks/useParsedQueryString' import useParsedQueryString from 'hooks/useParsedQueryString'
import { useAtom } from 'jotai' import { atomWithStorage, useAtomValue } from 'jotai/utils'
import { atomWithStorage } from 'jotai/utils'
import { useEffect, useMemo, useState } from 'react' import { useEffect, useMemo, useState } from 'react'
import { useParams } from 'react-router-dom' import { useParams } from 'react-router-dom'
import { getNativeTokenDBAddress } from 'utils/nativeTokens' import { getNativeTokenDBAddress } from 'utils/nativeTokens'
@ -19,7 +18,7 @@ export default function TokenDetailsPage() {
}>() }>()
const chain = validateUrlChainParam(chainName) const chain = validateUrlChainParam(chainName)
const isNative = tokenAddress === NATIVE_CHAIN_ID const isNative = tokenAddress === NATIVE_CHAIN_ID
const [timePeriod, setTimePeriod] = useAtom(pageTimePeriodAtom) const timePeriod = useAtomValue(pageTimePeriodAtom)
const [detailedTokenAddress, duration] = useMemo( const [detailedTokenAddress, duration] = useMemo(
// tokenAddress will always be defined in the path for for this page to render, but useParams will always // tokenAddress will always be defined in the path for for this page to render, but useParams will always
// return optional arguments; nullish coalescing operator is present here to appease typechecker // return optional arguments; nullish coalescing operator is present here to appease typechecker
@ -64,7 +63,6 @@ export default function TokenDetailsPage() {
chain={chain} chain={chain}
tokenQuery={tokenQuery} tokenQuery={tokenQuery}
tokenPriceQuery={currentPriceQuery} tokenPriceQuery={currentPriceQuery}
onChangeTimePeriod={setTimePeriod}
inputTokenAddress={parsedInputTokenAddress} inputTokenAddress={parsedInputTokenAddress}
/> />
) )