tableActions.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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={t(
  63. "There aren't that many results, start your export and it'll download immediately."
  64. )}
  65. >
  66. {t('Export All')}
  67. </Button>
  68. );
  69. }
  70. function renderAsyncExportButton(canEdit: boolean, props: Props) {
  71. const {isLoading, error, location, eventView} = props;
  72. const disabled = isLoading || error !== null || canEdit === false;
  73. return (
  74. <DataExport
  75. payload={{
  76. queryType: ExportQueryType.DISCOVER,
  77. queryInfo: eventView.getEventsAPIPayload(location),
  78. }}
  79. disabled={disabled}
  80. icon={<IconDownload />}
  81. >
  82. {t('Export All')}
  83. </DataExport>
  84. );
  85. }
  86. // Placate eslint proptype checking
  87. function renderEditButton(canEdit: boolean, props: Props) {
  88. const onClick = canEdit ? props.onEdit : undefined;
  89. return (
  90. <GuideAnchor target="columns_header_button">
  91. <Button
  92. size="sm"
  93. disabled={!canEdit}
  94. onClick={onClick}
  95. data-test-id="grid-edit-enable"
  96. icon={<IconSliders />}
  97. >
  98. {t('Columns')}
  99. </Button>
  100. </GuideAnchor>
  101. );
  102. }
  103. // Placate eslint proptype checking
  104. function renderSummaryButton({onChangeShowTags, showTags}: Props) {
  105. return (
  106. <Button size="sm" onClick={onChangeShowTags} icon={<IconTag />}>
  107. {showTags ? t('Hide Tags') : t('Show Tags')}
  108. </Button>
  109. );
  110. }
  111. type FeatureWrapperProps = Props & {
  112. children: (hasFeature: boolean, props: Props) => React.ReactNode;
  113. };
  114. function FeatureWrapper(props: FeatureWrapperProps) {
  115. const noEditMessage = t('Requires discover query feature.');
  116. const editFeatures = ['organizations:discover-query'];
  117. const renderDisabled = p => (
  118. <Hovercard
  119. body={
  120. <FeatureDisabled
  121. features={p.features}
  122. hideHelpToggle
  123. message={noEditMessage}
  124. featureName={noEditMessage}
  125. />
  126. }
  127. >
  128. {p.children(p)}
  129. </Hovercard>
  130. );
  131. return (
  132. <Feature
  133. hookName="feature-disabled:grid-editable-actions"
  134. renderDisabled={renderDisabled}
  135. features={editFeatures}
  136. >
  137. {({hasFeature}) => props.children(hasFeature, props)}
  138. </Feature>
  139. );
  140. }
  141. function TableActions(props: Props) {
  142. const location = useLocation();
  143. const cursor = location?.query?.cursor;
  144. const cursorOffset = parseCursor(cursor)?.offset ?? 0;
  145. const numSamples = props.tableData?.data?.length ?? null;
  146. const totalNumSamples = numSamples === null ? null : numSamples + cursorOffset;
  147. return (
  148. <Fragment>
  149. {props.supportsInvestigationRule && (
  150. <InvestigationRuleCreation
  151. {...props}
  152. buttonProps={{size: 'sm'}}
  153. numSamples={totalNumSamples}
  154. key="investigationRuleCreation"
  155. />
  156. )}
  157. <FeatureWrapper {...props} key="edit">
  158. {renderEditButton}
  159. </FeatureWrapper>
  160. <FeatureWrapper {...props} key="download">
  161. {renderDownloadButton}
  162. </FeatureWrapper>
  163. {renderSummaryButton(props)}
  164. </Fragment>
  165. );
  166. }
  167. export default TableActions;