| 1 | var _excluded = ["children"];
|
|---|
| 2 | function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
|
|---|
| 3 | function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
|
|---|
| 4 | import * as React from 'react';
|
|---|
| 5 | import { createContext, useContext, useEffect, useRef } from 'react';
|
|---|
| 6 | import { addErrorBar, removeErrorBar, replaceErrorBar } from '../state/errorBarSlice';
|
|---|
| 7 | import { useAppDispatch } from '../state/hooks';
|
|---|
| 8 | import { useGraphicalItemId } from './RegisterGraphicalItemId';
|
|---|
| 9 | var initialContextState = {
|
|---|
| 10 | data: [],
|
|---|
| 11 | xAxisId: 'xAxis-0',
|
|---|
| 12 | yAxisId: 'yAxis-0',
|
|---|
| 13 | dataPointFormatter: () => ({
|
|---|
| 14 | x: 0,
|
|---|
| 15 | y: 0,
|
|---|
| 16 | value: 0
|
|---|
| 17 | }),
|
|---|
| 18 | errorBarOffset: 0
|
|---|
| 19 | };
|
|---|
| 20 | var ErrorBarContext = /*#__PURE__*/createContext(initialContextState);
|
|---|
| 21 | export function SetErrorBarContext(props) {
|
|---|
| 22 | var {
|
|---|
| 23 | children
|
|---|
| 24 | } = props,
|
|---|
| 25 | rest = _objectWithoutProperties(props, _excluded);
|
|---|
| 26 | return /*#__PURE__*/React.createElement(ErrorBarContext.Provider, {
|
|---|
| 27 | value: rest
|
|---|
| 28 | }, children);
|
|---|
| 29 | }
|
|---|
| 30 | export var useErrorBarContext = () => useContext(ErrorBarContext);
|
|---|
| 31 | export function ReportErrorBarSettings(props) {
|
|---|
| 32 | var dispatch = useAppDispatch();
|
|---|
| 33 | var graphicalItemId = useGraphicalItemId();
|
|---|
| 34 | var prevPropsRef = useRef(null);
|
|---|
| 35 | useEffect(() => {
|
|---|
| 36 | if (graphicalItemId == null) {
|
|---|
| 37 | // ErrorBar outside a graphical item context does not do anything.
|
|---|
| 38 | return;
|
|---|
| 39 | }
|
|---|
| 40 | if (prevPropsRef.current === null) {
|
|---|
| 41 | dispatch(addErrorBar({
|
|---|
| 42 | itemId: graphicalItemId,
|
|---|
| 43 | errorBar: props
|
|---|
| 44 | }));
|
|---|
| 45 | } else if (prevPropsRef.current !== props) {
|
|---|
| 46 | dispatch(replaceErrorBar({
|
|---|
| 47 | itemId: graphicalItemId,
|
|---|
| 48 | prev: prevPropsRef.current,
|
|---|
| 49 | next: props
|
|---|
| 50 | }));
|
|---|
| 51 | }
|
|---|
| 52 | prevPropsRef.current = props;
|
|---|
| 53 | }, [dispatch, graphicalItemId, props]);
|
|---|
| 54 | useEffect(() => {
|
|---|
| 55 | return () => {
|
|---|
| 56 | if (prevPropsRef.current != null && graphicalItemId != null) {
|
|---|
| 57 | dispatch(removeErrorBar({
|
|---|
| 58 | itemId: graphicalItemId,
|
|---|
| 59 | errorBar: prevPropsRef.current
|
|---|
| 60 | }));
|
|---|
| 61 | prevPropsRef.current = null;
|
|---|
| 62 | }
|
|---|
| 63 | };
|
|---|
| 64 | }, [dispatch, graphicalItemId]);
|
|---|
| 65 | return null;
|
|---|
| 66 | } |
|---|