@grafana/ui
Version:
Grafana Components Library
54 lines (51 loc) • 1.85 kB
JavaScript
import { jsx } from 'react/jsx-runtime';
import { useMemo } from 'react';
import { RadioButtonGroup } from '../Forms/RadioButtonGroup/RadioButtonGroup.mjs';
import { getGroupDescriptionForScope, getGroupLabelForScope } from './utils.mjs';
export { getUniqueMatcherScopes } from './utils.mjs';
;
function buildScopeOptions(providedUniqScopes, currentScope, allowedScopes = Array.from(providedUniqScopes)) {
const uniqScopes = new Set(providedUniqScopes);
if (allowedScopes) {
uniqScopes.forEach((scope) => {
if (!allowedScopes.includes(scope)) {
uniqScopes.delete(scope);
}
});
}
const scopeNotFound = currentScope && !uniqScopes.has(currentScope);
if (scopeNotFound) {
uniqScopes.add(currentScope);
}
const arr = [
{
label: getGroupLabelForScope("series"),
description: getGroupDescriptionForScope("series"),
value: "series"
}
];
for (const scope of uniqScopes) {
if (scope === "series") {
continue;
}
arr.push({
label: getGroupLabelForScope(scope),
description: getGroupDescriptionForScope(scope),
value: scope
});
}
return arr;
}
function useMatcherScopesOptions(providedUniqScopes, currentScope, allowedScopes = Array.from(providedUniqScopes)) {
return useMemo(
() => buildScopeOptions(providedUniqScopes, currentScope, allowedScopes),
[providedUniqScopes, currentScope, allowedScopes]
);
}
function MatcherScopeSelector({ value, scopes, allowedScopes, ...rest }) {
var _a;
const options = useMatcherScopesOptions(scopes, value, allowedScopes);
return /* @__PURE__ */ jsx(RadioButtonGroup, { ...rest, options, value: value != null ? value : (_a = options[0]) == null ? void 0 : _a.value });
}
export { MatcherScopeSelector, buildScopeOptions };
//# sourceMappingURL=MatcherScopeSelector.mjs.map