tableActions.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import {Fragment} from 'react';
  2. import {Location} from 'history';
  3. import Feature from 'sentry/components/acl/feature';
  4. import FeatureDisabled from 'sentry/components/acl/featureDisabled';
  5. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  6. import Button from 'sentry/components/button';
  7. import DataExport, {ExportQueryType} from 'sentry/components/dataExport';
  8. import {Hovercard} from 'sentry/components/hovercard';
  9. import {IconDownload, IconStack, IconTag} from 'sentry/icons';
  10. import {t} from 'sentry/locale';
  11. import {OrganizationSummary} from 'sentry/types';
  12. import {trackAnalyticsEvent} from 'sentry/utils/analytics';
  13. import {TableData} from 'sentry/utils/discover/discoverQuery';
  14. import EventView from 'sentry/utils/discover/eventView';
  15. import {downloadAsCsv} from '../utils';
  16. type Props = {
  17. error: string | null;
  18. eventView: EventView;
  19. isLoading: boolean;
  20. location: Location;
  21. onChangeShowTags: () => void;
  22. onEdit: () => void;
  23. organization: OrganizationSummary;
  24. showTags: boolean;
  25. tableData: TableData | null | undefined;
  26. title: string;
  27. };
  28. function handleDownloadAsCsv(title: string, {organization, eventView, tableData}: Props) {
  29. trackAnalyticsEvent({
  30. eventKey: 'discover_v2.results.download_csv',
  31. eventName: 'Discoverv2: Download CSV',
  32. organization_id: parseInt(organization.id, 10),
  33. });
  34. downloadAsCsv(tableData, eventView.getColumns(), title);
  35. }
  36. function renderDownloadButton(canEdit: boolean, props: Props) {
  37. return (
  38. <Feature
  39. features={['organizations:discover-query']}
  40. renderDisabled={() => renderBrowserExportButton(canEdit, props)}
  41. >
  42. {renderAsyncExportButton(canEdit, props)}
  43. </Feature>
  44. );
  45. }
  46. function renderBrowserExportButton(canEdit: boolean, props: Props) {
  47. const {isLoading, error} = props;
  48. const disabled = isLoading || error !== null || canEdit === false;
  49. const onClick = disabled ? undefined : () => handleDownloadAsCsv(props.title, props);
  50. return (
  51. <Button
  52. size="sm"
  53. disabled={disabled}
  54. onClick={onClick}
  55. data-test-id="grid-download-csv"
  56. icon={<IconDownload size="xs" />}
  57. >
  58. {t('Export')}
  59. </Button>
  60. );
  61. }
  62. function renderAsyncExportButton(canEdit: boolean, props: Props) {
  63. const {isLoading, error, location, eventView} = props;
  64. const disabled = isLoading || error !== null || canEdit === false;
  65. return (
  66. <DataExport
  67. payload={{
  68. queryType: ExportQueryType.Discover,
  69. queryInfo: eventView.getEventsAPIPayload(location),
  70. }}
  71. disabled={disabled}
  72. icon={<IconDownload size="xs" />}
  73. >
  74. {t('Export All')}
  75. </DataExport>
  76. );
  77. }
  78. // Placate eslint proptype checking
  79. function renderEditButton(canEdit: boolean, props: Props) {
  80. const onClick = canEdit ? props.onEdit : undefined;
  81. return (
  82. <GuideAnchor target="columns_header_button">
  83. <Button
  84. size="sm"
  85. disabled={!canEdit}
  86. onClick={onClick}
  87. data-test-id="grid-edit-enable"
  88. icon={<IconStack size="xs" />}
  89. >
  90. {t('Columns')}
  91. </Button>
  92. </GuideAnchor>
  93. );
  94. }
  95. // Placate eslint proptype checking
  96. function renderSummaryButton({onChangeShowTags, showTags}: Props) {
  97. return (
  98. <Button
  99. data-test-id="toggle-show-tags"
  100. size="sm"
  101. onClick={onChangeShowTags}
  102. icon={<IconTag size="xs" />}
  103. >
  104. {showTags ? t('Hide Tags') : t('Show Tags')}
  105. </Button>
  106. );
  107. }
  108. type FeatureWrapperProps = Props & {
  109. children: (hasFeature: boolean, props: Props) => React.ReactNode;
  110. };
  111. function FeatureWrapper(props: FeatureWrapperProps) {
  112. const noEditMessage = t('Requires discover query feature.');
  113. const editFeatures = ['organizations:discover-query'];
  114. const renderDisabled = p => (
  115. <Hovercard
  116. body={
  117. <FeatureDisabled
  118. features={p.features}
  119. hideHelpToggle
  120. message={noEditMessage}
  121. featureName={noEditMessage}
  122. />
  123. }
  124. >
  125. {p.children(p)}
  126. </Hovercard>
  127. );
  128. return (
  129. <Feature
  130. hookName="feature-disabled:grid-editable-actions"
  131. renderDisabled={renderDisabled}
  132. features={editFeatures}
  133. >
  134. {({hasFeature}) => props.children(hasFeature, props)}
  135. </Feature>
  136. );
  137. }
  138. function HeaderActions(props: Props) {
  139. return (
  140. <Fragment>
  141. <FeatureWrapper {...props} key="edit">
  142. {renderEditButton}
  143. </FeatureWrapper>
  144. <FeatureWrapper {...props} key="download">
  145. {renderDownloadButton}
  146. </FeatureWrapper>
  147. {renderSummaryButton(props)}
  148. </Fragment>
  149. );
  150. }
  151. export default HeaderActions;