UNPKG

@grafana/alerting

Version:

Grafana Alerting Library – Build vertical integrations on top of the industry-leading alerting solution

91 lines (86 loc) 3.25 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var jsxRuntime = require('react/jsx-runtime'); var react = require('react'); var i18n = require('@grafana/i18n'); var ui = require('@grafana/ui'); var consts = require('../../consts.cjs'); var useRoutingTrees = require('../../hooks/useRoutingTrees.cjs'); "use strict"; const collator = new Intl.Collator("en", { sensitivity: "accent" }); function RoutingTreeSelector(props) { const { currentData: routingTrees, isLoading, isError } = useRoutingTrees.useListRoutingTrees({}, { refetchOnFocus: true, refetchOnMountOrArgChange: true }); const { options, treeLookup } = react.useMemo(() => { if (!(routingTrees == null ? void 0 : routingTrees.items)) { const empty = { options: [], treeLookup: /* @__PURE__ */ new Map() }; return empty; } const lookup = /* @__PURE__ */ new Map(); const opts = routingTrees.items.map((tree) => { var _a; const name = (_a = tree.metadata.name) != null ? _a : ""; const isDefault = name === consts.USER_DEFINED_TREE_NAME; lookup.set(name, tree); return { label: isDefault ? i18n.t("alerting.routing-tree-selector.default-policy", "Default policy") : name, value: name, description: isDefault ? i18n.t( "alerting.routing-tree-selector.default-policy-desc", "Routes alerts using the default notification policy tree" ) : i18n.t("alerting.routing-tree-selector.custom-policy-desc", "Route alerts through the {{name}} policy tree", { name }) }; }).sort((a, b) => { if (a.value === consts.USER_DEFINED_TREE_NAME) { return -1; } if (b.value === consts.USER_DEFINED_TREE_NAME) { return 1; } return collator.compare(a.label, b.label); }); return { options: opts, treeLookup: lookup }; }, [routingTrees == null ? void 0 : routingTrees.items]); if (isError) { return /* @__PURE__ */ jsxRuntime.jsx( ui.Alert, { severity: "warning", title: i18n.t("alerting.routing-tree-selector.error", "Failed to load notification policies") } ); } if (props.multi) { const { multi: _, onChange, ...rest } = props; const handleChange2 = (selectedOptions) => { const trees = selectedOptions.map((opt) => treeLookup.get(opt.value)).filter((tree) => tree != null); onChange(trees); }; return /* @__PURE__ */ jsxRuntime.jsx(ui.MultiCombobox, { ...rest, loading: isLoading, options, onChange: handleChange2 }); } const handleChange = (selectedOption) => { if (selectedOption == null && props.isClearable) { props.onChange(null); return; } if (selectedOption) { const tree = treeLookup.get(selectedOption.value); if (!tree) { console.warn(`RoutingTreeSelector: could not find routing tree for value "${selectedOption.value}"`); return; } props.onChange(tree); } }; return /* @__PURE__ */ jsxRuntime.jsx(ui.Combobox, { ...props, loading: isLoading, options, onChange: handleChange }); } exports.RoutingTreeSelector = RoutingTreeSelector; //# sourceMappingURL=RoutingTreeSelector.cjs.map