uploadBackup.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import {useContext, useRef, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {motion} from 'framer-motion';
  4. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  5. import {Client} from 'sentry/api';
  6. import {Button} from 'sentry/components/button';
  7. import Well from 'sentry/components/well';
  8. import {IconFile, IconUpload} from 'sentry/icons';
  9. import {t} from 'sentry/locale';
  10. import ConfigStore from 'sentry/stores/configStore';
  11. import {space} from 'sentry/styles/space';
  12. import testableTransition from 'sentry/utils/testableTransition';
  13. import useApi from 'sentry/utils/useApi';
  14. import StepHeading from 'sentry/views/relocation/components/stepHeading';
  15. import {RelocationOnboardingContext} from 'sentry/views/relocation/relocationOnboardingContext';
  16. import {StepProps} from './types';
  17. type UploadWellProps = {
  18. centered: boolean;
  19. draggedOver: boolean;
  20. onDragEnter: Function;
  21. onDragLeave: Function;
  22. onDragOver: Function;
  23. onDrop: Function;
  24. };
  25. const DEFAULT_ERROR_MSG = t(
  26. 'An error has occurred while trying to start relocation job. Please contact support for further assistance.'
  27. );
  28. const IN_PROGRESS_RELOCATION_ERROR_MSG = t(
  29. 'You already have an in-progress relocation job.'
  30. );
  31. const THROTTLED_RELOCATION_ERROR_MSG = t(
  32. 'We have reached the daily limit of relocations - please try again tomorrow, or contact support.'
  33. );
  34. const SESSION_EXPIRED_ERROR_MSG = t('Your session has expired.');
  35. export function UploadBackup({onComplete}: StepProps) {
  36. const api = useApi({
  37. api: new Client({headers: {Accept: 'application/json; charset=utf-8'}}),
  38. });
  39. const [file, setFile] = useState<File>();
  40. const [dragCounter, setDragCounter] = useState(0);
  41. const inputFileRef = useRef<HTMLInputElement>(null);
  42. const relocationOnboardingContext = useContext(RelocationOnboardingContext);
  43. const user = ConfigStore.get('user');
  44. const handleDragEnter = event => {
  45. event.preventDefault();
  46. setDragCounter(dragCounter + 1);
  47. };
  48. const handleDragLeave = () => {
  49. setDragCounter(dragCounter - 1);
  50. };
  51. const handleDrop = event => {
  52. event.preventDefault();
  53. setDragCounter(0);
  54. setFile(event.dataTransfer.files[0]);
  55. };
  56. const handleFileChange = event => {
  57. const newFile = event.target.files?.[0];
  58. // No file selected (e.g. user clicked "cancel")
  59. if (!newFile) {
  60. return;
  61. }
  62. setFile(newFile);
  63. };
  64. const onFileUploadLinkClick = () => {
  65. inputFileRef.current && inputFileRef.current.click();
  66. };
  67. const handleStartRelocation = async () => {
  68. const {orgSlugs, regionUrl} = relocationOnboardingContext.data;
  69. if (!orgSlugs || !regionUrl || !file) {
  70. addErrorMessage(DEFAULT_ERROR_MSG);
  71. return;
  72. }
  73. const formData = new FormData();
  74. formData.set('orgs', orgSlugs);
  75. formData.set('file', file);
  76. formData.set('owner', user.username);
  77. try {
  78. const result = await api.requestPromise(`/relocations/`, {
  79. method: 'POST',
  80. host: regionUrl,
  81. data: formData,
  82. });
  83. addSuccessMessage(
  84. t(
  85. "Your relocation has started - we'll email you with updates as soon as we have 'em!"
  86. )
  87. );
  88. onComplete(result.uuid);
  89. } catch (error) {
  90. if (error.status === 409) {
  91. addErrorMessage(IN_PROGRESS_RELOCATION_ERROR_MSG);
  92. } else if (error.status === 429) {
  93. addErrorMessage(THROTTLED_RELOCATION_ERROR_MSG);
  94. } else if (error.status === 401) {
  95. addErrorMessage(SESSION_EXPIRED_ERROR_MSG);
  96. } else {
  97. addErrorMessage(DEFAULT_ERROR_MSG);
  98. }
  99. }
  100. };
  101. return (
  102. <Wrapper data-test-id="upload-backup">
  103. <StepHeading step={4}>
  104. {t('Upload Tarball to begin the relocation process')}
  105. </StepHeading>
  106. <motion.div
  107. transition={testableTransition()}
  108. variants={{
  109. initial: {y: 30, opacity: 0},
  110. animate: {y: 0, opacity: 1},
  111. exit: {opacity: 0},
  112. }}
  113. >
  114. <p>
  115. {t(
  116. 'Nearly done! The file is being uploaded to sentry for the relocation process. You can close this tab if you like. We will email when complete.'
  117. )}
  118. </p>
  119. {file ? (
  120. <FinishedWell centered>
  121. <IconFile className="file-icon" size="xl" />
  122. <div>
  123. <p>{file.name}</p>
  124. <a onClick={() => setFile(undefined)}>{t('Remove file')}</a>
  125. </div>
  126. <StartRelocationButton
  127. priority="primary"
  128. onClick={handleStartRelocation}
  129. icon={<IconUpload className="upload-icon" size="xs" />}
  130. >
  131. {t('Start Relocation')}
  132. </StartRelocationButton>
  133. </FinishedWell>
  134. ) : (
  135. <UploadWell
  136. onDragEnter={handleDragEnter}
  137. onDragOver={event => event.preventDefault()}
  138. onDragLeave={handleDragLeave}
  139. onDrop={handleDrop}
  140. centered
  141. aria-label="dropzone"
  142. draggedOver={dragCounter > 0}
  143. >
  144. <StyledUploadIcon className="upload-icon" size="xl" />
  145. <UploadWrapper>
  146. <p>{t('Drag and Drop file here or')}</p>
  147. <a onClick={onFileUploadLinkClick}>{t('Choose file')}</a>
  148. <UploadInput
  149. name="file"
  150. type="file"
  151. aria-label="file-upload"
  152. accept=".tar"
  153. ref={inputFileRef}
  154. onChange={e => handleFileChange(e)}
  155. hidden
  156. title=""
  157. />
  158. </UploadWrapper>
  159. </UploadWell>
  160. )}
  161. </motion.div>
  162. </Wrapper>
  163. );
  164. }
  165. export default UploadBackup;
  166. const StyledUploadIcon = styled(IconUpload)`
  167. margin-top: ${space(2)};
  168. margin-bottom: ${space(1)};
  169. `;
  170. const Wrapper = styled('div')`
  171. max-width: 769px;
  172. max-height: 525px;
  173. margin-left: auto;
  174. margin-right: auto;
  175. padding: ${space(4)};
  176. background-color: ${p => p.theme.surface400};
  177. z-index: 100;
  178. box-shadow: 0 5px 10px rgba(0, 0, 0, 0.05);
  179. border-radius: 10px;
  180. width: 100%;
  181. font-size: 16px;
  182. color: ${p => p.theme.gray300};
  183. mark {
  184. border-radius: 8px;
  185. padding: ${space(0.25)} ${space(0.5)} ${space(0.25)} ${space(0.5)};
  186. background: ${p => p.theme.gray100};
  187. margin-right: ${space(1)};
  188. }
  189. h2 {
  190. color: ${p => p.theme.gray500};
  191. }
  192. p {
  193. margin-bottom: ${space(1)};
  194. }
  195. .encrypt-help {
  196. color: ${p => p.theme.gray500};
  197. }
  198. `;
  199. const StartRelocationButton = styled(Button)`
  200. margin-left: auto;
  201. `;
  202. const FinishedWell = styled(Well)`
  203. display: flex;
  204. align-items: center;
  205. text-align: left;
  206. div {
  207. margin-left: ${space(2)};
  208. line-height: 1;
  209. }
  210. a {
  211. color: ${p => p.theme.translucentGray200};
  212. font-size: 14px;
  213. }
  214. a:hover {
  215. color: ${p => p.theme.gray300};
  216. }
  217. `;
  218. const UploadWell = styled(Well)<UploadWellProps>`
  219. margin-top: ${space(2)};
  220. height: 140px;
  221. border-style: ${props => (props.draggedOver ? 'solid' : 'dashed')};
  222. border-width: medium;
  223. align-items: center;
  224. .file-icon,
  225. .upload-icon {
  226. color: ${p => p.theme.gray500};
  227. }
  228. background: ${props =>
  229. props.draggedOver ? p => p.theme.purple100 : p => p.theme.surface400};
  230. `;
  231. const UploadInput = styled('input')`
  232. opacity: 0;
  233. `;
  234. const UploadWrapper = styled('div')`
  235. display: flex;
  236. justify-content: center;
  237. a {
  238. padding-left: ${space(0.5)};
  239. }
  240. input[type='file'] {
  241. display: none;
  242. }
  243. `;