onboarding.tsx 16 KB

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