-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EVM Staking - Breadcrumbs & Header chips (#1781)
- Loading branch information
1 parent
2b2b4e0
commit 4ea6a42
Showing
19 changed files
with
231 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
import { Typography } from '@webb-tools/webb-ui-components'; | ||
import { HeaderChipsContainer } from '../containers/HeaderChipsContainer'; | ||
|
||
export default async function Index() { | ||
return <div></div>; | ||
return ( | ||
<div className="flex items-center justify-between"> | ||
<Typography variant="h4" fw="bold"> | ||
Staking Overview | ||
</Typography> | ||
|
||
<HeaderChipsContainer /> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use client'; | ||
|
||
import { FundsLine } from '@webb-tools/icons'; | ||
import { | ||
Breadcrumbs as BreadcrumbsCmp, | ||
BreadcrumbsItem, | ||
} from '@webb-tools/webb-ui-components'; | ||
import cx from 'classnames'; | ||
import Link from 'next/link'; | ||
import { usePathname, useRouter } from 'next/navigation'; | ||
import { type FC, useMemo, useEffect } from 'react'; | ||
import { BreadcrumbType } from './types'; | ||
import { getIconSizeInPixel } from '@webb-tools/icons/utils'; | ||
|
||
const Breadcrumbs: FC = () => { | ||
const router = useRouter(); | ||
const pathname = usePathname(); | ||
|
||
const breadCrumbs = useMemo<BreadcrumbType[]>(() => { | ||
const md = getIconSizeInPixel('md'); | ||
const lg = getIconSizeInPixel('lg'); | ||
|
||
return [ | ||
{ | ||
label: 'EVM Staking', | ||
isLast: true, | ||
icon: ( | ||
<FundsLine | ||
className={`w-[${md}] h-[${md}] lg:w-[${lg}] lg:h-[${lg}]`} | ||
/> | ||
), | ||
href: '/', | ||
}, | ||
]; | ||
}, []); | ||
|
||
return ( | ||
<BreadcrumbsCmp> | ||
{breadCrumbs.map((breadcrumb, index) => ( | ||
/** | ||
* Data on the client-side needs to be up-to-date when the user navigates to a page | ||
* Therefore, do not need to prefetch routes in breadcrumb items | ||
*/ | ||
<Link key={index} href={breadcrumb.href} prefetch={false}> | ||
<BreadcrumbsItem | ||
icon={breadcrumb.icon} | ||
className={cx('whitespace-nowrap', breadcrumb.className)} | ||
textClassName="!text-[12px] lg:!text-[16px] normal-case" | ||
isLast={breadcrumb.isLast} | ||
> | ||
{breadcrumb.label} | ||
</BreadcrumbsItem> | ||
</Link> | ||
))} | ||
</BreadcrumbsCmp> | ||
); | ||
}; | ||
|
||
export default Breadcrumbs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as Breadcrumbs } from './Breadcrumbs'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export type BreadcrumbType = { | ||
label: string; | ||
isLast: boolean; | ||
icon: JSX.Element; | ||
href: string; | ||
className?: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Suspense, type FC, useMemo } from 'react'; | ||
import { | ||
Chip, | ||
Tooltip, | ||
TooltipBody, | ||
TooltipTrigger, | ||
Typography, | ||
SkeletonLoader, | ||
} from '@webb-tools/webb-ui-components'; | ||
import { HeaderChipItemProps } from './types'; | ||
|
||
export const HeaderChip: FC<HeaderChipItemProps> = ({ | ||
Icon, | ||
label, | ||
hasTooltip = false, | ||
tooltipContent, | ||
dataFetcher, | ||
}: HeaderChipItemProps) => { | ||
const mainContent = useMemo( | ||
() => ( | ||
<Chip color="blue"> | ||
<Icon size="lg" className="stroke-blue-90 dark:stroke-blue-30" /> | ||
{label}:{' '} | ||
<Suspense fallback={<SkeletonLoader className="w-[100px]" />}> | ||
<HeaderChipValue dataFetcher={dataFetcher} /> | ||
</Suspense> | ||
</Chip> | ||
), | ||
[Icon, label, dataFetcher] | ||
); | ||
|
||
if (hasTooltip && tooltipContent) { | ||
return ( | ||
<Tooltip> | ||
<TooltipTrigger>{mainContent}</TooltipTrigger> | ||
<TooltipBody> | ||
<Typography variant="body2">{tooltipContent}</Typography> | ||
</TooltipBody> | ||
</Tooltip> | ||
); | ||
} | ||
|
||
return mainContent; | ||
}; | ||
|
||
const HeaderChipValue = async ({ | ||
dataFetcher, | ||
}: Pick<HeaderChipItemProps, 'dataFetcher'>) => { | ||
const value = await dataFetcher(); | ||
|
||
return <>{value}</>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './HeaderChip'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { type IconBase } from '@webb-tools/icons/types'; | ||
|
||
export interface HeaderChipItemProps { | ||
Icon: (props: IconBase) => JSX.Element; | ||
label: string; | ||
hasTooltip?: boolean; | ||
tooltipContent?: string; | ||
dataFetcher: () => Promise<number | undefined>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './sideBar'; | ||
export * from './Sidebar'; | ||
export * from './Breadcrumbs'; | ||
export * from './HeaderChip'; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './polkadot'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ApiPromise, WsProvider } from '@polkadot/api'; | ||
import { TANGLE_RPC_ENDPOINT } from '@webb-tools/webb-ui-components/constants'; | ||
|
||
export const getPolkadotApi = async ( | ||
endpoint: string = TANGLE_RPC_ENDPOINT | ||
): Promise<ApiPromise | undefined> => { | ||
try { | ||
const wsProvider = new WsProvider(endpoint); | ||
const apiPromise = await ApiPromise.create({ provider: wsProvider }); | ||
|
||
return apiPromise; | ||
} catch (e) { | ||
console.error(e); | ||
return undefined; | ||
} | ||
}; |
13 changes: 13 additions & 0 deletions
13
apps/tangle-dapp/containers/HeaderChipsContainer/HeaderChipsContainer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { BlockIcon } from '@webb-tools/icons'; | ||
import { HeaderChip } from '../../components'; | ||
import { getEra, getSession } from '../../data'; | ||
|
||
export const HeaderChipsContainer = () => { | ||
return ( | ||
<div className="items-center hidden gap-2 md:flex lg:gap-4"> | ||
<HeaderChip Icon={BlockIcon} label="ERA" dataFetcher={getEra} /> | ||
|
||
<HeaderChip Icon={BlockIcon} label="Session" dataFetcher={getSession} /> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './HeaderChipsContainer'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { getPolkadotApi } from '../../constants'; | ||
|
||
export const getEra = async (): Promise<number> => { | ||
const api = await getPolkadotApi(); | ||
|
||
if (!api) return NaN; | ||
|
||
try { | ||
let activeEra = await api.query.staking.activeEra(); | ||
activeEra = JSON.parse(JSON.stringify(activeEra)).index; | ||
|
||
return Number(activeEra.toString()); | ||
} catch (e: any) { | ||
console.error(e); | ||
|
||
return 0; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { getPolkadotApi } from '../../constants'; | ||
|
||
export const getSession = async (): Promise<number> => { | ||
const api = await getPolkadotApi(); | ||
|
||
if (!api) return NaN; | ||
|
||
try { | ||
const currentDKGPublicKey: any = await api.query.dkg.dkgPublicKey(); | ||
const currentSessionNumber = currentDKGPublicKey[0]; | ||
|
||
return Number(currentSessionNumber.toString()); | ||
} catch (e: any) { | ||
console.error(e); | ||
|
||
return 0; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './getEra'; | ||
export * from './getSession'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './HeaderChips'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters