From 2efde7c4526771bcc198c637e94ba76dbda88bd5 Mon Sep 17 00:00:00 2001 From: Jaco Date: Tue, 24 Jan 2023 10:29:48 +0200 Subject: [PATCH] Wrap Latency/Stats graphs inside NextTick (#8875) --- packages/page-explorer/src/Api/index.tsx | 65 ++++++++++++-------- packages/page-explorer/src/Latency/index.tsx | 27 ++++---- packages/react-components/src/NextTick.tsx | 42 +++++++++++++ packages/react-components/src/index.tsx | 1 + 4 files changed, 96 insertions(+), 39 deletions(-) create mode 100644 packages/react-components/src/NextTick.tsx diff --git a/packages/page-explorer/src/Api/index.tsx b/packages/page-explorer/src/Api/index.tsx index 1090e830f4..b704686698 100644 --- a/packages/page-explorer/src/Api/index.tsx +++ b/packages/page-explorer/src/Api/index.tsx @@ -7,7 +7,7 @@ import type { ApiStats } from '@polkadot/react-hooks/ctx/types'; import React, { useMemo } from 'react'; import styled from 'styled-components'; -import { CardSummary, Spinner, SummaryBox } from '@polkadot/react-components'; +import { CardSummary, NextTick, SummaryBox } from '@polkadot/react-components'; import { useApiStats } from '@polkadot/react-hooks'; import { formatNumber } from '@polkadot/util'; @@ -111,38 +111,55 @@ function Api ({ className }: Props): React.ReactElement { [stats] ); - if (stats.length <= 3) { - return ; - } - - const { stats: { total: { bytesRecv, bytesSent, requests: tReq, subscriptions: tSub } } } = stats[stats.length - 1]; + const last = stats[stats.length - 1]; + const isLoaded = last && (stats.length > 3); + const EMPTY_NUMBER = 99; + const EMPTY_BYTES = 1,000kB; return (
- ('sent')}>{formatNumber(bytesSent / 1024)}kB - ('recv')}>{formatNumber(bytesRecv / 1024)}kB + ('sent')}> + {isLoaded + ? <>{formatNumber(last.stats.total.bytesSent / 1024)}kB + : EMPTY_BYTES} + + ('recv')}> + {isLoaded + ? <>{formatNumber(last.stats.total.bytesRecv / 1024)}kB + : EMPTY_BYTES} +
- ('total req')}>{formatNumber(tReq)} - ('total sub')}>{formatNumber(tSub)} + ('total req')}> + {isLoaded + ? <>{formatNumber(last.stats.total.requests)} + : EMPTY_NUMBER} + + ('total sub')}> + {isLoaded + ? <>{formatNumber(last.stats.total.subscriptions)} + : EMPTY_NUMBER} +
- ('requests made')} - value={requestsChart} - /> - ('bytes transferred')} - value={bytesChart} - /> + + ('requests made')} + value={requestsChart} + /> + ('bytes transferred')} + value={bytesChart} + /> +
); } diff --git a/packages/page-explorer/src/Latency/index.tsx b/packages/page-explorer/src/Latency/index.tsx index 96de9277e5..215eb54751 100644 --- a/packages/page-explorer/src/Latency/index.tsx +++ b/packages/page-explorer/src/Latency/index.tsx @@ -6,7 +6,7 @@ import type { ChartContents, Detail } from './types'; import React, { useEffect, useMemo, useState } from 'react'; import styled from 'styled-components'; -import { CardSummary, Spinner, SummaryBox } from '@polkadot/react-components'; +import { CardSummary, NextTick, SummaryBox } from '@polkadot/react-components'; import { formatNumber, nextTick } from '@polkadot/util'; import { useTranslation } from '../translate'; @@ -178,20 +178,17 @@ function Latency ({ className }: Props): React.ReactElement { : EMPTY_TIME} - {isLoaded - ? ORDER.map((key, i) => - shouldRender[i] && ( - - ) - ) - : - } + + {ORDER.map((key) => ( + + ))} + ); } diff --git a/packages/react-components/src/NextTick.tsx b/packages/react-components/src/NextTick.tsx new file mode 100644 index 0000000000..bd07df3749 --- /dev/null +++ b/packages/react-components/src/NextTick.tsx @@ -0,0 +1,42 @@ +// Copyright 2017-2023 @polkadot/react-components authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import React, { useEffect, useState } from 'react'; + +import { nextTick } from '@polkadot/util'; + +import Spinner from './Spinner'; + +interface Props { + children: React.ReactNode[]; + isActive?: boolean; +} + +function NextTick ({ children, isActive = true }: Props): React.ReactElement { + const [visibility, setVisibility] = useState(() => new Array(children.length).fill(false)); + + useEffect((): void => { + if (isActive) { + const index = visibility.findIndex((v) => !v); + + if (index !== -1) { + nextTick(() => + setVisibility( + visibility.map((v, i) => (i === index) || v) + ) + ); + } + } + }, [isActive, visibility]); + + return ( + <> + {isActive + ? visibility.map((isVisible, index) => isVisible && children[index]) + : + } + + ); +} + +export default React.memo(NextTick); diff --git a/packages/react-components/src/index.tsx b/packages/react-components/src/index.tsx index fb07d57955..14851fd5ee 100644 --- a/packages/react-components/src/index.tsx +++ b/packages/react-components/src/index.tsx @@ -71,6 +71,7 @@ export { default as MarkError } from './MarkError'; export { default as MarkWarning } from './MarkWarning'; export { default as Menu } from './Menu'; export { default as Modal } from './Modal'; +export { default as NextTick } from './NextTick'; export { default as Nonce } from './Nonce'; export { default as Output } from './Output'; export { default as Password } from './Password';