dataExport.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import * as React from 'react';
  2. import debounce from 'lodash/debounce';
  3. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  4. import {Client} from 'sentry/api';
  5. import Feature from 'sentry/components/acl/feature';
  6. import Button from 'sentry/components/button';
  7. import {t} from 'sentry/locale';
  8. import {Organization} from 'sentry/types';
  9. import withApi from 'sentry/utils/withApi';
  10. import withOrganization from 'sentry/utils/withOrganization';
  11. // NOTE: Coordinate with other ExportQueryType (src/sentry/data_export/base.py)
  12. export enum ExportQueryType {
  13. IssuesByTag = 'Issues-by-Tag',
  14. Discover = 'Discover',
  15. }
  16. interface DataExportPayload {
  17. queryInfo: any;
  18. queryType: ExportQueryType; // TODO(ts): Formalize different possible payloads
  19. }
  20. interface DataExportProps {
  21. api: Client;
  22. organization: Organization;
  23. payload: DataExportPayload;
  24. children?: React.ReactNode;
  25. disabled?: boolean;
  26. icon?: React.ReactNode;
  27. }
  28. function DataExport({
  29. api,
  30. children,
  31. disabled,
  32. organization,
  33. payload,
  34. icon,
  35. }: DataExportProps): React.ReactElement {
  36. const [inProgress, setInProgress] = React.useState(false);
  37. React.useEffect(() => {
  38. if (inProgress) {
  39. setInProgress(false);
  40. }
  41. }, [payload.queryType, payload.queryInfo]);
  42. const handleDataExport = React.useCallback(() => {
  43. setInProgress(true);
  44. api
  45. .requestPromise(`/organizations/${organization.slug}/data-export/`, {
  46. includeAllArgs: true,
  47. method: 'POST',
  48. data: {
  49. query_type: payload.queryType,
  50. query_info: payload.queryInfo,
  51. },
  52. })
  53. .then(([_data, _, response]) => {
  54. addSuccessMessage(
  55. response?.status === 201
  56. ? t(
  57. "Sit tight. We'll shoot you an email when your data is ready for download."
  58. )
  59. : t("It looks like we're already working on it. Sit tight, we'll email you.")
  60. );
  61. })
  62. .catch(err => {
  63. const message =
  64. err?.responseJSON?.detail ??
  65. "We tried our hardest, but we couldn't export your data. Give it another go.";
  66. addErrorMessage(t(message));
  67. setInProgress(false);
  68. });
  69. }, [payload.queryInfo, payload.queryType, organization.slug, api]);
  70. return (
  71. <Feature features={['organizations:discover-query']}>
  72. {inProgress ? (
  73. <Button
  74. size="small"
  75. priority="default"
  76. title="You can get on with your life. We'll email you when your data's ready."
  77. disabled
  78. icon={icon}
  79. >
  80. {t("We're working on it...")}
  81. </Button>
  82. ) : (
  83. <Button
  84. onClick={debounce(handleDataExport, 500)}
  85. disabled={disabled || false}
  86. size="small"
  87. priority="default"
  88. title="Put your data to work. Start your export and we'll email you when it's finished."
  89. icon={icon}
  90. >
  91. {children ? children : t('Export All to CSV')}
  92. </Button>
  93. )}
  94. </Feature>
  95. );
  96. }
  97. export {DataExport};
  98. export default withApi(withOrganization(DataExport));