uploadBackup.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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(__props: 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.email);
  77. try {
  78. 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. } catch (error) {
  89. if (error.status === 409) {
  90. addErrorMessage(IN_PROGRESS_RELOCATION_ERROR_MSG);
  91. } else if (error.status === 429) {
  92. addErrorMessage(THROTTLED_RELOCATION_ERROR_MSG);
  93. } else if (error.status === 401) {
  94. addErrorMessage(SESSION_EXPIRED_ERROR_MSG);
  95. } else {
  96. addErrorMessage(DEFAULT_ERROR_MSG);
  97. }
  98. }
  99. };
  100. return (
  101. <Wrapper>
  102. <StepHeading step={4}>
  103. {t('Upload Tarball to begin the relocation process')}
  104. </StepHeading>
  105. <motion.div
  106. transition={testableTransition()}
  107. variants={{
  108. initial: {y: 30, opacity: 0},
  109. animate: {y: 0, opacity: 1},
  110. exit: {opacity: 0},
  111. }}
  112. >
  113. <p>
  114. {t(
  115. '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.'
  116. )}
  117. </p>
  118. {file ? (
  119. <FinishedWell centered>
  120. <IconFile className="file-icon" size="xl" />
  121. <div>
  122. <p>{file.name}</p>
  123. <a onClick={() => setFile(undefined)}>{t('Remove file')}</a>
  124. </div>
  125. <StartRelocationButton
  126. priority="primary"
  127. onClick={handleStartRelocation}
  128. icon={<IconUpload className="upload-icon" size="xs" />}
  129. >
  130. {t('Start Relocation')}
  131. </StartRelocationButton>
  132. </FinishedWell>
  133. ) : (
  134. <UploadWell
  135. onDragEnter={handleDragEnter}
  136. onDragOver={event => event.preventDefault()}
  137. onDragLeave={handleDragLeave}
  138. onDrop={handleDrop}
  139. centered
  140. aria-label="dropzone"
  141. draggedOver={dragCounter > 0}
  142. >
  143. <StyledUploadIcon className="upload-icon" size="xl" />
  144. <UploadWrapper>
  145. <p>{t('Drag and Drop file here or')}</p>
  146. <a onClick={onFileUploadLinkClick}>{t('Choose file')}</a>
  147. <UploadInput
  148. name="file"
  149. type="file"
  150. aria-label="file-upload"
  151. accept=".tar"
  152. ref={inputFileRef}
  153. onChange={e => handleFileChange(e)}
  154. hidden
  155. title=""
  156. />
  157. </UploadWrapper>
  158. </UploadWell>
  159. )}
  160. </motion.div>
  161. </Wrapper>
  162. );
  163. }
  164. export default UploadBackup;
  165. const StyledUploadIcon = styled(IconUpload)`
  166. margin-top: ${space(2)};
  167. margin-bottom: ${space(1)};
  168. `;
  169. const Wrapper = styled('div')`
  170. max-width: 769px;
  171. max-height: 525px;
  172. margin-left: auto;
  173. margin-right: auto;
  174. padding: ${space(4)};
  175. background-color: ${p => p.theme.surface400};
  176. z-index: 100;
  177. box-shadow: 0 5px 10px rgba(0, 0, 0, 0.05);
  178. border-radius: 10px;
  179. width: 100%;
  180. font-size: 16px;
  181. color: ${p => p.theme.gray300};
  182. mark {
  183. border-radius: 8px;
  184. padding: ${space(0.25)} ${space(0.5)} ${space(0.25)} ${space(0.5)};
  185. background: ${p => p.theme.gray100};
  186. margin-right: ${space(1)};
  187. }
  188. h2 {
  189. color: ${p => p.theme.gray500};
  190. }
  191. p {
  192. margin-bottom: ${space(1)};
  193. }
  194. .encrypt-help {
  195. color: ${p => p.theme.gray500};
  196. }
  197. `;
  198. const StartRelocationButton = styled(Button)`
  199. margin-left: auto;
  200. `;
  201. const FinishedWell = styled(Well)`
  202. display: flex;
  203. align-items: center;
  204. text-align: left;
  205. div {
  206. margin-left: ${space(2)};
  207. line-height: 1;
  208. }
  209. a {
  210. color: ${p => p.theme.translucentGray200};
  211. font-size: 14px;
  212. }
  213. a:hover {
  214. color: ${p => p.theme.gray300};
  215. }
  216. `;
  217. const UploadWell = styled(Well)<UploadWellProps>`
  218. margin-top: ${space(2)};
  219. height: 140px;
  220. border-style: ${props => (props.draggedOver ? 'solid' : 'dashed')};
  221. border-width: medium;
  222. align-items: center;
  223. .file-icon,
  224. .upload-icon {
  225. color: ${p => p.theme.gray500};
  226. }
  227. background: ${props =>
  228. props.draggedOver ? p => p.theme.purple100 : p => p.theme.surface400};
  229. `;
  230. const UploadInput = styled('input')`
  231. opacity: 0;
  232. `;
  233. const UploadWrapper = styled('div')`
  234. display: flex;
  235. justify-content: center;
  236. a {
  237. padding-left: ${space(0.5)};
  238. }
  239. input[type='file'] {
  240. display: none;
  241. }
  242. `;