tableActions.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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(true), title);
  35. }
  36. function renderDownloadButton(canEdit: boolean, props: Props) {
  37. const {tableData} = props;
  38. return (
  39. <Feature
  40. features={['organizations:discover-query']}
  41. renderDisabled={() => renderBrowserExportButton(canEdit, props)}
  42. >
  43. {tableData?.data && tableData.data.length < 50
  44. ? renderBrowserExportButton(canEdit, props)
  45. : renderAsyncExportButton(canEdit, props)}
  46. </Feature>
  47. );
  48. }
  49. function renderBrowserExportButton(canEdit: boolean, props: Props) {
  50. const {isLoading, error} = props;
  51. const disabled = isLoading || error !== null || canEdit === false;
  52. const onClick = disabled ? undefined : () => handleDownloadAsCsv(props.title, props);
  53. return (
  54. <Button
  55. size="sm"
  56. disabled={disabled}
  57. onClick={onClick}
  58. data-test-id="grid-download-csv"
  59. icon={<IconDownload size="xs" />}
  60. title={t(
  61. "There aren't that many results, start your export and it'll download immediately."
  62. )}
  63. >
  64. {t('Export All')}
  65. </Button>
  66. );
  67. }
  68. function renderAsyncExportButton(canEdit: boolean, props: Props) {
  69. const {isLoading, error, location, eventView} = props;
  70. const disabled = isLoading || error !== null || canEdit === false;
  71. return (
  72. <DataExport
  73. payload={{
  74. queryType: ExportQueryType.Discover,
  75. queryInfo: eventView.getEventsAPIPayload(location),
  76. }}
  77. disabled={disabled}
  78. icon={<IconDownload size="xs" />}
  79. >
  80. {t('Export All')}
  81. </DataExport>
  82. );
  83. }
  84. // Placate eslint proptype checking
  85. function renderEditButton(canEdit: boolean, props: Props) {
  86. const onClick = canEdit ? props.onEdit : undefined;
  87. return (
  88. <GuideAnchor target="columns_header_button">
  89. <Button
  90. size="sm"
  91. disabled={!canEdit}
  92. onClick={onClick}
  93. data-test-id="grid-edit-enable"
  94. icon={<IconStack size="xs" />}
  95. >
  96. {t('Columns')}
  97. </Button>
  98. </GuideAnchor>
  99. );
  100. }
  101. // Placate eslint proptype checking
  102. function renderSummaryButton({onChangeShowTags, showTags}: Props) {
  103. return (
  104. <Button
  105. data-test-id="toggle-show-tags"
  106. size="sm"
  107. onClick={onChangeShowTags}
  108. icon={<IconTag size="xs" />}
  109. >
  110. {showTags ? t('Hide Tags') : t('Show Tags')}
  111. </Button>
  112. );
  113. }
  114. type FeatureWrapperProps = Props & {
  115. children: (hasFeature: boolean, props: Props) => React.ReactNode;
  116. };
  117. function FeatureWrapper(props: FeatureWrapperProps) {
  118. const noEditMessage = t('Requires discover query feature.');
  119. const editFeatures = ['organizations:discover-query'];
  120. const renderDisabled = p => (
  121. <Hovercard
  122. body={
  123. <FeatureDisabled
  124. features={p.features}
  125. hideHelpToggle
  126. message={noEditMessage}
  127. featureName={noEditMessage}
  128. />
  129. }
  130. >
  131. {p.children(p)}
  132. </Hovercard>
  133. );
  134. return (
  135. <Feature
  136. hookName="feature-disabled:grid-editable-actions"
  137. renderDisabled={renderDisabled}
  138. features={editFeatures}
  139. >
  140. {({hasFeature}) => props.children(hasFeature, props)}
  141. </Feature>
  142. );
  143. }
  144. function HeaderActions(props: Props) {
  145. return (
  146. <Fragment>
  147. <FeatureWrapper {...props} key="edit">
  148. {renderEditButton}
  149. </FeatureWrapper>
  150. <FeatureWrapper {...props} key="download">
  151. {renderDownloadButton}
  152. </FeatureWrapper>
  153. {renderSummaryButton(props)}
  154. </Fragment>
  155. );
  156. }
  157. export default HeaderActions;