guideStore.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 HookStore from 'sentry/stores/hookStore';
  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. closeGuide(dismissed?: boolean): void;
  75. fetchSucceeded(data: GuidesServerData): void;
  76. nextStep(): void;
  77. recordCue(guide: string): void;
  78. registerAnchor(target: string): void;
  79. setForceHide(forceHide: boolean): void;
  80. state: GuideStoreState;
  81. toStep(step: number): void;
  82. unregisterAnchor(target: string): void;
  83. updatePrevGuide(nextGuide: Guide | null): void;
  84. }
  85. const storeConfig: GuideStoreDefinition = {
  86. state: defaultState,
  87. unsubscribeListeners: [],
  88. browserHistoryListener: null,
  89. init() {
  90. this.state = defaultState;
  91. this.unsubscribeListeners.push(
  92. this.listenTo(OrganizationsActions.setActive, this.onSetActiveOrganization)
  93. );
  94. window.addEventListener('load', this.onURLChange, false);
  95. this.browserHistoryListener = browserHistory.listen(() => this.onURLChange());
  96. },
  97. teardown() {
  98. cleanupActiveRefluxSubscriptions(this.unsubscribeListeners);
  99. window.removeEventListener('load', this.onURLChange);
  100. if (this.browserHistoryListener) {
  101. this.browserHistoryListener();
  102. }
  103. },
  104. onURLChange() {
  105. this.state.forceShow = window.location.hash === '#assistant';
  106. this.updateCurrentGuide();
  107. },
  108. onSetActiveOrganization(data) {
  109. this.state.orgId = data ? data.id : null;
  110. this.state.orgSlug = data ? data.slug : null;
  111. this.updateCurrentGuide();
  112. },
  113. fetchSucceeded(data) {
  114. // It's possible we can get empty responses (seems to be Firefox specific)
  115. // Do nothing if `data` is empty
  116. // also, temporarily check data is in the correct format from the updated
  117. // assistant endpoint
  118. if (!data || !Array.isArray(data)) {
  119. return;
  120. }
  121. const guidesContent: GuidesContent = getGuidesContent(this.state.orgSlug);
  122. // map server guide state (i.e. seen status) with guide content
  123. const guides = guidesContent.reduce((acc: Guide[], content) => {
  124. const serverGuide = data.find(guide => guide.guide === content.guide);
  125. serverGuide &&
  126. acc.push({
  127. ...content,
  128. ...serverGuide,
  129. });
  130. return acc;
  131. }, []);
  132. this.state.guides = guides;
  133. this.updateCurrentGuide();
  134. },
  135. closeGuide(dismissed?: boolean) {
  136. const {currentGuide, guides} = this.state;
  137. // update the current guide seen to true or all guides
  138. // if markOthersAsSeen is true and the user is dismissing
  139. guides
  140. .filter(
  141. guide =>
  142. guide.guide === currentGuide?.guide ||
  143. (currentGuide?.markOthersAsSeen && dismissed)
  144. )
  145. .forEach(guide => (guide.seen = true));
  146. this.state.forceShow = false;
  147. this.updateCurrentGuide();
  148. },
  149. nextStep() {
  150. this.state.currentStep += 1;
  151. this.trigger(this.state);
  152. },
  153. toStep(step: number) {
  154. this.state.currentStep = step;
  155. this.trigger(this.state);
  156. },
  157. registerAnchor(target) {
  158. this.state.anchors.add(target);
  159. this.updateCurrentGuide();
  160. },
  161. unregisterAnchor(target) {
  162. this.state.anchors.delete(target);
  163. this.updateCurrentGuide();
  164. },
  165. setForceHide(forceHide) {
  166. this.state.forceHide = forceHide;
  167. this.trigger(this.state);
  168. },
  169. recordCue(guide) {
  170. const user = ConfigStore.get('user');
  171. if (!user) {
  172. return;
  173. }
  174. const data = {
  175. guide,
  176. eventKey: 'assistant.guide_cued',
  177. eventName: 'Assistant Guide Cued',
  178. organization_id: this.state.orgId,
  179. user_id: parseInt(user.id, 10),
  180. };
  181. trackAnalyticsEvent(data);
  182. },
  183. updatePrevGuide(nextGuide) {
  184. const {prevGuide} = this.state;
  185. if (!nextGuide) {
  186. return;
  187. }
  188. if (!prevGuide || prevGuide.guide !== nextGuide.guide) {
  189. this.recordCue(nextGuide.guide);
  190. this.state.prevGuide = nextGuide;
  191. }
  192. },
  193. /**
  194. * Logic to determine if a guide is shown:
  195. *
  196. * - If any required target is missing, don't show the guide
  197. * - If the URL ends with #assistant, show the guide
  198. * - If the user has already seen the guide, don't show the guide
  199. * - Otherwise show the guide
  200. */
  201. updateCurrentGuide(dismissed?: boolean) {
  202. const {anchors, guides, forceShow} = this.state;
  203. let guideOptions = guides
  204. .sort(guidePrioritySort)
  205. .filter(guide => guide.requiredTargets.every(target => anchors.has(target)));
  206. const user = ConfigStore.get('user');
  207. const assistantThreshold = new Date(2019, 6, 1);
  208. const userDateJoined = new Date(user?.dateJoined);
  209. if (!forceShow) {
  210. guideOptions = guideOptions.filter(({seen, dateThreshold}) => {
  211. if (seen) {
  212. return false;
  213. }
  214. if (user?.isSuperuser && !IS_ACCEPTANCE_TEST) {
  215. return true;
  216. }
  217. if (dateThreshold) {
  218. // Show the guide to users who've joined before the date threshold
  219. return userDateJoined < dateThreshold;
  220. }
  221. return userDateJoined > assistantThreshold;
  222. });
  223. }
  224. // Remove steps that are missing anchors, unless the anchor is included in
  225. // the expectedTargets and will appear at the step.
  226. const nextGuide =
  227. guideOptions.length > 0
  228. ? {
  229. ...guideOptions[0],
  230. steps: guideOptions[0].steps.filter(
  231. step =>
  232. anchors.has(step.target) ||
  233. guideOptions[0]?.expectedTargets?.includes(step.target)
  234. ),
  235. }
  236. : null;
  237. this.updatePrevGuide(nextGuide);
  238. this.state.currentStep =
  239. this.state.currentGuide &&
  240. nextGuide &&
  241. this.state.currentGuide.guide === nextGuide.guide
  242. ? this.state.currentStep
  243. : 0;
  244. this.state.currentGuide = nextGuide;
  245. this.trigger(this.state);
  246. HookStore.get('callback:on-guide-update').map(cb => cb(nextGuide, {dismissed}));
  247. },
  248. };
  249. const GuideStore = createStore(makeSafeRefluxStore(storeConfig));
  250. export default GuideStore;