dataDownload.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Button, LinkButton} from 'sentry/components/button';
  4. import {ExportQueryType} from 'sentry/components/dataExport';
  5. import {DateTime} from 'sentry/components/dateTime';
  6. import LoadingIndicator from 'sentry/components/loadingIndicator';
  7. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  8. import {IconDownload} from 'sentry/icons';
  9. import {t, tct} from 'sentry/locale';
  10. import {space} from 'sentry/styles/space';
  11. import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
  12. import {useApiQuery} from 'sentry/utils/queryClient';
  13. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  14. import {useNavigate} from 'sentry/utils/useNavigate';
  15. import Layout from 'sentry/views/auth/layout';
  16. export enum DownloadStatus {
  17. EARLY = 'EARLY',
  18. VALID = 'VALID',
  19. EXPIRED = 'EXPIRED',
  20. }
  21. type RouteParams = {
  22. dataExportId: string;
  23. orgId: string;
  24. };
  25. type Download = {
  26. checksum: string;
  27. dateCreated: string;
  28. id: number;
  29. query: {
  30. info: object;
  31. type: ExportQueryType;
  32. };
  33. status: DownloadStatus;
  34. user: {
  35. email: string;
  36. id: number;
  37. username: string;
  38. };
  39. dateExpired?: string;
  40. dateFinished?: string;
  41. };
  42. type Props = {} & RouteComponentProps<RouteParams, {}>;
  43. function DataDownload({params: {orgId, dataExportId}}: Props) {
  44. const {
  45. data: download,
  46. isPending,
  47. isError,
  48. error,
  49. } = useApiQuery<Download>([`/organizations/${orgId}/data-export/${dataExportId}/`], {
  50. staleTime: 0,
  51. });
  52. const navigate = useNavigate();
  53. if (isError) {
  54. const errDetail = error?.responseJSON?.detail;
  55. return (
  56. <Layout>
  57. <main>
  58. <Header>
  59. <h3>
  60. {error.status} - {error.statusText}
  61. </h3>
  62. </Header>
  63. {errDetail && (
  64. <Body>
  65. <p>{errDetail as string}</p>
  66. </Body>
  67. )}
  68. </main>
  69. </Layout>
  70. );
  71. }
  72. if (isPending) {
  73. return <LoadingIndicator />;
  74. }
  75. const getActionLink = (queryType): string => {
  76. switch (queryType) {
  77. case ExportQueryType.ISSUES_BY_TAG:
  78. return `/organizations/${orgId}/issues/`;
  79. case ExportQueryType.DISCOVER:
  80. return `/organizations/${orgId}/discover/queries/`;
  81. default:
  82. return '/';
  83. }
  84. };
  85. const renderDate = (date: string | undefined): React.ReactNode => {
  86. if (!date) {
  87. return null;
  88. }
  89. const d = new Date(date);
  90. return (
  91. <strong>
  92. <DateTime date={d} />
  93. </strong>
  94. );
  95. };
  96. const renderEarly = (): React.ReactNode => {
  97. return (
  98. <Fragment>
  99. <Header>
  100. <h3>
  101. {t('What are')}
  102. <i>{t(' you ')}</i>
  103. {t('doing here?')}
  104. </h3>
  105. </Header>
  106. <Body>
  107. <p>
  108. {t(
  109. "Not that its any of our business, but were you invited to this page? It's just that we don't exactly remember emailing you about it."
  110. )}
  111. </p>
  112. <p>{t("Close this window and we'll email you when your download is ready.")}</p>
  113. </Body>
  114. </Fragment>
  115. );
  116. };
  117. const renderExpired = (): React.ReactNode => {
  118. const {query} = download;
  119. const actionLink = getActionLink(query.type);
  120. return (
  121. <Fragment>
  122. <Header>
  123. <h3>{t('This is awkward.')}</h3>
  124. </Header>
  125. <Body>
  126. <p>
  127. {t(
  128. "That link expired, so your download doesn't live here anymore. Just picked up one day and left town."
  129. )}
  130. </p>
  131. <p>
  132. {t(
  133. 'Make a new one with your latest data. Your old download will never see it coming.'
  134. )}
  135. </p>
  136. <DownloadButton href={actionLink} priority="primary">
  137. {t('Start a New Download')}
  138. </DownloadButton>
  139. </Body>
  140. </Fragment>
  141. );
  142. };
  143. const openInDiscover = () => {
  144. const {
  145. query: {info},
  146. } = download;
  147. const to = {
  148. pathname: `/organizations/${orgId}/discover/results/`,
  149. query: info,
  150. };
  151. navigate(normalizeUrl(to));
  152. };
  153. const renderOpenInDiscover = () => {
  154. const {
  155. query = {
  156. type: ExportQueryType.ISSUES_BY_TAG,
  157. info: {},
  158. },
  159. } = download;
  160. // default to IssuesByTag because we don't want to
  161. // display this unless we're sure its a discover query
  162. const {type = ExportQueryType.ISSUES_BY_TAG} = query;
  163. return type === 'Discover' ? (
  164. <Fragment>
  165. <p>{t('Need to make changes?')}</p>
  166. <Button priority="primary" onClick={() => openInDiscover()}>
  167. {t('Open in Discover')}
  168. </Button>
  169. <br />
  170. </Fragment>
  171. ) : null;
  172. };
  173. const renderValid = (): React.ReactNode => {
  174. const {dateExpired, checksum} = download;
  175. return (
  176. <Fragment>
  177. <Header>
  178. <h3>{t('All done.')}</h3>
  179. </Header>
  180. <Body>
  181. <p>{t("See, that wasn't so bad. Your data is all ready for download.")}</p>
  182. <LinkButton
  183. priority="primary"
  184. icon={<IconDownload />}
  185. href={`/api/0/organizations/${orgId}/data-export/${dataExportId}/?download=true`}
  186. >
  187. {t('Download CSV')}
  188. </LinkButton>
  189. <p>
  190. {t("That link won't last forever — it expires:")}
  191. <br />
  192. {renderDate(dateExpired)}
  193. </p>
  194. {renderOpenInDiscover()}
  195. <p>
  196. <small>
  197. <strong>SHA1:{checksum}</strong>
  198. </small>
  199. <br />
  200. {tct('Need help verifying? [link].', {
  201. link: (
  202. <a
  203. href="https://docs.sentry.io/product/discover-queries/query-builder/#filter-by-table-columns"
  204. target="_blank"
  205. rel="noopener noreferrer"
  206. >
  207. {t('Check out our docs')}
  208. </a>
  209. ),
  210. })}
  211. </p>
  212. </Body>
  213. </Fragment>
  214. );
  215. };
  216. const renderContent = () => {
  217. switch (download.status) {
  218. case DownloadStatus.EARLY:
  219. return renderEarly();
  220. case DownloadStatus.EXPIRED:
  221. return renderExpired();
  222. default:
  223. return renderValid();
  224. }
  225. };
  226. return (
  227. <SentryDocumentTitle title={t('Download Center')}>
  228. <Layout>
  229. <main>{renderContent()}</main>
  230. </Layout>
  231. </SentryDocumentTitle>
  232. );
  233. }
  234. const Header = styled('header')`
  235. border-bottom: 1px solid ${p => p.theme.border};
  236. padding: ${space(3)} 40px 0;
  237. h3 {
  238. font-size: 24px;
  239. margin: 0 0 ${space(3)} 0;
  240. }
  241. `;
  242. const Body = styled('div')`
  243. padding: ${space(2)} 40px;
  244. max-width: 500px;
  245. p {
  246. margin: ${space(1.5)} 0;
  247. }
  248. `;
  249. const DownloadButton = styled(LinkButton)`
  250. margin-bottom: ${space(1.5)};
  251. `;
  252. export default DataDownload;