Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

Commit

Permalink
Update summary cards each 10 seconds (#107)
Browse files Browse the repository at this point in the history
* update summary cards each 30 seconds

* move interval ms to a constant

* change interval to 10 seconds
  • Loading branch information
ramirotw authored Jun 8, 2022
1 parent e2c005c commit 27526e7
Showing 1 changed file with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { gql } from '@apollo/client'
import { COW_SDK } from 'const'
import { useEffect, useState } from 'react'
import { useCallback, useEffect, useState } from 'react'
import { useNetworkId } from 'state/network'
import { Network } from 'types'

Expand Down Expand Up @@ -79,19 +79,30 @@ export type TotalSummaryResponse = TotalSummary & {
isLoading: boolean
}

const FETCH_INTERVAL = 1000 * 10 // 10 seconds

export function useGetSummaryData(): TotalSummaryResponse | undefined {
const [summary, setSummary] = useState<TotalSummaryResponse | undefined>()
const network = useNetworkId() ?? Network.MAINNET

useEffect(() => {
const fetchAndBuildSummary = useCallback(async () => {
setSummary((summary) => ({ ...summary, isLoading: true }))
// TODO: Once the sdk's runQuery method accepts DocumentNode update this
COW_SDK[network]?.cowSubgraphApi.runQuery(summaryQuery as any).then((data: SummaryQuery) => {
const summary = buildSummary(data)
setSummary({ ...summary, isLoading: false })
})
const data = (await COW_SDK[network]?.cowSubgraphApi.runQuery(summaryQuery as any)) as SummaryQuery
const summary = buildSummary(data)
setSummary({ ...summary, isLoading: false })
}, [network])

useEffect(() => {
fetchAndBuildSummary()

const id = setInterval(() => {
fetchAndBuildSummary()
}, FETCH_INTERVAL)

return (): void => clearInterval(id)
}, [fetchAndBuildSummary])

return summary
}

Expand Down

0 comments on commit 27526e7

Please sign in to comment.