index.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import styled from '@emotion/styled';
  2. import ButtonBar from 'sentry/components/buttonBar';
  3. import Field from 'sentry/components/forms/field';
  4. import {t} from 'sentry/locale';
  5. import space from 'sentry/styles/space';
  6. import {TagCollection} from 'sentry/types';
  7. import {QueryFieldValue} from 'sentry/utils/discover/fields';
  8. import useCustomMeasurements from 'sentry/utils/useCustomMeasurements';
  9. import useOrganization from 'sentry/utils/useOrganization';
  10. import {getDatasetConfig} from 'sentry/views/dashboardsV2/datasetConfig/base';
  11. import {DisplayType, Widget} from 'sentry/views/dashboardsV2/types';
  12. import {QueryField} from 'sentry/views/eventsV2/table/queryField';
  13. import {FieldValueKind} from 'sentry/views/eventsV2/table/types';
  14. import {AddButton} from './addButton';
  15. import {DeleteButton} from './deleteButton';
  16. interface Props {
  17. aggregates: QueryFieldValue[];
  18. displayType: DisplayType;
  19. /**
  20. * Fired when aggregates are added/removed/modified/reordered.
  21. */
  22. onChange: (aggregates: QueryFieldValue[]) => void;
  23. tags: TagCollection;
  24. widgetType: Widget['widgetType'];
  25. errors?: Record<string, any>;
  26. noFieldsMessage?: string;
  27. }
  28. export function YAxisSelector({
  29. displayType,
  30. widgetType,
  31. aggregates,
  32. tags,
  33. onChange,
  34. errors,
  35. noFieldsMessage,
  36. }: Props) {
  37. const organization = useOrganization();
  38. const datasetConfig = getDatasetConfig(widgetType);
  39. const {customMeasurements} = useCustomMeasurements();
  40. function handleAddOverlay(event: React.MouseEvent) {
  41. event.preventDefault();
  42. const newAggregates = [
  43. ...aggregates,
  44. {kind: FieldValueKind.FIELD, field: ''} as QueryFieldValue,
  45. ];
  46. onChange(newAggregates);
  47. }
  48. function handleAddEquation(event: React.MouseEvent) {
  49. event.preventDefault();
  50. const newAggregates = [
  51. ...aggregates,
  52. {kind: FieldValueKind.EQUATION, field: ''} as QueryFieldValue,
  53. ];
  54. onChange(newAggregates);
  55. }
  56. function handleRemoveQueryField(event: React.MouseEvent, fieldIndex: number) {
  57. event.preventDefault();
  58. const newAggregates = [...aggregates];
  59. newAggregates.splice(fieldIndex, 1);
  60. onChange(newAggregates);
  61. }
  62. function handleChangeQueryField(value: QueryFieldValue, fieldIndex: number) {
  63. const newAggregates = [...aggregates];
  64. newAggregates[fieldIndex] = value;
  65. onChange(newAggregates);
  66. }
  67. const fieldError = errors?.find(error => error?.aggregates)?.aggregates;
  68. const canDelete = aggregates.length > 1;
  69. const hideAddYAxisButtons =
  70. ([DisplayType.WORLD_MAP, DisplayType.BIG_NUMBER].includes(displayType) &&
  71. aggregates.length === 1) ||
  72. ([DisplayType.LINE, DisplayType.AREA, DisplayType.BAR].includes(displayType) &&
  73. aggregates.length === 3);
  74. return (
  75. <Field inline={false} flexibleControlStateSize error={fieldError} stacked>
  76. {aggregates.map((fieldValue, i) => (
  77. <QueryFieldWrapper key={`${fieldValue}:${i}`}>
  78. <QueryField
  79. fieldValue={fieldValue}
  80. fieldOptions={datasetConfig.getTableFieldOptions(
  81. organization,
  82. tags,
  83. customMeasurements
  84. )}
  85. onChange={value => handleChangeQueryField(value, i)}
  86. filterPrimaryOptions={datasetConfig.filterYAxisOptions?.(displayType)}
  87. filterAggregateParameters={datasetConfig.filterYAxisAggregateParams?.(
  88. fieldValue,
  89. displayType
  90. )}
  91. otherColumns={aggregates}
  92. noFieldsMessage={noFieldsMessage}
  93. />
  94. {aggregates.length > 1 &&
  95. (canDelete || fieldValue.kind === FieldValueKind.EQUATION) && (
  96. <DeleteButton onDelete={event => handleRemoveQueryField(event, i)} />
  97. )}
  98. </QueryFieldWrapper>
  99. ))}
  100. {!hideAddYAxisButtons && (
  101. <Actions gap={1}>
  102. <AddButton title={t('Add Overlay')} onAdd={handleAddOverlay} />
  103. {datasetConfig.enableEquations && (
  104. <AddButton title={t('Add an Equation')} onAdd={handleAddEquation} />
  105. )}
  106. </Actions>
  107. )}
  108. </Field>
  109. );
  110. }
  111. const QueryFieldWrapper = styled('div')`
  112. display: flex;
  113. align-items: center;
  114. justify-content: space-between;
  115. :not(:last-child) {
  116. margin-bottom: ${space(1)};
  117. }
  118. > * + * {
  119. margin-left: ${space(1)};
  120. }
  121. `;
  122. const Actions = styled(ButtonBar)`
  123. justify-content: flex-start;
  124. `;