index.tsx 12 KB

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