guideAnchor.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import React from 'react';
  2. import styled from '@emotion/styled';
  3. import * as Sentry from '@sentry/react';
  4. import createReactClass from 'create-react-class';
  5. import {Query} from 'history';
  6. import Reflux from 'reflux';
  7. import {
  8. closeGuide,
  9. dismissGuide,
  10. nextStep,
  11. recordFinish,
  12. registerAnchor,
  13. unregisterAnchor,
  14. } from 'app/actionCreators/guides';
  15. import {Guide} from 'app/components/assistant/types';
  16. import Button from 'app/components/button';
  17. import Hovercard, {Body as HovercardBody} from 'app/components/hovercard';
  18. import {t, tct} from 'app/locale';
  19. import GuideStore, {GuideStoreState} from 'app/stores/guideStore';
  20. import space from 'app/styles/space';
  21. import theme from 'app/utils/theme';
  22. type Props = {
  23. target?: string; //Shouldn't target be mandatory?
  24. position?: React.ComponentProps<typeof Hovercard>['position'];
  25. offset?: string;
  26. to?: {
  27. pathname: string;
  28. query: Query;
  29. };
  30. onFinish?: () => void;
  31. /** Hovercard renders the container */
  32. containerClassName?: string;
  33. };
  34. type State = {
  35. active: boolean;
  36. orgId: string | null;
  37. currentGuide?: Guide;
  38. step?: number;
  39. };
  40. /**
  41. * A GuideAnchor puts an informative hovercard around an element.
  42. * Guide anchors register with the GuideStore, which uses registrations
  43. * from one or more anchors on the page to determine which guides can
  44. * be shown on the page.
  45. */
  46. export const GuideAnchor = createReactClass<Props, State>({
  47. mixins: [Reflux.listenTo(GuideStore, 'onGuideStateChange') as any],
  48. getInitialState() {
  49. return {
  50. active: false,
  51. orgId: null,
  52. };
  53. },
  54. componentDidMount() {
  55. const {target} = this.props;
  56. target && registerAnchor(target);
  57. },
  58. componentDidUpdate(_prevProps, prevState) {
  59. if (this.containerElement && !prevState.active && this.state.active) {
  60. try {
  61. const {top} = this.containerElement.getBoundingClientRect();
  62. const scrollTop = window.pageYOffset;
  63. const centerElement = top + scrollTop - window.innerHeight / 2;
  64. window.scrollTo({top: centerElement});
  65. } catch (err) {
  66. Sentry.captureException(err);
  67. }
  68. }
  69. },
  70. componentWillUnmount() {
  71. const {target} = this.props;
  72. target && unregisterAnchor(target);
  73. },
  74. onGuideStateChange(data: GuideStoreState) {
  75. const active =
  76. data.currentGuide &&
  77. data.currentGuide.steps[data.currentStep]?.target === this.props.target;
  78. this.setState({
  79. active,
  80. currentGuide: data.currentGuide,
  81. step: data.currentStep,
  82. orgId: data.orgId,
  83. });
  84. },
  85. /**
  86. * Terminology:
  87. *
  88. * - A guide can be FINISHED by clicking one of the buttons in the last step
  89. * - A guide can be DISMISSED by x-ing out of it at any step except the last (where there is no x)
  90. * - In both cases we consider it CLOSED
  91. */
  92. handleFinish(e: React.MouseEvent) {
  93. e.stopPropagation();
  94. const {onFinish} = this.props;
  95. if (onFinish) {
  96. onFinish();
  97. }
  98. const {currentGuide, orgId} = this.state;
  99. recordFinish(currentGuide.guide, orgId);
  100. closeGuide();
  101. },
  102. handleNextStep(e: React.MouseEvent) {
  103. e.stopPropagation();
  104. nextStep();
  105. },
  106. handleDismiss(e: React.MouseEvent) {
  107. e.stopPropagation();
  108. const {currentGuide, step, orgId} = this.state;
  109. dismissGuide(currentGuide.guide, step, orgId);
  110. },
  111. getHovercardBody() {
  112. const {to} = this.props;
  113. const {currentGuide, step} = this.state;
  114. const totalStepCount = currentGuide.steps.length;
  115. const currentStepCount = step + 1;
  116. const currentStep = currentGuide.steps[step];
  117. const lastStep = currentStepCount === totalStepCount;
  118. const hasManySteps = totalStepCount > 1;
  119. // to clear `#assistant` from the url
  120. const href = window.location.hash === '#assistant' ? '#' : '';
  121. const dismissButton = (
  122. <DismissButton
  123. size="small"
  124. href={href}
  125. onClick={this.handleDismiss}
  126. priority="link"
  127. >
  128. {currentStep.dismissText || t('Dismiss')}
  129. </DismissButton>
  130. );
  131. return (
  132. <GuideContainer>
  133. <GuideContent>
  134. {currentStep.title && <GuideTitle>{currentStep.title}</GuideTitle>}
  135. <GuideDescription>{currentStep.description}</GuideDescription>
  136. </GuideContent>
  137. <GuideAction>
  138. <div>
  139. {lastStep ? (
  140. <React.Fragment>
  141. <StyledButton size="small" to={to} onClick={this.handleFinish}>
  142. {currentStep.nextText ||
  143. (hasManySteps ? t('Enough Already') : t('Got It'))}
  144. </StyledButton>
  145. {currentStep.hasNextGuide && dismissButton}
  146. </React.Fragment>
  147. ) : (
  148. <React.Fragment>
  149. <StyledButton size="small" onClick={this.handleNextStep} to={to}>
  150. {currentStep.nextText || t('Next')}
  151. </StyledButton>
  152. {!currentStep.cantDismiss && dismissButton}
  153. </React.Fragment>
  154. )}
  155. </div>
  156. {hasManySteps && (
  157. <StepCount>
  158. {tct('[currentStepCount] of [totalStepCount]', {
  159. currentStepCount,
  160. totalStepCount,
  161. })}
  162. </StepCount>
  163. )}
  164. </GuideAction>
  165. </GuideContainer>
  166. );
  167. },
  168. render() {
  169. const {children, position, offset, containerClassName} = this.props;
  170. const {active} = this.state;
  171. if (!active) {
  172. return children ? children : null;
  173. }
  174. return (
  175. <StyledHovercard
  176. show
  177. body={this.getHovercardBody()}
  178. tipColor={theme.purple300}
  179. position={position}
  180. offset={offset}
  181. containerClassName={containerClassName}
  182. >
  183. <span ref={el => (this.containerElement = el)}>{children}</span>
  184. </StyledHovercard>
  185. );
  186. },
  187. });
  188. /**
  189. * Wraps the GuideAnchor so we don't have to render it if it's disabled
  190. * Using a class so we automatically have children as a typed prop
  191. */
  192. type WrapperProps = {disabled?: boolean} & Props;
  193. export default class GuideAnchorWrapper extends React.Component<WrapperProps> {
  194. render() {
  195. const {disabled, children, ...rest} = this.props;
  196. if (disabled || window.localStorage.getItem('hide_anchors') === '1') {
  197. return children || null;
  198. }
  199. return <GuideAnchor {...rest}>{children}</GuideAnchor>;
  200. }
  201. }
  202. const GuideContainer = styled('div')`
  203. display: grid;
  204. grid-template-rows: repeat(2, auto);
  205. grid-gap: ${space(2)};
  206. text-align: center;
  207. line-height: 1.5;
  208. background-color: ${p => p.theme.purple300};
  209. border-color: ${p => p.theme.purple300};
  210. color: ${p => p.theme.white};
  211. `;
  212. const GuideContent = styled('div')`
  213. display: grid;
  214. grid-template-rows: repeat(2, auto);
  215. grid-gap: ${space(1)};
  216. a {
  217. color: ${p => p.theme.white};
  218. text-decoration: underline;
  219. }
  220. `;
  221. const GuideTitle = styled('div')`
  222. font-weight: bold;
  223. font-size: ${p => p.theme.fontSizeExtraLarge};
  224. `;
  225. const GuideDescription = styled('div')`
  226. font-size: ${p => p.theme.fontSizeMedium};
  227. `;
  228. const GuideAction = styled('div')`
  229. display: grid;
  230. grid-template-rows: repeat(2, auto);
  231. grid-gap: ${space(1)};
  232. `;
  233. const StyledButton = styled(Button)`
  234. font-size: ${p => p.theme.fontSizeMedium};
  235. min-width: 40%;
  236. `;
  237. const DismissButton = styled(StyledButton)`
  238. margin-left: ${space(1)};
  239. &:hover,
  240. &:focus,
  241. &:active {
  242. color: ${p => p.theme.white};
  243. }
  244. color: ${p => p.theme.white};
  245. `;
  246. const StepCount = styled('div')`
  247. font-size: ${p => p.theme.fontSizeSmall};
  248. font-weight: bold;
  249. text-transform: uppercase;
  250. `;
  251. const StyledHovercard = styled(Hovercard)`
  252. ${HovercardBody} {
  253. background-color: ${theme.purple300};
  254. margin: -1px;
  255. border-radius: ${theme.borderRadius};
  256. width: 300px;
  257. }
  258. `;