onboarding.tsx 17 KB

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