import { CopyText } from "@/components/Copy" import { Popover } from "@/components/Popover" import { Link } from "@/hashParams" import { client$ } from "@/state/chains/chain.state" import { state, useStateObservable } from "@react-rxjs/core" import { FC } from "react" import { combineLatest, debounceTime, map, switchMap } from "rxjs" import { twMerge } from "tailwind-merge" import { BlockInfo, blocksByHeight$, finalized$ } from "./block.state" import { BlockPopover } from "./BlockPopover" import * as Finalizing from "./FinalizingTable" const best$ = client$.pipeState( switchMap((client) => client.bestBlocks$.pipe(map(([best]) => best))), ) interface PositionedBlock { block: BlockInfo position: number branched: number | null branches: number[] } const blockTable$ = state( combineLatest([blocksByHeight$, best$]).pipe( debounceTime(0), map(([blocks, best]) => { const result: Array = [] const blockPositions: Record = {} const positionsTaken = new Set() const getFreePosition = () => { for (let i = 0; ; i++) { if (!positionsTaken.has(i)) { return i } } } for (let height = best.number; blocks[height]; height--) { const competingBlocks = [...blocks[height].values()] if (competingBlocks.length > 1) { if (height === best.number) { competingBlocks.sort((a) => (a.hash === best.hash ? -1 : 1)) } else { competingBlocks.sort((a, b) => (blockPositions[a.hash] ?? Number.POSITIVE_INFINITY) < (blockPositions[b.hash] ?? Number.POSITIVE_INFINITY) ? -1 : 1, ) } } competingBlocks.forEach((block) => { const branches = [...positionsTaken] const position = blockPositions[block.hash] ?? getFreePosition() if (blockPositions[block.parent] != null) { // then it means the parent was already discovered by a previous // so this is the start of a branch result.push({ block, branched: blockPositions[block.parent], branches, position, }) positionsTaken.delete(position) } else { // We put our parent underneath us blockPositions[block.parent] = position positionsTaken.add(position) result.push({ block, branched: null, branches, position, }) } }) } return result }), ), [], ) export const BlockTable = () => { const rows = useStateObservable(blockTable$) const finalized = useStateObservable(finalized$) const numberSpan = (idx: number) => { const initialIdx = idx const number = rows[idx].block.number do { idx++ } while (number === rows[idx]?.block.number) return idx - initialIdx } if (!finalized) return null return ( Recent Blocks {rows.map((row, i) => ( {rows[i - 1]?.block.number !== row.block.number ? ( 1 ? twMerge( i > 0 ? "border-y" : "border-b", "border-card-foreground/25", ) : null, row.block.number === finalized.number && "border-t-card-foreground/50", row.block.number === finalized.number + 1 && "border-b-card-foreground/50", )} > {row.block.number.toLocaleString()} ) : null}
}>
))}
) } const CELL_WIDTH = 20 const CELL_HEIGHT = 40 const CIRCLE_R = 5 const ForkRenderer: FC<{ row: PositionedBlock }> = ({ row }) => { const totalCells = Math.max(row.position, ...row.branches) + 1 const getPositionCenter = (p: number) => CELL_WIDTH * p + CELL_WIDTH / 2 return ( {row.branches.map((branch, i) => ( ))} {row.branched != null ? ( ) : row.branches.includes(row.position) ? null : ( )} ) }