tableActions.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import {Fragment} from 'react';
  2. import type {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, IconSliders, IconTag} from 'sentry/icons';
  11. import {t} from 'sentry/locale';
  12. import type {OrganizationSummary} from 'sentry/types';
  13. import {trackAnalytics} from 'sentry/utils/analytics';
  14. import {parseCursor} from 'sentry/utils/cursor';
  15. import type {TableData} from 'sentry/utils/discover/discoverQuery';
  16. import type EventView from 'sentry/utils/discover/eventView';
  17. import {useLocation} from 'sentry/utils/useLocation';
  18. import {downloadAsCsv} from '../utils';
  19. type Props = {
  20. error: string | null;
  21. eventView: EventView;
  22. isLoading: boolean;
  23. location: Location;
  24. onChangeShowTags: () => void;
  25. onEdit: () => void;
  26. organization: OrganizationSummary;
  27. showTags: boolean;
  28. tableData: TableData | null | undefined;
  29. title: string;
  30. supportsInvestigationRule?: boolean;
  31. };
  32. function handleDownloadAsCsv(title: string, {organization, eventView, tableData}: Props) {
  33. trackAnalytics('discover_v2.results.download_csv', {
  34. organization: organization.id, // org summary
  35. });
  36. downloadAsCsv(tableData, eventView.getColumns(), title);
  37. }
  38. function renderDownloadButton(canEdit: boolean, props: Props) {
  39. const {tableData} = props;
  40. return (
  41. <Feature
  42. features="organizations:discover-query"
  43. renderDisabled={() => renderBrowserExportButton(canEdit, props)}
  44. >
  45. {tableData?.data && tableData.data.length < 50
  46. ? renderBrowserExportButton(canEdit, props)
  47. : renderAsyncExportButton(canEdit, props)}
  48. </Feature>
  49. );
  50. }
  51. function renderBrowserExportButton(canEdit: boolean, props: Props) {
  52. const {isLoading, error} = props;
  53. const disabled = isLoading || error !== null || canEdit === false;
  54. const onClick = disabled ? undefined : () => handleDownloadAsCsv(props.title, props);
  55. return (
  56. <Button
  57. size="sm"
  58. disabled={disabled}
  59. onClick={onClick}
  60. data-test-id="grid-download-csv"
  61. icon={<IconDownload />}
  62. title={
  63. !disabled
  64. ? t(
  65. "There aren't that many results, start your export and it'll download immediately."
  66. )
  67. : undefined
  68. }
  69. >
  70. {t('Export All')}
  71. </Button>
  72. );
  73. }
  74. function renderAsyncExportButton(canEdit: boolean, props: Props) {
  75. const {isLoading, error, location, eventView} = props;
  76. const disabled = isLoading || error !== null || canEdit === false;
  77. return (
  78. <DataExport
  79. payload={{
  80. queryType: ExportQueryType.DISCOVER,
  81. queryInfo: eventView.getEventsAPIPayload(location),
  82. }}
  83. disabled={disabled}
  84. icon={<IconDownload />}
  85. >
  86. {t('Export All')}
  87. </DataExport>
  88. );
  89. }
  90. // Placate eslint proptype checking
  91. function renderEditButton(canEdit: boolean, props: Props) {
  92. const onClick = canEdit ? props.onEdit : undefined;
  93. return (
  94. <GuideAnchor target="columns_header_button">
  95. <Button
  96. size="sm"
  97. disabled={!canEdit}
  98. onClick={onClick}
  99. data-test-id="grid-edit-enable"
  100. icon={<IconSliders />}
  101. >
  102. {t('Columns')}
  103. </Button>
  104. </GuideAnchor>
  105. );
  106. }
  107. // Placate eslint proptype checking
  108. function renderSummaryButton({onChangeShowTags, showTags}: Props) {
  109. return (
  110. <Button size="sm" onClick={onChangeShowTags} icon={<IconTag />}>
  111. {showTags ? t('Hide Tags') : t('Show Tags')}
  112. </Button>
  113. );
  114. }
  115. type FeatureWrapperProps = Props & {
  116. children: (hasFeature: boolean, props: Props) => React.ReactNode;
  117. };
  118. function FeatureWrapper(props: FeatureWrapperProps) {
  119. const noEditMessage = t('Requires discover query feature.');
  120. const editFeatures = ['organizations:discover-query'];
  121. const renderDisabled = p => (
  122. <Hovercard
  123. body={
  124. <FeatureDisabled
  125. features={p.features}
  126. hideHelpToggle
  127. message={noEditMessage}
  128. featureName={noEditMessage}
  129. />
  130. }
  131. >
  132. {p.children(p)}
  133. </Hovercard>
  134. );
  135. return (
  136. <Feature
  137. hookName="feature-disabled:grid-editable-actions"
  138. renderDisabled={renderDisabled}
  139. features={editFeatures}
  140. >
  141. {({hasFeature}) => props.children(hasFeature, props)}
  142. </Feature>
  143. );
  144. }
  145. function TableActions(props: Props) {
  146. const location = useLocation();
  147. const cursor = location?.query?.cursor;
  148. const cursorOffset = parseCursor(cursor)?.offset ?? 0;
  149. const numSamples = props.tableData?.data?.length ?? null;
  150. const totalNumSamples = numSamples === null ? null : numSamples + cursorOffset;
  151. return (
  152. <Fragment>
  153. {props.supportsInvestigationRule && (
  154. <InvestigationRuleCreation
  155. {...props}
  156. buttonProps={{size: 'sm'}}
  157. numSamples={totalNumSamples}
  158. key="investigationRuleCreation"
  159. />
  160. )}
  161. <FeatureWrapper {...props} key="edit">
  162. {renderEditButton}
  163. </FeatureWrapper>
  164. <FeatureWrapper {...props} key="download">
  165. {renderDownloadButton}
  166. </FeatureWrapper>
  167. {renderSummaryButton(props)}
  168. </Fragment>
  169. );
  170. }
  171. export default TableActions;