tableActions.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. 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 size="sm" onClick={onChangeShowTags} icon={<IconTag size="xs" />}>
  105. {showTags ? t('Hide Tags') : t('Show Tags')}
  106. </Button>
  107. );
  108. }
  109. type FeatureWrapperProps = Props & {
  110. children: (hasFeature: boolean, props: Props) => React.ReactNode;
  111. };
  112. function FeatureWrapper(props: FeatureWrapperProps) {
  113. const noEditMessage = t('Requires discover query feature.');
  114. const editFeatures = ['organizations:discover-query'];
  115. const renderDisabled = p => (
  116. <Hovercard
  117. body={
  118. <FeatureDisabled
  119. features={p.features}
  120. hideHelpToggle
  121. message={noEditMessage}
  122. featureName={noEditMessage}
  123. />
  124. }
  125. >
  126. {p.children(p)}
  127. </Hovercard>
  128. );
  129. return (
  130. <Feature
  131. hookName="feature-disabled:grid-editable-actions"
  132. renderDisabled={renderDisabled}
  133. features={editFeatures}
  134. >
  135. {({hasFeature}) => props.children(hasFeature, props)}
  136. </Feature>
  137. );
  138. }
  139. function HeaderActions(props: Props) {
  140. return (
  141. <Fragment>
  142. <FeatureWrapper {...props} key="edit">
  143. {renderEditButton}
  144. </FeatureWrapper>
  145. <FeatureWrapper {...props} key="download">
  146. {renderDownloadButton}
  147. </FeatureWrapper>
  148. {renderSummaryButton(props)}
  149. </Fragment>
  150. );
  151. }
  152. export default HeaderActions;