columnFields.tsx 2.4 KB

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