/** * The page. * * A chain and a window, with at most one of a block, a miner or an account in * focus above the standings. All of it lives in the path — see `lib/routes` for * the grammar and why it is a path rather than a hash — so every view here can * be linked to, opened in a new tab, and returned to with the back button. */ import { useCallback, useEffect, useMemo } from 'react' import { Link, useLocation, useNavigate } from 'react-router-dom' import { AccountPanel } from './components/AccountPanel' import { BlockPanel } from './components/BlockPanel' import { BlockTicker } from './components/BlockTicker' import { ChainSwitcher } from './components/ChainSwitcher' import { Leaderboard } from './components/Leaderboard' import { MinerPanel } from './components/MinerPanel' import { StatBar } from './components/StatBar' import { seconds, windowSpan } from './lib/format' import { href, parse, WINDOWS } from './lib/routes' import { useObserver, usePinnedMiners, useWatch } from './lib/store' export default function App() { const state = useObserver() const location = useLocation() const navigate = useNavigate() const { pinned } = usePinnedMiners() const route = useMemo(() => parse(location.pathname), [location.pathname]) // Default to the busiest chain we can actually track — the list is ordered // by node count, and an untracked chain has nothing behind it. const fallbackChain = state.chains.find((c) => c.tracking === 'full')?.id ?? null const chain = route.chain ?? fallbackChain const info = useMemo( () => state.chains.find((c) => c.id === chain) ?? null, [state.chains, chain], ) // `/` is not a page, it is "whichever chain the operator put first" — a // question only the backend can answer, and only once it has answered the // chain list. Replacing rather than pushing keeps the bare domain out of the // back stack, where it would bounce the user straight forward again. useEffect(() => { if (!route.chain && fallbackChain) { navigate(href({ chain: fallbackChain, window: route.window }), { replace: true }) } }, [route.chain, route.window, fallbackChain, navigate]) useWatch(chain, route.window) /** * A height has resolved to its hash; put the hash in the address bar. * * `replace`, not a navigation: it is the same block under the name that will * still mean this block after a reorg, so it belongs in place of the height * rather than on top of it in the back stack. The panel keeps rendering from * the height it was given — `BlockPanel` keys on its own prop — so this does * not refetch. */ const blockResolved = useCallback( (hash: string) => { if (!chain) return navigate(href({ chain, block: hash }), { replace: true }) }, [chain, navigate], ) /** Back to the standings, keeping the window the panel was opened from. */ const closePanel = useCallback( () => navigate(href({ chain, window: route.window })), [chain, route.window, navigate], ) const interval = state.summary?.block_interval_seconds ?? state.summary?.target_block_time_seconds ?? 6 const activeWindow = WINDOWS.find((w) => w.id === route.window) ?? WINDOWS[1]! return (