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

Update summary cards each 30 seconds #107

Merged
merged 3 commits into from
Jun 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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