guideStore.tsx 8.5 KB

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