uploadBackup.tsx 7.2 KB

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