From e8781ca02616890a9ee37c97a933ad68e7e9fdc9 Mon Sep 17 00:00:00 2001 From: evilpeach Date: Thu, 14 Mar 2024 08:36:52 +0700 Subject: [PATCH] feat: uptime blocks --- .../performance/RecentBlocksLegends.tsx | 32 ++++--- .../components/performance/UptimeSection.tsx | 85 +++++++++++++++++-- .../components/performance/index.tsx | 13 ++- .../components/validator-overview/index.tsx | 7 +- src/lib/pages/validator-details/index.tsx | 2 +- src/lib/services/validator.ts | 6 +- src/lib/services/validatorService.ts | 1 + src/lib/types/validator.ts | 11 +++ 8 files changed, 131 insertions(+), 26 deletions(-) diff --git a/src/lib/pages/validator-details/components/performance/RecentBlocksLegends.tsx b/src/lib/pages/validator-details/components/performance/RecentBlocksLegends.tsx index 652071482..eea90085c 100644 --- a/src/lib/pages/validator-details/components/performance/RecentBlocksLegends.tsx +++ b/src/lib/pages/validator-details/components/performance/RecentBlocksLegends.tsx @@ -1,15 +1,18 @@ import { Flex, Text } from "@chakra-ui/react"; +import type { ComputedUptime, Ratio } from "lib/types"; +import { formatRatio } from "lib/utils"; + const LegendItem = ({ label, color, value, - percent, + ratio, }: { label: string; color: string; - value: string; - percent: string; + value: number; + ratio: Ratio; }) => ( @@ -34,31 +37,38 @@ const LegendItem = ({ {value} - {percent} + {formatRatio(ratio)} ); -export const RecentBlocksLegends = () => { + +interface RecentBlocksLegendsProps { + uptime: ComputedUptime; +} + +export const RecentBlocksLegends = ({ + uptime: { signed, proposed, missed, signedRatio, proposedRatio, missedRatio }, +}: RecentBlocksLegendsProps) => { return ( ); diff --git a/src/lib/pages/validator-details/components/performance/UptimeSection.tsx b/src/lib/pages/validator-details/components/performance/UptimeSection.tsx index 469076cf4..c8b019862 100644 --- a/src/lib/pages/validator-details/components/performance/UptimeSection.tsx +++ b/src/lib/pages/validator-details/components/performance/UptimeSection.tsx @@ -1,24 +1,69 @@ -import { Button, Flex, Heading, Text } from "@chakra-ui/react"; +import { + Button, + Flex, + Heading, + Menu, + MenuButton, + MenuItem, + MenuList, + Text, +} from "@chakra-ui/react"; +import { isUndefined } from "lodash"; +import { useMemo } from "react"; import { PenaltyStatus } from "../../types"; import { useMobile } from "lib/app-provider"; import { CustomIcon } from "lib/components/icon"; +import { Loading } from "lib/components/Loading"; import { ValueWithIcon } from "lib/components/ValueWithIcon"; +import type { ValidatorUptimeResponse } from "lib/services/validator"; +import type { ComputedUptime, Option, Ratio } from "lib/types"; +import { formatRatio } from "lib/utils"; import { PenaltyStatusSection } from "./PenaltyStatusSection"; import { RecentBlocksLegends } from "./RecentBlocksLegends"; import { RecentBlocksSection } from "./RecentBlocksSection"; interface UptimeSectionProps { - isDetailPage?: boolean; + uptimeData: Option; + setUptimeBlock?: (block: number) => void; onViewMore?: () => void; } export const UptimeSection = ({ + uptimeData, + setUptimeBlock, onViewMore, - isDetailPage = false, }: UptimeSectionProps) => { const isMobile = useMobile(); + + const computed = useMemo(() => { + const data = uptimeData?.uptime; + if (!data) + return { + signed: 0, + proposed: 0, + missed: 0, + signedRatio: 0 as Ratio, + proposedRatio: 0 as Ratio, + missedRatio: 0 as Ratio, + uptimeRatio: 0 as Ratio, + }; + + return { + signed: data.signedBlocks, + proposed: data.proposedBlocks, + missed: data.missedBlocks, + signedRatio: (data.signedBlocks / data.total) as Ratio, + proposedRatio: (data.proposedBlocks / data.total) as Ratio, + missedRatio: (data.missedBlocks / data.total) as Ratio, + uptimeRatio: ((data.signedBlocks + data.proposedBlocks) / + data.total) as Ratio, + }; + }, [uptimeData?.uptime]); + + if (isUndefined(uptimeData)) return ; + return ( Uptime - - Latest 100 Blocks - + {setUptimeBlock ? ( + + + + Latest 100 Blocks + + + + + setUptimeBlock(100)}> + Latest 100 Blocks + + setUptimeBlock(1000)}> + Latest 1000 Blocks + + setUptimeBlock(10000)}> + Latest 10000 Blocks + + + + ) : ( + + Latest 100 Blocks + + )} {onViewMore && !isMobile && (