guideAnchor.tsx 8.3 KB

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