relocation.tsx 8.8 KB

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