|
|
|
@ -7,83 +7,75 @@ import { getBlockByNumber, getBlockByHash } from "src/api/client"; |
|
|
|
|
import React, { useEffect, useState } from "react"; |
|
|
|
|
import { Heading } from "grommet"; |
|
|
|
|
import { config } from "../config"; |
|
|
|
|
const { availableShards } = config |
|
|
|
|
|
|
|
|
|
export const BlockPage = () => { |
|
|
|
|
// hash or number
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
const { id } = useParams(); |
|
|
|
|
const [isBlockLoading, setBlockLoading] = useState(false); |
|
|
|
|
const [block, setBlock] = useState<Block | null>(null); |
|
|
|
|
const [blockNumber, setBlockNumber] = useState<number>(0); |
|
|
|
|
const [blockShardId, setBlockShardId] = useState<null | number>(null) |
|
|
|
|
|
|
|
|
|
const { availableShards } = config |
|
|
|
|
const isIdParamNumber = "" + +id === id |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
let cleanupFunction = false; |
|
|
|
|
let isBlockFetching = true; |
|
|
|
|
|
|
|
|
|
const exec = async () => { |
|
|
|
|
let block; |
|
|
|
|
if ("" + +id === id) { |
|
|
|
|
const findBlock = async () => { |
|
|
|
|
const shardsList = blockShardId === null |
|
|
|
|
? [...availableShards] // Search in all available shards
|
|
|
|
|
: [blockShardId] // Search in specific shardId
|
|
|
|
|
setBlock(null) |
|
|
|
|
setBlockLoading(true) |
|
|
|
|
for(let i=0; i < shardsList.length; i++) { // Search block on each shard until it will be found or not
|
|
|
|
|
const shardId = shardsList[i] |
|
|
|
|
try { |
|
|
|
|
block = await getBlockByNumber([0, +id]); |
|
|
|
|
setBlockNumber(0); |
|
|
|
|
} catch { |
|
|
|
|
try { |
|
|
|
|
if (!block && availableShards.find((i) => i === 1)) { |
|
|
|
|
block = await getBlockByNumber([1, +id]); |
|
|
|
|
setBlockNumber(1); |
|
|
|
|
} |
|
|
|
|
} catch { |
|
|
|
|
try { |
|
|
|
|
if (!block && availableShards.find((i) => i === 2)) { |
|
|
|
|
block = await getBlockByNumber([2, +id]); |
|
|
|
|
setBlockNumber(2); |
|
|
|
|
} |
|
|
|
|
} catch { |
|
|
|
|
if (!block && availableShards.find((i) => i === 3)) { |
|
|
|
|
block = await getBlockByNumber([3, +id]); |
|
|
|
|
setBlockNumber(3); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (isBlockFetching) { |
|
|
|
|
const blockData = isIdParamNumber |
|
|
|
|
? await getBlockByNumber([shardId, +id]) |
|
|
|
|
: await getBlockByHash([shardId, id]); |
|
|
|
|
setBlock(blockData as Block); |
|
|
|
|
setBlockShardId(shardId); |
|
|
|
|
break |
|
|
|
|
} else { |
|
|
|
|
try { |
|
|
|
|
block = await getBlockByHash([0, id]); |
|
|
|
|
setBlockNumber(0); |
|
|
|
|
} catch { |
|
|
|
|
try { |
|
|
|
|
if (!block && availableShards.find((i) => i === 1)) { |
|
|
|
|
block = await getBlockByHash([1, id]); |
|
|
|
|
setBlockNumber(1); |
|
|
|
|
} |
|
|
|
|
} catch { |
|
|
|
|
try { |
|
|
|
|
if (!block && availableShards.find((i) => i === 2)) { |
|
|
|
|
block = await getBlockByHash([2, id]); |
|
|
|
|
setBlockNumber(2); |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
} catch { |
|
|
|
|
if (!block && availableShards.find((i) => i === 3)) { |
|
|
|
|
block = await getBlockByHash([3, id]); |
|
|
|
|
setBlockNumber(3); |
|
|
|
|
} catch (e) { |
|
|
|
|
console.log(`Block with id/hash "${id}" not found on shard "${shardId}"`) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
setBlockLoading(false) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (!cleanupFunction) { |
|
|
|
|
setBlock(block as Block); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
exec(); |
|
|
|
|
|
|
|
|
|
findBlock(); |
|
|
|
|
|
|
|
|
|
return () => { |
|
|
|
|
cleanupFunction = true; |
|
|
|
|
isBlockFetching = false |
|
|
|
|
}; |
|
|
|
|
}, [id]); |
|
|
|
|
}, [id, blockShardId]); |
|
|
|
|
|
|
|
|
|
if (!block) { |
|
|
|
|
return null; |
|
|
|
|
if (!isIdParamNumber) { |
|
|
|
|
return null |
|
|
|
|
} |
|
|
|
|
const blockDetails = { number: id } as Block |
|
|
|
|
return ( |
|
|
|
|
<> |
|
|
|
|
<Heading size="xsmall" margin={{ top: "0" }}> |
|
|
|
|
Block <b>#{id} |
|
|
|
|
{!isBlockLoading && ' not found'}</b> |
|
|
|
|
</Heading> |
|
|
|
|
<BasePage> |
|
|
|
|
<BlockDetails |
|
|
|
|
block={blockDetails} |
|
|
|
|
blockShardId={blockShardId || 0} |
|
|
|
|
isShardIdSelectAvailable={isIdParamNumber} |
|
|
|
|
onSelectShardId={(shardId: number) => setBlockShardId(shardId)} |
|
|
|
|
/> |
|
|
|
|
</BasePage> |
|
|
|
|
</> |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
@ -92,7 +84,12 @@ export const BlockPage = () => { |
|
|
|
|
Block <b>#{block.number}</b> |
|
|
|
|
</Heading> |
|
|
|
|
<BasePage> |
|
|
|
|
<BlockDetails block={block} blockNumber={blockNumber} /> |
|
|
|
|
<BlockDetails |
|
|
|
|
block={block} |
|
|
|
|
blockShardId={blockShardId || 0} |
|
|
|
|
isShardIdSelectAvailable={isIdParamNumber} |
|
|
|
|
onSelectShardId={(shardId: number) => setBlockShardId(shardId)} |
|
|
|
|
/> |
|
|
|
|
</BasePage> |
|
|
|
|
</> |
|
|
|
|
); |
|
|
|
|