guideStore.tsx 8.2 KB

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