/**
* 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 { AccountsIndex } from './components/AccountsIndex'
import { BlockPanel } from './components/BlockPanel'
import { BlocksIndex } from './components/BlocksIndex'
import { CallPanel } from './components/CallPanel'
import { CallsIndex } from './components/CallsIndex'
import { EventPanel } from './components/EventPanel'
import { EventsIndex } from './components/EventsIndex'
import { BlockTicker } from './components/BlockTicker'
import { ChainSwitcher } from './components/ChainSwitcher'
import { Leaderboard } from './components/Leaderboard'
import { MinerPanel } from './components/MinerPanel'
import { RuntimePanel } from './components/RuntimePanel'
import { NetworkPanel } from './components/NetworkPanel'
import { WormholePanel } from './components/WormholePanel'
import { NodeRoute } from './components/NodeRoute'
import { NodesIndex } from './components/NodesIndex'
import { RuntimesIndex } from './components/RuntimesIndex'
import { ReversibleIndex } from './components/ReversibleIndex'
import { SectionNav } from './components/SectionNav'
import { ThemeToggle } from './components/ThemeToggle'
import { StateIndex } from './components/StateIndex'
import { StatBar } from './components/StatBar'
import { AboutPanel } from './components/AboutPanel'
import { VestingPanel } from './components/VestingPanel'
import { WormholeStage } from './components/WormholeStage'
import { measuredSpan, seconds, windowSpan } from './lib/format'
import { href, isStandings, 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])
// The standings and a miner's page, which is one row of them. Everything
// else is about a thing that happened once.
// The window means something on the standings and on a miner's page — which
// is one row of those same standings — and nowhere else.
const windowApplies = isStandings(route) || route.miner !== null
// Normalise a path that resolves to the standings but is not spelled like
// them: `/quantus`, `/quantus/miner` (the standings *are* the miner index),
// and anything unrecognised, which `parse` degrades to the front page rather
// than a 404. Without this the address bar keeps saying something the page is
// not, and the section nav cannot mark where the reader is.
useEffect(() => {
if (!chain) return
const standings = isStandings(route)
const canonical = href({ chain, window: route.window })
if (standings && location.pathname !== canonical) {
navigate(canonical, { replace: true })
}
}, [chain, route, location.pathname, 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],
)
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 (
blackbeard.observer
quantus mining · this is sparta
{/* Only where a window means something. It sets the span the ranks
are computed over, so it belongs on the standings and on a
miner's page — which is one row of those same standings. A block,
an account, a runtime and the three indexes have no window: a
block happened once, a balance has no span, and a control that
does nothing where it sits teaches a reader to distrust it. */}
{windowApplies && (
)}
{/* Before the connection light and the toggle, as the last thing in
the header that is about the site rather than about the chain. */}
{chain && (
About
)}
{state.connection}
{/* Last, and outside the window selector's conditional: the theme
applies to every route, including the ones where a window means
nothing. */}
{info?.status === 'awaiting' && (
{info.display_name} has not started producing blocks yet. This page will
come alive on its own the moment it does — no refresh needed.
)}
{info?.status === 'unreachable' && (
The observer has lost contact with the {info.display_name} node. The standings below are
the last it saw.
)}
{info?.status === 'syncing' && (
The node is still importing history. Block interval — and therefore every hashrate on this
page — is nominal until it reaches the tip.
)}
{/* The wormhole page trades the headline row for the live stage. Hashrate,
difficulty, block time, miners and height are on every other page and
say nothing about the pool; the stage is the one thing that can only
be shown here, and it wants the width. */}
{route.index === 'wormhole' ? (
) : (
)}
{route.block && chain && (
)}
{route.account && chain && (
)}
{route.miner && chain && (
)}
{/* Every one of these is a page, not something opened over the board. A
URL that names one thing has that thing as its whole subject, and
leaving eleven unrelated mining rows underneath is the site failing to
notice which page it is on. The board is what `/:chain/:window` is
for. */}
{route.runtime !== null && chain && }
{route.node && chain && }
{route.index === 'block' && chain && }
{route.index === 'call' && chain && }
{route.index === 'event' && chain && }
{route.event && chain && (
)}
{route.call && chain && (
)}
{route.index === 'account' && chain && (
)}
{route.index === 'reversible' && chain && (
)}
{route.index === 'state' && chain && (
)}
{route.index === 'runtime' && chain && (
)}
{route.index === 'node' && chain && }
{route.index === 'about' && }
{route.index === 'vesting' && chain && (
)}
{route.index === 'wormhole' && chain && (
)}
{route.index === 'network' && chain && (
)}
{/* The standings and the live ticker belong to the standings route and
nowhere else. A page that names one subject has that subject as its
whole subject; leaving thirty unrelated mining rows underneath is the
site failing to notice which page it is on. This was the fourth place
spelling out "is this the standings?" by hand — and the one that kept
showing the board under a node. */}
{isStandings(route) && (