columnEditModal.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import {Fragment, useEffect, useState} from 'react';
  2. import {css} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import {ModalRenderProps} from 'sentry/actionCreators/modal';
  5. import {Button} from 'sentry/components/button';
  6. import ButtonBar from 'sentry/components/buttonBar';
  7. import ExternalLink from 'sentry/components/links/externalLink';
  8. import {DISCOVER2_DOCS_URL} from 'sentry/constants';
  9. import {t, tct} from 'sentry/locale';
  10. import {space} from 'sentry/styles/space';
  11. import {Organization} from 'sentry/types';
  12. import {trackAnalytics} from 'sentry/utils/analytics';
  13. import {CustomMeasurementCollection} from 'sentry/utils/customMeasurements/customMeasurements';
  14. import {Column} from 'sentry/utils/discover/fields';
  15. import {FieldKey} from 'sentry/utils/fields';
  16. import theme from 'sentry/utils/theme';
  17. import useTags from 'sentry/utils/useTags';
  18. import {generateFieldOptions} from 'sentry/views/discover/utils';
  19. import ColumnEditCollection from './columnEditCollection';
  20. type Props = {
  21. columns: Column[];
  22. measurementKeys: null | string[];
  23. // Fired when column selections have been applied.
  24. onApply: (columns: Column[]) => void;
  25. organization: Organization;
  26. customMeasurements?: CustomMeasurementCollection;
  27. spanOperationBreakdownKeys?: string[];
  28. } & ModalRenderProps;
  29. function ColumnEditModal(props: Props) {
  30. const {
  31. Header,
  32. Body,
  33. Footer,
  34. measurementKeys,
  35. spanOperationBreakdownKeys,
  36. organization,
  37. onApply,
  38. closeModal,
  39. customMeasurements,
  40. } = props;
  41. // Only run once for each organization.id.
  42. useEffect(() => {
  43. trackAnalytics('discover_v2.column_editor.open', {organization});
  44. }, [organization]);
  45. const tags = useTags();
  46. const tagKeys = Object.keys(tags);
  47. const [columns, setColumns] = useState<Column[]>(props.columns);
  48. function handleApply() {
  49. onApply(columns);
  50. closeModal();
  51. }
  52. const fieldOptions = generateFieldOptions({
  53. organization,
  54. tagKeys,
  55. measurementKeys,
  56. spanOperationBreakdownKeys,
  57. customMeasurements:
  58. organization.features.includes('dashboards-mep') ||
  59. organization.features.includes('mep-rollout-flag')
  60. ? Object.values(customMeasurements ?? {}).map(({key, functions}) => ({
  61. key,
  62. functions,
  63. }))
  64. : undefined,
  65. });
  66. return (
  67. <Fragment>
  68. <Header closeButton>
  69. <h4>{t('Edit Columns')}</h4>
  70. </Header>
  71. <Body>
  72. <Instruction>
  73. {tct(
  74. 'To group events, add [functionLink: functions] f(x) that may take in additional parameters. [fieldTagLink: Tag and field] columns will help you view more details about the events (i.e. title).',
  75. {
  76. functionLink: (
  77. <ExternalLink href="https://docs.sentry.io/product/discover-queries/query-builder/#filter-by-table-columns" />
  78. ),
  79. fieldTagLink: (
  80. <ExternalLink href="https://docs.sentry.io/product/sentry-basics/search/searchable-properties/#event-properties" />
  81. ),
  82. }
  83. )}
  84. </Instruction>
  85. <ColumnEditCollection
  86. columns={columns}
  87. fieldOptions={fieldOptions}
  88. filterAggregateParameters={option =>
  89. option.value.meta.name !== FieldKey.TOTAL_COUNT
  90. }
  91. onChange={setColumns}
  92. organization={organization}
  93. />
  94. </Body>
  95. <Footer>
  96. <ButtonBar gap={1}>
  97. <Button priority="default" href={DISCOVER2_DOCS_URL} external>
  98. {t('Read the Docs')}
  99. </Button>
  100. <Button aria-label={t('Apply')} priority="primary" onClick={handleApply}>
  101. {t('Apply')}
  102. </Button>
  103. </ButtonBar>
  104. </Footer>
  105. </Fragment>
  106. );
  107. }
  108. const Instruction = styled('div')`
  109. margin-bottom: ${space(4)};
  110. `;
  111. const modalCss = css`
  112. @media (min-width: ${theme.breakpoints.medium}) {
  113. width: auto;
  114. max-width: 900px;
  115. }
  116. `;
  117. export default ColumnEditModal;
  118. export {modalCss};