@centreon/ui 23.10.18-MON-17793-create-graph-component-centreon-ui.11 → 23.10.18-MON-17793-create-graph-component-centreon-ui.13

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.11",
3
+ "version": "23.10.18-MON-17793-create-graph-component-centreon-ui.13",
4
4
  "description": "Centreon UI Components",
5
5
  "scripts": {
6
6
  "eslint": "eslint ./src --ext .js,.jsx,.ts,.tsx --max-warnings 0",
@@ -31,6 +31,11 @@ const useStyles = makeStyles()((theme) => ({
31
31
  },
32
32
  fill: theme.palette.text.primary,
33
33
  position: 'relative'
34
+ },
35
+ header: {
36
+ display: 'grid',
37
+ gridTemplateColumns: '0.4fr 1fr 0.4fr',
38
+ width: '100%'
34
39
  }
35
40
  }));
36
41
 
@@ -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,18 +34,19 @@ 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
- loading: boolean;
43
44
  shapeLines?: GlobalAreaLines;
44
45
  }
45
46
 
46
47
  const Graph = ({
47
48
  graphData,
48
- height,
49
+ height = 500,
49
50
  width,
50
51
  shapeLines,
51
52
  axis,
@@ -56,17 +57,19 @@ const Graph = ({
56
57
  timeShiftZones,
57
58
  annotationEvent,
58
59
  tooltip,
59
- legend
60
+ legend,
61
+ graphRef,
62
+ header
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 ?? true
137
+ display: shapeLines?.areaThresholdLines?.display ?? false
135
138
  };
136
139
 
137
140
  const areaRegularLines = {
@@ -214,10 +217,21 @@ 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
220
233
  displayTimeTick={displayTimeTick}
234
+ header={header}
221
235
  timeTick={timeTick}
222
236
  title={title}
223
237
  />
@@ -4,8 +4,12 @@ import Typography from '@mui/material/Typography';
4
4
 
5
5
  import { useLocaleDateTimeFormat } from '@centreon/ui';
6
6
 
7
+ import { useStyles } from '../Graph.styles';
8
+ import { HeaderGraph } from '../models';
9
+
7
10
  interface Props {
8
11
  displayTimeTick?: boolean;
12
+ header?: HeaderGraph;
9
13
  timeTick?: Date;
10
14
  title: string;
11
15
  }
@@ -13,22 +17,32 @@ interface Props {
13
17
  const Header = ({
14
18
  title,
15
19
  timeTick,
16
- displayTimeTick = true
20
+ displayTimeTick = true,
21
+ header
17
22
  }: Props): JSX.Element => {
23
+ const { classes } = useStyles();
18
24
  const { toDateTime } = useLocaleDateTimeFormat();
19
25
  const time =
20
26
  displayTimeTick && !isNil(timeTick) ? toDateTime(timeTick) : null;
21
27
 
28
+ const displayTitle = header?.displayTitle ?? true;
29
+
22
30
  return (
23
- <>
24
- <Typography align="center" variant="body1">
25
- {title}
26
- </Typography>
27
-
28
- <Typography align="center" style={{ height: 20 }} variant="body1">
29
- {time}
30
- </Typography>
31
- </>
31
+ <div className={classes.header}>
32
+ <div />
33
+ {displayTitle && (
34
+ <div>
35
+ <Typography align="center" variant="body1">
36
+ {title}
37
+ </Typography>
38
+
39
+ <Typography align="center" style={{ height: 20 }} variant="body1">
40
+ {time}
41
+ </Typography>
42
+ </div>
43
+ )}
44
+ {header?.extraComponent}
45
+ </div>
32
46
  );
33
47
  };
34
48
  export default 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
+ };
@@ -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,
@@ -39,16 +42,23 @@ const WrapperGraph = ({
39
42
  timeShiftZones,
40
43
  tooltip,
41
44
  annotationEvent,
42
- legend
45
+ legend,
46
+ header
43
47
  }: Props): JSX.Element | null => {
44
48
  const { adjustedData } = useGraphData({ data, end, start });
49
+ const graphRef = useRef<HTMLDivElement | null>(null);
45
50
 
46
- if (!adjustedData) {
47
- return null;
51
+ if (loading || !adjustedData) {
52
+ return (
53
+ <LoadingSkeleton
54
+ displayTitleSkeleton={header?.displayTitle ?? true}
55
+ graphHeight={height}
56
+ />
57
+ );
48
58
  }
49
59
 
50
60
  return (
51
- <div>
61
+ <div ref={graphRef as MutableRefObject<HTMLDivElement>}>
52
62
  <Responsive.ParentSize>
53
63
  {({
54
64
  height: responsiveHeight,
@@ -60,6 +70,8 @@ const WrapperGraph = ({
60
70
  axis={axis}
61
71
  graphData={{ ...adjustedData }}
62
72
  graphInterval={{ end, start }}
73
+ graphRef={graphRef}
74
+ header={header}
63
75
  height={height ?? responsiveHeight}
64
76
  legend={legend}
65
77
  loading={loading}
@@ -84,11 +84,18 @@ export interface AnnotationEvent {
84
84
  data?: Array<TimelineEvent>;
85
85
  }
86
86
 
87
+ export interface HeaderGraph {
88
+ displayTitle?: boolean;
89
+ extraComponent?: ReactNode;
90
+ }
91
+
87
92
  export interface GraphProps {
88
93
  anchorPoint?: AreaAnchorPoint;
89
94
  annotationEvent?: AnnotationEvent;
90
95
  axis?: Axis;
91
- height: number;
96
+ header?: HeaderGraph;
97
+ height?: number;
98
+ loading: boolean;
92
99
  timeShiftZones?: InteractedZone;
93
100
  tooltip?: Tooltip;
94
101
  width: number;
@@ -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
+ };