@centreon/ui 24.4.63 → 24.4.65

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.
Files changed (47) hide show
  1. package/package.json +8 -3
  2. package/src/Graph/LineChart/Header/index.tsx +3 -31
  3. package/src/Graph/LineChart/InteractiveComponents/AnchorPoint/useTickGraph.ts +9 -11
  4. package/src/Graph/LineChart/InteractiveComponents/GraphValueTooltip/GraphValueTooltip.tsx +68 -0
  5. package/src/Graph/LineChart/InteractiveComponents/GraphValueTooltip/useGraphValueTooltip.ts +27 -0
  6. package/src/Graph/LineChart/InteractiveComponents/GraphValueTooltip/useGraphValueTooltipStyles.ts +31 -0
  7. package/src/Graph/LineChart/InteractiveComponents/index.tsx +132 -16
  8. package/src/Graph/LineChart/InteractiveComponents/interactionWithGraphAtoms.ts +7 -27
  9. package/src/Graph/LineChart/Legend/LegendHeader.tsx +8 -5
  10. package/src/Graph/LineChart/Legend/index.tsx +10 -47
  11. package/src/Graph/LineChart/LineChart.cypress.spec.tsx +91 -0
  12. package/src/Graph/LineChart/LineChart.styles.ts +8 -0
  13. package/src/Graph/LineChart/LineChart.tsx +107 -109
  14. package/src/Graph/LineChart/mockedData/lastDayWithIncompleteValues.json +1320 -0
  15. package/src/Graph/LineChart/mockedData/lastDayWithNullValues.json +1314 -0
  16. package/src/Graph/LineChart/models.ts +12 -0
  17. package/src/Graph/Tree/DescendantNodes.tsx +88 -0
  18. package/src/Graph/Tree/Links.tsx +64 -0
  19. package/src/Graph/Tree/StandaloneTree.tsx +32 -0
  20. package/src/Graph/Tree/Tree.cypress.spec.tsx +171 -0
  21. package/src/Graph/Tree/Tree.stories.tsx +144 -0
  22. package/src/Graph/Tree/Tree.tsx +116 -0
  23. package/src/Graph/Tree/constants.ts +2 -0
  24. package/src/Graph/Tree/index.ts +4 -0
  25. package/src/Graph/Tree/models.ts +52 -0
  26. package/src/Graph/Tree/stories/contents.tsx +164 -0
  27. package/src/Graph/Tree/stories/datas.ts +305 -0
  28. package/src/Graph/Tree/utils.ts +49 -0
  29. package/src/Graph/common/timeSeries/index.ts +31 -1
  30. package/src/Graph/index.ts +1 -0
  31. package/src/components/Zoom/Minimap.tsx +127 -0
  32. package/src/components/Zoom/Zoom.cypress.spec.tsx +246 -0
  33. package/src/components/Zoom/Zoom.stories.tsx +115 -0
  34. package/src/components/Zoom/Zoom.styles.tsx +68 -0
  35. package/src/components/Zoom/Zoom.tsx +61 -0
  36. package/src/components/Zoom/ZoomContent.tsx +167 -0
  37. package/src/components/Zoom/constants.ts +2 -0
  38. package/src/components/Zoom/localPoint.ts +51 -0
  39. package/src/components/Zoom/models.ts +25 -0
  40. package/src/components/Zoom/useMinimap.ts +156 -0
  41. package/src/components/Zoom/useZoom.ts +70 -0
  42. package/src/components/Zoom/utils.ts +55 -0
  43. package/src/components/index.ts +1 -0
  44. package/src/Graph/LineChart/InteractiveComponents/AnchorPoint/TooltipAnchorPoint.tsx +0 -96
  45. package/src/Graph/LineChart/InteractiveComponents/AnchorPoint/useTooltipAnchorPoint.ts +0 -107
  46. package/src/Graph/LineChart/Legend/InteractiveValue.tsx +0 -22
  47. package/src/Graph/LineChart/Legend/useInteractiveValues.ts +0 -99
@@ -0,0 +1,51 @@
1
+ import type {
2
+ FocusEvent as ReactFocusEvent,
3
+ MouseEvent as ReactMouseEvent,
4
+ TouchEvent as ReactTouchEvent
5
+ } from 'react';
6
+
7
+ import { Point } from '@visx/point';
8
+
9
+ type EventType =
10
+ | MouseEvent
11
+ | TouchEvent
12
+ | FocusEvent
13
+ | ReactFocusEvent
14
+ | ReactMouseEvent
15
+ | ReactTouchEvent;
16
+
17
+ type PointCoords = Pick<Point, 'x' | 'y'>;
18
+
19
+ const DEFAULT_POINT = { x: 0, y: 0 };
20
+
21
+ const isTouchEvent = (event?: EventType): event is TouchEvent =>
22
+ !!event && 'changedTouches' in event;
23
+
24
+ export const isMouseEvent = (event?: EventType): event is MouseEvent =>
25
+ !!event && 'clientX' in event;
26
+
27
+ const getXAndYFromEvent = (event?: EventType): PointCoords => {
28
+ if (!event) return { ...DEFAULT_POINT };
29
+
30
+ if (isTouchEvent(event)) {
31
+ return event.changedTouches.length > 0
32
+ ? {
33
+ x: event.changedTouches[0].clientX,
34
+ y: event.changedTouches[0].clientY
35
+ }
36
+ : { ...DEFAULT_POINT };
37
+ }
38
+
39
+ if (isMouseEvent(event)) {
40
+ return {
41
+ x: event.clientX,
42
+ y: event.clientY
43
+ };
44
+ }
45
+
46
+ return { ...DEFAULT_POINT };
47
+ };
48
+
49
+ export const localPoint = (event: EventType): PointCoords | null => {
50
+ return getXAndYFromEvent(event);
51
+ };
@@ -0,0 +1,25 @@
1
+ export interface ZoomState {
2
+ transformMatrix: {
3
+ scaleX: number;
4
+ scaleY: number;
5
+ skewX: number;
6
+ skewY: number;
7
+ translateX: number;
8
+ translateY: number;
9
+ };
10
+ }
11
+
12
+ export type MinimapPosition =
13
+ | 'top-left'
14
+ | 'top-right'
15
+ | 'bottom-left'
16
+ | 'bottom-right';
17
+
18
+ export interface ChildrenProps extends ZoomState {
19
+ contentClientRect: {
20
+ height: number;
21
+ width: number;
22
+ } | null;
23
+ height: number;
24
+ width: number;
25
+ }
@@ -0,0 +1,156 @@
1
+ import { useCallback, useState } from 'react';
2
+
3
+ import { ProvidedZoom, Translate } from '@visx/zoom/lib/types';
4
+ import { equals, gt, isNil, pick } from 'ramda';
5
+ import { Point } from '@visx/point';
6
+
7
+ import { ZoomState } from './models';
8
+
9
+ export interface UseMinimapProps {
10
+ height: number;
11
+ isDraggingFromContainer: boolean;
12
+ minimapScale: number;
13
+ scale: number;
14
+ width: number;
15
+ zoom: ProvidedZoom<SVGSVGElement> & ZoomState;
16
+ }
17
+
18
+ interface UseMinimapState {
19
+ dragEnd: (e) => void;
20
+ dragStart: (e) => void;
21
+ move: (e) => void;
22
+ transformTo: (e) => void;
23
+ zoomInOut: (e) => void;
24
+ }
25
+
26
+ export const useMinimap = ({
27
+ width,
28
+ height,
29
+ zoom,
30
+ minimapScale,
31
+ isDraggingFromContainer,
32
+ scale
33
+ }: UseMinimapProps): UseMinimapState => {
34
+ const [startPoint, setStartPoint] = useState<Pick<Point, 'x' | 'y'> | null>(
35
+ null
36
+ );
37
+ const [startTranslate, setStartTranslate] = useState<Translate | null>(null);
38
+
39
+ const getMatrixPoint = useCallback(
40
+ (event, newScale?: number): { x: number; y: number } => {
41
+ const hasScale = scale > 1;
42
+ const point = {
43
+ x: event.nativeEvent.offsetX * (1 / minimapScale),
44
+ y: event.nativeEvent.offsetY * (1 / minimapScale)
45
+ };
46
+
47
+ const dx = -(
48
+ point.x * (newScale || zoom.transformMatrix.scaleX) -
49
+ width / 2
50
+ );
51
+ const dy = -(
52
+ point.y * (newScale || zoom.transformMatrix.scaleY) -
53
+ height / 2
54
+ );
55
+
56
+ return {
57
+ x: !hasScale ? dx : dx * scale - width / 2,
58
+ y: !hasScale ? dy : dy * scale - height / 2
59
+ };
60
+ },
61
+ [zoom.transformMatrix, scale, width, height, minimapScale]
62
+ );
63
+
64
+ const transformTo = useCallback(
65
+ (e): void => {
66
+ if (!isNil(e.nativeEvent.which) && !equals(e.nativeEvent.which, 1)) {
67
+ return;
68
+ }
69
+ const { x, y } = getMatrixPoint(e);
70
+ zoom.setTransformMatrix({
71
+ ...zoom.transformMatrix,
72
+ translateX: x,
73
+ translateY: y
74
+ });
75
+ },
76
+ [zoom.transformMatrix, scale]
77
+ );
78
+
79
+ const dragStart = (e): void => {
80
+ if (
81
+ (!isNil(e.nativeEvent.which) && !equals(e.nativeEvent.which, 1)) ||
82
+ isDraggingFromContainer
83
+ ) {
84
+ return;
85
+ }
86
+ setStartPoint(getMatrixPoint(e));
87
+ setStartTranslate(pick(['translateX', 'translateY'], zoom.transformMatrix));
88
+ };
89
+
90
+ const dragEnd = (): void => {
91
+ setStartPoint(null);
92
+ setStartTranslate(null);
93
+ };
94
+
95
+ const move = useCallback(
96
+ (e): void => {
97
+ if (!startPoint || !startTranslate) {
98
+ return;
99
+ }
100
+ const { x, y } = getMatrixPoint(e);
101
+
102
+ const diffX = startPoint.x - x;
103
+ const diffY = startPoint.y - y;
104
+
105
+ zoom.setTransformMatrix({
106
+ ...zoom.transformMatrix,
107
+ translateX: startTranslate.translateX - diffX,
108
+ translateY: startTranslate.translateY - diffY
109
+ });
110
+ },
111
+
112
+ [zoom.transformMatrix, isDraggingFromContainer, scale, startPoint]
113
+ );
114
+
115
+ const zoomInOut = useCallback(
116
+ (e): void => {
117
+ const isZoomIn = gt(0, e.deltaY);
118
+
119
+ const newScaleX = isZoomIn
120
+ ? zoom.transformMatrix.scaleX + 0.1
121
+ : zoom.transformMatrix.scaleX - 0.1;
122
+
123
+ const newScaleY = isZoomIn
124
+ ? zoom.transformMatrix.scaleX + 0.1
125
+ : zoom.transformMatrix.scaleX - 0.1;
126
+ const { x, y } = getMatrixPoint(e, newScaleX);
127
+
128
+ const diffX = x - zoom.transformMatrix.translateX;
129
+ const diffY = y - zoom.transformMatrix.translateY;
130
+
131
+ zoom.setTransformMatrix({
132
+ ...zoom.transformMatrix,
133
+ scaleX: newScaleX,
134
+ scaleY: newScaleY,
135
+ translateX: zoom.transformMatrix.translateX + diffX / 4,
136
+ translateY: zoom.transformMatrix.translateY + diffY / 4
137
+ });
138
+ },
139
+ [
140
+ zoom.transformMatrix,
141
+ width,
142
+ height,
143
+ isDraggingFromContainer,
144
+ scale,
145
+ startPoint
146
+ ]
147
+ );
148
+
149
+ return {
150
+ dragEnd,
151
+ dragStart,
152
+ move,
153
+ transformTo,
154
+ zoomInOut
155
+ };
156
+ };
@@ -0,0 +1,70 @@
1
+ import { useCallback, useState } from 'react';
2
+
3
+ import { Point, ProvidedZoom, Translate } from '@visx/zoom/lib/types';
4
+ import { equals, isNil } from 'ramda';
5
+
6
+ import { localPoint } from './localPoint';
7
+ import { ZoomState } from './models';
8
+
9
+ const isLeftMouseButtonClicked = (e): boolean =>
10
+ !isNil(e.nativeEvent.which) && equals(e.nativeEvent.which, 1);
11
+
12
+ interface UseZoomState {
13
+ dragEnd: () => void;
14
+ dragStart: (zoom: ProvidedZoom<SVGSVGElement> & ZoomState) => (e) => void;
15
+ isDragging: boolean;
16
+ move: (zoom: ProvidedZoom<SVGSVGElement> & ZoomState) => (e) => void;
17
+ }
18
+
19
+ export const useZoom = (): UseZoomState => {
20
+ const [startTranslate, setStartTranslate] = useState<Translate | null>(null);
21
+ const [startPoint, setStartPoint] = useState<Point | null>(null);
22
+
23
+ const dragStart = useCallback(
24
+ (zoom: ProvidedZoom<SVGSVGElement> & ZoomState) =>
25
+ (e): void => {
26
+ if (!isLeftMouseButtonClicked(e)) {
27
+ return;
28
+ }
29
+ const { translateX, translateY } = zoom.transformMatrix;
30
+ setStartPoint(localPoint(e) || null);
31
+ setStartTranslate({ translateX, translateY });
32
+ },
33
+ []
34
+ );
35
+
36
+ const move = useCallback(
37
+ (zoom: ProvidedZoom<SVGSVGElement> & ZoomState) =>
38
+ (e): void => {
39
+ if (!startPoint || !startTranslate) {
40
+ return;
41
+ }
42
+ const currentPoint = localPoint(e);
43
+ const dx = currentPoint
44
+ ? -(startPoint.x - currentPoint.x)
45
+ : -startPoint.x;
46
+ const dy = currentPoint
47
+ ? -(startPoint.y - currentPoint.y)
48
+ : -startPoint.y;
49
+
50
+ const translateX = startTranslate.translateX + dx;
51
+ const translateY = startTranslate.translateY + dy;
52
+ zoom.setTranslate({
53
+ translateX,
54
+ translateY
55
+ });
56
+ },
57
+ [startPoint, startTranslate]
58
+ );
59
+ const dragEnd = useCallback((): void => {
60
+ setStartPoint(null);
61
+ setStartTranslate(null);
62
+ }, []);
63
+
64
+ return {
65
+ dragEnd,
66
+ dragStart,
67
+ isDragging: Boolean(startPoint && startTranslate),
68
+ move
69
+ };
70
+ };
@@ -0,0 +1,55 @@
1
+ import { CSSProperties } from 'react';
2
+
3
+ interface Props {
4
+ contentClientRect: {
5
+ height: number;
6
+ width: number;
7
+ } | null;
8
+ }
9
+
10
+ export const applyTranformStylesForZoom = ({
11
+ contentClientRect
12
+ }: Props): CSSProperties => {
13
+ const contentRect = {
14
+ height: contentClientRect?.height || 1,
15
+ width: contentClientRect?.width || 1
16
+ };
17
+ const isPortrait = contentRect.height > contentRect.width;
18
+ const sizes = isPortrait ? ['width', 'height'] : ['height', 'width'];
19
+ const sizeScale = contentRect[sizes[0]] / contentRect[sizes[1]];
20
+
21
+ const lengthToUse = isPortrait
22
+ ? contentRect[sizes[1]] - contentRect[sizes[0]]
23
+ : contentRect[sizes[0]];
24
+
25
+ const t = sizeScale > 0.85 && isPortrait ? sizeScale * 4 : sizeScale / 2;
26
+ const xScaleFactor = sizeScale > 0.7 && !isPortrait ? 10 : 6;
27
+
28
+ return {
29
+ transform: `translate(-${isPortrait ? 0 : contentRect.width * (sizeScale / xScaleFactor)}px, -${lengthToUse * (isPortrait ? t + 0.08 : t / 2)}px)`
30
+ };
31
+ };
32
+
33
+ // DO NOT REMOVE: As the component is in work in progress, please this code in case we need
34
+ // const getAdditionalPadding = (): number => {
35
+ // if (additionalScale > 0.05) {
36
+ // return 0;
37
+ // }
38
+
39
+ // const padding =
40
+ // additionalScale > 0.012
41
+ // ? (1 / additionalScale) * (1 / zoom.transformMatrix.scaleY)
42
+ // : 1 / additionalScale / zoom.transformMatrix.scaleY;
43
+
44
+ // if (additionalScale < 0.009) {
45
+ // const tweakScale = scaleLinear({
46
+ // clamp: true,
47
+ // domain: [0.009, 0.002],
48
+ // range: [1, 8]
49
+ // });
50
+
51
+ // return padding - padding / tweakScale(additionalScale);
52
+ // }
53
+
54
+ // return padding;
55
+ // };
@@ -11,3 +11,4 @@ export * from './ItemComposition';
11
11
  export * from './Avatar';
12
12
  export * from './CollapsibleItem';
13
13
  export * from './Inputs';
14
+ export { default as Zoom } from './Zoom/Zoom';
@@ -1,96 +0,0 @@
1
- import { Tooltip } from '@visx/visx';
2
-
3
- import { Typography, useTheme } from '@mui/material';
4
-
5
- import useTooltipAnchorPoint from './useTooltipAnchorPoint';
6
- import { TooltipAnchorModel } from './models';
7
-
8
- const baseStyles = {
9
- ...Tooltip.defaultStyles,
10
- textAlign: 'center'
11
- };
12
-
13
- const TooltipAnchorPoint = ({
14
- timeSeries,
15
- xScale,
16
- graphHeight,
17
- leftScale,
18
- rightScale,
19
- graphWidth,
20
- lines,
21
- baseAxis
22
- }: TooltipAnchorModel): JSX.Element => {
23
- const theme = useTheme();
24
-
25
- const {
26
- tooltipDataAxisX,
27
- tooltipDataAxisYLeft,
28
- tooltipLeftAxisX,
29
- tooltipLeftAxisYLeft,
30
- tooltipTopAxisYLeft,
31
- tooltipDataAxisYRight,
32
- tooltipTopAxisYRight,
33
- tooltipLeftAxisYRight
34
- } = useTooltipAnchorPoint({
35
- baseAxis,
36
- graphHeight,
37
- graphWidth,
38
- leftScale,
39
- lines,
40
- rightScale,
41
- timeSeries,
42
- xScale
43
- });
44
-
45
- const cardStyles = {
46
- backgroundColor: theme.palette.background.paper,
47
- color: theme.palette.text.primary,
48
- padding: theme.spacing(0.25, 0.5)
49
- };
50
-
51
- return (
52
- <>
53
- {tooltipDataAxisX && (
54
- <Tooltip.Tooltip
55
- left={tooltipLeftAxisX}
56
- style={{
57
- ...baseStyles,
58
- ...cardStyles,
59
- transform: 'translateX(-70%)'
60
- }}
61
- top={0}
62
- >
63
- <Typography variant="caption">{tooltipDataAxisX}</Typography>
64
- </Tooltip.Tooltip>
65
- )}
66
- {tooltipDataAxisYLeft && (
67
- <Tooltip.Tooltip
68
- left={tooltipLeftAxisYLeft}
69
- style={{
70
- ...baseStyles,
71
- ...cardStyles,
72
- transform: 'translateX(-70%) translateY(-100%)'
73
- }}
74
- top={tooltipTopAxisYLeft}
75
- >
76
- <Typography variant="caption">{tooltipDataAxisYLeft}</Typography>
77
- </Tooltip.Tooltip>
78
- )}
79
- {tooltipDataAxisYRight && (
80
- <Tooltip.Tooltip
81
- left={tooltipLeftAxisYRight}
82
- style={{
83
- ...baseStyles,
84
- ...cardStyles,
85
- transform: 'translateX(-70%) translateY(-80%)'
86
- }}
87
- top={tooltipTopAxisYRight}
88
- >
89
- <Typography variant="caption">{tooltipDataAxisYRight}</Typography>
90
- </Tooltip.Tooltip>
91
- )}
92
- </>
93
- );
94
- };
95
-
96
- export default TooltipAnchorPoint;
@@ -1,107 +0,0 @@
1
- import { useEffect } from 'react';
2
-
3
- import { Tooltip } from '@visx/visx';
4
- import { isNil } from 'ramda';
5
-
6
- import { useLocaleDateTimeFormat } from '@centreon/ui';
7
-
8
- import { margin, timeFormat } from '../../common/index';
9
-
10
- import { TooltipAnchorModel, UseTooltipAnchorPointResult } from './models';
11
- import useTickGraph from './useTickGraph';
12
-
13
- const useTooltipAnchorPoint = ({
14
- timeSeries,
15
- xScale,
16
- graphHeight,
17
- leftScale,
18
- rightScale,
19
- graphWidth,
20
- lines,
21
- baseAxis
22
- }: TooltipAnchorModel): UseTooltipAnchorPointResult => {
23
- const { format } = useLocaleDateTimeFormat();
24
-
25
- const { positionX, positionY, tickAxisBottom, tickAxisLeft, tickAxisRight } =
26
- useTickGraph({
27
- baseAxis,
28
- leftScale,
29
- lines,
30
- rightScale,
31
- timeSeries,
32
- xScale
33
- });
34
-
35
- const {
36
- showTooltip: showTooltipAxisYLeft,
37
- tooltipData: tooltipDataAxisYLeft,
38
- tooltipLeft: tooltipLeftAxisYLeft,
39
- tooltipTop: tooltipTopAxisYLeft
40
- } = Tooltip.useTooltip();
41
- const {
42
- showTooltip: showTooltipAxisX,
43
- tooltipData: tooltipDataAxisX,
44
- tooltipLeft: tooltipLeftAxisX,
45
- tooltipTop: tooltipTopAxisX
46
- } = Tooltip.useTooltip();
47
-
48
- const {
49
- showTooltip: showTooltipAxisYRight,
50
- tooltipData: tooltipDataAxisYRight,
51
- tooltipLeft: tooltipLeftAxisYRight,
52
- tooltipTop: tooltipTopAxisYRight
53
- } = Tooltip.useTooltip();
54
-
55
- useEffect(() => {
56
- if (!positionX || !positionY || !tickAxisBottom) {
57
- return;
58
- }
59
-
60
- const dataAxisX = format({
61
- date: tickAxisBottom,
62
- formatString: timeFormat
63
- });
64
-
65
- showTooltipAxisX({
66
- tooltipData: dataAxisX,
67
- tooltipLeft: positionX + margin.left,
68
- tooltipTop: graphHeight + margin.top
69
- });
70
- }, [positionX, positionY, tickAxisBottom]);
71
-
72
- useEffect(() => {
73
- if (!positionX || !positionY || !tickAxisLeft) {
74
- return;
75
- }
76
- showTooltipAxisYLeft({
77
- tooltipData: tickAxisLeft,
78
- tooltipLeft: margin.left,
79
- tooltipTop: positionY + margin.top
80
- });
81
- }, [tickAxisLeft, positionX, positionY]);
82
-
83
- useEffect(() => {
84
- if (!positionX || !positionY || !tickAxisRight) {
85
- return;
86
- }
87
- showTooltipAxisYRight({
88
- tooltipData: tickAxisRight,
89
- tooltipLeft: graphWidth ? graphWidth + margin.left : 0,
90
- tooltipTop: positionY + margin.top
91
- });
92
- }, [positionX, positionY, tickAxisRight]);
93
-
94
- return {
95
- tooltipDataAxisX: !isNil(tickAxisBottom) ? tooltipDataAxisX : null,
96
- tooltipDataAxisYLeft: !isNil(tickAxisLeft) ? tooltipDataAxisYLeft : null,
97
- tooltipDataAxisYRight: !isNil(tickAxisRight) ? tooltipDataAxisYRight : null,
98
- tooltipLeftAxisX,
99
- tooltipLeftAxisYLeft,
100
- tooltipLeftAxisYRight,
101
- tooltipTopAxisX,
102
- tooltipTopAxisYLeft,
103
- tooltipTopAxisYRight
104
- } as UseTooltipAnchorPointResult;
105
- };
106
-
107
- export default useTooltipAnchorPoint;
@@ -1,22 +0,0 @@
1
- import { Typography } from '@mui/material';
2
-
3
- import { useLegendValueStyles } from './Legend.styles';
4
-
5
- interface Props {
6
- value?: string | null;
7
- }
8
-
9
- const InteractiveValue = ({ value }: Props): JSX.Element | null => {
10
- const { classes } = useLegendValueStyles({});
11
- if (!value) {
12
- return null;
13
- }
14
-
15
- return (
16
- <Typography className={classes.text} variant="h6">
17
- {value}
18
- </Typography>
19
- );
20
- };
21
-
22
- export default InteractiveValue;
@@ -1,99 +0,0 @@
1
- import { useMemo } from 'react';
2
-
3
- import { useAtomValue } from 'jotai';
4
- import { equals, find, isNil } from 'ramda';
5
-
6
- import { mousePositionAtom } from '../InteractiveComponents/interactionWithGraphAtoms';
7
- import {
8
- formatMetricValueWithUnit,
9
- getLineForMetric,
10
- getMetrics,
11
- getTimeValue
12
- } from '../../common/timeSeries';
13
- import { Line, TimeValue } from '../../common/timeSeries/models';
14
-
15
- import { FormattedMetricData } from './models';
16
-
17
- interface InteractiveValues {
18
- getFormattedValue: (line: Line) => string | null | undefined;
19
- }
20
-
21
- interface Props {
22
- base: number;
23
- lines: Array<Line>;
24
- timeSeries: Array<TimeValue>;
25
- xScale;
26
- }
27
-
28
- const useInteractiveValues = ({
29
- timeSeries,
30
- lines,
31
- base,
32
- xScale
33
- }: Props): InteractiveValues => {
34
- const mousePosition = useAtomValue(mousePositionAtom);
35
-
36
- const timeValue = getTimeValue({
37
- timeSeries,
38
- x: mousePosition?.[0],
39
- xScale
40
- });
41
-
42
- const graphTimeValue = timeSeries?.find((item) =>
43
- equals(item.timeTick, timeValue?.timeTick)
44
- );
45
-
46
- const getMetricsToDisplay = (): Array<number> => {
47
- if (isNil(graphTimeValue)) {
48
- return [];
49
- }
50
- const metricsData = getMetrics(graphTimeValue as TimeValue);
51
-
52
- const metricsToDisplay = metricsData.filter((metric_id) => {
53
- const line = getLineForMetric({ lines, metric_id: Number(metric_id) });
54
-
55
- return !isNil(graphTimeValue[metric_id]) && !isNil(line);
56
- });
57
-
58
- return metricsToDisplay.map(Number);
59
- };
60
-
61
- const metrics = useMemo(() => getMetricsToDisplay(), [graphTimeValue]);
62
-
63
- const getFormattedMetricData = (
64
- metric_id: number
65
- ): FormattedMetricData | null => {
66
- if (isNil(graphTimeValue)) {
67
- return null;
68
- }
69
- const value = graphTimeValue[metric_id] as number;
70
-
71
- const { color, name, unit } = getLineForMetric({
72
- lines,
73
- metric_id
74
- }) as Line;
75
-
76
- const formattedValue = formatMetricValueWithUnit({
77
- base,
78
- unit,
79
- value
80
- });
81
-
82
- return {
83
- color,
84
- formattedValue,
85
- name,
86
- unit
87
- };
88
- };
89
-
90
- const getFormattedValue = (line: Line): string | undefined | null => {
91
- const metric_id = find(equals(line.metric_id), metrics);
92
-
93
- return metric_id ? getFormattedMetricData(metric_id)?.formattedValue : null;
94
- };
95
-
96
- return { getFormattedValue };
97
- };
98
-
99
- export default useInteractiveValues;