relocation.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import {useCallback, useEffect, useRef, useState} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {AnimatePresence, motion, MotionProps, useAnimation} from 'framer-motion';
  5. import {Button, ButtonProps} from 'sentry/components/button';
  6. import LoadingError from 'sentry/components/loadingError';
  7. import LogoSentry from 'sentry/components/logoSentry';
  8. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  9. import {IconArrow} from 'sentry/icons';
  10. import {t} from 'sentry/locale';
  11. import {space} from 'sentry/styles/space';
  12. import Redirect from 'sentry/utils/redirect';
  13. import testableTransition from 'sentry/utils/testableTransition';
  14. import useApi from 'sentry/utils/useApi';
  15. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  16. import PageCorners from 'sentry/views/onboarding/components/pageCorners';
  17. import Stepper from 'sentry/views/onboarding/components/stepper';
  18. import {RelocationOnboardingContextProvider} from 'sentry/views/relocation/relocationOnboardingContext';
  19. import EncryptBackup from './encryptBackup';
  20. import GetStarted from './getStarted';
  21. import PublicKey from './publicKey';
  22. import {StepDescriptor} from './types';
  23. import UploadBackup from './uploadBackup';
  24. type RouteParams = {
  25. step: string;
  26. };
  27. type Props = RouteComponentProps<RouteParams, {}>;
  28. function getRelocationOnboardingSteps(): StepDescriptor[] {
  29. return [
  30. {
  31. id: 'get-started',
  32. title: t('Get Started'),
  33. Component: GetStarted,
  34. cornerVariant: 'top-left',
  35. },
  36. {
  37. id: 'public-key',
  38. title: t("Save Sentry's public key to your machine"),
  39. Component: PublicKey,
  40. cornerVariant: 'top-left',
  41. },
  42. {
  43. id: 'encrypt-backup',
  44. title: t('Encrypt backup'),
  45. Component: EncryptBackup,
  46. cornerVariant: 'top-left',
  47. },
  48. {
  49. id: 'upload-backup',
  50. title: t('Upload backup'),
  51. Component: UploadBackup,
  52. cornerVariant: 'top-left',
  53. },
  54. ];
  55. }
  56. function RelocationOnboarding(props: Props) {
  57. const [hasPublicKeyError, setHasError] = useState(false);
  58. // TODO(getsentry/team-ospo#214): We should use sessionStorage to track this, since it should not
  59. // change during a single run through this workflow.
  60. const [publicKey, setPublicKey] = useState('');
  61. const api = useApi();
  62. const fetchData = useCallback(() => {
  63. const endpoint = `/publickeys/relocations/`;
  64. return api
  65. .requestPromise(endpoint)
  66. .then(response => {
  67. setPublicKey(response.public_key);
  68. setHasError(false);
  69. })
  70. .catch(_error => {
  71. setPublicKey('');
  72. setHasError(true);
  73. });
  74. }, [api]);
  75. useEffect(() => {
  76. fetchData();
  77. }, [fetchData]);
  78. const loadingError = (
  79. <LoadingError message={t('Failed to load your public key.')} onRetry={fetchData} />
  80. );
  81. const {
  82. params: {step: stepId},
  83. } = props;
  84. const onboardingSteps = getRelocationOnboardingSteps();
  85. const stepObj = onboardingSteps.find(({id}) => stepId === id);
  86. const stepIndex = onboardingSteps.findIndex(({id}) => stepId === id);
  87. const cornerVariantTimeoutRed = useRef<number | undefined>(undefined);
  88. useEffect(() => {
  89. return () => {
  90. window.clearTimeout(cornerVariantTimeoutRed.current);
  91. };
  92. }, []);
  93. const cornerVariantControl = useAnimation();
  94. const updateCornerVariant = () => {
  95. // TODO(getsentry/team-ospo#214): Find a better way to delay the corner animation.
  96. window.clearTimeout(cornerVariantTimeoutRed.current);
  97. cornerVariantTimeoutRed.current = window.setTimeout(
  98. () => cornerVariantControl.start(stepIndex === 0 ? 'top-right' : 'top-left'),
  99. 1000
  100. );
  101. };
  102. useEffect(updateCornerVariant, [stepIndex, cornerVariantControl]);
  103. // Called onExitComplete
  104. const updateAnimationState = () => {
  105. if (!stepObj) {
  106. return;
  107. }
  108. };
  109. const goToStep = (step: StepDescriptor) => {
  110. if (!stepObj) {
  111. return;
  112. }
  113. if (step.cornerVariant !== stepObj.cornerVariant) {
  114. cornerVariantControl.start('none');
  115. }
  116. props.router.push(normalizeUrl(`/relocation/${step.id}/`));
  117. };
  118. const goNextStep = useCallback(
  119. (step: StepDescriptor) => {
  120. const currentStepIndex = onboardingSteps.findIndex(s => s.id === step.id);
  121. const nextStep = onboardingSteps[currentStepIndex + 1];
  122. if (step.cornerVariant !== nextStep.cornerVariant) {
  123. cornerVariantControl.start('none');
  124. }
  125. props.router.push(normalizeUrl(`/relocation/${nextStep.id}/`));
  126. },
  127. [onboardingSteps, cornerVariantControl, props.router]
  128. );
  129. if (!stepObj || stepIndex === -1) {
  130. return <Redirect to={normalizeUrl(`/relocation/${onboardingSteps[0].id}/`)} />;
  131. }
  132. return (
  133. <OnboardingWrapper data-test-id="relocation-onboarding">
  134. <RelocationOnboardingContextProvider>
  135. <SentryDocumentTitle title={stepObj.title} />
  136. <Header>
  137. <LogoSvg />
  138. {stepIndex !== -1 && (
  139. <StyledStepper
  140. numSteps={onboardingSteps.length}
  141. currentStepIndex={stepIndex}
  142. onClick={i => {
  143. goToStep(onboardingSteps[i]);
  144. }}
  145. />
  146. )}
  147. </Header>
  148. <Container>
  149. <Back
  150. onClick={() => goToStep(onboardingSteps[stepIndex - 1])}
  151. animate={stepIndex > 0 ? 'visible' : 'hidden'}
  152. />
  153. <AnimatePresence exitBeforeEnter onExitComplete={updateAnimationState}>
  154. <OnboardingStep
  155. key={stepObj.id}
  156. data-test-id={`onboarding-step-${stepObj.id}`}
  157. >
  158. {stepObj.Component && (
  159. <stepObj.Component
  160. active
  161. data-test-id={`onboarding-step-${stepObj.id}`}
  162. stepIndex={stepIndex}
  163. onComplete={() => {
  164. if (stepObj) {
  165. goNextStep(stepObj);
  166. }
  167. }}
  168. publicKey={publicKey}
  169. route={props.route}
  170. router={props.router}
  171. location={props.location}
  172. />
  173. )}
  174. </OnboardingStep>
  175. </AnimatePresence>
  176. <AdaptivePageCorners animateVariant={cornerVariantControl} />
  177. {stepObj.id === 'public-key' && hasPublicKeyError ? loadingError : null}
  178. </Container>
  179. </RelocationOnboardingContextProvider>
  180. </OnboardingWrapper>
  181. );
  182. }
  183. const Container = styled('div')`
  184. flex-grow: 1;
  185. display: flex;
  186. flex-direction: column;
  187. position: relative;
  188. background: #faf9fb;
  189. padding: 120px ${space(3)};
  190. width: 100%;
  191. margin: 0 auto;
  192. `;
  193. const Header = styled('header')`
  194. background: ${p => p.theme.background};
  195. padding-left: ${space(4)};
  196. padding-right: ${space(4)};
  197. position: sticky;
  198. height: 80px;
  199. align-items: center;
  200. top: 0;
  201. z-index: 100;
  202. box-shadow: 0 5px 10px rgba(0, 0, 0, 0.05);
  203. display: grid;
  204. grid-template-columns: 1fr 1fr 1fr;
  205. justify-items: stretch;
  206. `;
  207. const LogoSvg = styled(LogoSentry)`
  208. width: 130px;
  209. height: 30px;
  210. color: ${p => p.theme.textColor};
  211. `;
  212. const OnboardingStep = styled(motion.div)`
  213. flex-grow: 1;
  214. display: flex;
  215. flex-direction: column;
  216. `;
  217. OnboardingStep.defaultProps = {
  218. initial: 'initial',
  219. animate: 'animate',
  220. exit: 'exit',
  221. variants: {animate: {}},
  222. transition: testableTransition({
  223. staggerChildren: 0.2,
  224. }),
  225. };
  226. const AdaptivePageCorners = styled(PageCorners)`
  227. --corner-scale: 1;
  228. @media (max-width: ${p => p.theme.breakpoints.small}) {
  229. --corner-scale: 0.5;
  230. }
  231. `;
  232. const StyledStepper = styled(Stepper)`
  233. justify-self: center;
  234. @media (max-width: ${p => p.theme.breakpoints.medium}) {
  235. display: none;
  236. }
  237. `;
  238. interface BackButtonProps extends Omit<ButtonProps, 'icon' | 'priority'> {
  239. animate: MotionProps['animate'];
  240. className?: string;
  241. }
  242. const Back = styled(({className, animate, ...props}: BackButtonProps) => (
  243. <motion.div
  244. className={className}
  245. animate={animate}
  246. transition={testableTransition()}
  247. variants={{
  248. initial: {opacity: 0, visibility: 'hidden'},
  249. visible: {
  250. opacity: 1,
  251. visibility: 'visible',
  252. transition: testableTransition({delay: 1}),
  253. },
  254. hidden: {
  255. opacity: 0,
  256. transitionEnd: {
  257. visibility: 'hidden',
  258. },
  259. },
  260. }}
  261. >
  262. <Button {...props} icon={<IconArrow direction="left" />} priority="link">
  263. {t('Back')}
  264. </Button>
  265. </motion.div>
  266. ))`
  267. position: absolute;
  268. top: 40px;
  269. left: 20px;
  270. button {
  271. font-size: ${p => p.theme.fontSizeSmall};
  272. }
  273. `;
  274. const OnboardingWrapper = styled('main')`
  275. flex-grow: 1;
  276. display: flex;
  277. flex-direction: column;
  278. `;
  279. export default RelocationOnboarding;