guideAnchor.tsx 8.2 KB

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