mirror of
https://github.com/luxfi/explore.git
synced 2026-09-24 00:28:18 +00:00
* add prefix to API resources * fix pw tests * refactor config * remove unnecessary prefixes in resource names * fixes * update screenshots
62 lines
2.0 KiB
TypeScript
62 lines
2.0 KiB
TypeScript
import React from 'react';
|
|
|
|
import type { LineChart, Resolution } from '@blockscout/stats-types';
|
|
import type { StatsIntervalIds } from 'types/client/stats';
|
|
|
|
import useApiQuery from 'lib/api/useApiQuery';
|
|
import { useAppContext } from 'lib/contexts/app';
|
|
import { STATS_INTERVALS } from 'ui/stats/constants';
|
|
|
|
import formatDate from './utils/formatIntervalDate';
|
|
|
|
export default function useChartQuery(id: string, resolution: Resolution, interval: StatsIntervalIds, enabled = true) {
|
|
const { apiData } = useAppContext<'/stats/[id]'>();
|
|
|
|
const selectedInterval = STATS_INTERVALS[interval];
|
|
|
|
const endDate = selectedInterval.start ? formatDate(new Date()) : undefined;
|
|
const startDate = selectedInterval.start ? formatDate(selectedInterval.start) : undefined;
|
|
|
|
const [ info, setInfo ] = React.useState<LineChart['info']>(apiData || undefined);
|
|
|
|
const lineQuery = useApiQuery('stats:line', {
|
|
pathParams: { id },
|
|
queryParams: {
|
|
from: startDate,
|
|
to: endDate,
|
|
resolution,
|
|
},
|
|
queryOptions: {
|
|
enabled: enabled,
|
|
refetchOnMount: false,
|
|
placeholderData: {
|
|
info: {
|
|
title: 'Chart title placeholder',
|
|
description: 'Chart placeholder description chart placeholder description',
|
|
resolutions: [ 'DAY', 'WEEK', 'MONTH', 'YEAR' ],
|
|
id: 'placeholder',
|
|
units: undefined,
|
|
},
|
|
chart: [],
|
|
},
|
|
},
|
|
});
|
|
|
|
React.useEffect(() => {
|
|
if (!info && lineQuery.data?.info && !lineQuery.isPlaceholderData) {
|
|
// save info to keep title and description when change query params
|
|
setInfo(lineQuery.data?.info);
|
|
}
|
|
}, [ info, lineQuery.data?.info, lineQuery.isPlaceholderData ]);
|
|
|
|
const items = React.useMemo(() => lineQuery.data?.chart?.map((item) => {
|
|
return { date: new Date(item.date), date_to: new Date(item.date_to), value: Number(item.value), isApproximate: item.is_approximate };
|
|
}), [ lineQuery ]);
|
|
|
|
return {
|
|
items,
|
|
info,
|
|
lineQuery,
|
|
};
|
|
}
|