onboarding.tsx 9.4 KB

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