guideStore.tsx 7.5 KB

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