onboarding.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. import {useCallback, useContext, 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 Hook from 'sentry/components/hook';
  7. import Link from 'sentry/components/links/link';
  8. import LogoSentry from 'sentry/components/logoSentry';
  9. import {OnboardingContext} from 'sentry/components/onboarding/onboardingContext';
  10. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  11. import {IconArrow} from 'sentry/icons';
  12. import {t} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import {OnboardingSelectedSDK} from 'sentry/types';
  15. import {trackAnalytics} from 'sentry/utils/analytics';
  16. import Redirect from 'sentry/utils/redirect';
  17. import testableTransition from 'sentry/utils/testableTransition';
  18. import useOrganization from 'sentry/utils/useOrganization';
  19. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  20. import PageCorners from 'sentry/views/onboarding/components/pageCorners';
  21. import Stepper from './components/stepper';
  22. import {PlatformSelection} from './platformSelection';
  23. import SetupDocs from './setupDocs';
  24. import {StepDescriptor} from './types';
  25. import TargetedOnboardingWelcome from './welcome';
  26. type RouteParams = {
  27. step: string;
  28. };
  29. type Props = RouteComponentProps<RouteParams, {}>;
  30. function getOrganizationOnboardingSteps(): StepDescriptor[] {
  31. return [
  32. {
  33. id: 'welcome',
  34. title: t('Welcome'),
  35. Component: TargetedOnboardingWelcome,
  36. cornerVariant: 'top-right',
  37. },
  38. {
  39. id: 'select-platform',
  40. title: t('Select platform'),
  41. Component: PlatformSelection,
  42. hasFooter: true,
  43. cornerVariant: 'top-left',
  44. },
  45. {
  46. id: 'setup-docs',
  47. title: t('Install the Sentry SDK'),
  48. Component: SetupDocs,
  49. hasFooter: true,
  50. cornerVariant: 'top-left',
  51. },
  52. ];
  53. }
  54. function Onboarding(props: Props) {
  55. const organization = useOrganization();
  56. const onboardingContext = useContext(OnboardingContext);
  57. const selectedSDK = onboardingContext.data.selectedSDK;
  58. const selectedProjectSlug = selectedSDK?.key;
  59. const {
  60. params: {step: stepId},
  61. } = props;
  62. const cornerVariantTimeoutRed = useRef<number | undefined>(undefined);
  63. useEffect(() => {
  64. return () => {
  65. window.clearTimeout(cornerVariantTimeoutRed.current);
  66. };
  67. }, []);
  68. const heartbeatFooter = !!organization?.features.includes(
  69. 'onboarding-heartbeat-footer'
  70. );
  71. const projectDeletionOnBackClick = !!organization?.features.includes(
  72. 'onboarding-project-deletion-on-back-click'
  73. );
  74. const onboardingSteps = getOrganizationOnboardingSteps();
  75. const stepObj = onboardingSteps.find(({id}) => stepId === id);
  76. const stepIndex = onboardingSteps.findIndex(({id}) => stepId === id);
  77. const cornerVariantControl = useAnimation();
  78. const updateCornerVariant = () => {
  79. // TODO: find better way to delay the corner animation
  80. window.clearTimeout(cornerVariantTimeoutRed.current);
  81. cornerVariantTimeoutRed.current = window.setTimeout(
  82. () => cornerVariantControl.start(stepIndex === 0 ? 'top-right' : 'top-left'),
  83. 1000
  84. );
  85. };
  86. useEffect(updateCornerVariant, [stepIndex, cornerVariantControl]);
  87. // Called onExitComplete
  88. const [containerHasFooter, setContainerHasFooter] = useState<boolean>(false);
  89. const updateAnimationState = () => {
  90. if (!stepObj) {
  91. return;
  92. }
  93. setContainerHasFooter(stepObj.hasFooter ?? false);
  94. };
  95. const goToStep = (step: StepDescriptor) => {
  96. if (!stepObj) {
  97. return;
  98. }
  99. if (step.cornerVariant !== stepObj.cornerVariant) {
  100. cornerVariantControl.start('none');
  101. }
  102. props.router.push(normalizeUrl(`/onboarding/${organization.slug}/${step.id}/`));
  103. };
  104. const goNextStep = useCallback(
  105. (step: StepDescriptor, platform?: OnboardingSelectedSDK) => {
  106. const currentStepIndex = onboardingSteps.findIndex(s => s.id === step.id);
  107. const nextStep = onboardingSteps[currentStepIndex + 1];
  108. if (nextStep.id === 'setup-docs' && !platform) {
  109. return;
  110. }
  111. if (step.cornerVariant !== nextStep.cornerVariant) {
  112. cornerVariantControl.start('none');
  113. }
  114. props.router.push(normalizeUrl(`/onboarding/${organization.slug}/${nextStep.id}/`));
  115. },
  116. [organization.slug, onboardingSteps, cornerVariantControl, props.router]
  117. );
  118. // TODO(Priscila): will tackle this in a follow-up PR
  119. // const deleteProject = useCallback(
  120. // async (projectSlug: string) => {
  121. // try {
  122. // await removeProject(api, organization.slug, projectSlug);
  123. // } catch (error) {
  124. // handleXhrErrorResponse(t('Unable to delete project'))(error);
  125. // // we don't give the user any feedback regarding this error as this shall be silent
  126. // }
  127. // },
  128. // [api, organization.slug]
  129. // );
  130. const handleGoBack = useCallback(() => {
  131. if (!stepObj) {
  132. return;
  133. }
  134. const previousStep = onboardingSteps[stepIndex - 1];
  135. if (!previousStep) {
  136. return;
  137. }
  138. if (stepObj.cornerVariant !== previousStep.cornerVariant) {
  139. cornerVariantControl.start('none');
  140. }
  141. trackAnalytics('onboarding.back_button_clicked', {
  142. organization,
  143. from: onboardingSteps[stepIndex].id,
  144. to: previousStep.id,
  145. });
  146. // from selected platform to welcome
  147. if (onboardingSteps[stepIndex].id === 'select-platform') {
  148. onboardingContext.setData({...onboardingContext.data, selectedSDK: undefined});
  149. }
  150. // from setup docs to selected platform
  151. if (onboardingSteps[stepIndex].id === 'setup-docs' && projectDeletionOnBackClick) {
  152. if (!onboardingContext.data.selectedSDK) {
  153. return;
  154. }
  155. const newProjects = Object.keys(onboardingContext.data.projects).reduce(
  156. (acc, key) => {
  157. if (key !== onboardingContext.data.selectedSDK?.key) {
  158. acc[key] = onboardingContext.data.projects[key];
  159. }
  160. return acc;
  161. },
  162. {}
  163. );
  164. onboardingContext.setData({
  165. ...onboardingContext.data,
  166. projects: newProjects,
  167. });
  168. }
  169. props.router.replace(
  170. normalizeUrl(`/onboarding/${organization.slug}/${previousStep.id}/`)
  171. );
  172. }, [
  173. stepObj,
  174. stepIndex,
  175. onboardingSteps,
  176. organization,
  177. cornerVariantControl,
  178. props.router,
  179. projectDeletionOnBackClick,
  180. onboardingContext,
  181. ]);
  182. const genSkipOnboardingLink = () => {
  183. const source = `targeted-onboarding-${stepId}`;
  184. return (
  185. <SkipOnboardingLink
  186. onClick={() => {
  187. trackAnalytics('growth.onboarding_clicked_skip', {
  188. organization,
  189. source,
  190. });
  191. onboardingContext.setData({...onboardingContext.data, selectedSDK: undefined});
  192. }}
  193. to={normalizeUrl(
  194. `/organizations/${organization.slug}/issues/?referrer=onboarding-skip`
  195. )}
  196. >
  197. {t('Skip Onboarding')}
  198. </SkipOnboardingLink>
  199. );
  200. };
  201. if (!stepObj || stepIndex === -1) {
  202. return (
  203. <Redirect
  204. to={normalizeUrl(`/onboarding/${organization.slug}/${onboardingSteps[0].id}/`)}
  205. />
  206. );
  207. }
  208. return (
  209. <OnboardingWrapper data-test-id="targeted-onboarding">
  210. <SentryDocumentTitle title={stepObj.title} />
  211. <Header>
  212. <LogoSvg />
  213. {stepIndex !== -1 && (
  214. <StyledStepper
  215. numSteps={onboardingSteps.length}
  216. currentStepIndex={stepIndex}
  217. onClick={i => goToStep(onboardingSteps[i])}
  218. />
  219. )}
  220. <UpsellWrapper>
  221. <Hook
  222. name="onboarding:targeted-onboarding-header"
  223. source="targeted-onboarding"
  224. />
  225. </UpsellWrapper>
  226. </Header>
  227. <Container hasFooter={containerHasFooter} heartbeatFooter={heartbeatFooter}>
  228. <Back animate={stepIndex > 0 ? 'visible' : 'hidden'} onClick={handleGoBack} />
  229. <AnimatePresence exitBeforeEnter onExitComplete={updateAnimationState}>
  230. <OnboardingStep key={stepObj.id} data-test-id={`onboarding-step-${stepObj.id}`}>
  231. {stepObj.Component && (
  232. <stepObj.Component
  233. active
  234. data-test-id={`onboarding-step-${stepObj.id}`}
  235. stepIndex={stepIndex}
  236. onComplete={platform => {
  237. if (stepObj) {
  238. goNextStep(stepObj, platform);
  239. }
  240. }}
  241. orgId={organization.slug}
  242. search={props.location.search}
  243. route={props.route}
  244. router={props.router}
  245. location={props.location}
  246. selectedProjectSlug={selectedProjectSlug}
  247. {...{
  248. genSkipOnboardingLink,
  249. }}
  250. />
  251. )}
  252. </OnboardingStep>
  253. </AnimatePresence>
  254. <AdaptivePageCorners animateVariant={cornerVariantControl} />
  255. </Container>
  256. </OnboardingWrapper>
  257. );
  258. }
  259. const Container = styled('div')<{hasFooter: boolean; heartbeatFooter: boolean}>`
  260. flex-grow: 1;
  261. display: flex;
  262. flex-direction: column;
  263. position: relative;
  264. background: ${p => p.theme.background};
  265. padding: ${p =>
  266. p.heartbeatFooter ? `120px ${space(3)} 0 ${space(3)}` : `120px ${space(3)}`};
  267. width: 100%;
  268. margin: 0 auto;
  269. padding-bottom: ${p => p.hasFooter && '72px'};
  270. margin-bottom: ${p => p.hasFooter && '72px'};
  271. `;
  272. const Header = styled('header')`
  273. background: ${p => p.theme.background};
  274. padding-left: ${space(4)};
  275. padding-right: ${space(4)};
  276. position: sticky;
  277. height: 80px;
  278. align-items: center;
  279. top: 0;
  280. z-index: 100;
  281. box-shadow: 0 5px 10px rgba(0, 0, 0, 0.05);
  282. display: grid;
  283. grid-template-columns: 1fr 1fr 1fr;
  284. justify-items: stretch;
  285. `;
  286. const LogoSvg = styled(LogoSentry)`
  287. width: 130px;
  288. height: 30px;
  289. color: ${p => p.theme.textColor};
  290. `;
  291. const OnboardingStep = styled(motion.div)`
  292. flex-grow: 1;
  293. display: flex;
  294. flex-direction: column;
  295. `;
  296. OnboardingStep.defaultProps = {
  297. initial: 'initial',
  298. animate: 'animate',
  299. exit: 'exit',
  300. variants: {animate: {}},
  301. transition: testableTransition({
  302. staggerChildren: 0.2,
  303. }),
  304. };
  305. const Sidebar = styled(motion.div)`
  306. width: 850px;
  307. display: flex;
  308. flex-direction: column;
  309. align-items: center;
  310. `;
  311. Sidebar.defaultProps = {
  312. initial: 'initial',
  313. animate: 'animate',
  314. exit: 'exit',
  315. variants: {animate: {}},
  316. transition: testableTransition({
  317. staggerChildren: 0.2,
  318. }),
  319. };
  320. const AdaptivePageCorners = styled(PageCorners)`
  321. --corner-scale: 1;
  322. @media (max-width: ${p => p.theme.breakpoints.small}) {
  323. --corner-scale: 0.5;
  324. }
  325. `;
  326. const StyledStepper = styled(Stepper)`
  327. justify-self: center;
  328. @media (max-width: ${p => p.theme.breakpoints.medium}) {
  329. display: none;
  330. }
  331. `;
  332. interface BackButtonProps extends Omit<ButtonProps, 'icon' | 'priority'> {
  333. animate: MotionProps['animate'];
  334. className?: string;
  335. }
  336. const Back = styled(({className, animate, ...props}: BackButtonProps) => (
  337. <motion.div
  338. className={className}
  339. animate={animate}
  340. transition={testableTransition()}
  341. variants={{
  342. initial: {opacity: 0, visibility: 'hidden'},
  343. visible: {
  344. opacity: 1,
  345. visibility: 'visible',
  346. transition: testableTransition({delay: 1}),
  347. },
  348. hidden: {
  349. opacity: 0,
  350. transitionEnd: {
  351. visibility: 'hidden',
  352. },
  353. },
  354. }}
  355. >
  356. <Button {...props} icon={<IconArrow direction="left" size="sm" />} priority="link">
  357. {t('Back')}
  358. </Button>
  359. </motion.div>
  360. ))`
  361. position: absolute;
  362. top: 40px;
  363. left: 20px;
  364. button {
  365. font-size: ${p => p.theme.fontSizeSmall};
  366. }
  367. `;
  368. const SkipOnboardingLink = styled(Link)`
  369. margin: auto ${space(4)};
  370. `;
  371. const UpsellWrapper = styled('div')`
  372. grid-column: 3;
  373. margin-left: auto;
  374. `;
  375. const OnboardingWrapper = styled('main')`
  376. flex-grow: 1;
  377. display: flex;
  378. flex-direction: column;
  379. `;
  380. export default Onboarding;