Skip to content

Commit

Permalink
fix: undefined router address make endless loop getPairPrices
Browse files Browse the repository at this point in the history
  • Loading branch information
tiendn committed Aug 23, 2023
1 parent 8e65ec2 commit 64a4422
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/pages/info/pairs/[address].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Pool from 'views/Info/Pools/PoolPage'

const PoolPage = () => {
const router = useRouter()
return <Pool address={String(router.query.address)} />
return <Pool address={router.query.address ? String(router.query.address) : undefined} />
}

PoolPage.Layout = InfoPageLayout
Expand Down
14 changes: 11 additions & 3 deletions src/state/info/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useCallback, useEffect, useState, useMemo } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { getUnixTime, startOfHour, Duration, sub } from 'date-fns'
import useSWRImmutable from 'swr/immutable'
import useSWR from 'swr'
import { AppState, AppDispatch } from 'state'
import { isAddress } from 'utils'
import { Transaction } from 'state/info/types'
Expand Down Expand Up @@ -495,11 +496,18 @@ export const usePoolTransactionsSWR = (address: string): Transaction[] | undefin

export const usePoolChartDataSWR = (address: string): ChartEntry[] | undefined => {
const type = checkIsStableSwap() ? 'stableSwap' : 'swap'
const { data } = useSWRImmutable(
[`info/pool/chartData/${address}/${type}`],

const { data } = useSWR(
address ? `info/pool/chartData/${address}/${type}` : null,
() => fetchPoolChartData(address),
SWR_SETTINGS_WITHOUT_REFETCH,
{
...SWR_SETTINGS_WITHOUT_REFETCH,
revalidateIfStale: false,
revalidateOnFocus: false,
revalidateOnReconnect: false,
},
)

return data?.data ?? undefined
}

Expand Down
7 changes: 7 additions & 0 deletions src/state/info/queries/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ export const fetchChartData = async (
let skip = 0
let allFound = false

if (!address) {
return {
data: [],
error: false,
}
}

while (!allFound) {
// eslint-disable-next-line no-await-in-loop
const { data, error: fetchError } = await getEntityDayDatas(skip, address)
Expand Down
4 changes: 4 additions & 0 deletions src/state/info/queries/pools/chartData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { mapPairDayData, fetchChartData } from '../helpers'
*/
const getPoolChartData = async (skip: number, address: string): Promise<{ data?: ChartEntry[]; error: boolean }> => {
try {
if (!address || address === undefined || address === 'undefined') return { error: true, data: [] }

const query = gql`
query pairDayDatas($startTime: Int!, $skip: Int!, $address: Bytes!) {
pairDayDatas(
Expand Down Expand Up @@ -42,6 +44,8 @@ const getPoolChartData = async (skip: number, address: string): Promise<{ data?:
}

const fetchPoolChartData = async (address: string): Promise<{ data?: ChartEntry[]; error: boolean }> => {
if (!address || address === undefined || address === 'undefined') return { error: true, data: [] }

return fetchChartData(getPoolChartData, address)
}

Expand Down
2 changes: 1 addition & 1 deletion src/views/Info/Pools/PoolPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const PoolPage: React.FC<React.PropsWithChildren<{ address: string }>> = ({ addr
)

// In case somebody pastes checksummed address into url (since GraphQL expects lowercase address)
const address = routeAddress.toLowerCase()
const address = routeAddress ? routeAddress.toLowerCase() : undefined

const poolData = usePoolDatasSWR(useMemo(() => [address], [address]))[0]
const chartData = usePoolChartDataSWR(address)
Expand Down

0 comments on commit 64a4422

Please sign in to comment.