guideStore.tsx 8.1 KB

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