onboarding.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. {this.props.steps.map((step, index) => (
  105. <ProgressStep active={activeStepIndex === index} key={step.id} />
  106. ))}
  107. </ProgressBar>
  108. );
  109. }
  110. renderOnboardingStep() {
  111. const {orgId} = this.props.params;
  112. const step = this.activeStep;
  113. return (
  114. <OnboardingStep
  115. centered={step.centered}
  116. key={step.id}
  117. data-test-id={`onboarding-step-${step.id}`}
  118. >
  119. <step.Component
  120. active
  121. orgId={orgId}
  122. project={this.firstProject}
  123. platform={this.projectPlatform}
  124. onComplete={data => this.handleNextStep(step, data)}
  125. onUpdate={this.handleUpdate}
  126. organization={this.props.organization}
  127. />
  128. </OnboardingStep>
  129. );
  130. }
  131. Contents = () => {
  132. const cornerVariantControl = useAnimation();
  133. const updateCornerVariant = () => {
  134. cornerVariantControl.start(this.activeStepIndex === 0 ? 'top-right' : 'top-left');
  135. };
  136. // XXX(epurkhiser): We're using a react hook here becuase there's no other
  137. // way to create framer-motion controls than by using the `useAnimation`
  138. // hook.
  139. useEffect(updateCornerVariant, [cornerVariantControl]);
  140. return (
  141. <Container>
  142. <Back
  143. animate={this.activeStepIndex > 0 ? 'visible' : 'hidden'}
  144. onClick={this.handleGoBack}
  145. />
  146. <AnimatePresence exitBeforeEnter onExitComplete={updateCornerVariant}>
  147. {this.renderOnboardingStep()}
  148. </AnimatePresence>
  149. <PageCorners animateVariant={cornerVariantControl} />
  150. </Container>
  151. );
  152. };
  153. render() {
  154. if (this.activeStepIndex === -1) {
  155. return null;
  156. }
  157. return (
  158. <OnboardingWrapper>
  159. <SentryDocumentTitle title={this.activeStep.title} />
  160. <Header>
  161. <LogoSvg />
  162. <HeaderRight>
  163. {this.renderProgressBar()}
  164. <Hook
  165. name="onboarding:targeted-onboarding-header"
  166. source="simple-onboarding"
  167. />
  168. </HeaderRight>
  169. </Header>
  170. <this.Contents />
  171. </OnboardingWrapper>
  172. );
  173. }
  174. }
  175. const OnboardingWrapper = styled('main')`
  176. overflow: hidden;
  177. display: flex;
  178. flex-direction: column;
  179. flex-grow: 1;
  180. `;
  181. const Container = styled('div')`
  182. display: flex;
  183. justify-content: center;
  184. position: relative;
  185. background: ${p => p.theme.background};
  186. padding: 120px ${space(3)};
  187. padding-top: 12vh;
  188. width: 100%;
  189. margin: 0 auto;
  190. flex-grow: 1;
  191. `;
  192. const Header = styled('header')`
  193. background: ${p => p.theme.background};
  194. padding: ${space(4)};
  195. position: sticky;
  196. top: 0;
  197. z-index: 100;
  198. box-shadow: 0 5px 10px rgba(0, 0, 0, 0.05);
  199. display: flex;
  200. justify-content: space-between;
  201. `;
  202. const LogoSvg = styled(LogoSentry)`
  203. width: 130px;
  204. height: 30px;
  205. color: ${p => p.theme.textColor};
  206. `;
  207. const ProgressBar = styled('div')`
  208. margin: 0 ${space(4)};
  209. position: relative;
  210. display: flex;
  211. align-items: center;
  212. min-width: 120px;
  213. justify-content: space-between;
  214. &:before {
  215. position: absolute;
  216. display: block;
  217. content: '';
  218. height: 4px;
  219. background: ${p => p.theme.border};
  220. left: 2px;
  221. right: 2px;
  222. top: 50%;
  223. margin-top: -2px;
  224. }
  225. `;
  226. const ProgressStep = styled('div')<{active: boolean}>`
  227. position: relative;
  228. width: 16px;
  229. height: 16px;
  230. border-radius: 50%;
  231. border: 4px solid ${p => (p.active ? p.theme.active : p.theme.border)};
  232. background: ${p => p.theme.background};
  233. `;
  234. const ProgressStatus = styled(motion.div)`
  235. color: ${p => p.theme.subText};
  236. font-size: ${p => p.theme.fontSizeMedium};
  237. text-align: right;
  238. grid-column: 3;
  239. grid-row: 1;
  240. `;
  241. const HeaderRight = styled('div')`
  242. display: grid;
  243. grid-auto-flow: column;
  244. grid-auto-columns: max-content;
  245. gap: ${space(1)};
  246. `;
  247. ProgressStatus.defaultProps = {
  248. initial: {opacity: 0, y: -10},
  249. animate: {opacity: 1, y: 0},
  250. exit: {opacity: 0, y: 10},
  251. transition: testableTransition(),
  252. };
  253. interface BackButtonProps extends Omit<ButtonProps, 'icon' | 'priority'> {
  254. animate: MotionProps['animate'];
  255. className?: string;
  256. }
  257. const Back = styled(({className, animate, ...props}: BackButtonProps) => (
  258. <motion.div
  259. className={className}
  260. animate={animate}
  261. transition={testableTransition()}
  262. variants={{
  263. initial: {opacity: 0, visibility: 'hidden'},
  264. visible: {
  265. opacity: 1,
  266. visibility: 'visible',
  267. transition: testableTransition({delay: 1}),
  268. },
  269. hidden: {
  270. opacity: 0,
  271. transitionEnd: {
  272. visibility: 'hidden',
  273. },
  274. },
  275. }}
  276. >
  277. <Button {...props} icon={<IconChevron direction="left" size="sm" />} priority="link">
  278. {t('Go back')}
  279. </Button>
  280. </motion.div>
  281. ))`
  282. position: absolute;
  283. top: 40px;
  284. left: 20px;
  285. button {
  286. font-size: ${p => p.theme.fontSizeSmall};
  287. color: ${p => p.theme.subText};
  288. }
  289. `;
  290. const OnboardingStep = styled(motion.div)<{centered?: boolean}>`
  291. width: 850px;
  292. display: flex;
  293. flex-direction: column;
  294. ${p =>
  295. p.centered &&
  296. `justify-content: center;
  297. align-items: center;`};
  298. `;
  299. OnboardingStep.defaultProps = {
  300. initial: 'initial',
  301. animate: 'animate',
  302. exit: 'exit',
  303. variants: {animate: {}},
  304. transition: testableTransition({
  305. staggerChildren: 0.2,
  306. }),
  307. };
  308. export default withOrganization(withProjects(Onboarding));