@centreon/ui 23.10.18-MON-17793-create-graph-component-centreon-ui.11 → 23.10.18-MON-17793-create-graph-component-centreon-ui.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@centreon/ui",
|
|
3
|
-
"version": "23.10.18-MON-17793-create-graph-component-centreon-ui.
|
|
3
|
+
"version": "23.10.18-MON-17793-create-graph-component-centreon-ui.12",
|
|
4
4
|
"description": "Centreon UI Components",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"eslint": "eslint ./src --ext .js,.jsx,.ts,.tsx --max-warnings 0",
|
package/src/Graph/Graph.tsx
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { useMemo, useRef } from 'react';
|
|
1
|
+
import { MutableRefObject, useMemo, useRef } from 'react';
|
|
2
2
|
|
|
3
3
|
import { Group } from '@visx/visx';
|
|
4
4
|
|
|
5
|
-
import { ClickAwayListener } from '@mui/material';
|
|
5
|
+
import { ClickAwayListener, Skeleton } from '@mui/material';
|
|
6
6
|
|
|
7
7
|
import Axes from './BasicComponents/Axes';
|
|
8
8
|
import Grids from './BasicComponents/Grids';
|
|
@@ -34,10 +34,12 @@ import {
|
|
|
34
34
|
LegendModel
|
|
35
35
|
} from './models';
|
|
36
36
|
import { getLeftScale, getRightScale, getXScale } from './timeSeries';
|
|
37
|
+
import { useIntersection } from './useGraphIntersection';
|
|
37
38
|
|
|
38
39
|
interface Props extends GraphProps {
|
|
39
40
|
graphData: Data;
|
|
40
41
|
graphInterval: GraphInterval;
|
|
42
|
+
graphRef: MutableRefObject<HTMLDivElement | null>;
|
|
41
43
|
legend?: LegendModel;
|
|
42
44
|
loading: boolean;
|
|
43
45
|
shapeLines?: GlobalAreaLines;
|
|
@@ -45,7 +47,7 @@ interface Props extends GraphProps {
|
|
|
45
47
|
|
|
46
48
|
const Graph = ({
|
|
47
49
|
graphData,
|
|
48
|
-
height,
|
|
50
|
+
height = 500,
|
|
49
51
|
width,
|
|
50
52
|
shapeLines,
|
|
51
53
|
axis,
|
|
@@ -56,17 +58,18 @@ const Graph = ({
|
|
|
56
58
|
timeShiftZones,
|
|
57
59
|
annotationEvent,
|
|
58
60
|
tooltip,
|
|
59
|
-
legend
|
|
61
|
+
legend,
|
|
62
|
+
graphRef
|
|
60
63
|
}: Props): JSX.Element => {
|
|
61
64
|
const graphSvgRef = useRef<SVGSVGElement | null>(null);
|
|
62
65
|
|
|
63
66
|
const { classes } = useStyles();
|
|
67
|
+
const { isInViewport } = useIntersection({ element: graphRef?.current });
|
|
64
68
|
|
|
65
69
|
const graphWidth = width > 0 ? width - margin.left - margin.right : 0;
|
|
66
70
|
const graphHeight = height > 0 ? height - margin.top - margin.bottom : 0;
|
|
67
71
|
|
|
68
72
|
const { title, timeSeries, baseAxis, lines } = graphData;
|
|
69
|
-
|
|
70
73
|
const { displayedLines, newLines } = useFilterLines({
|
|
71
74
|
displayThreshold: shapeLines?.areaThresholdLines?.display,
|
|
72
75
|
lines
|
|
@@ -131,7 +134,7 @@ const Graph = ({
|
|
|
131
134
|
timeSeries,
|
|
132
135
|
...commonLinesProps,
|
|
133
136
|
...shapeLines?.areaThresholdLines,
|
|
134
|
-
display: shapeLines?.areaThresholdLines?.display ??
|
|
137
|
+
display: shapeLines?.areaThresholdLines?.display ?? false
|
|
135
138
|
};
|
|
136
139
|
|
|
137
140
|
const areaRegularLines = {
|
|
@@ -214,6 +217,16 @@ const Graph = ({
|
|
|
214
217
|
</g>
|
|
215
218
|
);
|
|
216
219
|
|
|
220
|
+
if (!isInViewport) {
|
|
221
|
+
return (
|
|
222
|
+
<Skeleton
|
|
223
|
+
height={graphSvgRef?.current?.clientHeight ?? graphHeight}
|
|
224
|
+
variant="rectangular"
|
|
225
|
+
width="100%"
|
|
226
|
+
/>
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
217
230
|
return (
|
|
218
231
|
<>
|
|
219
232
|
<Header
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { makeStyles } from 'tss-react/mui';
|
|
2
|
+
|
|
3
|
+
import { Skeleton } from '@mui/material';
|
|
4
|
+
|
|
5
|
+
interface Props {
|
|
6
|
+
displayTitleSkeleton: boolean;
|
|
7
|
+
graphHeight: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const useSkeletonStyles = makeStyles<Props>()(
|
|
11
|
+
(theme, { graphHeight, displayTitleSkeleton }) => ({
|
|
12
|
+
loadingSkeleton: {
|
|
13
|
+
display: 'grid',
|
|
14
|
+
gridGap: theme.spacing(1),
|
|
15
|
+
gridTemplateRows: `${
|
|
16
|
+
displayTitleSkeleton ? '1fr' : ''
|
|
17
|
+
} ${graphHeight}px ${theme.spacing(7)}`,
|
|
18
|
+
height: '100%'
|
|
19
|
+
},
|
|
20
|
+
loadingSkeletonLine: {
|
|
21
|
+
paddingBottom: theme.spacing(1),
|
|
22
|
+
transform: 'none'
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
const LoadingSkeleton = ({
|
|
28
|
+
graphHeight,
|
|
29
|
+
displayTitleSkeleton
|
|
30
|
+
}: Props): JSX.Element => {
|
|
31
|
+
const { classes } = useSkeletonStyles({ displayTitleSkeleton, graphHeight });
|
|
32
|
+
|
|
33
|
+
const skeletonLine = <Skeleton className={classes.loadingSkeletonLine} />;
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<div className={classes.loadingSkeleton}>
|
|
37
|
+
{displayTitleSkeleton && skeletonLine}
|
|
38
|
+
{skeletonLine}
|
|
39
|
+
{skeletonLine}
|
|
40
|
+
</div>
|
|
41
|
+
);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export default LoadingSkeleton;
|
|
@@ -111,6 +111,27 @@ const ExternalComponent = (tooltipData): JSX.Element => {
|
|
|
111
111
|
);
|
|
112
112
|
};
|
|
113
113
|
|
|
114
|
+
const GraphAndCLS = (args): JSX.Element => {
|
|
115
|
+
const [data, setData] = useState();
|
|
116
|
+
const [loading, setLoading] = useState(true);
|
|
117
|
+
|
|
118
|
+
useEffect(() => {
|
|
119
|
+
setTimeout(() => {
|
|
120
|
+
setLoading(false);
|
|
121
|
+
setData(dataLastDay);
|
|
122
|
+
}, 1000);
|
|
123
|
+
}, []);
|
|
124
|
+
|
|
125
|
+
return (
|
|
126
|
+
<WrapperGraph
|
|
127
|
+
{...args}
|
|
128
|
+
data={data}
|
|
129
|
+
loading={loading}
|
|
130
|
+
shapeLines={{ areaThresholdLines: { display: true } }}
|
|
131
|
+
/>
|
|
132
|
+
);
|
|
133
|
+
};
|
|
134
|
+
|
|
114
135
|
interface TimePeriodSwitchProps {
|
|
115
136
|
getDataSwitch: (value: boolean) => void;
|
|
116
137
|
}
|
|
@@ -327,3 +348,16 @@ export const withExclusionPeriods: Story = {
|
|
|
327
348
|
start: defaultStart
|
|
328
349
|
}
|
|
329
350
|
};
|
|
351
|
+
|
|
352
|
+
const GraphWithCLS: Story = {
|
|
353
|
+
render: (args) => <GraphAndCLS {...args} />
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
export const withCLS: Story = {
|
|
357
|
+
...GraphWithCLS,
|
|
358
|
+
args: {
|
|
359
|
+
end: defaultEnd,
|
|
360
|
+
height: 500,
|
|
361
|
+
start: defaultStart
|
|
362
|
+
}
|
|
363
|
+
};
|
package/src/Graph/index.tsx
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { MutableRefObject, useRef } from 'react';
|
|
2
|
+
|
|
1
3
|
import { Responsive } from '@visx/visx';
|
|
2
4
|
import dayjs from 'dayjs';
|
|
3
5
|
import 'dayjs/locale/en';
|
|
@@ -9,6 +11,7 @@ import timezonePlugin from 'dayjs/plugin/timezone';
|
|
|
9
11
|
import utcPlugin from 'dayjs/plugin/utc';
|
|
10
12
|
|
|
11
13
|
import Graph from './Graph';
|
|
14
|
+
import LoadingSkeleton from './LoadingSkeleton';
|
|
12
15
|
import { GlobalAreaLines, GraphData, GraphProps, LegendModel } from './models';
|
|
13
16
|
import useGraphData from './useGraphData';
|
|
14
17
|
|
|
@@ -28,7 +31,7 @@ interface Props extends Partial<GraphProps> {
|
|
|
28
31
|
const WrapperGraph = ({
|
|
29
32
|
end,
|
|
30
33
|
start,
|
|
31
|
-
height,
|
|
34
|
+
height = 500,
|
|
32
35
|
width,
|
|
33
36
|
shapeLines,
|
|
34
37
|
axis,
|
|
@@ -42,13 +45,14 @@ const WrapperGraph = ({
|
|
|
42
45
|
legend
|
|
43
46
|
}: Props): JSX.Element | null => {
|
|
44
47
|
const { adjustedData } = useGraphData({ data, end, start });
|
|
48
|
+
const graphRef = useRef<HTMLDivElement | null>(null);
|
|
45
49
|
|
|
46
|
-
if (!adjustedData) {
|
|
47
|
-
return
|
|
50
|
+
if (loading || !adjustedData) {
|
|
51
|
+
return <LoadingSkeleton displayTitleSkeleton graphHeight={height} />;
|
|
48
52
|
}
|
|
49
53
|
|
|
50
54
|
return (
|
|
51
|
-
<div>
|
|
55
|
+
<div ref={graphRef as MutableRefObject<HTMLDivElement>}>
|
|
52
56
|
<Responsive.ParentSize>
|
|
53
57
|
{({
|
|
54
58
|
height: responsiveHeight,
|
|
@@ -60,6 +64,7 @@ const WrapperGraph = ({
|
|
|
60
64
|
axis={axis}
|
|
61
65
|
graphData={{ ...adjustedData }}
|
|
62
66
|
graphInterval={{ end, start }}
|
|
67
|
+
graphRef={graphRef}
|
|
63
68
|
height={height ?? responsiveHeight}
|
|
64
69
|
legend={legend}
|
|
65
70
|
loading={loading}
|
package/src/Graph/models.ts
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
interface GraphIntersectionState {
|
|
4
|
+
isInViewport: boolean;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const useIntersection = ({ element }): GraphIntersectionState => {
|
|
8
|
+
const [entry, setEntry] = useState<IntersectionObserverEntry | null>(null);
|
|
9
|
+
|
|
10
|
+
const observer = useRef<IntersectionObserver | null>(null);
|
|
11
|
+
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
if (observer.current) {
|
|
14
|
+
observer.current.disconnect();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
observer.current = new window.IntersectionObserver(
|
|
18
|
+
([newEntry]) => setEntry(newEntry),
|
|
19
|
+
{
|
|
20
|
+
threshold: 0
|
|
21
|
+
}
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
if (element) {
|
|
25
|
+
observer.current.observe(element);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return (): void => {
|
|
29
|
+
observer.current?.disconnect();
|
|
30
|
+
};
|
|
31
|
+
}, [element]);
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
isInViewport: entry?.isIntersecting ?? true
|
|
35
|
+
};
|
|
36
|
+
};
|