guideStore.tsx 7.6 KB

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