The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/public/app/features/alerting/unified/components/AlertLabelDropdown.tsx

71 lines
2.3 KiB

import { css } from '@emotion/css';
import { FC, forwardRef } from 'react';
import { GroupBase, OptionsOrGroups, createFilter } from 'react-select';
Alerting: Suggest previously entered custom labels (#57783) * [Alerting] - replace label inputs with dropdowns (#57019) * Add AlertLabelDropdown component It will be used to pick from or create new labels * Adapt LabelsField component to use AlertLabelDropdown instead of inputs * Add tests for LabelsField component Plus a few other tests were adapted to work with the label dropdowns * Use ref in component * Fix showing placeholders in the label dropdowns * Minor syntax change * Remove unneeded import after rebase * Display custom labels When a label key is selected, its corresponding values are shown in the dropdown * Add tooltip explaining where labels in the dropdowns come from * Fix import of Stack component * Avoid duplicated values * Improvements based on review * Display labels for currently selected datasource only * Refactor AlertsField to allow to choose whether to suggest labels or not * Suggest labels for NotificationStep and tests * Don't suggest labels in TestContactPointModal * [LabelsField] - refactor: get dataSourceName as a parameter * [LabelsField] - extract common code into reusable components * Display loading spinner while fetching rules * LabelsField - refactor Removing the suggest prop and the default dataSource 'grafana'. Instead, the component now relies on the dataSourceName param. If it's set it means we want to show suggestions so we fetch the labels, otherwise, if not set, we show the plain input texts without suggestions. * Add test for LabelsField without suggestions * Show custom labels for grafana managed alerts When the dataSourceName in the NotificationsStep component has a null value, we can assume it's because we're dealing with grafana managed alerts. In that case we set the correct value. * Fix tests after latest changes Since we removed the combobox from the TestContactPoints modal, tests had to be adjusted * Update texts * initialize all new added inputs with empty data
3 years ago
import { SelectableValue } from '@grafana/data';
import { Field, Select, useStyles2 } from '@grafana/ui';
import { t } from 'app/core/internationalization';
Alerting: Suggest previously entered custom labels (#57783) * [Alerting] - replace label inputs with dropdowns (#57019) * Add AlertLabelDropdown component It will be used to pick from or create new labels * Adapt LabelsField component to use AlertLabelDropdown instead of inputs * Add tests for LabelsField component Plus a few other tests were adapted to work with the label dropdowns * Use ref in component * Fix showing placeholders in the label dropdowns * Minor syntax change * Remove unneeded import after rebase * Display custom labels When a label key is selected, its corresponding values are shown in the dropdown * Add tooltip explaining where labels in the dropdowns come from * Fix import of Stack component * Avoid duplicated values * Improvements based on review * Display labels for currently selected datasource only * Refactor AlertsField to allow to choose whether to suggest labels or not * Suggest labels for NotificationStep and tests * Don't suggest labels in TestContactPointModal * [LabelsField] - refactor: get dataSourceName as a parameter * [LabelsField] - extract common code into reusable components * Display loading spinner while fetching rules * LabelsField - refactor Removing the suggest prop and the default dataSource 'grafana'. Instead, the component now relies on the dataSourceName param. If it's set it means we want to show suggestions so we fetch the labels, otherwise, if not set, we show the plain input texts without suggestions. * Add test for LabelsField without suggestions * Show custom labels for grafana managed alerts When the dataSourceName in the NotificationsStep component has a null value, we can assume it's because we're dealing with grafana managed alerts. In that case we set the correct value. * Fix tests after latest changes Since we removed the combobox from the TestContactPoints modal, tests had to be adjusted * Update texts * initialize all new added inputs with empty data
3 years ago
export interface AlertLabelDropdownProps {
onChange: (newValue: SelectableValue<string>) => void;
onOpenMenu?: () => void;
options: SelectableValue[];
defaultValue?: SelectableValue;
type: 'key' | 'value';
}
const _customFilter = createFilter({ ignoreCase: false });
function customFilter(opt: SelectableValue, searchQuery: string) {
return _customFilter(
{
label: opt.label ?? '',
value: opt.value ?? '',
data: {},
},
searchQuery
);
}
const handleIsValidNewOption = (
inputValue: string,
_: SelectableValue<string> | null,
options: OptionsOrGroups<SelectableValue<string>, GroupBase<SelectableValue<string>>>
) => {
const exactValueExists = options.some((el) => el.label === inputValue);
const valueIsNotEmpty = inputValue.trim().length;
return !Boolean(exactValueExists) && Boolean(valueIsNotEmpty);
};
Alerting: Suggest previously entered custom labels (#57783) * [Alerting] - replace label inputs with dropdowns (#57019) * Add AlertLabelDropdown component It will be used to pick from or create new labels * Adapt LabelsField component to use AlertLabelDropdown instead of inputs * Add tests for LabelsField component Plus a few other tests were adapted to work with the label dropdowns * Use ref in component * Fix showing placeholders in the label dropdowns * Minor syntax change * Remove unneeded import after rebase * Display custom labels When a label key is selected, its corresponding values are shown in the dropdown * Add tooltip explaining where labels in the dropdowns come from * Fix import of Stack component * Avoid duplicated values * Improvements based on review * Display labels for currently selected datasource only * Refactor AlertsField to allow to choose whether to suggest labels or not * Suggest labels for NotificationStep and tests * Don't suggest labels in TestContactPointModal * [LabelsField] - refactor: get dataSourceName as a parameter * [LabelsField] - extract common code into reusable components * Display loading spinner while fetching rules * LabelsField - refactor Removing the suggest prop and the default dataSource 'grafana'. Instead, the component now relies on the dataSourceName param. If it's set it means we want to show suggestions so we fetch the labels, otherwise, if not set, we show the plain input texts without suggestions. * Add test for LabelsField without suggestions * Show custom labels for grafana managed alerts When the dataSourceName in the NotificationsStep component has a null value, we can assume it's because we're dealing with grafana managed alerts. In that case we set the correct value. * Fix tests after latest changes Since we removed the combobox from the TestContactPoints modal, tests had to be adjusted * Update texts * initialize all new added inputs with empty data
3 years ago
const AlertLabelDropdown: FC<AlertLabelDropdownProps> = forwardRef<HTMLDivElement, AlertLabelDropdownProps>(
function LabelPicker({ onChange, options, defaultValue, type, onOpenMenu = () => {} }, ref) {
const styles = useStyles2(getStyles);
Alerting: Suggest previously entered custom labels (#57783) * [Alerting] - replace label inputs with dropdowns (#57019) * Add AlertLabelDropdown component It will be used to pick from or create new labels * Adapt LabelsField component to use AlertLabelDropdown instead of inputs * Add tests for LabelsField component Plus a few other tests were adapted to work with the label dropdowns * Use ref in component * Fix showing placeholders in the label dropdowns * Minor syntax change * Remove unneeded import after rebase * Display custom labels When a label key is selected, its corresponding values are shown in the dropdown * Add tooltip explaining where labels in the dropdowns come from * Fix import of Stack component * Avoid duplicated values * Improvements based on review * Display labels for currently selected datasource only * Refactor AlertsField to allow to choose whether to suggest labels or not * Suggest labels for NotificationStep and tests * Don't suggest labels in TestContactPointModal * [LabelsField] - refactor: get dataSourceName as a parameter * [LabelsField] - extract common code into reusable components * Display loading spinner while fetching rules * LabelsField - refactor Removing the suggest prop and the default dataSource 'grafana'. Instead, the component now relies on the dataSourceName param. If it's set it means we want to show suggestions so we fetch the labels, otherwise, if not set, we show the plain input texts without suggestions. * Add test for LabelsField without suggestions * Show custom labels for grafana managed alerts When the dataSourceName in the NotificationsStep component has a null value, we can assume it's because we're dealing with grafana managed alerts. In that case we set the correct value. * Fix tests after latest changes Since we removed the combobox from the TestContactPoints modal, tests had to be adjusted * Update texts * initialize all new added inputs with empty data
3 years ago
return (
<div ref={ref}>
<Field disabled={false} data-testid={`alertlabel-${type}-picker`} className={styles.resetMargin}>
<Select<string>
placeholder={t('alerting.alert-label-dropdown.placeholder-select', 'Choose {{type}}', { type })}
Alerting: Suggest previously entered custom labels (#57783) * [Alerting] - replace label inputs with dropdowns (#57019) * Add AlertLabelDropdown component It will be used to pick from or create new labels * Adapt LabelsField component to use AlertLabelDropdown instead of inputs * Add tests for LabelsField component Plus a few other tests were adapted to work with the label dropdowns * Use ref in component * Fix showing placeholders in the label dropdowns * Minor syntax change * Remove unneeded import after rebase * Display custom labels When a label key is selected, its corresponding values are shown in the dropdown * Add tooltip explaining where labels in the dropdowns come from * Fix import of Stack component * Avoid duplicated values * Improvements based on review * Display labels for currently selected datasource only * Refactor AlertsField to allow to choose whether to suggest labels or not * Suggest labels for NotificationStep and tests * Don't suggest labels in TestContactPointModal * [LabelsField] - refactor: get dataSourceName as a parameter * [LabelsField] - extract common code into reusable components * Display loading spinner while fetching rules * LabelsField - refactor Removing the suggest prop and the default dataSource 'grafana'. Instead, the component now relies on the dataSourceName param. If it's set it means we want to show suggestions so we fetch the labels, otherwise, if not set, we show the plain input texts without suggestions. * Add test for LabelsField without suggestions * Show custom labels for grafana managed alerts When the dataSourceName in the NotificationsStep component has a null value, we can assume it's because we're dealing with grafana managed alerts. In that case we set the correct value. * Fix tests after latest changes Since we removed the combobox from the TestContactPoints modal, tests had to be adjusted * Update texts * initialize all new added inputs with empty data
3 years ago
width={29}
className="ds-picker select-container"
backspaceRemovesValue={false}
onChange={onChange}
onOpenMenu={onOpenMenu}
filterOption={customFilter}
isValidNewOption={handleIsValidNewOption}
Alerting: Suggest previously entered custom labels (#57783) * [Alerting] - replace label inputs with dropdowns (#57019) * Add AlertLabelDropdown component It will be used to pick from or create new labels * Adapt LabelsField component to use AlertLabelDropdown instead of inputs * Add tests for LabelsField component Plus a few other tests were adapted to work with the label dropdowns * Use ref in component * Fix showing placeholders in the label dropdowns * Minor syntax change * Remove unneeded import after rebase * Display custom labels When a label key is selected, its corresponding values are shown in the dropdown * Add tooltip explaining where labels in the dropdowns come from * Fix import of Stack component * Avoid duplicated values * Improvements based on review * Display labels for currently selected datasource only * Refactor AlertsField to allow to choose whether to suggest labels or not * Suggest labels for NotificationStep and tests * Don't suggest labels in TestContactPointModal * [LabelsField] - refactor: get dataSourceName as a parameter * [LabelsField] - extract common code into reusable components * Display loading spinner while fetching rules * LabelsField - refactor Removing the suggest prop and the default dataSource 'grafana'. Instead, the component now relies on the dataSourceName param. If it's set it means we want to show suggestions so we fetch the labels, otherwise, if not set, we show the plain input texts without suggestions. * Add test for LabelsField without suggestions * Show custom labels for grafana managed alerts When the dataSourceName in the NotificationsStep component has a null value, we can assume it's because we're dealing with grafana managed alerts. In that case we set the correct value. * Fix tests after latest changes Since we removed the combobox from the TestContactPoints modal, tests had to be adjusted * Update texts * initialize all new added inputs with empty data
3 years ago
options={options}
maxMenuHeight={500}
noOptionsMessage={t('alerting.label-picker.no-options-message', 'No labels found')}
Alerting: Suggest previously entered custom labels (#57783) * [Alerting] - replace label inputs with dropdowns (#57019) * Add AlertLabelDropdown component It will be used to pick from or create new labels * Adapt LabelsField component to use AlertLabelDropdown instead of inputs * Add tests for LabelsField component Plus a few other tests were adapted to work with the label dropdowns * Use ref in component * Fix showing placeholders in the label dropdowns * Minor syntax change * Remove unneeded import after rebase * Display custom labels When a label key is selected, its corresponding values are shown in the dropdown * Add tooltip explaining where labels in the dropdowns come from * Fix import of Stack component * Avoid duplicated values * Improvements based on review * Display labels for currently selected datasource only * Refactor AlertsField to allow to choose whether to suggest labels or not * Suggest labels for NotificationStep and tests * Don't suggest labels in TestContactPointModal * [LabelsField] - refactor: get dataSourceName as a parameter * [LabelsField] - extract common code into reusable components * Display loading spinner while fetching rules * LabelsField - refactor Removing the suggest prop and the default dataSource 'grafana'. Instead, the component now relies on the dataSourceName param. If it's set it means we want to show suggestions so we fetch the labels, otherwise, if not set, we show the plain input texts without suggestions. * Add test for LabelsField without suggestions * Show custom labels for grafana managed alerts When the dataSourceName in the NotificationsStep component has a null value, we can assume it's because we're dealing with grafana managed alerts. In that case we set the correct value. * Fix tests after latest changes Since we removed the combobox from the TestContactPoints modal, tests had to be adjusted * Update texts * initialize all new added inputs with empty data
3 years ago
defaultValue={defaultValue}
allowCustomValue
/>
</Field>
</div>
);
}
);
const getStyles = () => ({
resetMargin: css({ marginBottom: 0 }),
});
Alerting: Suggest previously entered custom labels (#57783) * [Alerting] - replace label inputs with dropdowns (#57019) * Add AlertLabelDropdown component It will be used to pick from or create new labels * Adapt LabelsField component to use AlertLabelDropdown instead of inputs * Add tests for LabelsField component Plus a few other tests were adapted to work with the label dropdowns * Use ref in component * Fix showing placeholders in the label dropdowns * Minor syntax change * Remove unneeded import after rebase * Display custom labels When a label key is selected, its corresponding values are shown in the dropdown * Add tooltip explaining where labels in the dropdowns come from * Fix import of Stack component * Avoid duplicated values * Improvements based on review * Display labels for currently selected datasource only * Refactor AlertsField to allow to choose whether to suggest labels or not * Suggest labels for NotificationStep and tests * Don't suggest labels in TestContactPointModal * [LabelsField] - refactor: get dataSourceName as a parameter * [LabelsField] - extract common code into reusable components * Display loading spinner while fetching rules * LabelsField - refactor Removing the suggest prop and the default dataSource 'grafana'. Instead, the component now relies on the dataSourceName param. If it's set it means we want to show suggestions so we fetch the labels, otherwise, if not set, we show the plain input texts without suggestions. * Add test for LabelsField without suggestions * Show custom labels for grafana managed alerts When the dataSourceName in the NotificationsStep component has a null value, we can assume it's because we're dealing with grafana managed alerts. In that case we set the correct value. * Fix tests after latest changes Since we removed the combobox from the TestContactPoints modal, tests had to be adjusted * Update texts * initialize all new added inputs with empty data
3 years ago
export default AlertLabelDropdown;