Skip to content

Commit 66b8345

Browse files
authored
Merge pull request #2345 from oasisprotocol/mz/homepage-recentBlock
Add recent blocks components
2 parents 6b88f06 + 41de002 commit 66b8345

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

.changelog/2345.trivial.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add recent blocks components
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { FC } from 'react'
2+
import { Link as RouterLink } from 'react-router-dom'
3+
import { useTranslation } from 'react-i18next'
4+
import { Link } from '@oasisprotocol/ui-library/src/components/link'
5+
import { RecentBlock } from '../../../oasis-nexus/api'
6+
import { useScreenSize } from '../../hooks/useScreensize'
7+
import { getLayerLabels } from '../../utils/content'
8+
import { RouteUtils } from '../../utils/route-utils'
9+
import { Table, TableCellAlign, TableColProps } from '../Table'
10+
import { TableCellAge } from '../TableCellAge'
11+
import { TableHeaderAge } from '../TableHeaderAge'
12+
import { BlockHashLink, BlockLink } from './BlockLink'
13+
14+
export type TableBlockList = {
15+
blocks: RecentBlock[]
16+
total_count: number
17+
is_total_count_clipped: boolean
18+
}
19+
20+
type RecentBlocksProps = {
21+
blocks?: RecentBlock[]
22+
isLoading: boolean
23+
limit: number
24+
}
25+
26+
export const RecentBlocks: FC<RecentBlocksProps> = ({ isLoading, blocks, limit }) => {
27+
const { t } = useTranslation()
28+
const { isLaptop } = useScreenSize()
29+
const layerLabels = getLayerLabels(t)
30+
const tableColumns: TableColProps[] = [
31+
{ key: 'layer', content: t('common.layer') },
32+
{ key: 'height', content: t('common.height') },
33+
{
34+
key: 'hash',
35+
content: t('common.hash'),
36+
},
37+
{ key: 'age', content: <TableHeaderAge />, align: TableCellAlign.Right },
38+
{
39+
key: 'transaction',
40+
content: isLaptop ? t('common.transactionAbbreviation') : t('common.transactions'),
41+
align: TableCellAlign.Right,
42+
},
43+
]
44+
45+
const tableRows = blocks?.map(block => {
46+
return {
47+
key: block.hash,
48+
data: [
49+
{
50+
content: (
51+
<Link asChild className="font-medium">
52+
<RouterLink to={RouteUtils.getDashboardRoute({ layer: block.layer, network: block.network })}>
53+
{layerLabels[block.layer]}
54+
</RouterLink>
55+
</Link>
56+
),
57+
key: 'layer',
58+
},
59+
60+
{
61+
content: <BlockLink scope={{ network: block.network, layer: block.layer }} height={block.height} />,
62+
key: 'block',
63+
},
64+
{
65+
content: (
66+
<BlockHashLink
67+
scope={{ network: block.network, layer: block.layer }}
68+
hash={block.hash}
69+
height={block.height}
70+
alwaysTrim
71+
/>
72+
),
73+
key: 'hash',
74+
},
75+
{
76+
align: TableCellAlign.Right,
77+
content: <TableCellAge sinceTimestamp={block.timestamp} />,
78+
key: 'timestamp',
79+
},
80+
{
81+
align: TableCellAlign.Right,
82+
content: block.num_transactions.toLocaleString(),
83+
key: 'txs',
84+
},
85+
],
86+
}
87+
})
88+
89+
return (
90+
<Table
91+
columns={tableColumns}
92+
rows={tableRows}
93+
rowsNumber={limit}
94+
name={t('blocks.latest')}
95+
isLoading={isLoading}
96+
pagination={false}
97+
/>
98+
)
99+
}

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 './RecentBlocks'
34
export enum BlocksTableType {
45
Mobile,
56
DesktopLite,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { FC } from 'react'
2+
import { useTranslation } from 'react-i18next'
3+
import { Card, CardContent, CardHeader, CardTitle } from '@oasisprotocol/ui-library/src/components/cards'
4+
import { Typography } from '@oasisprotocol/ui-library/src/components/typography'
5+
import { useGetRecentBlocks } from '../../../oasis-nexus/api'
6+
import { RecentBlocks } from '../../components/Blocks/RecentBlocks'
7+
import { NUMBER_OF_ITEMS_ON_DASHBOARD } from '../../../config'
8+
import { ErrorBoundary } from '../../components/ErrorBoundary'
9+
10+
const limit = NUMBER_OF_ITEMS_ON_DASHBOARD
11+
12+
const RecentBlocksContent: FC = () => {
13+
const recentBlocksQuery = useGetRecentBlocks('mainnet')
14+
const recentBlocks = recentBlocksQuery.data?.data.blocks
15+
const filteredBlocks = recentBlocks?.slice(0, 5)
16+
17+
return <RecentBlocks isLoading={recentBlocksQuery.isLoading} blocks={filteredBlocks} limit={limit} />
18+
}
19+
20+
export const RecentBlocksCard: FC = () => {
21+
const { t } = useTranslation()
22+
23+
return (
24+
<Card variant="layout">
25+
<CardHeader>
26+
<CardTitle>
27+
<Typography variant="h3">{t('blocks.latest')}</Typography>
28+
</CardTitle>
29+
</CardHeader>
30+
<CardContent>
31+
<ErrorBoundary light minHeight={400}>
32+
<RecentBlocksContent />
33+
</ErrorBoundary>
34+
</CardContent>
35+
</Card>
36+
)
37+
}

src/locales/en/translation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
"hide": "Hide",
107107
"id": "ID",
108108
"invalidVotes": "Invalid votes",
109+
"layer": "Layer",
109110
"localnet": "Localnet",
110111
"loadMore": "Load more",
111112
"lessThanAmount": "&lt;{{value, number}} <TickerLink />",

0 commit comments

Comments
 (0)