import { CircularProgress } from "@/components/CircularProgress" import { CopyText } from "@/components/Copy" import { Link } from "@/hashParams" import { groupBy } from "@/lib/groupBy" import { runtimeCtx$ } from "@/state/chains/chain.state" import { state, useStateObservable } from "@react-rxjs/core" import { FC } from "react" import { map } from "rxjs" import { blockInfoState$ } from "./block.state" import { BlockStatusIcon, statusText } from "./Detail/BlockState" import { filterEvt } from "./events.state" const maxBlockWeight$ = state( runtimeCtx$.pipe( map(({ dynamicBuilder, lookup }) => { const ct = lookup.metadata.pallets .find((p) => p.name === "System") ?.constants.find((ct) => ct.name === "BlockWeights") if (!ct) return null return dynamicBuilder.buildDefinition(ct.type).dec(ct.value) .max_block as Weight }), ), null, ) export const BlockPopover: FC<{ hash: string }> = ({ hash }) => { const block = useStateObservable(blockInfoState$(hash)) if (!block) return null const eventGroups = block.events ? groupBy(block.events, (evt) => evt.phase.type) : null const filteredEvents = eventGroups?.["ApplyExtrinsic"]?.filter(filterEvt).length ?? 0 const extrinsicEvents = block.events?.filter( (evt) => evt.event.type === "System" && ["ExtrinsicFailed", "ExtrinsicSuccess"].includes(evt.event.value.type), ) const weight = extrinsicEvents ?.map( (evt) => (evt.event.value.value.dispatch_info?.weight ?? null) as Weight | null, ) .reduce(addWeight, { proof_size: 0n, ref_time: 0n, }) ?? null return (

Block {hash.slice(0, 18)}…

Status: {statusText[block.status]}

{eventGroups && (

Events

Initialization {eventGroups["Initialization"]?.length ?? 0}
Extrinsic {filteredEvents}/ {eventGroups["ApplyExtrinsic"]?.length ?? 0} {" "} (filtered/total)
Finalization {eventGroups["Finalization"]?.length ?? 0}
)}
) } const WeightLimits: FC<{ weight: Weight | null }> = ({ weight }) => { const maxBlockWeight = useStateObservable(maxBlockWeight$) if (!weight || !maxBlockWeight) return null const refTimeP = Number(weight.ref_time) / Number(maxBlockWeight.ref_time) const proofSizeP = Number(weight.proof_size) / Number(maxBlockWeight.proof_size) const pct = (progress: number) => Math.round(progress * 100) + "%" return (
Ref Time
Proof Size
) } type Weight = { ref_time: bigint proof_size: bigint } function addWeight(a: Weight, b: Weight | null): Weight { if (!b) { console.warn("some weight is undefined", { a, b }) return a } return { proof_size: a.proof_size + b.proof_size, ref_time: a.ref_time + b.ref_time, } }