From c5091cf777ea42441b3ea4927eb9fd84cbcb049a Mon Sep 17 00:00:00 2001 From: Victor Oliva Date: Thu, 31 Oct 2024 09:58:23 +0100 Subject: [PATCH] improve table visualisation --- src/pages/Explorer/Explorer.tsx | 142 +++++++++++++++++------------- src/pages/Explorer/block.state.ts | 10 ++- 2 files changed, 87 insertions(+), 65 deletions(-) diff --git a/src/pages/Explorer/Explorer.tsx b/src/pages/Explorer/Explorer.tsx index 89883e8..3fcfaf7 100644 --- a/src/pages/Explorer/Explorer.tsx +++ b/src/pages/Explorer/Explorer.tsx @@ -1,16 +1,18 @@ import { withSubscribe } from "@/components/withSuspense" import { state, useStateObservable } from "@react-rxjs/core" +import { FC } from "react" import { animationFrames, + combineLatest, + debounceTime, distinctUntilChanged, map, merge, switchMap, withLatestFrom, } from "rxjs" -import { BlockInfo, blocksByHeight$, chainHead$ } from "./block.state" -import { FC } from "react" import { twMerge } from "tailwind-merge" +import { BlockInfo, blocksByHeight$, chainHead$ } from "./block.state" const finalized$ = state( chainHead$.pipe(switchMap((chainHead) => chainHead.finalized$)), @@ -68,68 +70,70 @@ export interface PositionedBlock { branched: number | null branches: number[] } -const blockTable$ = blocksByHeight$.pipeState( - withLatestFrom(best$), - map(([blocks, best]) => { - const result: Array = [] +const blockTable$ = state( + combineLatest([blocksByHeight$, best$]).pipe( + debounceTime(0), + map(([blocks, best]) => { + const result: Array = [] - const blockPositions: Record = {} - const positionsTaken = new Set() - const lockFreePosition = () => { - for (let i = 0; ; i++) { - if (!positionsTaken.has(i)) { - positionsTaken.add(i) - return i + const blockPositions: Record = {} + const positionsTaken = new Set() + const lockFreePosition = () => { + for (let i = 0; ; i++) { + if (!positionsTaken.has(i)) { + positionsTaken.add(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, + 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, + ) + } + } + const positionsMerged: number[] = [] + competingBlocks.forEach((block) => { + const branches = [...positionsTaken].filter( + (v) => !positionsMerged.includes(v), ) - } + + const position = blockPositions[block.hash] ?? lockFreePosition() + 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, + }) + positionsMerged.push(position) + } else { + // We put our parent underneath us + blockPositions[block.parent] = position + result.push({ + block, + branched: null, + branches, + position, + }) + } + }) + positionsMerged.forEach((v) => positionsTaken.delete(v)) } - const positionsMerged: number[] = [] - competingBlocks.forEach((block) => { - const branches = [...positionsTaken].filter( - (v) => !positionsMerged.includes(v), - ) - const position = blockPositions[block.hash] ?? lockFreePosition() - 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, - }) - positionsMerged.push(position) - } else { - // We put our parent underneath us - blockPositions[block.parent] = position - result.push({ - block, - branched: null, - branches, - position, - }) - } - }) - positionsMerged.forEach((v) => positionsTaken.delete(v)) - } - - return result - }), + return result + }), + ), ) const BlockTable = () => { @@ -166,8 +170,16 @@ const BlockTable = () => { )} > {rows[i - 1]?.block.number !== row.block.number ? ( - - {row.block.number} + 1 && "border-y border-slate-500", + row.block.number === finalized.number && "border-t-white", + row.block.number === finalized.number + 1 && "border-b-white", + )} + > + {row.block.number.toLocaleString()} ) : null} @@ -191,8 +203,8 @@ const BlockTable = () => { ) } -const CELL_WIDTH = 26 -const CELL_HEIGHT = 26 +const CELL_WIDTH = 20 +const CELL_HEIGHT = 36 const CIRCLE_R = 5 const ForkRenderer: FC<{ row: PositionedBlock }> = ({ row }) => { const totalCells = Math.max(row.position, ...row.branches) + 1 @@ -211,7 +223,11 @@ const ForkRenderer: FC<{ row: PositionedBlock }> = ({ row }) => { x1={getPositionCenter(branch)} y1={0} x2={getPositionCenter(branch)} - y2={CELL_HEIGHT} + y2={ + row.branched != null && branch === row.position + ? CELL_HEIGHT / 2 + : CELL_HEIGHT + } /> ))} {row.branched != null ? ( diff --git a/src/pages/Explorer/block.state.ts b/src/pages/Explorer/block.state.ts index 4b3cfc6..bdb8dff 100644 --- a/src/pages/Explorer/block.state.ts +++ b/src/pages/Explorer/block.state.ts @@ -1,14 +1,17 @@ import { chainClient$ } from "@/chain.state" -import { FollowEventWithRuntime } from "@polkadot-api/substrate-client" import { SystemEvent } from "@polkadot-api/observable-client" +import { FollowEventWithRuntime } from "@polkadot-api/substrate-client" import { state } from "@react-rxjs/core" import { partitionByKey } from "@react-rxjs/utils" import { combineLatest, + concat, concatMap, + finalize, forkJoin, map, mergeMap, + NEVER, Observable, of, scan, @@ -22,7 +25,10 @@ import { export const chainHead$ = state( chainClient$.pipe( - map(({ observableClient }) => observableClient.chainHead$()), + switchMap(({ observableClient }) => { + const chainHead = observableClient.chainHead$() + return concat(of(chainHead), NEVER).pipe(finalize(chainHead.unfollow)) + }), ), )