dataExport.tsx 3.3 KB

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