Skip to content

Commit

Permalink
Merge pull request #1722 from blockscout/fe-1721
Browse files Browse the repository at this point in the history
stats price diff on the homepage
  • Loading branch information
isstuev authored Mar 19, 2024
2 parents 9933b2a + f56c38e commit 3699cb4
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 5 deletions.
3 changes: 3 additions & 0 deletions icons/up.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions public/icons/name.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
| "txn_batches"
| "unfinalized"
| "uniswap"
| "up"
| "user_op_slim"
| "user_op"
| "validator"
Expand Down
28 changes: 26 additions & 2 deletions ui/home/indicators/ChainIndicatorItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import type { ChainIndicatorId } from 'types/homepage';

import type { ResourceError } from 'lib/api/resources';
import useIsMobile from 'lib/hooks/useIsMobile';
import IconSvg from 'ui/shared/IconSvg';

interface Props {
id: ChainIndicatorId;
title: string;
value: (stats: HomeStats) => string;
valueDiff?: (stats?: HomeStats) => number | null | undefined;
icon: React.ReactNode;
isSelected: boolean;
onClick: (id: ChainIndicatorId) => void;
stats: UseQueryResult<HomeStats, ResourceError<unknown>>;
}

const ChainIndicatorItem = ({ id, title, value, icon, isSelected, onClick, stats }: Props) => {
const ChainIndicatorItem = ({ id, title, value, valueDiff, icon, isSelected, onClick, stats }: Props) => {
const isMobile = useIsMobile();

const activeBgColorDesktop = useColorModeValue('white', 'gray.900');
Expand Down Expand Up @@ -53,6 +55,25 @@ const ChainIndicatorItem = ({ id, title, value, icon, isSelected, onClick, stats
return <Text variant="secondary" fontWeight={ 600 }>{ value(stats.data) }</Text>;
})();

const valueDiffContent = (() => {
if (isMobile || !valueDiff) {
return null;
}
const diff = valueDiff(stats.data);
if (diff === undefined || diff === null) {
return null;
}

const diffColor = diff >= 0 ? 'green.500' : 'red.500';

return (
<Skeleton isLoaded={ !stats.isPlaceholderData } ml={ 3 } display="flex" alignItems="center" color={ diffColor }>
<IconSvg name="up" boxSize={ 5 } mr={ 1 } transform={ diff < 0 ? 'rotate(180deg)' : 'rotate(0)' }/>
<Text color={ diffColor } fontWeight={ 600 }>{ diff }%</Text>
</Skeleton>
);
})();

return (
<Flex
alignItems="center"
Expand All @@ -73,7 +94,10 @@ const ChainIndicatorItem = ({ id, title, value, icon, isSelected, onClick, stats
{ icon }
<Box>
<Text fontFamily="heading" fontWeight={ 500 }>{ title }</Text>
{ valueContent }
<Flex alignItems="center">
{ valueContent }
{ valueDiffContent }
</Flex>
</Box>
</Flex>
);
Expand Down
30 changes: 27 additions & 3 deletions ui/home/indicators/ChainIndicators.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Flex, Skeleton, Text, useColorModeValue } from '@chakra-ui/react';
import { Box, Flex, Skeleton, Text, useColorModeValue } from '@chakra-ui/react';
import React from 'react';

import config from 'configs/app';
import useApiQuery from 'lib/api/useApiQuery';
import { HOMEPAGE_STATS } from 'stubs/stats';
import Hint from 'ui/shared/Hint';
import IconSvg from 'ui/shared/IconSvg';

import ChainIndicatorChartContainer from './ChainIndicatorChartContainer';
import ChainIndicatorItem from './ChainIndicatorItem';
Expand Down Expand Up @@ -56,12 +57,32 @@ const ChainIndicators = () => {
}

return (
<Text fontWeight={ 600 } fontFamily="heading" fontSize="48px" lineHeight="48px" mt={ 3 } mb={ 4 }>
<Text fontWeight={ 600 } fontFamily="heading" fontSize="48px" lineHeight="48px" mt={ 3 }>
{ indicator?.value(statsQueryResult.data) }
</Text>
);
})();

const valueDiff = (() => {
if (!statsQueryResult.data || !indicator?.valueDiff) {
return null;
}

const diff = indicator.valueDiff(statsQueryResult.data);
if (diff === undefined || diff === null) {
return null;
}

const diffColor = diff >= 0 ? 'green.500' : 'red.500';

return (
<Skeleton isLoaded={ !statsQueryResult.isPlaceholderData } display="flex" alignItems="center" color={ diffColor } mt={ 2 }>
<IconSvg name="up" boxSize={ 5 } mr={ 1 } transform={ diff < 0 ? 'rotate(180deg)' : 'rotate(0)' }/>
<Text color={ diffColor } fontWeight={ 600 }>{ diff }%</Text>
</Skeleton>
);
})();

return (
<Flex
p={{ base: 0, lg: 8 }}
Expand All @@ -80,7 +101,10 @@ const ChainIndicators = () => {
<Text fontWeight={ 500 } fontFamily="heading" fontSize="lg">{ indicator?.title }</Text>
{ indicator?.hint && <Hint label={ indicator.hint } ml={ 1 }/> }
</Flex>
{ valueTitle }
<Box mb={ 4 }>
{ valueTitle }
{ valueDiff }
</Box>
<ChainIndicatorChartContainer { ...queryResult }/>
</Flex>
{ indicators.length > 1 && (
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions ui/home/indicators/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface TChainIndicator<R extends ChartsResources> {
id: ChainIndicatorId;
title: string;
value: (stats: HomeStats) => string;
valueDiff?: (stats?: HomeStats) => number | null | undefined;
icon: React.ReactNode;
hint?: string;
api: {
Expand Down
1 change: 1 addition & 0 deletions ui/home/indicators/utils/indicators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const coinPriceIndicator: TChainIndicator<'stats_charts_market'> = {
value: (stats) => stats.coin_price === null ?
'$N/A' :
'$' + Number(stats.coin_price).toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 6 }),
valueDiff: (stats) => stats?.coin_price !== null ? stats?.coin_price_change_percentage : null,
icon: <TokenEntity.Icon token={ nativeTokenData } boxSize={ 6 } marginRight={ 0 }/>,
hint: `${ config.chain.governanceToken.symbol || config.chain.currency.symbol } token daily price in USD.`,
api: {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3699cb4

Please sign in to comment.