guideStore.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import {browserHistory} from 'react-router';
  2. import {createStore} from 'reflux';
  3. import getGuidesContent from 'sentry/components/assistant/getGuidesContent';
  4. import {Guide, GuidesContent, GuidesServerData} from 'sentry/components/assistant/types';
  5. import {IS_ACCEPTANCE_TEST} from 'sentry/constants';
  6. import ConfigStore from 'sentry/stores/configStore';
  7. import HookStore from 'sentry/stores/hookStore';
  8. import {Organization} from 'sentry/types';
  9. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  10. import {CommonStoreDefinition} from './types';
  11. function guidePrioritySort(a: Guide, b: Guide) {
  12. const a_priority = a.priority ?? Number.MAX_SAFE_INTEGER;
  13. const b_priority = b.priority ?? Number.MAX_SAFE_INTEGER;
  14. if (a_priority === b_priority) {
  15. return a.guide.localeCompare(b.guide);
  16. }
  17. // lower number takes priority
  18. return a_priority - b_priority;
  19. }
  20. export type GuideStoreState = {
  21. /**
  22. * Anchors that are currently mounted
  23. */
  24. anchors: Set<string>;
  25. /**
  26. * The current guide
  27. */
  28. currentGuide: Guide | null;
  29. /**
  30. * Current step of the current guide
  31. */
  32. currentStep: number;
  33. /**
  34. * Hides guides that normally would be shown
  35. */
  36. forceHide: boolean;
  37. /**
  38. * We force show a guide if the URL contains #assistant
  39. */
  40. forceShow: boolean;
  41. /**
  42. * All tooltip guides
  43. */
  44. guides: Guide[];
  45. /**
  46. * Current organization id
  47. */
  48. orgId: string | null;
  49. /**
  50. * Current organization slug
  51. */
  52. orgSlug: string | null;
  53. /**
  54. * The previously shown guide
  55. */
  56. prevGuide: Guide | null;
  57. };
  58. const defaultState: GuideStoreState = {
  59. forceHide: false,
  60. guides: [],
  61. anchors: new Set(),
  62. currentGuide: null,
  63. currentStep: 0,
  64. orgId: null,
  65. orgSlug: null,
  66. forceShow: false,
  67. prevGuide: null,
  68. };
  69. interface GuideStoreDefinition extends CommonStoreDefinition<GuideStoreState> {
  70. browserHistoryListener: null | (() => void);
  71. closeGuide(dismissed?: boolean): void;
  72. fetchSucceeded(data: GuidesServerData): void;
  73. nextStep(): void;
  74. recordCue(guide: string): void;
  75. registerAnchor(target: string): void;
  76. setActiveOrganization(data: Organization): void;
  77. setForceHide(forceHide: boolean): void;
  78. state: GuideStoreState;
  79. teardown(): void;
  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. browserHistoryListener: null,
  87. init() {
  88. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  89. // listeners due to their leaky nature in tests.
  90. this.state = defaultState;
  91. window.addEventListener('load', this.onURLChange, false);
  92. this.browserHistoryListener = browserHistory.listen(() => this.onURLChange());
  93. },
  94. teardown() {
  95. window.removeEventListener('load', this.onURLChange);
  96. if (this.browserHistoryListener) {
  97. this.browserHistoryListener();
  98. }
  99. },
  100. getState() {
  101. return this.state;
  102. },
  103. onURLChange() {
  104. this.state.forceShow = window.location.hash === '#assistant';
  105. this.updateCurrentGuide();
  106. },
  107. setActiveOrganization(data: Organization) {
  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. trackAdvancedAnalyticsEvent('assistant.guide_cued', {
  174. organization: this.state.orgId,
  175. guide,
  176. });
  177. },
  178. updatePrevGuide(nextGuide) {
  179. const {prevGuide} = this.state;
  180. if (!nextGuide) {
  181. return;
  182. }
  183. if (!prevGuide || prevGuide.guide !== nextGuide.guide) {
  184. this.recordCue(nextGuide.guide);
  185. this.state.prevGuide = nextGuide;
  186. }
  187. },
  188. /**
  189. * Logic to determine if a guide is shown:
  190. *
  191. * - If any required target is missing, don't show the guide
  192. * - If the URL ends with #assistant, show the guide
  193. * - If the user has already seen the guide, don't show the guide
  194. * - Otherwise show the guide
  195. */
  196. updateCurrentGuide(dismissed?: boolean) {
  197. const {anchors, guides, forceShow} = this.state;
  198. let guideOptions = guides
  199. .sort(guidePrioritySort)
  200. .filter(guide => guide.requiredTargets.every(target => anchors.has(target)));
  201. const user = ConfigStore.get('user');
  202. const assistantThreshold = new Date(2019, 6, 1);
  203. const userDateJoined = new Date(user?.dateJoined);
  204. if (!forceShow) {
  205. guideOptions = guideOptions.filter(({seen, dateThreshold}) => {
  206. if (seen) {
  207. return false;
  208. }
  209. if (user?.isSuperuser && !IS_ACCEPTANCE_TEST) {
  210. return true;
  211. }
  212. if (dateThreshold) {
  213. // Show the guide to users who've joined before the date threshold
  214. return userDateJoined < dateThreshold;
  215. }
  216. return userDateJoined > assistantThreshold;
  217. });
  218. }
  219. // Remove steps that are missing anchors, unless the anchor is included in
  220. // the expectedTargets and will appear at the step.
  221. const nextGuide =
  222. guideOptions.length > 0
  223. ? {
  224. ...guideOptions[0],
  225. steps: guideOptions[0].steps.filter(
  226. step =>
  227. anchors.has(step.target) ||
  228. guideOptions[0]?.expectedTargets?.includes(step.target)
  229. ),
  230. }
  231. : null;
  232. this.updatePrevGuide(nextGuide);
  233. this.state.currentStep =
  234. this.state.currentGuide &&
  235. nextGuide &&
  236. this.state.currentGuide.guide === nextGuide.guide
  237. ? this.state.currentStep
  238. : 0;
  239. this.state.currentGuide = nextGuide;
  240. this.trigger(this.state);
  241. HookStore.get('callback:on-guide-update').map(cb => cb(nextGuide, {dismissed}));
  242. },
  243. };
  244. const GuideStore = createStore(storeConfig);
  245. export default GuideStore;