@netdata/charts
Version:
Netdata frontend SDK and chart utilities
145 lines • 6.29 kB
JavaScript
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; }
import React, { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
import styled from "styled-components";
import { Flex, TextMicro } from "@netdata/netdata-ui";
import { useAttributeValue, useUnitSign } from "../../provider";
import { measureTextWidth } from "../../../helpers/canvas";
import Timestamp from "./timestamp";
import UpdateEvery from "./updateEvery";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
var contextMaxFontSize = 12;
var contextMinFontSize = 9;
var HeaderContainer = styled(Flex).attrs({
"data-testid": "chartPopover-header",
column: true,
gap: 1,
width: {
min: "0px",
base: "100%"
}
}).withConfig({
displayName: "header__HeaderContainer",
componentId: "sc-13omdh0-0"
})([""]);
var ContextText = styled(TextMicro).attrs({
"data-testid": "chartPopover-context",
strong: true
}).withConfig({
displayName: "header__ContextText",
componentId: "sc-13omdh0-1"
})(["display:block;min-width:0;width:100%;max-width:100%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:", "px;"], function (_ref) {
var $fontSize = _ref.$fontSize;
return $fontSize;
});
var MetaRow = styled(Flex).attrs({
alignItems: "center",
justifyContent: "between",
gap: 3,
width: {
min: "0px",
base: "100%"
}
}).withConfig({
displayName: "header__MetaRow",
componentId: "sc-13omdh0-2"
})([""]);
var TimestampCell = styled(Flex).attrs({
overflow: "hidden",
width: {
min: "0px"
}
}).withConfig({
displayName: "header__TimestampCell",
componentId: "sc-13omdh0-3"
})([""]);
var SourceUnits = styled(TextMicro).attrs({
color: "textLite",
"data-testid": "chartPopover-sourceUnits"
}).withConfig({
displayName: "header__SourceUnits",
componentId: "sc-13omdh0-4"
})(["flex:0 0 auto;white-space:nowrap;"]);
var getMeasuredFont = function getMeasuredFont(element, fontSize) {
var style = window.getComputedStyle(element);
return "".concat(style.fontStyle, " ").concat(style.fontVariant, " ").concat(style.fontWeight, " ").concat(fontSize, "px ").concat(style.fontFamily);
};
var getContextFontSize = function getContextFontSize(element, text) {
if (!element || !text) return contextMaxFontSize;
var width = element.getBoundingClientRect().width;
if (!width) return contextMaxFontSize;
var textWidth = measureTextWidth(text, getMeasuredFont(element, contextMaxFontSize));
if (!textWidth || textWidth <= width) return contextMaxFontSize;
var next = Math.floor(width / textWidth * contextMaxFontSize * 10) / 10;
return Math.max(contextMinFontSize, Math.min(contextMaxFontSize, next));
};
var useContextFontSize = function useContextFontSize(text) {
var ref = useRef();
var _useState = useState(contextMaxFontSize),
_useState2 = _slicedToArray(_useState, 2),
fontSize = _useState2[0],
setFontSize = _useState2[1];
var update = useCallback(function () {
var next = getContextFontSize(ref.current, text);
setFontSize(function (prev) {
return prev === next ? prev : next;
});
}, [text]);
useLayoutEffect(update, [update]);
useEffect(function () {
if (!ref.current) return;
if (typeof ResizeObserver === "undefined") {
window.addEventListener("resize", update);
return function () {
return window.removeEventListener("resize", update);
};
}
var observer = new ResizeObserver(update);
observer.observe(ref.current);
return function () {
return observer.disconnect();
};
}, [update]);
return [ref, fontSize];
};
var Context = function Context() {
var contextScope = useAttributeValue("contextScope");
var name = useAttributeValue("name");
var context = useMemo(function () {
return contextScope && contextScope.length ? contextScope.join(", ") : name || "";
}, [contextScope, name]);
var _useContextFontSize = useContextFontSize(context),
_useContextFontSize2 = _slicedToArray(_useContextFontSize, 2),
ref = _useContextFontSize2[0],
fontSize = _useContextFontSize2[1];
if (!context) return null;
return /*#__PURE__*/_jsx(ContextText, {
ref: ref,
title: context,
$fontSize: fontSize,
children: context
});
};
var Header = function Header(_ref2) {
var timestamp = _ref2.timestamp;
var sourceUnits = useUnitSign({
withoutConversion: true,
"long": true
});
return /*#__PURE__*/_jsxs(HeaderContainer, {
children: [/*#__PURE__*/_jsx(Context, {}), /*#__PURE__*/_jsxs(MetaRow, {
children: [/*#__PURE__*/_jsx(TimestampCell, {
children: timestamp && /*#__PURE__*/_jsx(Timestamp, {
value: timestamp
})
}), !!sourceUnits && /*#__PURE__*/_jsxs(SourceUnits, {
children: ["[", sourceUnits, "]"]
})]
}), /*#__PURE__*/_jsx(UpdateEvery, {})]
});
};
export default Header;