onboarding.tsx 9.5 KB

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