@netdata/charts
Version:
Netdata frontend SDK and chart utilities
151 lines (150 loc) • 6.75 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { memo } from "react";
import Icon, { Button } from "../../../icon";
import { useAttributeValue, useChart } from "../../../provider";
import Tooltip from "../../../tooltip";
import { Flex, TextNano } from "@netdata/netdata-ui";
import plusIcon from "@netdata/netdata-ui/dist/components/icon/assets/plus.svg";
import xIcon from "@netdata/netdata-ui/dist/components/icon/assets/x.svg";
import styled from "styled-components";
import { Divider } from "../highlight";
import makeLog from "../../../../sdk/makeLog";
import AnnotationForm from "./form";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
var StyledDraftAnnotation = styled(Flex).attrs({
justifyContent: "center",
alignItems: "center",
gap: 2,
height: "40px",
alignSelf: "center",
round: true,
width: {
min: "120px"
},
padding: [1, 2],
border: {
side: "all",
color: "borderSecondary",
style: "dashed"
},
background: "mainBackground",
backgroundOpacity: 0.8,
zIndex: 10
}).withConfig({
displayName: "draft__StyledDraftAnnotation",
componentId: "sc-12pquls-0"
})([""]);
var DraftAnnotationContent = /*#__PURE__*/memo(function (_ref) {
var annotation = _ref.annotation;
var chart = useChart();
return /*#__PURE__*/_jsxs(Flex, {
column: true,
gap: [0.5],
children: [/*#__PURE__*/_jsx(TextNano, {
strong: true,
color: "textLite",
children: "New annotation"
}), /*#__PURE__*/_jsxs(TextNano, {
color: "textLite",
children: [chart.formatDate(annotation.timestamp * 1000), " \u2022", " ", chart.formatTime(annotation.timestamp * 1000)]
})]
});
});
var DraftAnnotationActions = /*#__PURE__*/memo(function () {
var chart = useChart();
var draftAnnotation = useAttributeValue("draftAnnotation");
var handleStartEditing = function handleStartEditing() {
chart.updateAttribute("draftAnnotation", _objectSpread(_objectSpread({}, draftAnnotation), {}, {
status: "editing"
}));
};
var handleCancel = function handleCancel() {
chart.updateAttribute("draftAnnotation", null);
};
if (draftAnnotation.status === "editing") {
return /*#__PURE__*/_jsx(DraftAnnotationForm, {});
}
return /*#__PURE__*/_jsxs(Flex, {
gap: 1,
children: [/*#__PURE__*/_jsx(Tooltip, {
content: "Add annotation text",
children: /*#__PURE__*/_jsx(Button, {
icon: /*#__PURE__*/_jsx(Icon, {
svg: plusIcon,
size: "16px"
}),
onClick: handleStartEditing,
"data-testid": "draft-annotation-add",
"data-track": chart.track("annotation-start-editing")
})
}), /*#__PURE__*/_jsx(Tooltip, {
content: "Cancel annotation",
children: /*#__PURE__*/_jsx(Button, {
icon: /*#__PURE__*/_jsx(Icon, {
svg: xIcon,
size: "16px"
}),
onClick: handleCancel,
"data-testid": "draft-annotation-cancel",
"data-track": chart.track("annotation-draft-cancel")
})
})]
});
});
var DraftAnnotationForm = /*#__PURE__*/memo(function () {
var chart = useChart();
var draftAnnotation = useAttributeValue("draftAnnotation");
var handleSave = function handleSave(formData) {
chart.updateAttribute("draftAnnotation", _objectSpread(_objectSpread({}, draftAnnotation), {}, {
status: "saving"
}));
var newAnnotation = {
id: "annotation_".concat(draftAnnotation.timestamp, "_").concat(formData.text.toLowerCase().replace(/[^a-z0-9]/g, "").substring(0, 8)),
type: "annotation",
timestamp: draftAnnotation.timestamp,
text: formData.text,
created: new Date().toISOString(),
color: formData.color,
priority: formData.priority
};
// Log annotation creation for debugging
makeLog(chart)({
event: "annotation_created",
annotationId: newAnnotation.id,
timestamp: newAnnotation.timestamp,
textLength: formData.text.length,
priority: formData.priority
});
var overlays = chart.getAttribute("overlays");
chart.updateAttribute("overlays", _objectSpread(_objectSpread({}, overlays), {}, _defineProperty({}, newAnnotation.id, newAnnotation)));
chart.updateAttribute("draftAnnotation", null);
chart.trigger("annotationCreated", newAnnotation);
chart.sdk.trigger("annotationCreated", chart, newAnnotation);
};
var handleCancel = function handleCancel() {
return chart.updateAttribute("draftAnnotation", null);
};
return /*#__PURE__*/_jsx(AnnotationForm, {
placeholder: "Annotation text...",
onSave: handleSave,
onCancel: handleCancel,
autoFocus: true
});
});
var DraftAnnotation = function DraftAnnotation() {
var chart = useChart();
var draftAnnotation = useAttributeValue("draftAnnotation");
if (!draftAnnotation) return null;
return /*#__PURE__*/_jsxs(StyledDraftAnnotation, {
"data-track": chart.track("annotation-draft-display"),
children: [/*#__PURE__*/_jsx(DraftAnnotationContent, {
annotation: draftAnnotation
}), /*#__PURE__*/_jsx(Divider, {}), /*#__PURE__*/_jsx(DraftAnnotationActions, {})]
});
};
export default DraftAnnotation;