onboarding.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import {Component, useEffect} 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 LogoSentry from 'sentry/components/logoSentry';
  8. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  9. import {IconChevron} from 'sentry/icons';
  10. import {t} from 'sentry/locale';
  11. import space from 'sentry/styles/space';
  12. import {Organization, Project} from 'sentry/types';
  13. import testableTransition from 'sentry/utils/testableTransition';
  14. import withOrganization from 'sentry/utils/withOrganization';
  15. import withProjects from 'sentry/utils/withProjects';
  16. import PageCorners from './components/pageCorners';
  17. import OnboardingPlatform from './platform';
  18. import SdkConfiguration from './sdkConfiguration';
  19. import {StepData, StepDescriptor} from './types';
  20. import OnboardingWelcome from './welcome';
  21. const ONBOARDING_STEPS: StepDescriptor[] = [
  22. {
  23. id: 'welcome',
  24. title: t('Welcome'),
  25. Component: OnboardingWelcome,
  26. centered: true,
  27. },
  28. {
  29. id: 'select-platform',
  30. title: t('Select a platform'),
  31. Component: OnboardingPlatform,
  32. },
  33. {
  34. id: 'get-started',
  35. title: t('Install the Sentry SDK'),
  36. Component: SdkConfiguration,
  37. },
  38. ];
  39. type RouteParams = {
  40. orgId: string;
  41. step: string;
  42. };
  43. type DefaultProps = {
  44. steps: StepDescriptor[];
  45. };
  46. type Props = RouteComponentProps<RouteParams, {}> &
  47. DefaultProps & {
  48. organization: Organization;
  49. projects: Project[];
  50. };
  51. type State = StepData;
  52. class Onboarding extends Component<Props, State> {
  53. static defaultProps: DefaultProps = {
  54. steps: ONBOARDING_STEPS,
  55. };
  56. state: State = {};
  57. componentDidMount() {
  58. this.validateActiveStep();
  59. }
  60. componentDidUpdate() {
  61. this.validateActiveStep();
  62. }
  63. validateActiveStep() {
  64. if (this.activeStepIndex === -1) {
  65. const firstStep = this.props.steps[0].id;
  66. browserHistory.replace(`/onboarding/${this.props.params.orgId}/${firstStep}/`);
  67. }
  68. }
  69. get activeStepIndex() {
  70. return this.props.steps.findIndex(({id}) => this.props.params.step === id);
  71. }
  72. get activeStep() {
  73. return this.props.steps[this.activeStepIndex];
  74. }
  75. get firstProject() {
  76. const sortedProjects = this.props.projects.sort(
  77. (a, b) => new Date(a.dateCreated).getTime() - new Date(b.dateCreated).getTime()
  78. );
  79. return sortedProjects.length > 0 ? sortedProjects[0] : null;
  80. }
  81. get projectPlatform() {
  82. return this.state.platform ?? this.firstProject?.platform ?? null;
  83. }
  84. handleUpdate = (data: StepData) => {
  85. this.setState(data);
  86. };
  87. handleNextStep(step: StepDescriptor, data: StepData) {
  88. this.handleUpdate(data);
  89. if (step !== this.activeStep) {
  90. return;
  91. }
  92. const {orgId} = this.props.params;
  93. const nextStep = this.props.steps[this.activeStepIndex + 1];
  94. browserHistory.push(`/onboarding/${orgId}/${nextStep.id}/`);
  95. }
  96. handleGoBack = () => {
  97. const previousStep = this.props.steps[this.activeStepIndex - 1];
  98. browserHistory.replace(`/onboarding/${this.props.params.orgId}/${previousStep.id}/`);
  99. };
  100. renderProgressBar() {
  101. const activeStepIndex = this.activeStepIndex;
  102. return (
  103. <ProgressBar
  104. role="progressbar"
  105. aria-valuenow={activeStepIndex + 1}
  106. aria-valuemax={this.props.steps.length}
  107. >
  108. {this.props.steps.map((step, index) => (
  109. <ProgressStep active={activeStepIndex === index} key={step.id} />
  110. ))}
  111. </ProgressBar>
  112. );
  113. }
  114. renderOnboardingStep() {
  115. const {orgId} = this.props.params;
  116. const step = this.activeStep;
  117. return (
  118. <OnboardingStep
  119. centered={step.centered}
  120. key={step.id}
  121. data-test-id={`onboarding-step-${step.id}`}
  122. >
  123. <step.Component
  124. active
  125. orgId={orgId}
  126. project={this.firstProject}
  127. platform={this.projectPlatform}
  128. onComplete={data => this.handleNextStep(step, data)}
  129. onUpdate={this.handleUpdate}
  130. organization={this.props.organization}
  131. />
  132. </OnboardingStep>
  133. );
  134. }
  135. Contents = () => {
  136. const cornerVariantControl = useAnimation();
  137. const updateCornerVariant = () => {
  138. cornerVariantControl.start(this.activeStepIndex === 0 ? 'top-right' : 'top-left');
  139. };
  140. // XXX(epurkhiser): We're using a react hook here becuase there's no other
  141. // way to create framer-motion controls than by using the `useAnimation`
  142. // hook.
  143. useEffect(updateCornerVariant, [cornerVariantControl]);
  144. return (
  145. <Container>
  146. <Back
  147. animate={this.activeStepIndex > 0 ? 'visible' : 'hidden'}
  148. onClick={this.handleGoBack}
  149. />
  150. <AnimatePresence exitBeforeEnter onExitComplete={updateCornerVariant}>
  151. {this.renderOnboardingStep()}
  152. </AnimatePresence>
  153. <PageCorners animateVariant={cornerVariantControl} />
  154. </Container>
  155. );
  156. };
  157. render() {
  158. if (this.activeStepIndex === -1) {
  159. return null;
  160. }
  161. return (
  162. <OnboardingWrapper>
  163. <SentryDocumentTitle title={this.activeStep.title} />
  164. <Header>
  165. <LogoSvg />
  166. <HeaderRight>
  167. {this.renderProgressBar()}
  168. <Hook
  169. name="onboarding:targeted-onboarding-header"
  170. source="simple-onboarding"
  171. />
  172. </HeaderRight>
  173. </Header>
  174. <this.Contents />
  175. </OnboardingWrapper>
  176. );
  177. }
  178. }
  179. const OnboardingWrapper = styled('main')`
  180. overflow: hidden;
  181. display: flex;
  182. flex-direction: column;
  183. flex-grow: 1;
  184. `;
  185. const Container = styled('div')`
  186. display: flex;
  187. justify-content: center;
  188. position: relative;
  189. background: ${p => p.theme.background};
  190. padding: 120px ${space(3)};
  191. padding-top: 12vh;
  192. width: 100%;
  193. margin: 0 auto;
  194. flex-grow: 1;
  195. `;
  196. const Header = styled('header')`
  197. background: ${p => p.theme.background};
  198. padding: ${space(4)};
  199. position: sticky;
  200. top: 0;
  201. z-index: 100;
  202. box-shadow: 0 5px 10px rgba(0, 0, 0, 0.05);
  203. display: flex;
  204. justify-content: space-between;
  205. `;
  206. const LogoSvg = styled(LogoSentry)`
  207. width: 130px;
  208. height: 30px;
  209. color: ${p => p.theme.textColor};
  210. `;
  211. const ProgressBar = styled('div')`
  212. margin: 0 ${space(4)};
  213. position: relative;
  214. display: flex;
  215. align-items: center;
  216. min-width: 120px;
  217. justify-content: space-between;
  218. &:before {
  219. position: absolute;
  220. display: block;
  221. content: '';
  222. height: 4px;
  223. background: ${p => p.theme.border};
  224. left: 2px;
  225. right: 2px;
  226. top: 50%;
  227. margin-top: -2px;
  228. }
  229. `;
  230. const ProgressStep = styled('div')<{active: boolean}>`
  231. position: relative;
  232. width: 16px;
  233. height: 16px;
  234. border-radius: 50%;
  235. border: 4px solid ${p => (p.active ? p.theme.active : p.theme.border)};
  236. background: ${p => p.theme.background};
  237. `;
  238. const ProgressStatus = styled(motion.div)`
  239. color: ${p => p.theme.subText};
  240. font-size: ${p => p.theme.fontSizeMedium};
  241. text-align: right;
  242. grid-column: 3;
  243. grid-row: 1;
  244. `;
  245. const HeaderRight = styled('div')`
  246. display: grid;
  247. grid-auto-flow: column;
  248. grid-auto-columns: max-content;
  249. gap: ${space(1)};
  250. `;
  251. ProgressStatus.defaultProps = {
  252. initial: {opacity: 0, y: -10},
  253. animate: {opacity: 1, y: 0},
  254. exit: {opacity: 0, y: 10},
  255. transition: testableTransition(),
  256. };
  257. interface BackButtonProps extends Omit<ButtonProps, 'icon' | 'priority'> {
  258. animate: MotionProps['animate'];
  259. className?: string;
  260. }
  261. const Back = styled(({className, animate, ...props}: BackButtonProps) => (
  262. <motion.div
  263. className={className}
  264. animate={animate}
  265. transition={testableTransition()}
  266. variants={{
  267. initial: {opacity: 0, visibility: 'hidden'},
  268. visible: {
  269. opacity: 1,
  270. visibility: 'visible',
  271. transition: testableTransition({delay: 1}),
  272. },
  273. hidden: {
  274. opacity: 0,
  275. transitionEnd: {
  276. visibility: 'hidden',
  277. },
  278. },
  279. }}
  280. >
  281. <Button {...props} icon={<IconChevron direction="left" size="sm" />} priority="link">
  282. {t('Go back')}
  283. </Button>
  284. </motion.div>
  285. ))`
  286. position: absolute;
  287. top: 40px;
  288. left: 20px;
  289. button {
  290. font-size: ${p => p.theme.fontSizeSmall};
  291. color: ${p => p.theme.subText};
  292. }
  293. `;
  294. const OnboardingStep = styled(motion.div)<{centered?: boolean}>`
  295. width: 850px;
  296. display: flex;
  297. flex-direction: column;
  298. ${p =>
  299. p.centered &&
  300. `justify-content: center;
  301. align-items: center;`};
  302. `;
  303. OnboardingStep.defaultProps = {
  304. initial: 'initial',
  305. animate: 'animate',
  306. exit: 'exit',
  307. variants: {animate: {}},
  308. transition: testableTransition({
  309. staggerChildren: 0.2,
  310. }),
  311. };
  312. export default withOrganization(withProjects(Onboarding));