From df393a4de8d07dc3da43a4883763d52ad0f81f41 Mon Sep 17 00:00:00 2001 From: Matthew Russell Date: Tue, 7 Feb 2023 19:12:18 -0800 Subject: [PATCH] chore: fix polling not working --- .../node-switcher-2/node-switcher.tsx | 242 ++++++++++++------ 1 file changed, 167 insertions(+), 75 deletions(-) diff --git a/libs/environment/src/components/node-switcher-2/node-switcher.tsx b/libs/environment/src/components/node-switcher-2/node-switcher.tsx index e3cffdfc2..9bd2230a6 100644 --- a/libs/environment/src/components/node-switcher-2/node-switcher.tsx +++ b/libs/environment/src/components/node-switcher-2/node-switcher.tsx @@ -1,12 +1,21 @@ -import { ApolloProvider } from '@apollo/client'; +import type { ApolloClient, NormalizedCacheObject } from '@apollo/client'; import { createClient, useHeaderStore } from '@vegaprotocol/apollo-client'; -import { Dialog, Radio } from '@vegaprotocol/ui-toolkit'; +import { t } from '@vegaprotocol/react-helpers'; +import { + Button, + Dialog, + Input, + Radio, + RadioGroup, +} from '@vegaprotocol/ui-toolkit'; import classNames from 'classnames'; import type { ReactNode } from 'react'; import { useCallback, useEffect, useMemo, useState } from 'react'; import { useEnvironment } from '../../hooks'; import { useStatisticsQuery } from '../../utils/__generated__/Node'; +const POLL_INTERVAL = 3000; + export const NodeSwitcher = ({ open, setOpen, @@ -20,8 +29,7 @@ export const NodeSwitcher = ({ setUrl: store.setUrl, })); - const [customUrl, setCustomUrl] = useState(''); - const [displayCustom, setDisplayCustom] = useState(false); + const [nodeRadio, setNodeRadio] = useState(''); const [highestBlock, setHighestBlock] = useState(null); const handleHighestBlock = useCallback((blockHeight: number) => { @@ -38,83 +46,75 @@ export const NodeSwitcher = ({ return ( - - - - - - - - - - - {nodes.map((node) => { - return ( - setUrl(node)}> - - - - - ); - })} - -
noderesponse timecore block heightdatanode block height
-
- Custom - setCustomUrl(e.target.value)} - className="border" - /> - -
- - - - - - - - - - - setUrl(customUrl)}> - {displayCustom && ( - - + + + + + + + + + + {nodes.map((node) => { + return ( + + ( + <> + + + + )} + /> + + ); + })} + + - - )} - - -
noderesponse timecore block heightdatanode block height
+ noderesponse timecore block heightdatanode block height
+ + + + +
+ +
+
); }; -const RowWrapper = ({ +const ClientWrapper = ({ url, - children, + renderChildren, }: { url: string; - children: ReactNode; + renderChildren: (client: ApolloClient) => ReactNode; }) => { const client = useMemo( () => @@ -123,28 +123,58 @@ const RowWrapper = ({ cacheConfig: undefined, retry: false, connectToDevTools: false, + connectToHeaderStore: true, }), [url] ); - return {children}; + return <>{renderChildren(client)}; }; -const Row = ({ + +const RowLabels = ({ url }: { url: string }) => { + return ( + <> + + + + {url} + + ); +}; + +const RowData = ({ + client, url, highestBlock, onBlockHeight, }: { + client: ApolloClient; url: string; highestBlock: number | null; onBlockHeight: (blockHeight: number) => void; }) => { + console.log(client); const [time, setTime] = useState(); - const { data } = useStatisticsQuery({ - pollInterval: 3000, - fetchPolicy: 'no-cache', + const { data, startPolling, stopPolling } = useStatisticsQuery({ + client, + // pollInterval doesnt seem to work any more + // https://github.com/apollographql/apollo-client/issues/9819 + ssr: false, }); const headerStore = useHeaderStore(); const headers = headerStore[url]; + useEffect(() => { + const handleStartPoll = () => startPolling(POLL_INTERVAL); + const handleStopPoll = () => stopPolling(); + window.addEventListener('blur', handleStopPoll); + window.addEventListener('focus', handleStartPoll); + handleStartPoll(); + return () => { + window.removeEventListener('blur', handleStopPoll); + window.removeEventListener('focus', handleStartPoll); + }; + }, [startPolling, stopPolling]); + useEffect(() => { const requestUrl = new URL(url); const requests = window.performance.getEntriesByName(requestUrl.href); @@ -174,7 +204,6 @@ const Row = ({ return ( <> - {url} {time ? time.toFixed(2) + 'ms' : 'n/a'} {data?.statistics.blockHeight || '-'} {headers?.blockHeight || '-'} @@ -182,6 +211,69 @@ const Row = ({ ); }; +const CustomRowWrapper = ({ + highestBlock, + nodeRadio, + onBlockHeight, +}: { + highestBlock: number | null; + nodeRadio: string; + onBlockHeight: (blockHeight: number) => void; +}) => { + const [customUrl, setCustomUrl] = useState(''); + const [displayCustom, setDisplayCustom] = useState(false); + + return ( + <> + + + + + {nodeRadio === 'other' ? ( + <> + setCustomUrl(e.target.value)} + className="border" + /> + + + ) : ( + t('Other') + )} + + {displayCustom ? ( + ( + + )} + > + ) : ( + <> + + + + + )} + + ); +}; + const isValidUrl = (url?: string) => { if (!url) return false; try {