onboarding.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 projectDeletionOnBackClick = !!organization?.features.includes(
  125. 'onboarding-project-deletion-on-back-click'
  126. );
  127. const shallProjectBeDeleted =
  128. projectDeletionOnBackClick &&
  129. onboardingSteps[stepIndex].id === 'setup-docs' &&
  130. recentCreatedProject &&
  131. // if the project has received a first error, we don't delete it
  132. recentCreatedProject.firstError === false &&
  133. // if the project has received a first transaction, we don't delete it
  134. recentCreatedProject.firstTransaction === false &&
  135. // if the project has replays, we don't delete it
  136. recentCreatedProject.hasReplays === false &&
  137. // if the project has sessions, we don't delete it
  138. recentCreatedProject.hasSessions === false &&
  139. // if the project is older than one hour, we don't delete it
  140. recentCreatedProject.olderThanOneHour === false;
  141. const cornerVariantControl = useAnimation();
  142. const updateCornerVariant = () => {
  143. // TODO: find better way to delay the corner animation
  144. window.clearTimeout(cornerVariantTimeoutRed.current);
  145. cornerVariantTimeoutRed.current = window.setTimeout(
  146. () => cornerVariantControl.start(stepIndex === 0 ? 'top-right' : 'top-left'),
  147. 1000
  148. );
  149. };
  150. useEffect(updateCornerVariant, [stepIndex, cornerVariantControl]);
  151. // Called onExitComplete
  152. const [containerHasFooter, setContainerHasFooter] = useState<boolean>(false);
  153. const updateAnimationState = () => {
  154. if (!stepObj) {
  155. return;
  156. }
  157. setContainerHasFooter(stepObj.hasFooter ?? false);
  158. };
  159. const goToStep = (step: StepDescriptor) => {
  160. if (!stepObj) {
  161. return;
  162. }
  163. if (step.cornerVariant !== stepObj.cornerVariant) {
  164. cornerVariantControl.start('none');
  165. }
  166. props.router.push(normalizeUrl(`/onboarding/${organization.slug}/${step.id}/`));
  167. };
  168. const goNextStep = useCallback(
  169. (step: StepDescriptor, platform?: OnboardingSelectedSDK) => {
  170. const currentStepIndex = onboardingSteps.findIndex(s => s.id === step.id);
  171. const nextStep = onboardingSteps[currentStepIndex + 1];
  172. if (nextStep.id === 'setup-docs' && !platform) {
  173. return;
  174. }
  175. if (step.cornerVariant !== nextStep.cornerVariant) {
  176. cornerVariantControl.start('none');
  177. }
  178. props.router.push(normalizeUrl(`/onboarding/${organization.slug}/${nextStep.id}/`));
  179. },
  180. [organization.slug, onboardingSteps, cornerVariantControl, props.router]
  181. );
  182. const deleteRecentCreatedProject = useCallback(async () => {
  183. if (!recentCreatedProject?.slug) {
  184. return;
  185. }
  186. const newProjects = Object.keys(onboardingContext.data.projects).reduce(
  187. (acc, key) => {
  188. if (
  189. onboardingContext.data.projects[key].slug !==
  190. onboardingContext.data.selectedSDK?.key
  191. ) {
  192. acc[key] = onboardingContext.data.projects[key];
  193. }
  194. return acc;
  195. },
  196. {}
  197. );
  198. try {
  199. await removeProject({
  200. api,
  201. orgSlug: organization.slug,
  202. projectSlug: recentCreatedProject.slug,
  203. origin: 'onboarding',
  204. });
  205. onboardingContext.setData({
  206. ...onboardingContext.data,
  207. projects: newProjects,
  208. });
  209. trackAnalytics('onboarding.data_removed', {
  210. organization,
  211. date_created: recentCreatedProject.dateCreated,
  212. platform: recentCreatedProject.slug,
  213. project_id: recentCreatedProject.id,
  214. });
  215. } catch (error) {
  216. handleXhrErrorResponse('Unable to delete project in onboarding', error);
  217. // we don't give the user any feedback regarding this error as this shall be silent
  218. }
  219. }, [api, organization, recentCreatedProject, onboardingContext]);
  220. const handleGoBack = useCallback(
  221. (goToStepIndex?: number) => {
  222. if (!stepObj) {
  223. return;
  224. }
  225. const previousStep = defined(goToStepIndex)
  226. ? onboardingSteps[goToStepIndex]
  227. : onboardingSteps[stepIndex - 1];
  228. if (!previousStep) {
  229. return;
  230. }
  231. if (stepObj.cornerVariant !== previousStep.cornerVariant) {
  232. cornerVariantControl.start('none');
  233. }
  234. trackAnalytics('onboarding.back_button_clicked', {
  235. organization,
  236. from: onboardingSteps[stepIndex].id,
  237. to: previousStep.id,
  238. });
  239. // from selected platform to welcome
  240. if (onboardingSteps[stepIndex].id === 'select-platform') {
  241. onboardingContext.setData({...onboardingContext.data, selectedSDK: undefined});
  242. props.router.replace(
  243. normalizeUrl(`/onboarding/${organization.slug}/${previousStep.id}/`)
  244. );
  245. return;
  246. }
  247. // from setup docs to selected platform
  248. if (onboardingSteps[stepIndex].id === 'setup-docs' && shallProjectBeDeleted) {
  249. trackAnalytics('onboarding.data_removal_modal_confirm_button_clicked', {
  250. organization,
  251. platform: recentCreatedProject.slug,
  252. project_id: recentCreatedProject.id,
  253. });
  254. deleteRecentCreatedProject();
  255. }
  256. props.router.replace(
  257. normalizeUrl(`/onboarding/${organization.slug}/${previousStep.id}/`)
  258. );
  259. },
  260. [
  261. stepObj,
  262. stepIndex,
  263. onboardingSteps,
  264. organization,
  265. cornerVariantControl,
  266. props.router,
  267. onboardingContext,
  268. shallProjectBeDeleted,
  269. deleteRecentCreatedProject,
  270. recentCreatedProject,
  271. ]
  272. );
  273. const genSkipOnboardingLink = () => {
  274. const source = `targeted-onboarding-${stepId}`;
  275. return (
  276. <SkipOnboardingLink
  277. onClick={() => {
  278. trackAnalytics('growth.onboarding_clicked_skip', {
  279. organization,
  280. source,
  281. });
  282. onboardingContext.setData({...onboardingContext.data, selectedSDK: undefined});
  283. }}
  284. to={normalizeUrl(
  285. `/organizations/${organization.slug}/issues/?referrer=onboarding-skip`
  286. )}
  287. >
  288. {t('Skip Onboarding')}
  289. </SkipOnboardingLink>
  290. );
  291. };
  292. if (!stepObj || stepIndex === -1) {
  293. return (
  294. <Redirect
  295. to={normalizeUrl(`/onboarding/${organization.slug}/${onboardingSteps[0].id}/`)}
  296. />
  297. );
  298. }
  299. const goBackDeletionAlertModalProps: OpenConfirmOptions = {
  300. message: t(
  301. "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?"
  302. ),
  303. priority: 'danger',
  304. confirmText: t("Yes I'm sure"),
  305. onConfirm: handleGoBack,
  306. onClose: () => {
  307. if (!recentCreatedProject) {
  308. return;
  309. }
  310. trackAnalytics('onboarding.data_removal_modal_dismissed', {
  311. organization,
  312. platform: recentCreatedProject.slug,
  313. project_id: recentCreatedProject.id,
  314. });
  315. },
  316. onRender: () => {
  317. if (!recentCreatedProject) {
  318. return;
  319. }
  320. trackAnalytics('onboarding.data_removal_modal_rendered', {
  321. organization,
  322. platform: recentCreatedProject.slug,
  323. project_id: recentCreatedProject.id,
  324. });
  325. },
  326. };
  327. return (
  328. <OnboardingWrapper data-test-id="targeted-onboarding">
  329. <SentryDocumentTitle title={stepObj.title} />
  330. <Header>
  331. <LogoSvg />
  332. {stepIndex !== -1 && (
  333. <StyledStepper
  334. numSteps={onboardingSteps.length}
  335. currentStepIndex={stepIndex}
  336. onClick={i => {
  337. if (i < stepIndex && shallProjectBeDeleted) {
  338. openConfirmModal({
  339. ...goBackDeletionAlertModalProps,
  340. onConfirm: () => handleGoBack(i),
  341. });
  342. return;
  343. }
  344. goToStep(onboardingSteps[i]);
  345. }}
  346. />
  347. )}
  348. <UpsellWrapper>
  349. <Hook
  350. name="onboarding:targeted-onboarding-header"
  351. source="targeted-onboarding"
  352. />
  353. </UpsellWrapper>
  354. </Header>
  355. <Container hasFooter={containerHasFooter}>
  356. <Confirm bypass={!shallProjectBeDeleted} {...goBackDeletionAlertModalProps}>
  357. <Back animate={stepIndex > 0 ? 'visible' : 'hidden'} />
  358. </Confirm>
  359. <AnimatePresence exitBeforeEnter onExitComplete={updateAnimationState}>
  360. <OnboardingStep key={stepObj.id} data-test-id={`onboarding-step-${stepObj.id}`}>
  361. {stepObj.Component && (
  362. <stepObj.Component
  363. active
  364. data-test-id={`onboarding-step-${stepObj.id}`}
  365. stepIndex={stepIndex}
  366. onComplete={platform => {
  367. if (stepObj) {
  368. goNextStep(stepObj, platform);
  369. }
  370. }}
  371. orgId={organization.slug}
  372. search={props.location.search}
  373. route={props.route}
  374. router={props.router}
  375. location={props.location}
  376. recentCreatedProject={recentCreatedProject}
  377. {...{
  378. genSkipOnboardingLink,
  379. }}
  380. />
  381. )}
  382. </OnboardingStep>
  383. </AnimatePresence>
  384. <AdaptivePageCorners animateVariant={cornerVariantControl} />
  385. </Container>
  386. </OnboardingWrapper>
  387. );
  388. }
  389. const Container = styled('div')<{hasFooter: boolean}>`
  390. flex-grow: 1;
  391. display: flex;
  392. flex-direction: column;
  393. position: relative;
  394. background: ${p => p.theme.background};
  395. padding: 120px ${space(3)};
  396. width: 100%;
  397. margin: 0 auto;
  398. padding-bottom: ${p => p.hasFooter && '72px'};
  399. margin-bottom: ${p => p.hasFooter && '72px'};
  400. `;
  401. const Header = styled('header')`
  402. background: ${p => p.theme.background};
  403. padding-left: ${space(4)};
  404. padding-right: ${space(4)};
  405. position: sticky;
  406. height: 80px;
  407. align-items: center;
  408. top: 0;
  409. z-index: 100;
  410. box-shadow: 0 5px 10px rgba(0, 0, 0, 0.05);
  411. display: grid;
  412. grid-template-columns: 1fr 1fr 1fr;
  413. justify-items: stretch;
  414. `;
  415. const LogoSvg = styled(LogoSentry)`
  416. width: 130px;
  417. height: 30px;
  418. color: ${p => p.theme.textColor};
  419. `;
  420. const OnboardingStep = styled(motion.div)`
  421. flex-grow: 1;
  422. display: flex;
  423. flex-direction: column;
  424. `;
  425. OnboardingStep.defaultProps = {
  426. initial: 'initial',
  427. animate: 'animate',
  428. exit: 'exit',
  429. variants: {animate: {}},
  430. transition: testableTransition({
  431. staggerChildren: 0.2,
  432. }),
  433. };
  434. const Sidebar = styled(motion.div)`
  435. width: 850px;
  436. display: flex;
  437. flex-direction: column;
  438. align-items: center;
  439. `;
  440. Sidebar.defaultProps = {
  441. initial: 'initial',
  442. animate: 'animate',
  443. exit: 'exit',
  444. variants: {animate: {}},
  445. transition: testableTransition({
  446. staggerChildren: 0.2,
  447. }),
  448. };
  449. const AdaptivePageCorners = styled(PageCorners)`
  450. --corner-scale: 1;
  451. @media (max-width: ${p => p.theme.breakpoints.small}) {
  452. --corner-scale: 0.5;
  453. }
  454. `;
  455. const StyledStepper = styled(Stepper)`
  456. justify-self: center;
  457. @media (max-width: ${p => p.theme.breakpoints.medium}) {
  458. display: none;
  459. }
  460. `;
  461. interface BackButtonProps extends Omit<ButtonProps, 'icon' | 'priority'> {
  462. animate: MotionProps['animate'];
  463. className?: string;
  464. }
  465. const Back = styled(({className, animate, ...props}: BackButtonProps) => (
  466. <motion.div
  467. className={className}
  468. animate={animate}
  469. transition={testableTransition()}
  470. variants={{
  471. initial: {opacity: 0, visibility: 'hidden'},
  472. visible: {
  473. opacity: 1,
  474. visibility: 'visible',
  475. transition: testableTransition({delay: 1}),
  476. },
  477. hidden: {
  478. opacity: 0,
  479. transitionEnd: {
  480. visibility: 'hidden',
  481. },
  482. },
  483. }}
  484. >
  485. <Button {...props} icon={<IconArrow direction="left" size="sm" />} priority="link">
  486. {t('Back')}
  487. </Button>
  488. </motion.div>
  489. ))`
  490. position: absolute;
  491. top: 40px;
  492. left: 20px;
  493. button {
  494. font-size: ${p => p.theme.fontSizeSmall};
  495. }
  496. `;
  497. const SkipOnboardingLink = styled(Link)`
  498. margin: auto ${space(4)};
  499. `;
  500. const UpsellWrapper = styled('div')`
  501. grid-column: 3;
  502. margin-left: auto;
  503. `;
  504. const OnboardingWrapper = styled('main')`
  505. flex-grow: 1;
  506. display: flex;
  507. flex-direction: column;
  508. `;
  509. export default Onboarding;