import { useApi } from '@gear-js/react-hooks'; import { U128 } from '@polkadot/types'; import { useState, useEffect } from 'react'; import { CSSTransition } from 'react-transition-group'; import clsx from 'clsx'; import { useLocation } from 'react-router-dom'; import { useBlocks, useOutsideClick } from '@/hooks'; import { AnimationTimeout } from '@/shared/config'; import ArrowSVG from '@/shared/assets/images/actions/arrowRight.svg?react'; import { RecentBlock } from '../types'; import { getMinWidth } from '../helpers'; import { Graph } from './graph'; import { RecentBlocksList } from './recentBlocksList'; import styles from './RecentBlocks.module.scss'; const RecentBlocks = () => { const { api, isApiReady } = useApi(); const blocks = useBlocks(); const [block, setBlock] = useState(); const [gearBlock, setGearBlock] = useState(); const [isOpen, setIsOpen] = useState(false); const [timeInstance, setTimeInstance] = useState(0); const toggleList = () => setIsOpen((prevValue) => !prevValue); const closeList = () => setIsOpen(false); const sectionRef = useOutsideClick(closeList, isOpen); const location = useLocation(); useEffect(() => { const intervalId = setInterval(() => { setTimeInstance((prevState) => prevState + 0.1); }, 100); return () => { clearInterval(intervalId); }; }, []); useEffect(() => { if (!blocks.length) return; const lastBlock = blocks[0]; if (lastBlock.hash !== block?.hash) { setTimeInstance(0); } setBlock(lastBlock); // eslint-disable-next-line react-hooks/exhaustive-deps }, [blocks]); useEffect(() => { if (!isApiReady) return; api.query.gear.blockNumber((result: U128) => setGearBlock(result.toNumber())); // eslint-disable-next-line react-hooks/exhaustive-deps }, [isApiReady]); useEffect(() => { closeList(); }, [location]); const time = `${timeInstance.toFixed(1)} s`; const blockNumber = `#${block?.number ?? '00000'}`; const gearBlockNumber = `#${gearBlock ?? '00000'}`; const arrowClassName = clsx(styles.arrow, isOpen && styles.rotated); const blocksClasses = clsx(styles.recentBlocks, isOpen && styles.open); return (
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}

Recent block

{blockNumber} {time}

Gear block

{gearBlockNumber}

); }; export { RecentBlocks };