UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

149 lines (148 loc) 10.3 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 _search = require("../search"); var _columns = require("./columns"); 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 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); } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } 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 noop = function noop() {}; var cannotExpand = function cannotExpand() { return false; }; var getRowId = function getRowId(row) { return row.rowId; }; var getRowCanExpand = function getRowCanExpand(row) { return row.original.kind === "context"; }; var CorrelationTable = function CorrelationTable(_ref) { var data = _ref.data; var tableRef = (0, _react.useRef)(null); var containerRef = (0, _react.useRef)(null); var restoreSearchFocusRef = (0, _react.useRef)(false); var _useAttribute = (0, _provider.useAttribute)("correlate.expanded"), _useAttribute2 = _slicedToArray(_useAttribute, 2), _useAttribute2$ = _useAttribute2[0], expanded = _useAttribute2$ === void 0 ? {} : _useAttribute2$, setExpanded = _useAttribute2[1]; var _useAttribute3 = (0, _provider.useAttribute)("correlate.search"), _useAttribute4 = _slicedToArray(_useAttribute3, 2), _useAttribute4$ = _useAttribute4[0], search = _useAttribute4$ === void 0 ? "" : _useAttribute4$, setSearch = _useAttribute4[1]; var columns = (0, _react.useMemo)(function () { return [(0, _columns.nameColumn)(), (0, _columns.correlationColumn)(), (0, _columns.changeColumn)(), (0, _columns.sparklineColumn)()]; }, []); var searching = Boolean((0, _search.normalizeSearch)(search)); var searchableData = (0, _react.useMemo)(function () { return data.flatMap(function (context) { return [_objectSpread(_objectSpread({}, context), {}, { children: undefined, searchDepth: 0 })].concat(_toConsumableArray((context.children || []).map(function (dimension) { return _objectSpread(_objectSpread({}, dimension), {}, { children: undefined, searchDepth: 1, searchSiblings: context.children }); }))); }); }, [data]); var displayData = (0, _react.useMemo)(function () { return searching ? searchableData.filter(function (row) { return (0, _search.matchesSearch)(row.kind === "context" ? [row.contextName] : [row.dimensionName, row.nodeName, row.context], search); }) : data; }, [data, search, searchableData, searching]); var dimensionCount = (0, _react.useMemo)(function () { return displayData.reduce(function (count, row) { var _row$children; return count + (row.kind === "dimension" ? 1 : ((_row$children = row.children) === null || _row$children === void 0 ? void 0 : _row$children.length) || row.count || 0); }, 0); }, [displayData]); var contextCount = (0, _react.useMemo)(function () { return new Set(displayData.map(function (row) { return row.context; })).size; }, [displayData]); var allExpanded = searching || expanded === true || data.every(function (context) { return Boolean(expanded === null || expanded === void 0 ? void 0 : expanded[context.rowId]); }); var getItemKey = (0, _react.useCallback)(function (index) { var _tableRef$current, _displayData$index; return ((_tableRef$current = tableRef.current) === null || _tableRef$current === void 0 || (_tableRef$current = _tableRef$current.getRowModel().rows[index]) === null || _tableRef$current === void 0 ? void 0 : _tableRef$current.id) || ((_displayData$index = displayData[index]) === null || _displayData$index === void 0 ? void 0 : _displayData$index.rowId) || index; }, [displayData]); var virtualizeOptions = (0, _react.useMemo)(function () { return { overscan: 1, getItemKey: getItemKey }; }, [getItemKey]); var onSearch = (0, _react.useCallback)(function (query) { var _containerRef$current; restoreSearchFocusRef.current = ((_containerRef$current = containerRef.current) === null || _containerRef$current === void 0 ? void 0 : _containerRef$current.querySelector('[data-testid="table-global-search-filter"]')) === document.activeElement; setSearch(query); }, [setSearch]); (0, _react.useLayoutEffect)(function () { var _containerRef$current2; if (!restoreSearchFocusRef.current) return; var input = (_containerRef$current2 = containerRef.current) === null || _containerRef$current2 === void 0 ? void 0 : _containerRef$current2.querySelector('[data-testid="table-global-search-filter"]'); input === null || input === void 0 || input.focus(); input === null || input === void 0 || input.setSelectionRange(input.value.length, input.value.length); restoreSearchFocusRef.current = false; }, [searching]); return /*#__PURE__*/(0, _jsxRuntime.jsx)(_netdataUi.Flex, { flex: true, column: true, height: { min: "0px", base: "100%" }, overflow: "hidden", ref: containerRef, children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_netdataUi.Table, { title: "Found ".concat(dimensionCount, " correlated dimensions across ").concat(contextCount, " contexts"), headerChildren: /*#__PURE__*/(0, _jsxRuntime.jsx)(_netdataUi.Button, { neutral: true, disabled: searching, label: allExpanded ? "Collapse all" : "Expand all", onClick: function onClick() { return setExpanded(allExpanded ? {} : true); } }), enableCustomSearch: true, dataColumns: columns, data: displayData, expanded: searching ? {} : expanded, onExpandedChange: searching ? noop : setExpanded, globalFilter: search, onSearch: onSearch, getRowId: getRowId, getRowCanExpand: searching ? cannotExpand : getRowCanExpand, virtualizeOptions: virtualizeOptions, tableRef: tableRef, width: "100%" }, searching ? "search" : "browse") }); }; var _default = exports["default"] = CorrelationTable;