guideStore.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import {browserHistory} from 'react-router';
  2. import {createStore, StoreDefinition} from 'reflux';
  3. import GuideActions from 'sentry/actions/guideActions';
  4. import OrganizationsActions from 'sentry/actions/organizationsActions';
  5. import getGuidesContent from 'sentry/components/assistant/getGuidesContent';
  6. import {Guide, GuidesContent, GuidesServerData} from 'sentry/components/assistant/types';
  7. import {IS_ACCEPTANCE_TEST} from 'sentry/constants';
  8. import ConfigStore from 'sentry/stores/configStore';
  9. import {trackAnalyticsEvent} from 'sentry/utils/analytics';
  10. import {
  11. cleanupActiveRefluxSubscriptions,
  12. makeSafeRefluxStore,
  13. } from 'sentry/utils/makeSafeRefluxStore';
  14. function guidePrioritySort(a: Guide, b: Guide) {
  15. const a_priority = a.priority ?? Number.MAX_SAFE_INTEGER;
  16. const b_priority = b.priority ?? Number.MAX_SAFE_INTEGER;
  17. if (a_priority === b_priority) {
  18. return a.guide.localeCompare(b.guide);
  19. }
  20. // lower number takes priority
  21. return a_priority - b_priority;
  22. }
  23. export type GuideStoreState = {
  24. /**
  25. * Anchors that are currently mounted
  26. */
  27. anchors: Set<string>;
  28. /**
  29. * The current guide
  30. */
  31. currentGuide: Guide | null;
  32. /**
  33. * Current step of the current guide
  34. */
  35. currentStep: number;
  36. /**
  37. * Hides guides that normally would be shown
  38. */
  39. forceHide: boolean;
  40. /**
  41. * We force show a guide if the URL contains #assistant
  42. */
  43. forceShow: boolean;
  44. /**
  45. * All tooltip guides
  46. */
  47. guides: Guide[];
  48. /**
  49. * Current organization id
  50. */
  51. orgId: string | null;
  52. /**
  53. * Current organization slug
  54. */
  55. orgSlug: string | null;
  56. /**
  57. * The previously shown guide
  58. */
  59. prevGuide: Guide | null;
  60. };
  61. const defaultState: GuideStoreState = {
  62. forceHide: false,
  63. guides: [],
  64. anchors: new Set(),
  65. currentGuide: null,
  66. currentStep: 0,
  67. orgId: null,
  68. orgSlug: null,
  69. forceShow: false,
  70. prevGuide: null,
  71. };
  72. interface GuideStoreDefinition extends StoreDefinition {
  73. browserHistoryListener: null | (() => void);
  74. onFetchSucceeded(data: GuidesServerData): void;
  75. onRegisterAnchor(target: string): void;
  76. onSetForceHide(forceHide: boolean): void;
  77. onUnregisterAnchor(target: string): void;
  78. recordCue(guide: string): void;
  79. state: GuideStoreState;
  80. updatePrevGuide(nextGuide: Guide | null): void;
  81. }
  82. const storeConfig: GuideStoreDefinition = {
  83. state: defaultState,
  84. unsubscribeListeners: [],
  85. browserHistoryListener: null,
  86. init() {
  87. this.state = defaultState;
  88. this.unsubscribeListeners.push(
  89. this.listenTo(GuideActions.fetchSucceeded, this.onFetchSucceeded)
  90. );
  91. this.unsubscribeListeners.push(
  92. this.listenTo(GuideActions.closeGuide, this.onCloseGuide)
  93. );
  94. this.unsubscribeListeners.push(this.listenTo(GuideActions.nextStep, this.onNextStep));
  95. this.unsubscribeListeners.push(this.listenTo(GuideActions.toStep, this.onToStep));
  96. this.unsubscribeListeners.push(
  97. this.listenTo(GuideActions.registerAnchor, this.onRegisterAnchor)
  98. );
  99. this.unsubscribeListeners.push(
  100. this.listenTo(GuideActions.unregisterAnchor, this.onUnregisterAnchor)
  101. );
  102. this.unsubscribeListeners.push(
  103. this.listenTo(GuideActions.setForceHide, this.onSetForceHide)
  104. );
  105. this.unsubscribeListeners.push(
  106. this.listenTo(OrganizationsActions.setActive, this.onSetActiveOrganization)
  107. );
  108. window.addEventListener('load', this.onURLChange, false);
  109. this.browserHistoryListener = browserHistory.listen(() => this.onURLChange());
  110. },
  111. teardown() {
  112. cleanupActiveRefluxSubscriptions(this.unsubscribeListeners);
  113. window.removeEventListener('load', this.onURLChange);
  114. if (this.browserHistoryListener) {
  115. this.browserHistoryListener();
  116. }
  117. },
  118. onURLChange() {
  119. this.state.forceShow = window.location.hash === '#assistant';
  120. this.updateCurrentGuide();
  121. },
  122. onSetActiveOrganization(data) {
  123. this.state.orgId = data ? data.id : null;
  124. this.state.orgSlug = data ? data.slug : null;
  125. this.updateCurrentGuide();
  126. },
  127. onFetchSucceeded(data) {
  128. // It's possible we can get empty responses (seems to be Firefox specific)
  129. // Do nothing if `data` is empty
  130. // also, temporarily check data is in the correct format from the updated
  131. // assistant endpoint
  132. if (!data || !Array.isArray(data)) {
  133. return;
  134. }
  135. const guidesContent: GuidesContent = getGuidesContent(this.state.orgSlug);
  136. // map server guide state (i.e. seen status) with guide content
  137. const guides = guidesContent.reduce((acc: Guide[], content) => {
  138. const serverGuide = data.find(guide => guide.guide === content.guide);
  139. serverGuide &&
  140. acc.push({
  141. ...content,
  142. ...serverGuide,
  143. });
  144. return acc;
  145. }, []);
  146. this.state.guides = guides;
  147. this.updateCurrentGuide();
  148. },
  149. onCloseGuide(dismissed?: boolean) {
  150. const {currentGuide, guides} = this.state;
  151. // update the current guide seen to true or all guides
  152. // if markOthersAsSeen is true and the user is dismissing
  153. guides
  154. .filter(
  155. guide =>
  156. guide.guide === currentGuide?.guide ||
  157. (currentGuide?.markOthersAsSeen && dismissed)
  158. )
  159. .forEach(guide => (guide.seen = true));
  160. this.state.forceShow = false;
  161. this.updateCurrentGuide();
  162. },
  163. onNextStep() {
  164. this.state.currentStep += 1;
  165. this.trigger(this.state);
  166. },
  167. onToStep(step: number) {
  168. this.state.currentStep = step;
  169. this.trigger(this.state);
  170. },
  171. onRegisterAnchor(target) {
  172. this.state.anchors.add(target);
  173. this.updateCurrentGuide();
  174. },
  175. onUnregisterAnchor(target) {
  176. this.state.anchors.delete(target);
  177. this.updateCurrentGuide();
  178. },
  179. onSetForceHide(forceHide) {
  180. this.state.forceHide = forceHide;
  181. this.trigger(this.state);
  182. },
  183. recordCue(guide) {
  184. const user = ConfigStore.get('user');
  185. if (!user) {
  186. return;
  187. }
  188. const data = {
  189. guide,
  190. eventKey: 'assistant.guide_cued',
  191. eventName: 'Assistant Guide Cued',
  192. organization_id: this.state.orgId,
  193. user_id: parseInt(user.id, 10),
  194. };
  195. trackAnalyticsEvent(data);
  196. },
  197. updatePrevGuide(nextGuide) {
  198. const {prevGuide} = this.state;
  199. if (!nextGuide) {
  200. return;
  201. }
  202. if (!prevGuide || prevGuide.guide !== nextGuide.guide) {
  203. this.recordCue(nextGuide.guide);
  204. this.state.prevGuide = nextGuide;
  205. }
  206. },
  207. /**
  208. * Logic to determine if a guide is shown:
  209. *
  210. * - If any required target is missing, don't show the guide
  211. * - If the URL ends with #assistant, show the guide
  212. * - If the user has already seen the guide, don't show the guide
  213. * - Otherwise show the guide
  214. */
  215. updateCurrentGuide() {
  216. const {anchors, guides, forceShow} = this.state;
  217. let guideOptions = guides
  218. .sort(guidePrioritySort)
  219. .filter(guide => guide.requiredTargets.every(target => anchors.has(target)));
  220. const user = ConfigStore.get('user');
  221. const assistantThreshold = new Date(2019, 6, 1);
  222. const userDateJoined = new Date(user?.dateJoined);
  223. if (!forceShow) {
  224. guideOptions = guideOptions.filter(({seen, dateThreshold}) => {
  225. if (seen) {
  226. return false;
  227. }
  228. if (user?.isSuperuser && !IS_ACCEPTANCE_TEST) {
  229. return true;
  230. }
  231. if (dateThreshold) {
  232. // Show the guide to users who've joined before the date threshold
  233. return userDateJoined < dateThreshold;
  234. }
  235. return userDateJoined > assistantThreshold;
  236. });
  237. }
  238. // Remove steps that are missing anchors, unless the anchor is included in
  239. // the expectedTargets and will appear at the step.
  240. const nextGuide =
  241. guideOptions.length > 0
  242. ? {
  243. ...guideOptions[0],
  244. steps: guideOptions[0].steps.filter(
  245. step =>
  246. anchors.has(step.target) ||
  247. guideOptions[0]?.expectedTargets?.includes(step.target)
  248. ),
  249. }
  250. : null;
  251. this.updatePrevGuide(nextGuide);
  252. this.state.currentStep =
  253. this.state.currentGuide &&
  254. nextGuide &&
  255. this.state.currentGuide.guide === nextGuide.guide
  256. ? this.state.currentStep
  257. : 0;
  258. this.state.currentGuide = nextGuide;
  259. this.trigger(this.state);
  260. },
  261. };
  262. const GuideStore = createStore(makeSafeRefluxStore(storeConfig));
  263. export default GuideStore;