onboarding.tsx 15 KB

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