onboarding.tsx 16 KB

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