UNPKG

opendash

Version:

open.DASH data visualization framework

186 lines 9.02 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; import * as React from "react"; import { useElementSize, useImmerState, useDataItems, WidgetConfigError, useTranslation, useOpenDashServices, useSource, } from ".."; export function useWidgetContextSetup(context) { var _a, _b; var t = useTranslation(["opendash"])[0]; var DashboardService = useOpenDashServices().DashboardService; var _c = useImmerState({}), draft = _c[0], updateDraft = _c[1], replaceDraft = _c[2], assignToDraft = _c[3]; React.useEffect(function () { var _a, _b, _c, _d; if ((_b = (_a = context) === null || _a === void 0 ? void 0 : _a.widget) === null || _b === void 0 ? void 0 : _b.config) { replaceDraft((_d = (_c = context) === null || _c === void 0 ? void 0 : _c.widget) === null || _d === void 0 ? void 0 : _d.config); } }, [(_b = (_a = context) === null || _a === void 0 ? void 0 : _a.widget) === null || _b === void 0 ? void 0 : _b.config]); function saveDraft() { DashboardService.updateWidget(__assign(__assign({}, context.widget), { id: context.id, config: draft })); } function setLoading(loading) { context.setState({ loading: loading }); } function useContainerSize() { return useElementSize(context.container, 250); } function useSourceConfig() { var _a, _b; var type = (_a = context) === null || _a === void 0 ? void 0 : _a.type.dataItems; var config = (_b = context) === null || _b === void 0 ? void 0 : _b.widget.config._sources; var _c = useSource(), allSources = _c[2]; if (!type) { throw new WidgetConfigError(t("monitoring.widgets.config_error.sources_unsupported")); } if (type.select !== "source") { throw new WidgetConfigError(t("monitoring.widgets.config_error.sources_dev_bad_config")); } if (!config) { throw new WidgetConfigError(t("monitoring.widgets.config_error.sources_notfound")); } if (config.length > type.max) { throw new WidgetConfigError(t("monitoring.widgets.config_error.sources_max")); } if (config.length < type.min) { throw new WidgetConfigError(t("monitoring.widgets.config_error.sources_min")); } var sources = config.map(function (id) { return allSources.find(function (source) { return source.id === id; }); }); for (var _i = 0, sources_1 = sources; _i < sources_1.length; _i++) { var source = sources_1[_i]; if (!source) { throw new WidgetConfigError(t("monitoring.widgets.config_error.sources_missing")); } } return sources; } function useItemConfig() { var _a, _b; var type = (_a = context) === null || _a === void 0 ? void 0 : _a.type.dataItems; var config = (_b = context) === null || _b === void 0 ? void 0 : _b.widget.config._items; var allItems = useDataItems(); if (!type) { throw new WidgetConfigError(t("monitoring.widgets.config_error.items_unsupported")); } if (type.select !== "item") { throw new WidgetConfigError(t("monitoring.widgets.config_error.items_dev_bad_config")); } if (!config) { throw new WidgetConfigError(t("monitoring.widgets.config_error.items_notfound")); } if (config.length > type.max) { throw new WidgetConfigError(t("monitoring.widgets.config_error.items_max")); } if (config.length < type.min) { throw new WidgetConfigError(t("monitoring.widgets.config_error.items_min")); } var items = config.map(function (_a) { var source = _a[0], id = _a[1]; return allItems.find(function (item) { return item.id === id && item.source === source; }); }); for (var _i = 0, items_1 = items; _i < items_1.length; _i++) { var item = items_1[_i]; if (!item) { throw new WidgetConfigError(t("monitoring.widgets.config_error.items_missing")); } } return items; } function useItemDimensionConfig() { var _a, _b; var type = (_a = context) === null || _a === void 0 ? void 0 : _a.type.dataItems; var config = (_b = context) === null || _b === void 0 ? void 0 : _b.widget.config._dimensions; var allItems = useDataItems(); if (!type) { throw new WidgetConfigError(t("monitoring.widgets.config_error.items_unsupported")); } if (type.select !== "dimension") { throw new WidgetConfigError(t("monitoring.widgets.config_error.items_dev_bad_config")); } if (!config) { throw new WidgetConfigError(t("monitoring.widgets.config_error.items_notfound")); } if (config.length > type.max) { throw new WidgetConfigError(t("monitoring.widgets.config_error.items_max")); } if (config.length < type.min) { throw new WidgetConfigError(t("monitoring.widgets.config_error.items_min")); } var items = config.map(function (_a) { var source = _a[0], id = _a[1], dimension = _a[2]; return [ allItems.find(function (item) { return item.id === id && item.source === source; }), dimension, ]; }); for (var _i = 0, items_2 = items; _i < items_2.length; _i++) { var _c = items_2[_i], item = _c[0], dimension = _c[1]; if (!item) { throw new WidgetConfigError(t("monitoring.widgets.config_error.items_missing")); } if (!type.types.includes(item.valueTypes[dimension].type)) { throw new WidgetConfigError(t("monitoring.widgets.config_error.items_type")); } } return items; } function useFetchHistory(overwriteOptions) { if (overwriteOptions === void 0) { overwriteOptions = {}; } var _a; var allItems = useDataItems(); var itemsToFetch = React.useMemo(function () { var _a, _b; var config = __spreadArrays((((_a = context) === null || _a === void 0 ? void 0 : _a.widget.config._items) || []), (((_b = context) === null || _b === void 0 ? void 0 : _b.widget.config._dimensions) || [])); return config.map(function (_a) { var source = _a[0], id = _a[1]; return allItems.find(function (item) { return item.source === source && item.id === id; }); }); }, [allItems, (_a = context) === null || _a === void 0 ? void 0 : _a.widget.config]); return []; } function useHistoryConfig() { var _a, _b, _c, _d, _e; var type = (_a = context) === null || _a === void 0 ? void 0 : _a.type.dataHistory; var config = (_b = context) === null || _b === void 0 ? void 0 : _b.widget.config._history; if (!type) { throw new WidgetConfigError(t("monitoring.widgets.config_error.history_unsupported")); } if (!config) { throw new WidgetConfigError(t("monitoring.widgets.config_error.history_notfound")); } return (_e = (_d = (_c = context) === null || _c === void 0 ? void 0 : _c.widget) === null || _d === void 0 ? void 0 : _d.config) === null || _e === void 0 ? void 0 : _e._history; } return React.useMemo(function () { var _a; return ({ config: (_a = context) === null || _a === void 0 ? void 0 : _a.widget.config, draft: draft, updateDraft: updateDraft, replaceDraft: replaceDraft, assignToDraft: assignToDraft, saveDraft: saveDraft, setLoading: setLoading, useContainerSize: useContainerSize, useSourceConfig: useSourceConfig, useItemConfig: useItemConfig, useItemDimensionConfig: useItemDimensionConfig, useHistoryConfig: useHistoryConfig, }); }, [context, draft]); } //# sourceMappingURL=useWidgetContextSetup.js.map