UNPKG

@grafana/alerting

Version:

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

87 lines (84 loc) 3.09 kB
import { jsx } from 'react/jsx-runtime'; import { useMemo } from 'react'; import { t } from '@grafana/i18n'; import { Alert, MultiCombobox, Combobox } from '@grafana/ui'; import { USER_DEFINED_TREE_NAME } from '../../consts.mjs'; import { useListRoutingTrees } from '../../hooks/useRoutingTrees.mjs'; "use strict"; const collator = new Intl.Collator("en", { sensitivity: "accent" }); function RoutingTreeSelector(props) { const { currentData: routingTrees, isLoading, isError } = useListRoutingTrees({}, { refetchOnFocus: true, refetchOnMountOrArgChange: true }); const { options, treeLookup } = 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 === USER_DEFINED_TREE_NAME; lookup.set(name, tree); return { label: isDefault ? t("alerting.routing-tree-selector.default-policy", "Default policy") : name, value: name, description: isDefault ? t( "alerting.routing-tree-selector.default-policy-desc", "Routes alerts using the default notification policy tree" ) : t("alerting.routing-tree-selector.custom-policy-desc", "Route alerts through the {{name}} policy tree", { name }) }; }).sort((a, b) => { if (a.value === USER_DEFINED_TREE_NAME) { return -1; } if (b.value === 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__ */ jsx( Alert, { severity: "warning", title: 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__ */ jsx(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__ */ jsx(Combobox, { ...props, loading: isLoading, options, onChange: handleChange }); } export { RoutingTreeSelector }; //# sourceMappingURL=RoutingTreeSelector.mjs.map