Commit eafb2ab2 authored by tom's avatar tom

skeletons for stats counters

parent 59c5442d
import type { HomeStats, StatsChartsSection } from 'types/api/stats'; import type { Counter, HomeStats, StatsChartsSection } from 'types/api/stats';
export const HOMEPAGE_STATS: HomeStats = { export const HOMEPAGE_STATS: HomeStats = {
average_block_time: 14346, average_block_time: 14346,
...@@ -53,3 +53,10 @@ export const STATS_CHARTS_SECTION: StatsChartsSection = { ...@@ -53,3 +53,10 @@ export const STATS_CHARTS_SECTION: StatsChartsSection = {
export const STATS_CHARTS = { export const STATS_CHARTS = {
sections: [ STATS_CHARTS_SECTION ], sections: [ STATS_CHARTS_SECTION ],
}; };
export const STATS_COUNTER: Counter = {
id: 'stub',
value: '9074405',
title: 'Placeholder Counter',
units: '',
};
...@@ -23,7 +23,7 @@ export type Counters = { ...@@ -23,7 +23,7 @@ export type Counters = {
counters: Array<Counter>; counters: Array<Counter>;
} }
type Counter = { export type Counter = {
id: string; id: string;
value: string; value: string;
title: string; title: string;
......
import { Box, Text, useColorModeValue } from '@chakra-ui/react'; import { Box, Skeleton, useColorModeValue } from '@chakra-ui/react';
import React from 'react'; import React from 'react';
type Props = { type Props = {
label: string; label: string;
value: string; value: string;
isLoading?: boolean;
} }
const NumberWidget = ({ label, value }: Props) => { const NumberWidget = ({ label, value, isLoading }: Props) => {
const bgColor = useColorModeValue('blue.50', 'blue.800');
const skeletonBgColor = useColorModeValue('blackAlpha.50', 'whiteAlpha.50');
return ( return (
<Box <Box
bg={ useColorModeValue('blue.50', 'blue.800') } bg={ isLoading ? skeletonBgColor : bgColor }
px={ 3 } px={ 3 }
py={{ base: 2, lg: 3 }} py={{ base: 2, lg: 3 }}
borderRadius={ 12 } borderRadius={ 12 }
> >
<Text <Skeleton
variant="secondary" isLoaded={ !isLoading }
color="text_secondary"
fontSize="xs" fontSize="xs"
w="fit-content"
> >
{ label } <span>{ label }</span>
</Text> </Skeleton>
<Text <Skeleton
isLoaded={ !isLoading }
fontWeight={ 500 } fontWeight={ 500 }
fontSize="lg" fontSize="lg"
w="fit-content"
> >
{ value } { value }
</Text> </Skeleton>
</Box> </Box>
); );
}; };
......
import { Box, Skeleton, useColorModeValue } from '@chakra-ui/react';
import React from 'react';
const NumberWidgetSkeleton = () => {
const bgColor = useColorModeValue('blackAlpha.50', 'whiteAlpha.50');
return (
<Box
backgroundColor={ bgColor }
p={ 3 }
borderRadius={ 12 }
>
<Skeleton w="70px" h="10px" mb={ 2 }/>
<Skeleton w="100px" h="27px"/>
</Box>
);
};
export default NumberWidgetSkeleton;
...@@ -2,18 +2,17 @@ import { Grid } from '@chakra-ui/react'; ...@@ -2,18 +2,17 @@ import { Grid } from '@chakra-ui/react';
import React from 'react'; import React from 'react';
import useApiQuery from 'lib/api/useApiQuery'; import useApiQuery from 'lib/api/useApiQuery';
import { STATS_COUNTER } from 'stubs/stats';
import DataFetchAlert from '../shared/DataFetchAlert'; import DataFetchAlert from '../shared/DataFetchAlert';
import NumberWidget from './NumberWidget'; import NumberWidget from './NumberWidget';
import NumberWidgetSkeleton from './NumberWidgetSkeleton';
const skeletonsCount = 8;
const NumberWidgetsList = () => { const NumberWidgetsList = () => {
const { data, isLoading, isError } = useApiQuery('stats_counters'); const { data, isPlaceholderData, isError } = useApiQuery('stats_counters', {
queryOptions: {
const skeletonElement = [ ...Array(skeletonsCount) ] placeholderData: { counters: Array(10).fill(STATS_COUNTER) },
.map((e, i) => <NumberWidgetSkeleton key={ i }/>); },
});
if (isError) { if (isError) {
return <DataFetchAlert/>; return <DataFetchAlert/>;
...@@ -24,17 +23,19 @@ const NumberWidgetsList = () => { ...@@ -24,17 +23,19 @@ const NumberWidgetsList = () => {
gridTemplateColumns={{ base: 'repeat(2, 1fr)', lg: 'repeat(4, 1fr)' }} gridTemplateColumns={{ base: 'repeat(2, 1fr)', lg: 'repeat(4, 1fr)' }}
gridGap={ 4 } gridGap={ 4 }
> >
{ isLoading ? skeletonElement : {
data?.counters?.map(({ id, title, value, units }) => { data?.counters?.map(({ id, title, value, units }, index) => {
return ( return (
<NumberWidget <NumberWidget
key={ id } key={ id + (isPlaceholderData ? index : '') }
label={ title } label={ title }
value={ `${ Number(value).toLocaleString(undefined, { maximumFractionDigits: 3, notation: 'compact' }) } ${ units ? units : '' }` } value={ `${ Number(value).toLocaleString(undefined, { maximumFractionDigits: 3, notation: 'compact' }) } ${ units ? units : '' }` }
isLoading={ isPlaceholderData }
/> />
); );
}) } })
}
</Grid> </Grid>
); );
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment