guideStore.tsx 7.4 KB

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