index.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. import {useCallback, useEffect, useRef, useState} from 'react';
  2. import {browserHistory, RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import * as Sentry from '@sentry/react';
  5. import {AnimatePresence, motion, MotionProps, useAnimation} from 'framer-motion';
  6. import {removeProject} from 'sentry/actionCreators/projects';
  7. import {Button, ButtonProps} from 'sentry/components/button';
  8. import Hook from 'sentry/components/hook';
  9. import Link from 'sentry/components/links/link';
  10. import LogoSentry from 'sentry/components/logoSentry';
  11. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  12. import {PlatformKey} from 'sentry/data/platformCategories';
  13. import {IconArrow} from 'sentry/icons';
  14. import {t} from 'sentry/locale';
  15. import {space} from 'sentry/styles/space';
  16. import {Organization, Project} from 'sentry/types';
  17. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  18. import handleXhrErrorResponse from 'sentry/utils/handleXhrErrorResponse';
  19. import Redirect from 'sentry/utils/redirect';
  20. import testableTransition from 'sentry/utils/testableTransition';
  21. import useApi from 'sentry/utils/useApi';
  22. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  23. import withOrganization from 'sentry/utils/withOrganization';
  24. import withProjects from 'sentry/utils/withProjects';
  25. import PageCorners from 'sentry/views/onboarding/components/pageCorners';
  26. import Stepper from './components/stepper';
  27. import OnboardingPlatform from './deprecatedPlatform';
  28. import {PlatformSelection} from './platformSelection';
  29. import SetupDocs from './setupDocs';
  30. import {StepDescriptor} from './types';
  31. import {usePersistedOnboardingState} from './utils';
  32. import TargetedOnboardingWelcome from './welcome';
  33. type RouteParams = {
  34. step: string;
  35. };
  36. type Props = RouteComponentProps<RouteParams, {}> & {
  37. organization: Organization;
  38. projects: Project[];
  39. };
  40. function getOrganizationOnboardingSteps(singleSelectPlatform: boolean): StepDescriptor[] {
  41. return [
  42. {
  43. id: 'welcome',
  44. title: t('Welcome'),
  45. Component: TargetedOnboardingWelcome,
  46. cornerVariant: 'top-right',
  47. },
  48. {
  49. ...(singleSelectPlatform
  50. ? {
  51. id: 'select-platform',
  52. title: t('Select platform'),
  53. Component: PlatformSelection,
  54. hasFooter: true,
  55. cornerVariant: 'top-left',
  56. }
  57. : {
  58. id: 'select-platform',
  59. title: t('Select platforms'),
  60. Component: OnboardingPlatform,
  61. hasFooter: true,
  62. cornerVariant: 'top-left',
  63. }),
  64. },
  65. {
  66. id: 'setup-docs',
  67. title: t('Install the Sentry SDK'),
  68. Component: SetupDocs,
  69. hasFooter: true,
  70. cornerVariant: 'top-left',
  71. },
  72. ];
  73. }
  74. function Onboarding(props: Props) {
  75. const api = useApi();
  76. const {
  77. organization,
  78. params: {step: stepId},
  79. } = props;
  80. const cornerVariantTimeoutRed = useRef<number | undefined>(undefined);
  81. const [clientState, setClientState] = usePersistedOnboardingState();
  82. useEffect(() => {
  83. return () => {
  84. window.clearTimeout(cornerVariantTimeoutRed.current);
  85. };
  86. }, []);
  87. const heartbeatFooter = !!props.organization?.features.includes(
  88. 'onboarding-heartbeat-footer'
  89. );
  90. const singleSelectPlatform = !!props.organization?.features.includes(
  91. 'onboarding-remove-multiselect-platform'
  92. );
  93. const projectDeletionOnBackClick = !!props.organization?.features.includes(
  94. 'onboarding-project-deletion-on-back-click'
  95. );
  96. const onboardingSteps = getOrganizationOnboardingSteps(singleSelectPlatform);
  97. const stepObj = onboardingSteps.find(({id}) => stepId === id);
  98. const stepIndex = onboardingSteps.findIndex(({id}) => stepId === id);
  99. const cornerVariantControl = useAnimation();
  100. const updateCornerVariant = () => {
  101. // TODO: find better way to delay the corner animation
  102. window.clearTimeout(cornerVariantTimeoutRed.current);
  103. cornerVariantTimeoutRed.current = window.setTimeout(
  104. () => cornerVariantControl.start(stepIndex === 0 ? 'top-right' : 'top-left'),
  105. 1000
  106. );
  107. };
  108. useEffect(updateCornerVariant, [stepIndex, cornerVariantControl]);
  109. // Called onExitComplete
  110. const [containerHasFooter, setContainerHasFooter] = useState<boolean>(false);
  111. const updateAnimationState = () => {
  112. if (!stepObj) {
  113. return;
  114. }
  115. setContainerHasFooter(stepObj.hasFooter ?? false);
  116. };
  117. const goToStep = (step: StepDescriptor) => {
  118. if (!stepObj) {
  119. return;
  120. }
  121. if (step.cornerVariant !== stepObj.cornerVariant) {
  122. cornerVariantControl.start('none');
  123. }
  124. browserHistory.push(normalizeUrl(`/onboarding/${organization.slug}/${step.id}/`));
  125. };
  126. const goNextStep = (step: StepDescriptor) => {
  127. const currentStepIndex = onboardingSteps.findIndex(s => s.id === step.id);
  128. const nextStep = onboardingSteps[currentStepIndex + 1];
  129. if (step.cornerVariant !== nextStep.cornerVariant) {
  130. cornerVariantControl.start('none');
  131. }
  132. browserHistory.push(normalizeUrl(`/onboarding/${organization.slug}/${nextStep.id}/`));
  133. };
  134. const handleGoBack = async () => {
  135. if (!stepObj) {
  136. return;
  137. }
  138. const previousStep = onboardingSteps[stepIndex - 1];
  139. if (!previousStep) {
  140. return;
  141. }
  142. // The user is going back to select a new platform,
  143. // so we silently delete the last created project
  144. if (projectDeletionOnBackClick && stepIndex === onboardingSteps.length - 1) {
  145. const selectedPlatforms = clientState?.selectedPlatforms || [];
  146. const platformToProjectIdMap = clientState?.platformToProjectIdMap || {};
  147. const selectedProjectSlugs = selectedPlatforms
  148. .map(platform => platformToProjectIdMap[platform])
  149. .filter((slug): slug is string => slug !== undefined);
  150. try {
  151. await removeProject(api, organization.slug, selectedProjectSlugs[0]);
  152. if (clientState) {
  153. setClientState({
  154. url: 'setup-docs/',
  155. state: 'projects_selected',
  156. selectedPlatforms: [selectedProjectSlugs[0] as PlatformKey],
  157. platformToProjectIdMap: Object.keys(platformToProjectIdMap).reduce(
  158. (acc, value) => {
  159. if (value !== selectedProjectSlugs[0] && !acc[value]) {
  160. acc[value] = value;
  161. }
  162. return acc;
  163. },
  164. {}
  165. ),
  166. });
  167. }
  168. } catch (error) {
  169. handleXhrErrorResponse(t('Unable to delete project'))(error);
  170. // we don't give the user any feedback regarding this error as this shall be silent
  171. }
  172. }
  173. if (stepObj.cornerVariant !== previousStep.cornerVariant) {
  174. cornerVariantControl.start('none');
  175. }
  176. trackAdvancedAnalyticsEvent('onboarding.back_button_clicked', {
  177. organization,
  178. from: onboardingSteps[stepIndex].id,
  179. to: previousStep.id,
  180. });
  181. browserHistory.replace(
  182. normalizeUrl(`/onboarding/${organization.slug}/${previousStep.id}/`)
  183. );
  184. };
  185. const genSkipOnboardingLink = () => {
  186. const source = `targeted-onboarding-${stepId}`;
  187. return (
  188. <SkipOnboardingLink
  189. onClick={() => {
  190. trackAdvancedAnalyticsEvent('growth.onboarding_clicked_skip', {
  191. organization,
  192. source,
  193. });
  194. if (clientState) {
  195. setClientState({
  196. ...clientState,
  197. state: 'skipped',
  198. });
  199. }
  200. }}
  201. to={normalizeUrl(
  202. `/organizations/${organization.slug}/issues/?referrer=onboarding-skip`
  203. )}
  204. >
  205. {t('Skip Onboarding')}
  206. </SkipOnboardingLink>
  207. );
  208. };
  209. const jumpToSetupProject = useCallback(() => {
  210. const nextStep = onboardingSteps.find(({id}) => id === 'setup-docs');
  211. if (!nextStep) {
  212. Sentry.captureMessage(
  213. 'Missing step in onboarding: `setup-docs` when trying to jump there'
  214. );
  215. return;
  216. }
  217. browserHistory.push(normalizeUrl(`/onboarding/${organization.slug}/${nextStep.id}/`));
  218. }, [onboardingSteps, organization]);
  219. if (!stepObj || stepIndex === -1) {
  220. return (
  221. <Redirect
  222. to={normalizeUrl(`/onboarding/${organization.slug}/${onboardingSteps[0].id}/`)}
  223. />
  224. );
  225. }
  226. return (
  227. <OnboardingWrapper data-test-id="targeted-onboarding">
  228. <SentryDocumentTitle title={stepObj.title} />
  229. <Header>
  230. <LogoSvg />
  231. {stepIndex !== -1 && (
  232. <StyledStepper
  233. numSteps={onboardingSteps.length}
  234. currentStepIndex={stepIndex}
  235. onClick={i => goToStep(onboardingSteps[i])}
  236. />
  237. )}
  238. <UpsellWrapper>
  239. <Hook
  240. name="onboarding:targeted-onboarding-header"
  241. source="targeted-onboarding"
  242. />
  243. </UpsellWrapper>
  244. </Header>
  245. <Container hasFooter={containerHasFooter} heartbeatFooter={heartbeatFooter}>
  246. <Back animate={stepIndex > 0 ? 'visible' : 'hidden'} onClick={handleGoBack} />
  247. <AnimatePresence exitBeforeEnter onExitComplete={updateAnimationState}>
  248. <OnboardingStep key={stepObj.id} data-test-id={`onboarding-step-${stepObj.id}`}>
  249. {stepObj.Component && (
  250. <stepObj.Component
  251. active
  252. data-test-id={`onboarding-step-${stepObj.id}`}
  253. stepIndex={stepIndex}
  254. onComplete={() => stepObj && goNextStep(stepObj)}
  255. orgId={organization.slug}
  256. organization={props.organization}
  257. search={props.location.search}
  258. route={props.route}
  259. router={props.router}
  260. location={props.location}
  261. jumpToSetupProject={jumpToSetupProject}
  262. {...{
  263. genSkipOnboardingLink,
  264. }}
  265. />
  266. )}
  267. </OnboardingStep>
  268. </AnimatePresence>
  269. <AdaptivePageCorners animateVariant={cornerVariantControl} />
  270. </Container>
  271. </OnboardingWrapper>
  272. );
  273. }
  274. const Container = styled('div')<{hasFooter: boolean; heartbeatFooter: boolean}>`
  275. flex-grow: 1;
  276. display: flex;
  277. flex-direction: column;
  278. position: relative;
  279. background: ${p => p.theme.background};
  280. padding: ${p =>
  281. p.heartbeatFooter ? `120px ${space(3)} 0 ${space(3)}` : `120px ${space(3)}`};
  282. width: 100%;
  283. margin: 0 auto;
  284. padding-bottom: ${p => p.hasFooter && '72px'};
  285. margin-bottom: ${p => p.hasFooter && '72px'};
  286. `;
  287. const Header = styled('header')`
  288. background: ${p => p.theme.background};
  289. padding-left: ${space(4)};
  290. padding-right: ${space(4)};
  291. position: sticky;
  292. height: 80px;
  293. align-items: center;
  294. top: 0;
  295. z-index: 100;
  296. box-shadow: 0 5px 10px rgba(0, 0, 0, 0.05);
  297. display: grid;
  298. grid-template-columns: 1fr 1fr 1fr;
  299. justify-items: stretch;
  300. `;
  301. const LogoSvg = styled(LogoSentry)`
  302. width: 130px;
  303. height: 30px;
  304. color: ${p => p.theme.textColor};
  305. `;
  306. const OnboardingStep = styled(motion.div)`
  307. flex-grow: 1;
  308. display: flex;
  309. flex-direction: column;
  310. `;
  311. OnboardingStep.defaultProps = {
  312. initial: 'initial',
  313. animate: 'animate',
  314. exit: 'exit',
  315. variants: {animate: {}},
  316. transition: testableTransition({
  317. staggerChildren: 0.2,
  318. }),
  319. };
  320. const Sidebar = styled(motion.div)`
  321. width: 850px;
  322. display: flex;
  323. flex-direction: column;
  324. align-items: center;
  325. `;
  326. Sidebar.defaultProps = {
  327. initial: 'initial',
  328. animate: 'animate',
  329. exit: 'exit',
  330. variants: {animate: {}},
  331. transition: testableTransition({
  332. staggerChildren: 0.2,
  333. }),
  334. };
  335. const AdaptivePageCorners = styled(PageCorners)`
  336. --corner-scale: 1;
  337. @media (max-width: ${p => p.theme.breakpoints.small}) {
  338. --corner-scale: 0.5;
  339. }
  340. `;
  341. const StyledStepper = styled(Stepper)`
  342. justify-self: center;
  343. @media (max-width: ${p => p.theme.breakpoints.medium}) {
  344. display: none;
  345. }
  346. `;
  347. interface BackButtonProps extends Omit<ButtonProps, 'icon' | 'priority'> {
  348. animate: MotionProps['animate'];
  349. className?: string;
  350. }
  351. const Back = styled(({className, animate, ...props}: BackButtonProps) => (
  352. <motion.div
  353. className={className}
  354. animate={animate}
  355. transition={testableTransition()}
  356. variants={{
  357. initial: {opacity: 0, visibility: 'hidden'},
  358. visible: {
  359. opacity: 1,
  360. visibility: 'visible',
  361. transition: testableTransition({delay: 1}),
  362. },
  363. hidden: {
  364. opacity: 0,
  365. transitionEnd: {
  366. visibility: 'hidden',
  367. },
  368. },
  369. }}
  370. >
  371. <Button {...props} icon={<IconArrow direction="left" size="sm" />} priority="link">
  372. {t('Back')}
  373. </Button>
  374. </motion.div>
  375. ))`
  376. position: absolute;
  377. top: 40px;
  378. left: 20px;
  379. button {
  380. font-size: ${p => p.theme.fontSizeSmall};
  381. }
  382. `;
  383. const SkipOnboardingLink = styled(Link)`
  384. margin: auto ${space(4)};
  385. `;
  386. const UpsellWrapper = styled('div')`
  387. grid-column: 3;
  388. margin-left: auto;
  389. `;
  390. const OnboardingWrapper = styled('main')`
  391. flex-grow: 1;
  392. display: flex;
  393. flex-direction: column;
  394. `;
  395. export default withOrganization(withProjects(Onboarding));