onboarding.tsx 16 KB

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