index.tsx 10 KB

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