columnFields.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import styled from '@emotion/styled';
  2. import FieldGroup from 'sentry/components/forms/fieldGroup';
  3. import {space} from 'sentry/styles/space';
  4. import type {Organization} from 'sentry/types';
  5. import type {QueryFieldValue} from 'sentry/utils/discover/fields';
  6. import type {WidgetType} from 'sentry/views/dashboards/types';
  7. import {DisplayType} from 'sentry/views/dashboards/types';
  8. import ColumnEditCollection from 'sentry/views/discover/table/columnEditCollection';
  9. import type {FieldValueOption} from 'sentry/views/discover/table/queryField';
  10. import type {generateFieldOptions} from 'sentry/views/discover/utils';
  11. interface Props {
  12. displayType: DisplayType;
  13. fieldOptions: ReturnType<typeof generateFieldOptions>;
  14. fields: QueryFieldValue[];
  15. onChange: (newColumns: QueryFieldValue[]) => void;
  16. organization: Organization;
  17. widgetType: WidgetType;
  18. errors?: Record<string, string>[];
  19. filterAggregateParameters?: (option: FieldValueOption) => boolean;
  20. filterPrimaryOptions?: (option: FieldValueOption) => boolean;
  21. noFieldsMessage?: string;
  22. }
  23. export function ColumnFields({
  24. displayType,
  25. fieldOptions,
  26. widgetType,
  27. fields,
  28. organization,
  29. errors,
  30. onChange,
  31. filterAggregateParameters,
  32. filterPrimaryOptions,
  33. noFieldsMessage,
  34. }: Props) {
  35. return (
  36. <FieldGroup
  37. inline={false}
  38. error={errors?.find(error => error?.fields)?.fields}
  39. flexibleControlStateSize
  40. stacked
  41. >
  42. {displayType === DisplayType.TABLE ? (
  43. <ColumnCollectionEdit
  44. columns={fields}
  45. onChange={onChange}
  46. fieldOptions={fieldOptions}
  47. organization={organization}
  48. source={widgetType}
  49. showAliasField
  50. filterAggregateParameters={filterAggregateParameters}
  51. filterPrimaryOptions={filterPrimaryOptions}
  52. noFieldsMessage={noFieldsMessage}
  53. />
  54. ) : (
  55. // The only other display type this component
  56. // renders for is TOP_N, where the n - 1 fields
  57. // are columns and the nth field is the y-axis
  58. <ColumnCollectionEdit
  59. columns={fields.slice(0, fields.length - 1)}
  60. onChange={newColumns => {
  61. onChange([...newColumns, fields[fields.length - 1]]);
  62. }}
  63. fieldOptions={fieldOptions}
  64. organization={organization}
  65. source={widgetType}
  66. filterPrimaryOptions={filterPrimaryOptions}
  67. noFieldsMessage={noFieldsMessage}
  68. />
  69. )}
  70. </FieldGroup>
  71. );
  72. }
  73. const ColumnCollectionEdit = styled(ColumnEditCollection)`
  74. margin-top: ${space(1)};
  75. `;