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 {trackAnalyticsEvent} from 'sentry/utils/analytics';
  13. import {CustomMeasurementCollection} from 'sentry/utils/customMeasurements/customMeasurements';
  14. import {Column} from 'sentry/utils/discover/fields';
  15. import theme from 'sentry/utils/theme';
  16. import useTags from 'sentry/utils/useTags';
  17. import {generateFieldOptions} from 'sentry/views/eventsV2/utils';
  18. import ColumnEditCollection from './columnEditCollection';
  19. type Props = {
  20. columns: Column[];
  21. measurementKeys: null | string[];
  22. // Fired when column selections have been applied.
  23. onApply: (columns: Column[]) => void;
  24. organization: Organization;
  25. customMeasurements?: CustomMeasurementCollection;
  26. spanOperationBreakdownKeys?: string[];
  27. } & ModalRenderProps;
  28. function ColumnEditModal(props: Props) {
  29. const {
  30. Header,
  31. Body,
  32. Footer,
  33. measurementKeys,
  34. spanOperationBreakdownKeys,
  35. organization,
  36. onApply,
  37. closeModal,
  38. customMeasurements,
  39. } = props;
  40. // Only run once for each organization.id.
  41. useEffect(() => {
  42. trackAnalyticsEvent({
  43. eventKey: 'discover_v2.column_editor.open',
  44. eventName: 'Discoverv2: Open column editor',
  45. organization_id: parseInt(organization.id, 10),
  46. });
  47. }, [organization.id]);
  48. const {tags} = useTags();
  49. const tagKeys = Object.values(tags).map(({key}) => key);
  50. const [columns, setColumns] = useState<Column[]>(props.columns);
  51. function handleApply() {
  52. onApply(columns);
  53. closeModal();
  54. }
  55. const fieldOptions = generateFieldOptions({
  56. organization,
  57. tagKeys,
  58. measurementKeys,
  59. spanOperationBreakdownKeys,
  60. customMeasurements:
  61. organization.features.includes('dashboards-mep') ||
  62. organization.features.includes('mep-rollout-flag')
  63. ? Object.values(customMeasurements ?? {}).map(({key, functions}) => ({
  64. key,
  65. functions,
  66. }))
  67. : undefined,
  68. });
  69. return (
  70. <Fragment>
  71. <Header closeButton>
  72. <h4>{t('Edit Columns')}</h4>
  73. </Header>
  74. <Body>
  75. <Instruction>
  76. {tct(
  77. '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).',
  78. {
  79. functionLink: (
  80. <ExternalLink href="https://docs.sentry.io/product/discover-queries/query-builder/#filter-by-table-columns" />
  81. ),
  82. fieldTagLink: (
  83. <ExternalLink href="https://docs.sentry.io/product/sentry-basics/search/searchable-properties/#event-properties" />
  84. ),
  85. }
  86. )}
  87. </Instruction>
  88. <ColumnEditCollection
  89. columns={columns}
  90. fieldOptions={fieldOptions}
  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};