tableActions.tsx 5.2 KB

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