onboarding.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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 Redirect from 'sentry/components/redirect';
  17. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  18. import categoryList from 'sentry/data/platformPickerCategories';
  19. import platforms from 'sentry/data/platforms';
  20. import {IconArrow} from 'sentry/icons';
  21. import {t} from 'sentry/locale';
  22. import {space} from 'sentry/styles/space';
  23. import type {OnboardingSelectedSDK} from 'sentry/types/onboarding';
  24. import {defined} from 'sentry/utils';
  25. import {trackAnalytics} from 'sentry/utils/analytics';
  26. import {handleXhrErrorResponse} from 'sentry/utils/handleXhrErrorResponse';
  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 projectSlug =
  78. stepObj && stepObj.id === 'setup-docs' ? selectedProjectSlug : undefined;
  79. const recentCreatedProject = useRecentCreatedProject({
  80. orgSlug: organization.slug,
  81. projectSlug,
  82. });
  83. const cornerVariantTimeoutRed = useRef<number | undefined>(undefined);
  84. useEffect(() => {
  85. return () => {
  86. window.clearTimeout(cornerVariantTimeoutRed.current);
  87. };
  88. }, []);
  89. useEffect(() => {
  90. if (
  91. props.location.pathname === `/onboarding/${onboardingSteps[2].id}/` &&
  92. props.location.query?.platform &&
  93. onboardingContext.data.selectedSDK === undefined
  94. ) {
  95. const platformKey = Object.keys(platforms).find(
  96. key => platforms[key].id === props.location.query.platform
  97. );
  98. const platform = platformKey ? platforms[platformKey] : undefined;
  99. // if no platform found, we redirect the user to the platform select page
  100. if (!platform) {
  101. props.router.push(
  102. normalizeUrl(`/onboarding/${organization.slug}/${onboardingSteps[1].id}/`)
  103. );
  104. return;
  105. }
  106. const frameworkCategory =
  107. categoryList.find(category => {
  108. return category.platforms?.has(platform.id);
  109. })?.id ?? 'all';
  110. onboardingContext.setData({
  111. ...onboardingContext.data,
  112. selectedSDK: {
  113. key: props.location.query.platform,
  114. category: frameworkCategory,
  115. language: platform.language,
  116. type: platform.type,
  117. },
  118. });
  119. }
  120. }, [
  121. props.location.query,
  122. props.router,
  123. onboardingContext,
  124. onboardingSteps,
  125. organization.slug,
  126. props.location.pathname,
  127. ]);
  128. const shallProjectBeDeleted =
  129. stepObj?.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. // Redirect to the first step if we end up in an invalid state
  293. const isInvalidDocsStep = stepId === 'setup-docs' && !projectSlug;
  294. if (!stepObj || stepIndex === -1 || isInvalidDocsStep) {
  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}>
  358. <Confirm bypass={!shallProjectBeDeleted} {...goBackDeletionAlertModalProps}>
  359. <Back animate={stepIndex > 0 ? 'visible' : 'hidden'} />
  360. </Confirm>
  361. <AnimatePresence mode="wait" 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}>`
  392. flex-grow: 1;
  393. display: flex;
  394. flex-direction: column;
  395. position: relative;
  396. background: ${p => p.theme.background};
  397. padding: 120px ${space(3)};
  398. width: 100%;
  399. margin: 0 auto;
  400. padding-bottom: ${p => p.hasFooter && '72px'};
  401. margin-bottom: ${p => p.hasFooter && '72px'};
  402. `;
  403. const Header = styled('header')`
  404. background: ${p => p.theme.background};
  405. padding-left: ${space(4)};
  406. padding-right: ${space(4)};
  407. position: sticky;
  408. height: 80px;
  409. align-items: center;
  410. top: 0;
  411. z-index: 100;
  412. box-shadow: 0 5px 10px rgba(0, 0, 0, 0.05);
  413. display: grid;
  414. grid-template-columns: 1fr 1fr 1fr;
  415. justify-items: stretch;
  416. `;
  417. const LogoSvg = styled(LogoSentry)`
  418. width: 130px;
  419. height: 30px;
  420. color: ${p => p.theme.textColor};
  421. `;
  422. const OnboardingStep = styled(motion.div)`
  423. flex-grow: 1;
  424. display: flex;
  425. flex-direction: column;
  426. `;
  427. OnboardingStep.defaultProps = {
  428. initial: 'initial',
  429. animate: 'animate',
  430. exit: 'exit',
  431. variants: {animate: {}},
  432. transition: testableTransition({
  433. staggerChildren: 0.2,
  434. }),
  435. };
  436. const Sidebar = styled(motion.div)`
  437. width: 850px;
  438. display: flex;
  439. flex-direction: column;
  440. align-items: center;
  441. `;
  442. Sidebar.defaultProps = {
  443. initial: 'initial',
  444. animate: 'animate',
  445. exit: 'exit',
  446. variants: {animate: {}},
  447. transition: testableTransition({
  448. staggerChildren: 0.2,
  449. }),
  450. };
  451. const AdaptivePageCorners = styled(PageCorners)`
  452. --corner-scale: 1;
  453. @media (max-width: ${p => p.theme.breakpoints.small}) {
  454. --corner-scale: 0.5;
  455. }
  456. `;
  457. const StyledStepper = styled(Stepper)`
  458. justify-self: center;
  459. @media (max-width: ${p => p.theme.breakpoints.medium}) {
  460. display: none;
  461. }
  462. `;
  463. interface BackButtonProps extends Omit<ButtonProps, 'icon' | 'priority'> {
  464. animate: MotionProps['animate'];
  465. className?: string;
  466. }
  467. const Back = styled(({className, animate, ...props}: BackButtonProps) => (
  468. <motion.div
  469. className={className}
  470. animate={animate}
  471. transition={testableTransition()}
  472. variants={{
  473. initial: {opacity: 0, visibility: 'hidden'},
  474. visible: {
  475. opacity: 1,
  476. visibility: 'visible',
  477. transition: testableTransition({delay: 1}),
  478. },
  479. hidden: {
  480. opacity: 0,
  481. transitionEnd: {
  482. visibility: 'hidden',
  483. },
  484. },
  485. }}
  486. >
  487. <Button {...props} icon={<IconArrow direction="left" />} priority="link">
  488. {t('Back')}
  489. </Button>
  490. </motion.div>
  491. ))`
  492. position: absolute;
  493. top: 40px;
  494. left: 20px;
  495. button {
  496. font-size: ${p => p.theme.fontSizeSmall};
  497. }
  498. `;
  499. const SkipOnboardingLink = styled(Link)`
  500. margin: auto ${space(4)};
  501. `;
  502. const UpsellWrapper = styled('div')`
  503. grid-column: 3;
  504. margin-left: auto;
  505. `;
  506. const OnboardingWrapper = styled('main')`
  507. flex-grow: 1;
  508. display: flex;
  509. flex-direction: column;
  510. `;
  511. export default Onboarding;