dataDownload.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import {Fragment} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Button} from 'sentry/components/button';
  5. import {ExportQueryType} from 'sentry/components/dataExport';
  6. import {DateTime} from 'sentry/components/dateTime';
  7. import LoadingIndicator from 'sentry/components/loadingIndicator';
  8. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  9. import {IconDownload} from 'sentry/icons';
  10. import {t, tct} from 'sentry/locale';
  11. import {space} from 'sentry/styles/space';
  12. import {useApiQuery} from 'sentry/utils/queryClient';
  13. import {useRouteContext} from 'sentry/utils/useRouteContext';
  14. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  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. isLoading,
  47. isError,
  48. error,
  49. } = useApiQuery<Download>([`/organizations/${orgId}/data-export/${dataExportId}/`], {
  50. staleTime: 0,
  51. });
  52. const route = useRouteContext();
  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 (isLoading) {
  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 navigator = route.router;
  145. const {
  146. query: {info},
  147. } = download;
  148. const to = {
  149. pathname: `/organizations/${orgId}/discover/results/`,
  150. query: info,
  151. };
  152. navigator.push(normalizeUrl(to));
  153. };
  154. const renderOpenInDiscover = () => {
  155. const {
  156. query = {
  157. type: ExportQueryType.ISSUES_BY_TAG,
  158. info: {},
  159. },
  160. } = download;
  161. // default to IssuesByTag because we don't want to
  162. // display this unless we're sure its a discover query
  163. const {type = ExportQueryType.ISSUES_BY_TAG} = query;
  164. return type === 'Discover' ? (
  165. <Fragment>
  166. <p>{t('Need to make changes?')}</p>
  167. <Button priority="primary" onClick={() => openInDiscover()}>
  168. {t('Open in Discover')}
  169. </Button>
  170. <br />
  171. </Fragment>
  172. ) : null;
  173. };
  174. const renderValid = (): React.ReactNode => {
  175. const {dateExpired, checksum} = download;
  176. return (
  177. <Fragment>
  178. <Header>
  179. <h3>{t('All done.')}</h3>
  180. </Header>
  181. <Body>
  182. <p>{t("See, that wasn't so bad. Your data is all ready for download.")}</p>
  183. <Button
  184. priority="primary"
  185. icon={<IconDownload />}
  186. href={`/api/0/organizations/${orgId}/data-export/${dataExportId}/?download=true`}
  187. >
  188. {t('Download CSV')}
  189. </Button>
  190. <p>
  191. {t("That link won't last forever — it expires:")}
  192. <br />
  193. {renderDate(dateExpired)}
  194. </p>
  195. {renderOpenInDiscover()}
  196. <p>
  197. <small>
  198. <strong>SHA1:{checksum}</strong>
  199. </small>
  200. <br />
  201. {tct('Need help verifying? [link].', {
  202. link: (
  203. <a
  204. href="https://docs.sentry.io/product/discover-queries/query-builder/#filter-by-table-columns"
  205. target="_blank"
  206. rel="noopener noreferrer"
  207. >
  208. {t('Check out our docs')}
  209. </a>
  210. ),
  211. })}
  212. </p>
  213. </Body>
  214. </Fragment>
  215. );
  216. };
  217. const renderContent = () => {
  218. switch (download.status) {
  219. case DownloadStatus.EARLY:
  220. return renderEarly();
  221. case DownloadStatus.EXPIRED:
  222. return renderExpired();
  223. default:
  224. return renderValid();
  225. }
  226. };
  227. return (
  228. <SentryDocumentTitle title={t('Download Center')}>
  229. <Layout>
  230. <main>{renderContent()}</main>
  231. </Layout>
  232. </SentryDocumentTitle>
  233. );
  234. }
  235. const Header = styled('header')`
  236. border-bottom: 1px solid ${p => p.theme.border};
  237. padding: ${space(3)} 40px 0;
  238. h3 {
  239. font-size: 24px;
  240. margin: 0 0 ${space(3)} 0;
  241. }
  242. `;
  243. const Body = styled('div')`
  244. padding: ${space(2)} 40px;
  245. max-width: 500px;
  246. p {
  247. margin: ${space(1.5)} 0;
  248. }
  249. `;
  250. const DownloadButton = styled(Button)`
  251. margin-bottom: ${space(1.5)};
  252. `;
  253. export default DataDownload;