onboarding.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 {removeProject} from 'sentry/actionCreators/projects';
  6. import {Button, ButtonProps} from 'sentry/components/button';
  7. import Confirm, {openConfirmModal, OpenConfirmOptions} from 'sentry/components/confirm';
  8. import Hook from 'sentry/components/hook';
  9. import Link from 'sentry/components/links/link';
  10. import LogoSentry from 'sentry/components/logoSentry';
  11. import {OnboardingContext} from 'sentry/components/onboarding/onboardingContext';
  12. import {useRecentCreatedProject} from 'sentry/components/onboarding/useRecentCreatedProject';
  13. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  14. import categoryList from 'sentry/data/platformCategories';
  15. import platforms from 'sentry/data/platforms';
  16. import {IconArrow} from 'sentry/icons';
  17. import {t} from 'sentry/locale';
  18. import {space} from 'sentry/styles/space';
  19. import {OnboardingSelectedSDK} from 'sentry/types';
  20. import {defined} from 'sentry/utils';
  21. import {trackAnalytics} from 'sentry/utils/analytics';
  22. import {handleXhrErrorResponse} from 'sentry/utils/handleXhrErrorResponse';
  23. import Redirect from 'sentry/utils/redirect';
  24. import testableTransition from 'sentry/utils/testableTransition';
  25. import useApi from 'sentry/utils/useApi';
  26. import useOrganization from 'sentry/utils/useOrganization';
  27. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  28. import PageCorners from 'sentry/views/onboarding/components/pageCorners';
  29. import Stepper from './components/stepper';
  30. import {PlatformSelection} from './platformSelection';
  31. import SetupDocs from './setupDocs';
  32. import {StepDescriptor} from './types';
  33. import TargetedOnboardingWelcome from './welcome';
  34. type RouteParams = {
  35. step: string;
  36. };
  37. type Props = RouteComponentProps<RouteParams, {}>;
  38. function getOrganizationOnboardingSteps(): StepDescriptor[] {
  39. return [
  40. {
  41. id: 'welcome',
  42. title: t('Welcome'),
  43. Component: TargetedOnboardingWelcome,
  44. cornerVariant: 'top-right',
  45. },
  46. {
  47. id: 'select-platform',
  48. title: t('Select platform'),
  49. Component: PlatformSelection,
  50. hasFooter: true,
  51. cornerVariant: 'top-left',
  52. },
  53. {
  54. id: 'setup-docs',
  55. title: t('Install the Sentry SDK'),
  56. Component: SetupDocs,
  57. hasFooter: true,
  58. cornerVariant: 'top-left',
  59. },
  60. ];
  61. }
  62. function Onboarding(props: Props) {
  63. const api = useApi();
  64. const organization = useOrganization();
  65. const onboardingContext = useContext(OnboardingContext);
  66. const selectedSDK = onboardingContext.data.selectedSDK;
  67. const selectedProjectSlug = selectedSDK?.key;
  68. const {
  69. params: {step: stepId},
  70. } = props;
  71. const onboardingSteps = getOrganizationOnboardingSteps();
  72. const stepObj = onboardingSteps.find(({id}) => stepId === id);
  73. const stepIndex = onboardingSteps.findIndex(({id}) => stepId === id);
  74. const recentCreatedProject = useRecentCreatedProject({
  75. orgSlug: organization.slug,
  76. projectSlug:
  77. onboardingSteps[stepIndex].id === 'setup-docs' ? selectedProjectSlug : undefined,
  78. });
  79. const cornerVariantTimeoutRed = useRef<number | undefined>(undefined);
  80. useEffect(() => {
  81. return () => {
  82. window.clearTimeout(cornerVariantTimeoutRed.current);
  83. };
  84. }, []);
  85. useEffect(() => {
  86. if (
  87. props.location.pathname === `/onboarding/${onboardingSteps[2].id}/` &&
  88. props.location.query?.platform &&
  89. onboardingContext.data.selectedSDK === undefined
  90. ) {
  91. const platformKey = Object.keys(platforms).find(
  92. key => platforms[key].id === props.location.query.platform
  93. );
  94. const platform = platformKey ? platforms[platformKey] : undefined;
  95. // if no platform found, we redirect the user to the platform select page
  96. if (!platform) {
  97. props.router.push(
  98. normalizeUrl(`/onboarding/${organization.slug}/${onboardingSteps[1].id}/`)
  99. );
  100. return;
  101. }
  102. const frameworkCategory =
  103. categoryList.find(category => {
  104. return category.platforms.includes(platform.id as never);
  105. })?.id ?? 'all';
  106. onboardingContext.setData({
  107. ...onboardingContext.data,
  108. selectedSDK: {
  109. key: props.location.query.platform,
  110. category: frameworkCategory,
  111. language: platform.language,
  112. type: platform.type,
  113. },
  114. });
  115. }
  116. }, [
  117. props.location.query,
  118. props.router,
  119. onboardingContext,
  120. onboardingSteps,
  121. organization.slug,
  122. props.location.pathname,
  123. ]);
  124. const heartbeatFooter = !!organization?.features.includes(
  125. 'onboarding-heartbeat-footer'
  126. );
  127. const projectDeletionOnBackClick = !!organization?.features.includes(
  128. 'onboarding-project-deletion-on-back-click'
  129. );
  130. const shallProjectBeDeleted =
  131. projectDeletionOnBackClick &&
  132. onboardingSteps[stepIndex].id === 'setup-docs' &&
  133. recentCreatedProject &&
  134. // if the project has received a first error, we don't delete it
  135. recentCreatedProject.firstError === false &&
  136. // if the project has received a first transaction, we don't delete it
  137. recentCreatedProject.firstTransaction === false &&
  138. // if the project has replays, we don't delete it
  139. recentCreatedProject.hasReplays === false &&
  140. // if the project has sessions, we don't delete it
  141. recentCreatedProject.hasSessions === false &&
  142. // if the project is older than one hour, we don't delete it
  143. recentCreatedProject.olderThanOneHour === false;
  144. const cornerVariantControl = useAnimation();
  145. const updateCornerVariant = () => {
  146. // TODO: find better way to delay the corner animation
  147. window.clearTimeout(cornerVariantTimeoutRed.current);
  148. cornerVariantTimeoutRed.current = window.setTimeout(
  149. () => cornerVariantControl.start(stepIndex === 0 ? 'top-right' : 'top-left'),
  150. 1000
  151. );
  152. };
  153. useEffect(updateCornerVariant, [stepIndex, cornerVariantControl]);
  154. // Called onExitComplete
  155. const [containerHasFooter, setContainerHasFooter] = useState<boolean>(false);
  156. const updateAnimationState = () => {
  157. if (!stepObj) {
  158. return;
  159. }
  160. setContainerHasFooter(stepObj.hasFooter ?? false);
  161. };
  162. const goToStep = (step: StepDescriptor) => {
  163. if (!stepObj) {
  164. return;
  165. }
  166. if (step.cornerVariant !== stepObj.cornerVariant) {
  167. cornerVariantControl.start('none');
  168. }
  169. props.router.push(normalizeUrl(`/onboarding/${organization.slug}/${step.id}/`));
  170. };
  171. const goNextStep = useCallback(
  172. (step: StepDescriptor, platform?: OnboardingSelectedSDK) => {
  173. const currentStepIndex = onboardingSteps.findIndex(s => s.id === step.id);
  174. const nextStep = onboardingSteps[currentStepIndex + 1];
  175. if (nextStep.id === 'setup-docs' && !platform) {
  176. return;
  177. }
  178. if (step.cornerVariant !== nextStep.cornerVariant) {
  179. cornerVariantControl.start('none');
  180. }
  181. props.router.push(normalizeUrl(`/onboarding/${organization.slug}/${nextStep.id}/`));
  182. },
  183. [organization.slug, onboardingSteps, cornerVariantControl, props.router]
  184. );
  185. const deleteRecentCreatedProject = useCallback(async () => {
  186. if (!recentCreatedProject?.slug) {
  187. return;
  188. }
  189. const newProjects = Object.keys(onboardingContext.data.projects).reduce(
  190. (acc, key) => {
  191. if (
  192. onboardingContext.data.projects[key].slug !==
  193. onboardingContext.data.selectedSDK?.key
  194. ) {
  195. acc[key] = onboardingContext.data.projects[key];
  196. }
  197. return acc;
  198. },
  199. {}
  200. );
  201. try {
  202. await removeProject({
  203. api,
  204. orgSlug: organization.slug,
  205. projectSlug: recentCreatedProject.slug,
  206. origin: 'onboarding',
  207. });
  208. onboardingContext.setData({
  209. ...onboardingContext.data,
  210. projects: newProjects,
  211. });
  212. trackAnalytics('onboarding.data_removed', {
  213. organization,
  214. date_created: recentCreatedProject.dateCreated,
  215. platform: recentCreatedProject.slug,
  216. project_id: recentCreatedProject.id,
  217. });
  218. } catch (error) {
  219. handleXhrErrorResponse('Unable to delete project in onboarding', error);
  220. // we don't give the user any feedback regarding this error as this shall be silent
  221. }
  222. }, [api, organization, recentCreatedProject, onboardingContext]);
  223. const handleGoBack = useCallback(
  224. (goToStepIndex?: number) => {
  225. if (!stepObj) {
  226. return;
  227. }
  228. const previousStep = defined(goToStepIndex)
  229. ? onboardingSteps[goToStepIndex]
  230. : onboardingSteps[stepIndex - 1];
  231. if (!previousStep) {
  232. return;
  233. }
  234. if (stepObj.cornerVariant !== previousStep.cornerVariant) {
  235. cornerVariantControl.start('none');
  236. }
  237. trackAnalytics('onboarding.back_button_clicked', {
  238. organization,
  239. from: onboardingSteps[stepIndex].id,
  240. to: previousStep.id,
  241. });
  242. // from selected platform to welcome
  243. if (onboardingSteps[stepIndex].id === 'select-platform') {
  244. onboardingContext.setData({...onboardingContext.data, selectedSDK: undefined});
  245. props.router.replace(
  246. normalizeUrl(`/onboarding/${organization.slug}/${previousStep.id}/`)
  247. );
  248. return;
  249. }
  250. // from setup docs to selected platform
  251. if (onboardingSteps[stepIndex].id === 'setup-docs' && shallProjectBeDeleted) {
  252. trackAnalytics('onboarding.data_removal_modal_confirm_button_clicked', {
  253. organization,
  254. platform: recentCreatedProject.slug,
  255. project_id: recentCreatedProject.id,
  256. });
  257. deleteRecentCreatedProject();
  258. }
  259. props.router.replace(
  260. normalizeUrl(`/onboarding/${organization.slug}/${previousStep.id}/`)
  261. );
  262. },
  263. [
  264. stepObj,
  265. stepIndex,
  266. onboardingSteps,
  267. organization,
  268. cornerVariantControl,
  269. props.router,
  270. onboardingContext,
  271. shallProjectBeDeleted,
  272. deleteRecentCreatedProject,
  273. recentCreatedProject,
  274. ]
  275. );
  276. const genSkipOnboardingLink = () => {
  277. const source = `targeted-onboarding-${stepId}`;
  278. return (
  279. <SkipOnboardingLink
  280. onClick={() => {
  281. trackAnalytics('growth.onboarding_clicked_skip', {
  282. organization,
  283. source,
  284. });
  285. onboardingContext.setData({...onboardingContext.data, selectedSDK: undefined});
  286. }}
  287. to={normalizeUrl(
  288. `/organizations/${organization.slug}/issues/?referrer=onboarding-skip`
  289. )}
  290. >
  291. {t('Skip Onboarding')}
  292. </SkipOnboardingLink>
  293. );
  294. };
  295. if (!stepObj || stepIndex === -1) {
  296. return (
  297. <Redirect
  298. to={normalizeUrl(`/onboarding/${organization.slug}/${onboardingSteps[0].id}/`)}
  299. />
  300. );
  301. }
  302. const goBackDeletionAlertModalProps: OpenConfirmOptions = {
  303. message: t(
  304. "Hey, just a heads up - we haven't received any data for this SDK yet and by going back all changes will be discarded. Are you sure you want to head back?"
  305. ),
  306. priority: 'danger',
  307. confirmText: t("Yes I'm sure"),
  308. onConfirm: handleGoBack,
  309. onClose: () => {
  310. if (!recentCreatedProject) {
  311. return;
  312. }
  313. trackAnalytics('onboarding.data_removal_modal_dismissed', {
  314. organization,
  315. platform: recentCreatedProject.slug,
  316. project_id: recentCreatedProject.id,
  317. });
  318. },
  319. onRender: () => {
  320. if (!recentCreatedProject) {
  321. return;
  322. }
  323. trackAnalytics('onboarding.data_removal_modal_rendered', {
  324. organization,
  325. platform: recentCreatedProject.slug,
  326. project_id: recentCreatedProject.id,
  327. });
  328. },
  329. };
  330. return (
  331. <OnboardingWrapper data-test-id="targeted-onboarding">
  332. <SentryDocumentTitle title={stepObj.title} />
  333. <Header>
  334. <LogoSvg />
  335. {stepIndex !== -1 && (
  336. <StyledStepper
  337. numSteps={onboardingSteps.length}
  338. currentStepIndex={stepIndex}
  339. onClick={i => {
  340. if (i < stepIndex && shallProjectBeDeleted) {
  341. openConfirmModal({
  342. ...goBackDeletionAlertModalProps,
  343. onConfirm: () => handleGoBack(i),
  344. });
  345. return;
  346. }
  347. goToStep(onboardingSteps[i]);
  348. }}
  349. />
  350. )}
  351. <UpsellWrapper>
  352. <Hook
  353. name="onboarding:targeted-onboarding-header"
  354. source="targeted-onboarding"
  355. />
  356. </UpsellWrapper>
  357. </Header>
  358. <Container hasFooter={containerHasFooter} heartbeatFooter={heartbeatFooter}>
  359. <Confirm bypass={!shallProjectBeDeleted} {...goBackDeletionAlertModalProps}>
  360. <Back animate={stepIndex > 0 ? 'visible' : 'hidden'} />
  361. </Confirm>
  362. <AnimatePresence exitBeforeEnter onExitComplete={updateAnimationState}>
  363. <OnboardingStep key={stepObj.id} data-test-id={`onboarding-step-${stepObj.id}`}>
  364. {stepObj.Component && (
  365. <stepObj.Component
  366. active
  367. data-test-id={`onboarding-step-${stepObj.id}`}
  368. stepIndex={stepIndex}
  369. onComplete={platform => {
  370. if (stepObj) {
  371. goNextStep(stepObj, platform);
  372. }
  373. }}
  374. orgId={organization.slug}
  375. search={props.location.search}
  376. route={props.route}
  377. router={props.router}
  378. location={props.location}
  379. recentCreatedProject={recentCreatedProject}
  380. {...{
  381. genSkipOnboardingLink,
  382. }}
  383. />
  384. )}
  385. </OnboardingStep>
  386. </AnimatePresence>
  387. <AdaptivePageCorners animateVariant={cornerVariantControl} />
  388. </Container>
  389. </OnboardingWrapper>
  390. );
  391. }
  392. const Container = styled('div')<{hasFooter: boolean; heartbeatFooter: boolean}>`
  393. flex-grow: 1;
  394. display: flex;
  395. flex-direction: column;
  396. position: relative;
  397. background: ${p => p.theme.background};
  398. padding: ${p =>
  399. p.heartbeatFooter ? `120px ${space(3)} 0 ${space(3)}` : `120px ${space(3)}`};
  400. width: 100%;
  401. margin: 0 auto;
  402. padding-bottom: ${p => p.hasFooter && '72px'};
  403. margin-bottom: ${p => p.hasFooter && '72px'};
  404. `;
  405. const Header = styled('header')`
  406. background: ${p => p.theme.background};
  407. padding-left: ${space(4)};
  408. padding-right: ${space(4)};
  409. position: sticky;
  410. height: 80px;
  411. align-items: center;
  412. top: 0;
  413. z-index: 100;
  414. box-shadow: 0 5px 10px rgba(0, 0, 0, 0.05);
  415. display: grid;
  416. grid-template-columns: 1fr 1fr 1fr;
  417. justify-items: stretch;
  418. `;
  419. const LogoSvg = styled(LogoSentry)`
  420. width: 130px;
  421. height: 30px;
  422. color: ${p => p.theme.textColor};
  423. `;
  424. const OnboardingStep = styled(motion.div)`
  425. flex-grow: 1;
  426. display: flex;
  427. flex-direction: column;
  428. `;
  429. OnboardingStep.defaultProps = {
  430. initial: 'initial',
  431. animate: 'animate',
  432. exit: 'exit',
  433. variants: {animate: {}},
  434. transition: testableTransition({
  435. staggerChildren: 0.2,
  436. }),
  437. };
  438. const Sidebar = styled(motion.div)`
  439. width: 850px;
  440. display: flex;
  441. flex-direction: column;
  442. align-items: center;
  443. `;
  444. Sidebar.defaultProps = {
  445. initial: 'initial',
  446. animate: 'animate',
  447. exit: 'exit',
  448. variants: {animate: {}},
  449. transition: testableTransition({
  450. staggerChildren: 0.2,
  451. }),
  452. };
  453. const AdaptivePageCorners = styled(PageCorners)`
  454. --corner-scale: 1;
  455. @media (max-width: ${p => p.theme.breakpoints.small}) {
  456. --corner-scale: 0.5;
  457. }
  458. `;
  459. const StyledStepper = styled(Stepper)`
  460. justify-self: center;
  461. @media (max-width: ${p => p.theme.breakpoints.medium}) {
  462. display: none;
  463. }
  464. `;
  465. interface BackButtonProps extends Omit<ButtonProps, 'icon' | 'priority'> {
  466. animate: MotionProps['animate'];
  467. className?: string;
  468. }
  469. const Back = styled(({className, animate, ...props}: BackButtonProps) => (
  470. <motion.div
  471. className={className}
  472. animate={animate}
  473. transition={testableTransition()}
  474. variants={{
  475. initial: {opacity: 0, visibility: 'hidden'},
  476. visible: {
  477. opacity: 1,
  478. visibility: 'visible',
  479. transition: testableTransition({delay: 1}),
  480. },
  481. hidden: {
  482. opacity: 0,
  483. transitionEnd: {
  484. visibility: 'hidden',
  485. },
  486. },
  487. }}
  488. >
  489. <Button {...props} icon={<IconArrow direction="left" size="sm" />} priority="link">
  490. {t('Back')}
  491. </Button>
  492. </motion.div>
  493. ))`
  494. position: absolute;
  495. top: 40px;
  496. left: 20px;
  497. button {
  498. font-size: ${p => p.theme.fontSizeSmall};
  499. }
  500. `;
  501. const SkipOnboardingLink = styled(Link)`
  502. margin: auto ${space(4)};
  503. `;
  504. const UpsellWrapper = styled('div')`
  505. grid-column: 3;
  506. margin-left: auto;
  507. `;
  508. const OnboardingWrapper = styled('main')`
  509. flex-grow: 1;
  510. display: flex;
  511. flex-direction: column;
  512. `;
  513. export default Onboarding;