guideStore.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 type {StrictStoreDefinition} from './types';
  14. function guidePrioritySort(a: Guide, b: Guide) {
  15. const a_priority = a.priority ?? Number.MAX_SAFE_INTEGER;
  16. const b_priority = b.priority ?? Number.MAX_SAFE_INTEGER;
  17. if (a_priority === b_priority) {
  18. return a.guide.localeCompare(b.guide);
  19. }
  20. // lower number takes priority
  21. return a_priority - b_priority;
  22. }
  23. export type GuideStoreState = {
  24. /**
  25. * Anchors that are currently mounted
  26. */
  27. anchors: Set<string>;
  28. /**
  29. * The current guide
  30. */
  31. currentGuide: Guide | null;
  32. /**
  33. * Current step of the current guide
  34. */
  35. currentStep: number;
  36. /**
  37. * Hides guides that normally would be shown
  38. */
  39. forceHide: boolean;
  40. /**
  41. * We force show a guide if the URL contains #assistant
  42. */
  43. forceShow: boolean;
  44. /**
  45. * All tooltip guides
  46. */
  47. guides: Guide[];
  48. /**
  49. * Current organization id
  50. */
  51. orgId: string | null;
  52. /**
  53. * Current organization slug
  54. */
  55. orgSlug: string | null;
  56. /**
  57. * Current Organization
  58. */
  59. organization: Organization | null;
  60. /**
  61. * The previously shown guide
  62. */
  63. prevGuide: Guide | null;
  64. };
  65. const defaultState: GuideStoreState = {
  66. forceHide: false,
  67. guides: [],
  68. anchors: new Set(),
  69. currentGuide: null,
  70. currentStep: 0,
  71. orgId: null,
  72. orgSlug: null,
  73. organization: null,
  74. forceShow: false,
  75. prevGuide: null,
  76. };
  77. function isForceEnabled() {
  78. return window.location.hash === '#assistant';
  79. }
  80. interface GuideStoreDefinition extends StrictStoreDefinition<GuideStoreState> {
  81. closeGuide(dismissed?: boolean): void;
  82. fetchSucceeded(data: GuidesServerData): void;
  83. modalStoreListener: null | Function;
  84. nextStep(): void;
  85. onURLChange(): void;
  86. recordCue(guide: string): void;
  87. registerAnchor(target: string): void;
  88. setActiveOrganization(data: Organization): void;
  89. setForceHide(forceHide: boolean): void;
  90. teardown(): void;
  91. toStep(step: number): void;
  92. unregisterAnchor(target: string): void;
  93. updateCurrentGuide(dismissed?: boolean): void;
  94. updatePrevGuide(nextGuide: Guide | null): void;
  95. }
  96. const storeConfig: GuideStoreDefinition = {
  97. state: {...defaultState},
  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, forceShow: isForceEnabled()};
  103. window.addEventListener('load', this.onURLChange, false);
  104. // Guides will show above modals, but are not interactable because
  105. // of the focus trap, so we force them to be hidden while a modal is open.
  106. this.modalStoreListener = ModalStore.listen(() => {
  107. const isOpen = typeof ModalStore.getState().renderer === 'function';
  108. if (isOpen) {
  109. this.setForceHide(true);
  110. } else {
  111. this.setForceHide(false);
  112. }
  113. }, undefined);
  114. },
  115. teardown() {
  116. window.removeEventListener('load', this.onURLChange);
  117. if (this.modalStoreListener) {
  118. this.modalStoreListener();
  119. }
  120. },
  121. getState() {
  122. return this.state;
  123. },
  124. onURLChange() {
  125. this.state = {...this.state, forceShow: isForceEnabled()};
  126. this.updateCurrentGuide();
  127. },
  128. setActiveOrganization(data: Organization) {
  129. this.state = {
  130. ...this.state,
  131. orgId: data ? data.id : null,
  132. orgSlug: data ? data.slug : null,
  133. organization: data ? data : null,
  134. };
  135. this.updateCurrentGuide();
  136. },
  137. fetchSucceeded(data) {
  138. // It's possible we can get empty responses (seems to be Firefox specific)
  139. // Do nothing if `data` is empty
  140. // also, temporarily check data is in the correct format from the updated
  141. // assistant endpoint
  142. if (!data || !Array.isArray(data)) {
  143. return;
  144. }
  145. const guidesContent: GuidesContent = getGuidesContent(this.state.organization);
  146. // map server guide state (i.e. seen status) with guide content
  147. const guides = guidesContent.reduce((acc: Guide[], content) => {
  148. const serverGuide = data.find(guide => guide.guide === content.guide);
  149. serverGuide &&
  150. acc.push({
  151. ...content,
  152. ...serverGuide,
  153. });
  154. return acc;
  155. }, []);
  156. this.state = {...this.state, guides};
  157. this.updateCurrentGuide();
  158. },
  159. closeGuide(dismissed?: boolean) {
  160. const {currentGuide, guides} = this.state;
  161. const newGuides = guides.map(guide => {
  162. // update the current guide seen to true or all guides
  163. // if markOthersAsSeen is true and the user is dismissing
  164. if (
  165. guide.guide === currentGuide?.guide ||
  166. (currentGuide?.markOthersAsSeen && dismissed)
  167. ) {
  168. return {
  169. ...guide,
  170. seen: true,
  171. };
  172. }
  173. return guide;
  174. });
  175. this.state = {...this.state, guides: newGuides, forceShow: false};
  176. this.updateCurrentGuide();
  177. },
  178. nextStep() {
  179. this.state = {...this.state, currentStep: this.state.currentStep + 1};
  180. this.trigger(this.state);
  181. },
  182. toStep(step: number) {
  183. this.state = {...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 = {...this.state, 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 = {...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. const currentStep =
  264. this.state.currentGuide &&
  265. nextGuide &&
  266. this.state.currentGuide.guide === nextGuide.guide
  267. ? this.state.currentStep
  268. : 0;
  269. this.state = {...this.state, currentGuide: nextGuide, currentStep};
  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;