Files
explore/ui/home/fallbacks/LatestBlocksDegraded.tsx
tom goriunovandGitHub f4cab7f9d7 UI/UX: Graceful service degradation: Home page (#3233)
* fallback components

* home page graphs: fix tab title flickering

* degraded views

* fix tx degraded view when initial query is refetching

* call unwatch when components unmount

* tests

* use context to store rpc data

* stats degraded state

* update tests

* RPC data hint

* fixes

* review fixes

* condition fix

* cover ads in test
2026-02-24 11:15:58 +01:00

68 lines
1.8 KiB
TypeScript

import { Box, Flex, VStack } from '@chakra-ui/react';
import React from 'react';
import type { Block } from 'types/api/block';
import { route } from 'nextjs-routes';
import useInitialList from 'lib/hooks/useInitialList';
import { publicClient } from 'lib/web3/client';
import { BLOCK } from 'stubs/block';
import { Link } from 'toolkit/chakra/link';
import LatestBlocksItem from '../LatestBlocksItem';
import LatestBlocksFallback from './LatestBlocksFallback';
import { useHomeRpcDataContext } from './rpcDataContext';
interface Props {
maxNum: number;
}
const LatestBlocksDegraded = ({ maxNum }: Props) => {
const { blocks, isError, isLoading, enable } = useHomeRpcDataContext();
React.useEffect(() => {
enable(true, 'latest-blocks');
return () => {
enable(false, 'latest-blocks');
};
}, [ enable ]);
const initialList = useInitialList({
data: [] as Array<Block>,
idFn: (block) => block.height,
enabled: !isError,
});
if (isError || !publicClient) {
return <LatestBlocksFallback/>;
}
const items = isLoading ? Array(maxNum).fill(BLOCK) : blocks.slice(0, maxNum);
if (items.length === 0) {
return <Box textStyle="sm">No latest blocks found.</Box>;
}
return (
<>
<VStack gap={ 2 } mb={ 3 } overflow="hidden" alignItems="stretch">
{ items.map(((block, index) => (
<LatestBlocksItem
key={ block.height + (isLoading ? String(index) : '') }
block={ block }
isLoading={ isLoading }
animation={ initialList.getAnimationProp(block) }
/>
))) }
</VStack>
<Flex justifyContent="center">
<Link textStyle="sm" href={ route({ pathname: '/blocks' }) } loading={ isLoading }>View all blocks</Link>
</Flex>
</>
);
};
export default React.memo(LatestBlocksDegraded);