GroupBy.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { uniq } from 'lodash';
  2. import React from 'react';
  3. import { SelectableValue } from '@grafana/data';
  4. import { Icon, Label, MultiSelect } from '@grafana/ui';
  5. import { AlertmanagerGroup } from 'app/plugins/datasource/alertmanager/types';
  6. interface Props {
  7. className?: string;
  8. groups: AlertmanagerGroup[];
  9. groupBy: string[];
  10. onGroupingChange: (keys: string[]) => void;
  11. }
  12. export const GroupBy = ({ className, groups, groupBy, onGroupingChange }: Props) => {
  13. const labelKeyOptions = uniq(groups.flatMap((group) => group.alerts).flatMap(({ labels }) => Object.keys(labels)))
  14. .filter((label) => !(label.startsWith('__') && label.endsWith('__'))) // Filter out private labels
  15. .map<SelectableValue>((key) => ({
  16. label: key,
  17. value: key,
  18. }));
  19. return (
  20. <div data-testid={'group-by-container'} className={className}>
  21. <Label>Custom group by</Label>
  22. <MultiSelect
  23. aria-label={'group by label keys'}
  24. value={groupBy}
  25. placeholder="Group by"
  26. prefix={<Icon name={'tag-alt'} />}
  27. onChange={(items) => {
  28. onGroupingChange(items.map(({ value }) => value as string));
  29. }}
  30. options={labelKeyOptions}
  31. />
  32. </div>
  33. );
  34. };