Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions src/pages/blocks/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,30 @@ export const blocksRowSections = (block: IBlock): IRowSection[] => {
const Blocks: React.FC<PropsWithChildren<IBlocks>> = () => {
const blocksWatcherInterval = 4 * 1000; // 4 secs
const [blocksInterval, setBlocksInterval] = useState(0);
const { data: blocksStatsToday, isLoading: isLoadingBlocksStatsToday } =
useQuery({
queryKey: ['statisticsCall'],
queryFn: totalStatisticsCall,
});
const {
data: blocksStatsToday,
isLoading: isLoadingBlocksStatsToday,
refetch: refetchTotal,
dataUpdatedAt: totalUpdatedAt,
} = useQuery({
queryKey: ['statisticsCall'],
queryFn: totalStatisticsCall,
});
const {
data: blocksStatsYesterday,
refetch,
refetch: refetchYesterday,
isLoading: isLoadingBlocksStatsYesterday,
dataUpdatedAt: yesterdayUpdatedAt,
} = useQuery({
queryKey: ['yesterdayStatisticsCall'],
queryFn: yesterdayStatisticsCall,
});

const dataUpdatedAt =
totalUpdatedAt && yesterdayUpdatedAt
? Math.min(totalUpdatedAt, yesterdayUpdatedAt)
: totalUpdatedAt || yesterdayUpdatedAt || 0;

const updateBlocks = useCallback(async () => {
const newState = storageUpdateBlocks(!!blocksInterval);
if (newState) {
Expand All @@ -171,7 +181,8 @@ const Blocks: React.FC<PropsWithChildren<IBlocks>> = () => {
useEffect(() => {
if (blocksInterval) {
const intervalId = setInterval(() => {
refetch();
refetchYesterday();
refetchTotal();
}, blocksWatcherInterval);
return () => clearInterval(intervalId);
}
Expand Down Expand Up @@ -246,33 +257,37 @@ const Blocks: React.FC<PropsWithChildren<IBlocks>> = () => {
},
];

const CardContent: React.FC<PropsWithChildren<ICard>> = ({
interface CardContentProps extends ICard {
dataUpdatedAt: number;
}

const CardContent: React.FC<PropsWithChildren<CardContentProps>> = ({
title,
headers,
values,
dataUpdatedAt,
}) => {
const [uptime] = useState(new Date().getTime());
const [age, setAge] = useState(getAge(new Date()));
const [age, setAge] = useState(
dataUpdatedAt ? getAge(new Date(dataUpdatedAt)) : '',
);

useEffect(() => {
const interval = setInterval(() => {
const newAge = getAge(new Date(uptime));

setAge(newAge);
setAge(dataUpdatedAt ? getAge(new Date(dataUpdatedAt)) : '');
}, 1 * 1000); // 1 sec

return () => {
clearInterval(interval);
};
}, []);
}, [dataUpdatedAt]);

return (
<Card>
<div>
<span>
<strong>{title}</strong>
</span>
<p>{age} ago</p>
<p>{age ? `${age} ago` : ''}</p>
</div>
<div>
<span>
Expand Down Expand Up @@ -308,7 +323,7 @@ const Blocks: React.FC<PropsWithChildren<IBlocks>> = () => {

<CardContainer>
{cards.map((card, index) => (
<CardContent key={index} {...card} />
<CardContent key={index} {...card} dataUpdatedAt={dataUpdatedAt} />
))}
</CardContainer>

Expand Down
27 changes: 13 additions & 14 deletions src/services/apiCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import { IBlockResponse, IBlockStats } from '@/types/blocks';
export const blockCall = async (
page = 1,
limit = 10,
): Promise<IBlockResponse> =>
new Promise<IBlockResponse>(async (resolve, reject) => {
const res = await api.get({
route: `block/list?page=${page}&limit=${limit}`,
});
): Promise<IBlockResponse> => {
const res = await api.get({
route: `block/list?page=${page}&limit=${limit}`,
});

if (!res.error || res.error === '') {
resolve(res);
}
if (!res.error || res.error === '') {
return res;
}

reject(res.error);
});
throw new Error(typeof res.error === 'string' ? res.error : 'Failed to fetch blocks');
};

export const yesterdayStatisticsCall = async (): Promise<IBlockStats> => {
const res = await api.get({
Expand All @@ -25,9 +24,9 @@ export const yesterdayStatisticsCall = async (): Promise<IBlockStats> => {

if (!res.error || res.error === '') {
return res.data.block_stats_by_day[0];
} else {
return res.error;
}

throw new Error(typeof res.error === 'string' ? res.error : 'Failed to fetch yesterday statistics');
};

export const totalStatisticsCall = async (): Promise<IBlockStats> => {
Expand All @@ -37,9 +36,9 @@ export const totalStatisticsCall = async (): Promise<IBlockStats> => {

if (!res.error || res.error === '') {
return res.data.block_stats_total;
} else {
return res.error;
}

throw new Error(typeof res.error === 'string' ? res.error : 'Failed to fetch total statistics');
};

export const defaultPagination: IPagination = {
Expand Down