Skip to content

Commit 9a82322

Browse files
committed
Add latest blocks section to homepage
1 parent 7df6696 commit 9a82322

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { useTranslation } from 'react-i18next'
2+
import { RuntimeBlock } from '../../../oasis-nexus/api'
3+
import { Table, TableCellAlign, TableColProps } from '../../components/Table'
4+
import { paraTimesConfig } from '../../../config'
5+
import { BlockHashLink, BlockLink } from './BlockLink'
6+
import { useScreenSize } from '../../hooks/useScreensize'
7+
import { FC } from 'react'
8+
import { TableHeaderAge } from '../TableHeaderAge'
9+
import { TableCellAge } from '../TableCellAge'
10+
11+
export type TableBlockList = {
12+
blocks: RuntimeBlock[]
13+
total_count: number
14+
is_total_count_clipped: boolean
15+
}
16+
17+
type BlocksProps = {
18+
blocks?: RuntimeBlock[]
19+
isLoading: boolean
20+
limit: number
21+
}
22+
23+
export const Blocks: FC<BlocksProps> = ({ isLoading, blocks, limit }) => {
24+
const { t } = useTranslation()
25+
const { isLaptop } = useScreenSize()
26+
const tableColumns: TableColProps[] = [
27+
{ key: 'layer', content: t('common.layer') },
28+
{ key: 'height', content: t('common.height') },
29+
{
30+
key: 'hash',
31+
content: t('common.hash'),
32+
},
33+
{ key: 'age', content: <TableHeaderAge />, align: TableCellAlign.Right },
34+
{
35+
key: 'transaction',
36+
content: isLaptop ? t('common.transactionAbbreviation') : t('common.transactions'),
37+
align: TableCellAlign.Right,
38+
},
39+
]
40+
41+
const tableRows = blocks?.map(block => {
42+
const blockGasLimit = paraTimesConfig[block.layer]?.[block.network]?.blockGasLimit
43+
if (!blockGasLimit) throw new Error('blockGasLimit is not configured')
44+
return {
45+
key: block.hash,
46+
data: [
47+
{
48+
content: 'Consensus',
49+
key: 'layer',
50+
},
51+
52+
{
53+
content: <BlockLink scope={block} height={block.round} />,
54+
key: 'block',
55+
},
56+
{
57+
content: <BlockHashLink scope={block} hash={block.hash} height={block.round} alwaysTrim />,
58+
key: 'hash',
59+
},
60+
{
61+
align: TableCellAlign.Right,
62+
content: <TableCellAge sinceTimestamp={block.timestamp} />,
63+
key: 'timestamp',
64+
},
65+
{
66+
align: TableCellAlign.Right,
67+
content: block.num_transactions.toLocaleString(),
68+
key: 'txs',
69+
},
70+
],
71+
}
72+
})
73+
74+
return (
75+
<Table
76+
columns={tableColumns}
77+
rows={tableRows}
78+
rowsNumber={limit}
79+
name={t('blocks.latest')}
80+
isLoading={isLoading}
81+
pagination={false}
82+
/>
83+
)
84+
}

src/app/components/Blocks/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './RuntimeBlocks'
22
export * from './ConsensusBlocks'
3+
export * from './Blocks'
34
export enum BlocksTableType {
45
Mobile,
56
DesktopLite,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// TODO: Remove runtime related code from commit when API supports latest blocks for all layers
2+
import { FC } from 'react'
3+
import { useTranslation } from 'react-i18next'
4+
import { Card, CardContent, CardHeader, CardTitle } from '@oasisprotocol/ui-library/src/components/cards'
5+
import { Typography } from '@oasisprotocol/ui-library/src/components/typography'
6+
import { useGetRuntimeBlocks } from '../../../oasis-nexus/api'
7+
import { Blocks } from '../../components/Blocks/Blocks'
8+
import { NUMBER_OF_ITEMS_ON_DASHBOARD } from '../../../config'
9+
import { RuntimeScope } from '../../../types/searchScope'
10+
import { ErrorBoundary } from '../../components/ErrorBoundary'
11+
12+
const limit = NUMBER_OF_ITEMS_ON_DASHBOARD
13+
14+
const LatestBlocksContent: FC = () => {
15+
const scope = { network: 'mainnet', layer: 'sapphire' } as RuntimeScope
16+
const { network, layer } = scope
17+
const blocksQuery = useGetRuntimeBlocks(
18+
network,
19+
layer,
20+
{ limit },
21+
{
22+
query: {
23+
cacheTime: 0,
24+
},
25+
},
26+
)
27+
28+
return <Blocks isLoading={blocksQuery.isLoading} blocks={blocksQuery.data?.data.blocks} limit={limit} />
29+
}
30+
31+
export const LatestBlocks: FC = () => {
32+
const { t } = useTranslation()
33+
34+
return (
35+
<Card variant="layout" className="md:mb-0">
36+
<CardHeader>
37+
<CardTitle>
38+
<Typography variant="h3">{t('blocks.latest')}</Typography>
39+
</CardTitle>
40+
</CardHeader>
41+
<CardContent>
42+
<ErrorBoundary light minHeight={400}>
43+
<LatestBlocksContent />
44+
</ErrorBoundary>
45+
</CardContent>
46+
</Card>
47+
)
48+
}

src/app/pages/HomePage/index.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { RoflAppsCard } from './RoflAppsCard'
77
import { FooterBanner } from './FooterBanner'
88
import { HomeSearch } from './HomeSearch'
99
import { Header } from 'app/components/PageLayout/Header'
10+
import { LatestBlocks } from './LatestBlocks'
1011

1112
export const HomePage: FC = () => {
1213
const { network } = useSearchQueryNetworkParam()
@@ -18,6 +19,10 @@ export const HomePage: FC = () => {
1819
<Header sticky={false} />
1920
<div className="flex flex-col px-6 gap-6">
2021
<HomeSearch />
22+
<div className="flex gap-6 flex-col md:flex-row">
23+
<LatestBlocks />
24+
<div>Placeholder</div>
25+
</div>
2126
<RoflAppsCard />
2227
<FooterBanner />
2328
</div>

0 commit comments

Comments
 (0)