guideStore.tsx 8.1 KB

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