index.tsx 12 KB

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