-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1569 from oasisprotocol/mz/uptime
Prepare components to render validator uptime
- Loading branch information
Showing
7 changed files
with
133 additions
and
5 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Prepare components to render validator uptime |
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,49 @@ | ||
import { ensureTwelveElements } from '..' | ||
|
||
describe('ensureTwelveElements', () => { | ||
test('fills an array with undefined values when arary has less than 12 elements', () => { | ||
const input = [1, 2, 3] | ||
const result = ensureTwelveElements(input) | ||
expect(result.length).toBe(12) | ||
expect(result).toStrictEqual([ | ||
1, | ||
2, | ||
3, | ||
undefined, | ||
undefined, | ||
undefined, | ||
undefined, | ||
undefined, | ||
undefined, | ||
undefined, | ||
undefined, | ||
undefined, | ||
]) | ||
}) | ||
|
||
test('returns an array with the same 12 elements', () => { | ||
const input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] | ||
const result = ensureTwelveElements(input) | ||
expect(result.length).toBe(12) | ||
expect(result).toStrictEqual(input) | ||
}) | ||
|
||
test('truncates the array if it has more than 12 elements', () => { | ||
const input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] | ||
const result = ensureTwelveElements(input) | ||
expect(result.length).toBe(12) | ||
expect(result).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) | ||
}) | ||
|
||
test('returns an array with 12 undefined values when array is empty', () => { | ||
const result = ensureTwelveElements([]) | ||
expect(result.length).toBe(12) | ||
expect(result).toStrictEqual(new Array(12).fill(undefined)) | ||
}) | ||
|
||
test('returns an array with 12 undefined values when array is not provided', () => { | ||
const result = ensureTwelveElements() | ||
expect(result.length).toBe(12) | ||
expect(result).toStrictEqual(new Array(12).fill(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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { FC } from 'react' | ||
import Box from '@mui/material/Box' | ||
import { styled } from '@mui/material/styles' | ||
import { COLORS } from '../../../styles/theme/colors' | ||
import { PercentageValue } from '../PercentageValue' | ||
|
||
const getUptimeItemColor = (value: number | undefined) => { | ||
if (!value) { | ||
return COLORS.grayMediumLight | ||
} | ||
|
||
if (value > 90) { | ||
return COLORS.eucalyptus | ||
} | ||
|
||
return COLORS.errorIndicatorBackground | ||
} | ||
|
||
type UptimeItem = { | ||
small?: boolean | ||
value?: number | ||
} | ||
|
||
const StyledBox = styled(Box)<UptimeItem>(({ small, value }) => ({ | ||
background: getUptimeItemColor(value), | ||
width: small ? '3px' : '7px', | ||
minWidth: small ? '3px' : '7px', | ||
height: small ? '15px' : '45px', | ||
borderRadius: small ? '2px' : '4px', | ||
color: COLORS.white, | ||
marginRight: small ? 1 : 2, | ||
})) | ||
|
||
export const ensureTwelveElements = (inputArray: number[] = []) => { | ||
return [...inputArray, ...new Array(12).fill(undefined)].slice(0, 12) | ||
} | ||
|
||
type UptimeStatusProps = { | ||
percentage?: number | ||
small?: boolean | ||
status?: number[] | ||
} | ||
|
||
export const UptimeStatus: FC<UptimeStatusProps> = ({ percentage, status, small }) => { | ||
const adjustedStatus = ensureTwelveElements(status) | ||
|
||
return ( | ||
<Box sx={{ display: 'flex', alignItems: 'center', gap: small ? 4 : 5 }}> | ||
<Box sx={{ display: 'flex' }}> | ||
{adjustedStatus?.map((value, index) => ( | ||
<StyledBox small={small} key={`${value}-${index}`} value={value} /> | ||
))} | ||
</Box> | ||
{percentage && ( | ||
<Box> | ||
<PercentageValue value={percentage} /> | ||
</Box> | ||
)} | ||
</Box> | ||
) | ||
} |
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 |
---|---|---|
@@ -1,13 +1,24 @@ | ||
import { FC } from 'react' | ||
import { useTranslation } from 'react-i18next' | ||
import Typography from '@mui/material/Typography' | ||
import { COLORS } from '../../../styles/theme/colors' | ||
import { SnapshotTextCard } from '../../components/Snapshots/SnapshotCard' | ||
import { UptimeStatus } from '../../components/UptimeStatus' | ||
|
||
export const UptimeCard: FC = () => { | ||
const { t } = useTranslation() | ||
|
||
return ( | ||
<SnapshotTextCard title={t('validator.uptime')}> | ||
{/* TODO: provide uptime stats when API is ready */}- | ||
<SnapshotTextCard | ||
title={t('validator.uptime')} | ||
label={ | ||
<Typography sx={{ fontSize: 12, color: COLORS.grayMedium }}> | ||
{t('validator.signedBlocksPercentage')} | ||
</Typography> | ||
} | ||
> | ||
{/* TODO: provide data when API is ready */} | ||
<UptimeStatus percentage={94} status={[100, 100, 100, 50]} /> | ||
</SnapshotTextCard> | ||
) | ||
} |
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