onboarding.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. import {useCallback, useContext, useEffect, useRef, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {MotionProps} from 'framer-motion';
  4. import {AnimatePresence, motion, useAnimation} from 'framer-motion';
  5. import {removeProject} from 'sentry/actionCreators/projects';
  6. import type {ButtonProps} from 'sentry/components/button';
  7. import {Button} from 'sentry/components/button';
  8. import type {OpenConfirmOptions} from 'sentry/components/confirm';
  9. import Confirm, {openConfirmModal} from 'sentry/components/confirm';
  10. import Hook from 'sentry/components/hook';
  11. import Link from 'sentry/components/links/link';
  12. import LogoSentry from 'sentry/components/logoSentry';
  13. import {OnboardingContext} from 'sentry/components/onboarding/onboardingContext';
  14. import {useRecentCreatedProject} from 'sentry/components/onboarding/useRecentCreatedProject';
  15. import Redirect from 'sentry/components/redirect';
  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 {RouteComponentProps} from 'sentry/types/legacyReactRouter';
  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 normalizeUrl from 'sentry/utils/url/normalizeUrl';
  29. import useApi from 'sentry/utils/useApi';
  30. import useOrganization from 'sentry/utils/useOrganization';
  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. link: platform.link,
  118. name: platform.name,
  119. },
  120. });
  121. }
  122. }, [
  123. props.location.query,
  124. props.router,
  125. onboardingContext,
  126. onboardingSteps,
  127. organization.slug,
  128. props.location.pathname,
  129. ]);
  130. const shallProjectBeDeleted =
  131. stepObj?.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('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. // Redirect to the first step if we end up in an invalid state
  295. const isInvalidDocsStep = stepId === 'setup-docs' && !projectSlug;
  296. if (!stepObj || stepIndex === -1 || isInvalidDocsStep) {
  297. return (
  298. <Redirect
  299. to={normalizeUrl(`/onboarding/${organization.slug}/${onboardingSteps[0].id}/`)}
  300. />
  301. );
  302. }
  303. const goBackDeletionAlertModalProps: OpenConfirmOptions = {
  304. message: t(
  305. "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?"
  306. ),
  307. priority: 'danger',
  308. confirmText: t("Yes I'm sure"),
  309. onConfirm: handleGoBack,
  310. onClose: () => {
  311. if (!recentCreatedProject) {
  312. return;
  313. }
  314. trackAnalytics('onboarding.data_removal_modal_dismissed', {
  315. organization,
  316. platform: recentCreatedProject.slug,
  317. project_id: recentCreatedProject.id,
  318. });
  319. },
  320. onRender: () => {
  321. if (!recentCreatedProject) {
  322. return;
  323. }
  324. trackAnalytics('onboarding.data_removal_modal_rendered', {
  325. organization,
  326. platform: recentCreatedProject.slug,
  327. project_id: recentCreatedProject.id,
  328. });
  329. },
  330. };
  331. return (
  332. <OnboardingWrapper data-test-id="targeted-onboarding">
  333. <SentryDocumentTitle title={stepObj.title} />
  334. <Header>
  335. <LogoSvg />
  336. {stepIndex !== -1 && (
  337. <StyledStepper
  338. numSteps={onboardingSteps.length}
  339. currentStepIndex={stepIndex}
  340. onClick={i => {
  341. if (i < stepIndex && shallProjectBeDeleted) {
  342. openConfirmModal({
  343. ...goBackDeletionAlertModalProps,
  344. onConfirm: () => handleGoBack(i),
  345. });
  346. return;
  347. }
  348. goToStep(onboardingSteps[i]);
  349. }}
  350. />
  351. )}
  352. <UpsellWrapper>
  353. <Hook
  354. name="onboarding:targeted-onboarding-header"
  355. source="targeted-onboarding"
  356. />
  357. </UpsellWrapper>
  358. </Header>
  359. <Container hasFooter={containerHasFooter}>
  360. <Confirm bypass={!shallProjectBeDeleted} {...goBackDeletionAlertModalProps}>
  361. <Back animate={stepIndex > 0 ? 'visible' : 'hidden'} />
  362. </Confirm>
  363. <AnimatePresence mode="wait" onExitComplete={updateAnimationState}>
  364. <OnboardingStep key={stepObj.id} data-test-id={`onboarding-step-${stepObj.id}`}>
  365. {stepObj.Component && (
  366. <stepObj.Component
  367. active
  368. data-test-id={`onboarding-step-${stepObj.id}`}
  369. stepIndex={stepIndex}
  370. onComplete={platform => {
  371. if (stepObj) {
  372. goNextStep(stepObj, platform);
  373. }
  374. }}
  375. orgId={organization.slug}
  376. search={props.location.search}
  377. route={props.route}
  378. router={props.router}
  379. location={props.location}
  380. recentCreatedProject={recentCreatedProject}
  381. {...{
  382. genSkipOnboardingLink,
  383. }}
  384. />
  385. )}
  386. </OnboardingStep>
  387. </AnimatePresence>
  388. <AdaptivePageCorners animateVariant={cornerVariantControl} />
  389. </Container>
  390. </OnboardingWrapper>
  391. );
  392. }
  393. const Container = styled('div')<{hasFooter: boolean}>`
  394. flex-grow: 1;
  395. display: flex;
  396. flex-direction: column;
  397. position: relative;
  398. background: ${p => p.theme.background};
  399. padding: 120px ${space(3)};
  400. width: 100%;
  401. margin: 0 auto;
  402. padding-bottom: ${p => p.hasFooter && '72px'};
  403. margin-bottom: ${p => p.hasFooter && '72px'};
  404. `;
  405. const Header = styled('header')`
  406. background: ${p => p.theme.background};
  407. padding-left: ${space(4)};
  408. padding-right: ${space(4)};
  409. position: sticky;
  410. height: 80px;
  411. align-items: center;
  412. top: 0;
  413. z-index: 100;
  414. box-shadow: 0 5px 10px rgba(0, 0, 0, 0.05);
  415. display: grid;
  416. grid-template-columns: 1fr 1fr 1fr;
  417. justify-items: stretch;
  418. `;
  419. const LogoSvg = styled(LogoSentry)`
  420. width: 130px;
  421. height: 30px;
  422. color: ${p => p.theme.textColor};
  423. `;
  424. const OnboardingStep = styled(motion.div)`
  425. flex-grow: 1;
  426. display: flex;
  427. flex-direction: column;
  428. `;
  429. OnboardingStep.defaultProps = {
  430. initial: 'initial',
  431. animate: 'animate',
  432. exit: 'exit',
  433. variants: {animate: {}},
  434. transition: testableTransition({
  435. staggerChildren: 0.2,
  436. }),
  437. };
  438. const Sidebar = styled(motion.div)`
  439. width: 850px;
  440. display: flex;
  441. flex-direction: column;
  442. align-items: center;
  443. `;
  444. Sidebar.defaultProps = {
  445. initial: 'initial',
  446. animate: 'animate',
  447. exit: 'exit',
  448. variants: {animate: {}},
  449. transition: testableTransition({
  450. staggerChildren: 0.2,
  451. }),
  452. };
  453. const AdaptivePageCorners = styled(PageCorners)`
  454. --corner-scale: 1;
  455. @media (max-width: ${p => p.theme.breakpoints.small}) {
  456. --corner-scale: 0.5;
  457. }
  458. `;
  459. const StyledStepper = styled(Stepper)`
  460. justify-self: center;
  461. @media (max-width: ${p => p.theme.breakpoints.medium}) {
  462. display: none;
  463. }
  464. `;
  465. interface BackButtonProps extends Omit<ButtonProps, 'icon' | 'priority'> {
  466. animate: MotionProps['animate'];
  467. className?: string;
  468. }
  469. const Back = styled(({className, animate, ...props}: BackButtonProps) => (
  470. <motion.div
  471. className={className}
  472. animate={animate}
  473. transition={testableTransition()}
  474. variants={{
  475. initial: {opacity: 0, visibility: 'hidden'},
  476. visible: {
  477. opacity: 1,
  478. visibility: 'visible',
  479. transition: testableTransition({delay: 1}),
  480. },
  481. hidden: {
  482. opacity: 0,
  483. transitionEnd: {
  484. visibility: 'hidden',
  485. },
  486. },
  487. }}
  488. >
  489. <Button {...props} icon={<IconArrow direction="left" />} priority="link">
  490. {t('Back')}
  491. </Button>
  492. </motion.div>
  493. ))`
  494. position: absolute;
  495. top: 40px;
  496. left: 20px;
  497. button {
  498. font-size: ${p => p.theme.fontSizeSmall};
  499. }
  500. `;
  501. const SkipOnboardingLink = styled(Link)`
  502. margin: auto ${space(4)};
  503. `;
  504. const UpsellWrapper = styled('div')`
  505. grid-column: 3;
  506. margin-left: auto;
  507. `;
  508. const OnboardingWrapper = styled('main')`
  509. flex-grow: 1;
  510. display: flex;
  511. flex-direction: column;
  512. `;
  513. export default Onboarding;