onboarding.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 withOrganization from 'sentry/utils/withOrganization';
  18. import withProjects from 'sentry/utils/withProjects';
  19. import PageCorners from 'sentry/views/onboarding/components/pageCorners';
  20. import Stepper from './components/stepper';
  21. import PlatformSelection from './platform';
  22. import SetupDocs from './setupDocs';
  23. import {StepDescriptor} from './types';
  24. import {usePersistedOnboardingState} from './utils';
  25. import TargetedOnboardingWelcome from './welcome';
  26. type RouteParams = {
  27. orgId: string;
  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(`/onboarding/${props.params.orgId}/${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(`/onboarding/${props.params.orgId}/${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(`/onboarding/${props.params.orgId}/${previousStep.id}/`);
  120. };
  121. const genSkipOnboardingLink = () => {
  122. const source = `targeted-onboarding-${stepId}`;
  123. return (
  124. <SkipOnboardingLink
  125. onClick={() => {
  126. trackAdvancedAnalyticsEvent('growth.onboarding_clicked_skip', {
  127. organization,
  128. source,
  129. });
  130. if (clientState) {
  131. setClientState({
  132. ...clientState,
  133. state: 'skipped',
  134. });
  135. }
  136. }}
  137. to={`/organizations/${organization.slug}/issues/`}
  138. >
  139. {t('Skip Onboarding')}
  140. </SkipOnboardingLink>
  141. );
  142. };
  143. if (!stepObj || stepIndex === -1) {
  144. return <Redirect to={`/onboarding/${organization.slug}/${onboardingSteps[0].id}/`} />;
  145. }
  146. return (
  147. <OnboardingWrapper data-test-id="targeted-onboarding">
  148. <SentryDocumentTitle title={stepObj.title} />
  149. <Header>
  150. <LogoSvg />
  151. {stepIndex !== -1 && (
  152. <StyledStepper
  153. numSteps={onboardingSteps.length}
  154. currentStepIndex={stepIndex}
  155. onClick={i => goToStep(onboardingSteps[i])}
  156. />
  157. )}
  158. <UpsellWrapper>
  159. <Hook
  160. name="onboarding:targeted-onboarding-header"
  161. source="targeted-onboarding"
  162. />
  163. </UpsellWrapper>
  164. </Header>
  165. <Container hasFooter={containerHasFooter}>
  166. <Back animate={stepIndex > 0 ? 'visible' : 'hidden'} onClick={handleGoBack} />
  167. <AnimatePresence exitBeforeEnter onExitComplete={updateAnimationState}>
  168. <OnboardingStep key={stepObj.id} data-test-id={`onboarding-step-${stepObj.id}`}>
  169. {stepObj.Component && (
  170. <stepObj.Component
  171. active
  172. data-test-id={`onboarding-step-${stepObj.id}`}
  173. stepIndex={stepIndex}
  174. onComplete={() => stepObj && goNextStep(stepObj)}
  175. orgId={props.params.orgId}
  176. organization={props.organization}
  177. search={props.location.search}
  178. {...{
  179. genSkipOnboardingLink,
  180. }}
  181. />
  182. )}
  183. </OnboardingStep>
  184. </AnimatePresence>
  185. <AdaptivePageCorners animateVariant={cornerVariantControl} />
  186. </Container>
  187. </OnboardingWrapper>
  188. );
  189. }
  190. const Container = styled('div')<{hasFooter: boolean}>`
  191. flex-grow: 1;
  192. display: flex;
  193. flex-direction: column;
  194. position: relative;
  195. background: ${p => p.theme.background};
  196. padding: 120px ${space(3)};
  197. width: 100%;
  198. margin: 0 auto;
  199. padding-bottom: ${p => p.hasFooter && '72px'};
  200. margin-bottom: ${p => p.hasFooter && '72px'};
  201. `;
  202. const Header = styled('header')`
  203. background: ${p => p.theme.background};
  204. padding-left: ${space(4)};
  205. padding-right: ${space(4)};
  206. position: sticky;
  207. height: 80px;
  208. align-items: center;
  209. top: 0;
  210. z-index: 100;
  211. box-shadow: 0 5px 10px rgba(0, 0, 0, 0.05);
  212. display: grid;
  213. grid-template-columns: 1fr 1fr 1fr;
  214. justify-items: stretch;
  215. `;
  216. const LogoSvg = styled(LogoSentry)`
  217. width: 130px;
  218. height: 30px;
  219. color: ${p => p.theme.textColor};
  220. `;
  221. const OnboardingStep = styled(motion.div)`
  222. flex-grow: 1;
  223. display: flex;
  224. flex-direction: column;
  225. `;
  226. OnboardingStep.defaultProps = {
  227. initial: 'initial',
  228. animate: 'animate',
  229. exit: 'exit',
  230. variants: {animate: {}},
  231. transition: testableTransition({
  232. staggerChildren: 0.2,
  233. }),
  234. };
  235. const Sidebar = styled(motion.div)`
  236. width: 850px;
  237. display: flex;
  238. flex-direction: column;
  239. align-items: center;
  240. `;
  241. Sidebar.defaultProps = {
  242. initial: 'initial',
  243. animate: 'animate',
  244. exit: 'exit',
  245. variants: {animate: {}},
  246. transition: testableTransition({
  247. staggerChildren: 0.2,
  248. }),
  249. };
  250. const AdaptivePageCorners = styled(PageCorners)`
  251. --corner-scale: 1;
  252. @media (max-width: ${p => p.theme.breakpoints.small}) {
  253. --corner-scale: 0.5;
  254. }
  255. `;
  256. const StyledStepper = styled(Stepper)`
  257. justify-self: center;
  258. @media (max-width: ${p => p.theme.breakpoints.medium}) {
  259. display: none;
  260. }
  261. `;
  262. interface BackButtonProps extends Omit<ButtonProps, 'icon' | 'priority'> {
  263. animate: MotionProps['animate'];
  264. className?: string;
  265. }
  266. const Back = styled(({className, animate, ...props}: BackButtonProps) => (
  267. <motion.div
  268. className={className}
  269. animate={animate}
  270. transition={testableTransition()}
  271. variants={{
  272. initial: {opacity: 0, visibility: 'hidden'},
  273. visible: {
  274. opacity: 1,
  275. visibility: 'visible',
  276. transition: testableTransition({delay: 1}),
  277. },
  278. hidden: {
  279. opacity: 0,
  280. transitionEnd: {
  281. visibility: 'hidden',
  282. },
  283. },
  284. }}
  285. >
  286. <Button {...props} icon={<IconArrow direction="left" size="sm" />} priority="link">
  287. {t('Back')}
  288. </Button>
  289. </motion.div>
  290. ))`
  291. position: absolute;
  292. top: 40px;
  293. left: 20px;
  294. button {
  295. font-size: ${p => p.theme.fontSizeSmall};
  296. }
  297. `;
  298. const SkipOnboardingLink = styled(Link)`
  299. margin: auto ${space(4)};
  300. `;
  301. const UpsellWrapper = styled('div')`
  302. grid-column: 3;
  303. margin-left: auto;
  304. `;
  305. const OnboardingWrapper = styled('main')`
  306. flex-grow: 1;
  307. display: flex;
  308. flex-direction: column;
  309. `;
  310. export default withOrganization(withProjects(Onboarding));