UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

170 lines (169 loc) 7.91 kB
"use strict"; 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); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireWildcard(require("react")); var _netdataUi = require("@netdata/netdata-ui"); var _provider = require("../../provider"); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } var normalizeInitialValues = function normalizeInitialValues(initialValues) { var _ref = initialValues || {}, _ref$label = _ref.label, label = _ref$label === void 0 ? "" : _ref$label, _ref$offsetSeconds = _ref.offsetSeconds, offsetSeconds = _ref$offsetSeconds === void 0 ? 0 : _ref$offsetSeconds; var days = Math.floor(offsetSeconds / (24 * 60 * 60)); var hours = Math.floor(offsetSeconds % (24 * 60 * 60) / (60 * 60)); return { label: label, days: days ? days.toString() : "", hours: hours ? hours.toString() : "" }; }; var generateLabel = function generateLabel(days, hours, locale) { var rtf = new Intl.RelativeTimeFormat(locale || "en", { numeric: "always" }); if (days > 0 && hours === 0) { return rtf.format(-days, "day"); } if (hours > 0 && days === 0) { return rtf.format(-hours, "hour"); } if (days > 0 && hours > 0) { return "".concat(rtf.format(-days, "day"), " and ").concat(rtf.format(-hours, "hour")); } return ""; }; var CustomPeriodForm = function CustomPeriodForm(_ref2) { var onSubmit = _ref2.onSubmit, onCancel = _ref2.onCancel, initialValues = _ref2.initialValues; var normalizedValues = normalizeInitialValues(initialValues); var locale = (0, _provider.useAttributeValue)("locale"); var _useState = (0, _react.useState)(normalizedValues.label), _useState2 = _slicedToArray(_useState, 2), label = _useState2[0], setLabel = _useState2[1]; var _useState3 = (0, _react.useState)(normalizedValues.days), _useState4 = _slicedToArray(_useState3, 2), offsetDays = _useState4[0], setOffsetDays = _useState4[1]; var _useState5 = (0, _react.useState)(normalizedValues.hours), _useState6 = _slicedToArray(_useState5, 2), offsetHours = _useState6[0], setOffsetHours = _useState6[1]; var handleAdd = function handleAdd() { var days = parseInt(offsetDays) || 0; var hours = parseInt(offsetHours) || 0; var offsetSeconds = days * 24 * 60 * 60 + hours * 60 * 60; if (offsetSeconds <= 0) return; var finalLabel = label.trim() || generateLabel(days, hours, locale); if (!finalLabel) return; var customPeriod = { id: (initialValues === null || initialValues === void 0 ? void 0 : initialValues.id) || "custom_".concat(Date.now()), label: finalLabel, offsetSeconds: offsetSeconds }; onSubmit(customPeriod); }; return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_netdataUi.Flex, { column: true, gap: 2, padding: [3], border: "all", round: true, width: { min: "200px" }, background: "neutral", children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_netdataUi.TextSmall, { strong: true, children: "Add Custom Period" }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_netdataUi.Flex, { column: true, gap: 1, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_netdataUi.TextMicro, { children: "Label" }), /*#__PURE__*/(0, _jsxRuntime.jsx)("input", { type: "text", value: label, onChange: function onChange(e) { return setLabel(e.target.value); }, placeholder: "e.g. 3 days ago", style: { padding: "4px", fontSize: "12px", border: "1px solid #ddd", borderRadius: "4px" } })] }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_netdataUi.Flex, { gap: 2, children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_netdataUi.Flex, { column: true, gap: 1, flex: 1, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_netdataUi.TextMicro, { children: "Days" }), /*#__PURE__*/(0, _jsxRuntime.jsx)("input", { type: "number", value: offsetDays, onChange: function onChange(e) { return setOffsetDays(e.target.value); }, min: "0", style: { padding: "4px", fontSize: "12px", border: "1px solid #ddd", borderRadius: "4px" } })] }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_netdataUi.Flex, { column: true, gap: 1, flex: 1, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_netdataUi.TextMicro, { children: "Hours" }), /*#__PURE__*/(0, _jsxRuntime.jsx)("input", { type: "number", value: offsetHours, onChange: function onChange(e) { return setOffsetHours(e.target.value); }, min: "0", max: "23", style: { padding: "4px", fontSize: "12px", border: "1px solid #ddd", borderRadius: "4px" } })] })] }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_netdataUi.Flex, { gap: 2, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_netdataUi.Button, { tiny: true, label: initialValues ? "Update" : "Add", onClick: handleAdd }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_netdataUi.Button, { tiny: true, label: "Cancel", onClick: onCancel })] })] }); }; var _default = exports["default"] = CustomPeriodForm;