Commit 79ec74ef authored by Yuri Mikhin's avatar Yuri Mikhin Committed by Yuri Mikhin

Move ChartWidget to ui/shared/chart.

parent 59c3d6f9
...@@ -24,8 +24,10 @@ export type Stats = { ...@@ -24,8 +24,10 @@ export type Stats = {
} }
export type Charts = { export type Charts = {
'chart': Array<{ chart: Array<ChartsItem>;
date: string; }
value: string;
}>; export type ChartsItem ={
date: string;
value: string;
} }
import { Box, Grid, Heading, Icon, IconButton, Menu, MenuButton, MenuItem, MenuList, Text, useColorModeValue, VisuallyHidden } from '@chakra-ui/react'; import { Box, Grid, Heading, Icon, IconButton, Menu, MenuButton, MenuItem, MenuList, Text, useColorModeValue, VisuallyHidden } from '@chakra-ui/react';
import { useQuery } from '@tanstack/react-query';
import React, { useCallback, useState } from 'react'; import React, { useCallback, useState } from 'react';
import type { Charts } from 'types/api/stats'; import type { TimeChartItem } from './types';
import { QueryKeys } from 'types/client/queries';
import type { StatsIntervalIds } from 'types/client/stats';
import repeatArrow from 'icons/repeat_arrow.svg'; import repeatArrow from 'icons/repeat_arrow.svg';
import dotsIcon from 'icons/vertical_dots.svg'; import dotsIcon from 'icons/vertical_dots.svg';
import useFetch from 'lib/hooks/useFetch';
import ChartWidgetGraph from './ChartWidgetGraph'; import ChartWidgetGraph from './ChartWidgetGraph';
import ChartWidgetSkeleton from './ChartWidgetSkeleton'; import ChartWidgetSkeleton from './ChartWidgetSkeleton';
import { STATS_INTERVALS } from './constants';
import FullscreenChartModal from './FullscreenChartModal'; import FullscreenChartModal from './FullscreenChartModal';
type Props = { type Props = {
id: string; items?: Array<TimeChartItem>;
title: string; title: string;
description: string; description: string;
interval: StatsIntervalIds; isLoading: boolean;
} }
function formatDate(date: Date) { const ChartWidget = ({ items, title, description, isLoading }: Props) => {
return date.toISOString().substring(0, 10);
}
const ChartWidget = ({ id, title, description, interval }: Props) => {
const fetch = useFetch();
const selectedInterval = STATS_INTERVALS[interval];
const [ isFullscreen, setIsFullscreen ] = useState(false); const [ isFullscreen, setIsFullscreen ] = useState(false);
const [ isZoomResetInitial, setIsZoomResetInitial ] = React.useState(true); const [ isZoomResetInitial, setIsZoomResetInitial ] = React.useState(true);
const endDate = selectedInterval.start ? formatDate(new Date()) : undefined;
const startDate = selectedInterval.start ? formatDate(selectedInterval.start) : undefined;
const menuButtonColor = useColorModeValue('black', 'white'); const menuButtonColor = useColorModeValue('black', 'white');
const borderColor = useColorModeValue('gray.200', 'gray.600'); const borderColor = useColorModeValue('gray.200', 'gray.600');
const url = `/node-api/stats/charts?name=${ id }${ startDate ? `&from=${ startDate }&to=${ endDate }` : '' }`;
const { data, isLoading } = useQuery<unknown, unknown, Charts>(
[ QueryKeys.charts, id, startDate ],
async() => await fetch(url),
);
const handleZoom = useCallback(() => { const handleZoom = useCallback(() => {
setIsZoomResetInitial(false); setIsZoomResetInitial(false);
}, []); }, []);
...@@ -75,12 +52,7 @@ const ChartWidget = ({ id, title, description, interval }: Props) => { ...@@ -75,12 +52,7 @@ const ChartWidget = ({ id, title, description, interval }: Props) => {
return <ChartWidgetSkeleton/>; return <ChartWidgetSkeleton/>;
} }
if (data) { if (items) {
const items = data.chart
.map((item) => {
return { date: new Date(item.date), value: Number(item.value) };
});
return ( return (
<> <>
<Box <Box
......
...@@ -78,7 +78,7 @@ const ChartWidgetGraph = ({ items, onZoom, isZoomResetInitial, title }: Props) = ...@@ -78,7 +78,7 @@ const ChartWidgetGraph = ({ items, onZoom, isZoomResetInitial, title }: Props) =
xScale={ xScale } xScale={ xScale }
yScale={ yScale } yScale={ yScale }
stroke={ color } stroke={ color }
animation="left" animation="none"
strokeWidth={ 3 } strokeWidth={ 3 }
/> />
......
import { Button, Flex, Heading, Modal, ModalBody, ModalCloseButton, ModalContent, ModalHeader, ModalOverlay } from '@chakra-ui/react'; import { Button, Flex, Heading, Modal, ModalBody, ModalCloseButton, ModalContent, ModalHeader, ModalOverlay } from '@chakra-ui/react';
import React, { useCallback } from 'react'; import React, { useCallback } from 'react';
import type { TimeChartItem } from '../shared/chart/types'; import type { TimeChartItem } from './types';
import ChartWidgetGraph from './ChartWidgetGraph'; import ChartWidgetGraph from './ChartWidgetGraph';
......
import { useQuery } from '@tanstack/react-query';
import React from 'react';
import type { Charts } from 'types/api/stats';
import { QueryKeys } from 'types/client/queries';
import type { StatsIntervalIds } from 'types/client/stats';
import useFetch from 'lib/hooks/useFetch';
import ChartWidget from '../shared/chart/ChartWidget';
import { STATS_INTERVALS } from './constants';
type Props = {
id: string;
title: string;
description: string;
interval: StatsIntervalIds;
}
function formatDate(date: Date) {
return date.toISOString().substring(0, 10);
}
const ChartWidgetContainer = ({ id, title, description, interval }: Props) => {
const fetch = useFetch();
const selectedInterval = STATS_INTERVALS[interval];
const endDate = selectedInterval.start ? formatDate(new Date()) : undefined;
const startDate = selectedInterval.start ? formatDate(selectedInterval.start) : undefined;
const url = `/node-api/stats/charts?name=${ id }${ startDate ? `&from=${ startDate }&to=${ endDate }` : '' }`;
const { data, isLoading } = useQuery<unknown, unknown, Charts>(
[ QueryKeys.charts, id, startDate ],
async() => await fetch(url),
);
const items = data?.chart
.map((item) => {
return { date: new Date(item.date), value: Number(item.value) };
});
return (
<ChartWidget
items={ items }
title={ title }
description={ description }
isLoading={ isLoading }
/>
);
};
export default ChartWidgetContainer;
...@@ -6,7 +6,7 @@ import type { StatsIntervalIds, StatsSection } from 'types/client/stats'; ...@@ -6,7 +6,7 @@ import type { StatsIntervalIds, StatsSection } from 'types/client/stats';
import { apos } from 'lib/html-entities'; import { apos } from 'lib/html-entities';
import EmptySearchResult from '../apps/EmptySearchResult'; import EmptySearchResult from '../apps/EmptySearchResult';
import ChartWidget from './ChartWidget'; import ChartWidgetContainer from './ChartWidgetContainer';
type Props = { type Props = {
charts: Array<StatsSection>; charts: Array<StatsSection>;
...@@ -46,7 +46,7 @@ const ChartsWidgetsList = ({ charts, interval }: Props) => { ...@@ -46,7 +46,7 @@ const ChartsWidgetsList = ({ charts, interval }: Props) => {
<GridItem <GridItem
key={ chart.id } key={ chart.id }
> >
<ChartWidget <ChartWidgetContainer
id={ chart.id } id={ chart.id }
title={ chart.title } title={ chart.title }
description={ chart.description } description={ chart.description }
......
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