tableActions.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 {InvestigationRuleCreation} from 'sentry/components/dynamicSampling/investigationRule';
  9. import {Hovercard} from 'sentry/components/hovercard';
  10. import {IconDownload, IconStack, IconTag} from 'sentry/icons';
  11. import {t} from 'sentry/locale';
  12. import {OrganizationSummary} from 'sentry/types';
  13. import {trackAnalytics} from 'sentry/utils/analytics';
  14. import {TableData} from 'sentry/utils/discover/discoverQuery';
  15. import EventView from 'sentry/utils/discover/eventView';
  16. import {downloadAsCsv} from '../utils';
  17. type Props = {
  18. error: string | null;
  19. eventView: EventView;
  20. isLoading: boolean;
  21. location: Location;
  22. onChangeShowTags: () => void;
  23. onEdit: () => void;
  24. organization: OrganizationSummary;
  25. showTags: boolean;
  26. tableData: TableData | null | undefined;
  27. title: string;
  28. };
  29. function handleDownloadAsCsv(title: string, {organization, eventView, tableData}: Props) {
  30. trackAnalytics('discover_v2.results.download_csv', {
  31. organization: organization.id, // org summary
  32. });
  33. downloadAsCsv(tableData, eventView.getColumns(), title);
  34. }
  35. function renderDownloadButton(canEdit: boolean, props: Props) {
  36. const {tableData} = props;
  37. return (
  38. <Feature
  39. features={['organizations:discover-query']}
  40. renderDisabled={() => renderBrowserExportButton(canEdit, props)}
  41. >
  42. {tableData?.data && tableData.data.length < 50
  43. ? renderBrowserExportButton(canEdit, props)
  44. : renderAsyncExportButton(canEdit, props)}
  45. </Feature>
  46. );
  47. }
  48. function renderBrowserExportButton(canEdit: boolean, props: Props) {
  49. const {isLoading, error} = props;
  50. const disabled = isLoading || error !== null || canEdit === false;
  51. const onClick = disabled ? undefined : () => handleDownloadAsCsv(props.title, props);
  52. return (
  53. <Button
  54. size="sm"
  55. disabled={disabled}
  56. onClick={onClick}
  57. data-test-id="grid-download-csv"
  58. icon={<IconDownload size="xs" />}
  59. title={t(
  60. "There aren't that many results, start your export and it'll download immediately."
  61. )}
  62. >
  63. {t('Export All')}
  64. </Button>
  65. );
  66. }
  67. function renderAsyncExportButton(canEdit: boolean, props: Props) {
  68. const {isLoading, error, location, eventView} = props;
  69. const disabled = isLoading || error !== null || canEdit === false;
  70. return (
  71. <DataExport
  72. payload={{
  73. queryType: ExportQueryType.DISCOVER,
  74. queryInfo: eventView.getEventsAPIPayload(location),
  75. }}
  76. disabled={disabled}
  77. icon={<IconDownload size="xs" />}
  78. >
  79. {t('Export All')}
  80. </DataExport>
  81. );
  82. }
  83. // Placate eslint proptype checking
  84. function renderEditButton(canEdit: boolean, props: Props) {
  85. const onClick = canEdit ? props.onEdit : undefined;
  86. return (
  87. <GuideAnchor target="columns_header_button">
  88. <Button
  89. size="sm"
  90. disabled={!canEdit}
  91. onClick={onClick}
  92. data-test-id="grid-edit-enable"
  93. icon={<IconStack size="xs" />}
  94. >
  95. {t('Columns')}
  96. </Button>
  97. </GuideAnchor>
  98. );
  99. }
  100. // Placate eslint proptype checking
  101. function renderSummaryButton({onChangeShowTags, showTags}: Props) {
  102. return (
  103. <Button size="sm" onClick={onChangeShowTags} icon={<IconTag size="xs" />}>
  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 TableActions(props: Props) {
  139. return (
  140. <Fragment>
  141. <InvestigationRuleCreation
  142. {...props}
  143. buttonProps={{size: 'sm'}}
  144. numSamples={props.tableData?.data?.length}
  145. key="investigationRuleCreation"
  146. />
  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 TableActions;