columnFields.tsx 2.9 KB

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