guideStore.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import {browserHistory} from 'react-router';
  2. import Reflux from 'reflux';
  3. import GuideActions from 'sentry/actions/guideActions';
  4. import OrganizationsActions from 'sentry/actions/organizationsActions';
  5. import {Client} from 'sentry/api';
  6. import getGuidesContent from 'sentry/components/assistant/getGuidesContent';
  7. import {Guide, GuidesContent, GuidesServerData} from 'sentry/components/assistant/types';
  8. import ConfigStore from 'sentry/stores/configStore';
  9. import {trackAnalyticsEvent} from 'sentry/utils/analytics';
  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. * All tooltip guides
  22. */
  23. guides: Guide[];
  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. * Current organization id
  38. */
  39. orgId: string | null;
  40. /**
  41. * Current organization slug
  42. */
  43. orgSlug: string | null;
  44. /**
  45. * We force show a guide if the URL contains #assistant
  46. */
  47. forceShow: boolean;
  48. /**
  49. * The previously shown guide
  50. */
  51. prevGuide: Guide | null;
  52. };
  53. const defaultState: GuideStoreState = {
  54. guides: [],
  55. anchors: new Set(),
  56. currentGuide: null,
  57. currentStep: 0,
  58. orgId: null,
  59. orgSlug: null,
  60. forceShow: false,
  61. prevGuide: null,
  62. };
  63. type GuideStoreInterface = {
  64. state: GuideStoreState;
  65. onFetchSucceeded(data: GuidesServerData): void;
  66. onRegisterAnchor(target: string): void;
  67. onUnregisterAnchor(target: string): void;
  68. recordCue(guide: string): void;
  69. updatePrevGuide(nextGuide: Guide | null): void;
  70. };
  71. const storeConfig: Reflux.StoreDefinition & GuideStoreInterface = {
  72. state: defaultState,
  73. init() {
  74. this.state = defaultState;
  75. this.api = new Client();
  76. this.listenTo(GuideActions.fetchSucceeded, this.onFetchSucceeded);
  77. this.listenTo(GuideActions.closeGuide, this.onCloseGuide);
  78. this.listenTo(GuideActions.nextStep, this.onNextStep);
  79. this.listenTo(GuideActions.toStep, this.onToStep);
  80. this.listenTo(GuideActions.registerAnchor, this.onRegisterAnchor);
  81. this.listenTo(GuideActions.unregisterAnchor, this.onUnregisterAnchor);
  82. this.listenTo(OrganizationsActions.setActive, this.onSetActiveOrganization);
  83. window.addEventListener('load', this.onURLChange, false);
  84. browserHistory.listen(() => this.onURLChange());
  85. },
  86. onURLChange() {
  87. this.state.forceShow = window.location.hash === '#assistant';
  88. this.updateCurrentGuide();
  89. },
  90. onSetActiveOrganization(data) {
  91. this.state.orgId = data ? data.id : null;
  92. this.state.orgSlug = data ? data.slug : null;
  93. this.updateCurrentGuide();
  94. },
  95. onFetchSucceeded(data) {
  96. // It's possible we can get empty responses (seems to be Firefox specific)
  97. // Do nothing if `data` is empty
  98. // also, temporarily check data is in the correct format from the updated
  99. // assistant endpoint
  100. if (!data || !Array.isArray(data)) {
  101. return;
  102. }
  103. const guidesContent: GuidesContent = getGuidesContent(this.state.orgSlug);
  104. // map server guide state (i.e. seen status) with guide content
  105. const guides = guidesContent.reduce((acc: Guide[], content) => {
  106. const serverGuide = data.find(guide => guide.guide === content.guide);
  107. serverGuide &&
  108. acc.push({
  109. ...content,
  110. ...serverGuide,
  111. });
  112. return acc;
  113. }, []);
  114. this.state.guides = guides;
  115. this.updateCurrentGuide();
  116. },
  117. onCloseGuide(dismissed?: boolean) {
  118. const {currentGuide, guides} = this.state;
  119. // update the current guide seen to true or all guides
  120. // if markOthersAsSeen is true and the user is dismissing
  121. guides
  122. .filter(
  123. guide =>
  124. guide.guide === currentGuide?.guide ||
  125. (currentGuide?.markOthersAsSeen && dismissed)
  126. )
  127. .forEach(guide => (guide.seen = true));
  128. this.state.forceShow = false;
  129. this.updateCurrentGuide();
  130. },
  131. onNextStep() {
  132. this.state.currentStep += 1;
  133. this.trigger(this.state);
  134. },
  135. onToStep(step: number) {
  136. this.state.currentStep = step;
  137. this.trigger(this.state);
  138. },
  139. onRegisterAnchor(target) {
  140. this.state.anchors.add(target);
  141. this.updateCurrentGuide();
  142. },
  143. onUnregisterAnchor(target) {
  144. this.state.anchors.delete(target);
  145. this.updateCurrentGuide();
  146. },
  147. recordCue(guide) {
  148. const user = ConfigStore.get('user');
  149. if (!user) {
  150. return;
  151. }
  152. const data = {
  153. guide,
  154. eventKey: 'assistant.guide_cued',
  155. eventName: 'Assistant Guide Cued',
  156. organization_id: this.state.orgId,
  157. user_id: parseInt(user.id, 10),
  158. };
  159. trackAnalyticsEvent(data);
  160. },
  161. updatePrevGuide(nextGuide) {
  162. const {prevGuide} = this.state;
  163. if (!nextGuide) {
  164. return;
  165. }
  166. if (!prevGuide || prevGuide.guide !== nextGuide.guide) {
  167. this.recordCue(nextGuide.guide);
  168. this.state.prevGuide = nextGuide;
  169. }
  170. },
  171. /**
  172. * Logic to determine if a guide is shown:
  173. *
  174. * - If any required target is missing, don't show the guide
  175. * - If the URL ends with #assistant, show the guide
  176. * - If the user has already seen the guide, don't show the guide
  177. * - Otherwise show the guide
  178. */
  179. updateCurrentGuide() {
  180. const {anchors, guides, forceShow} = this.state;
  181. let guideOptions = guides
  182. .sort(guidePrioritySort)
  183. .filter(guide => guide.requiredTargets.every(target => anchors.has(target)));
  184. const user = ConfigStore.get('user');
  185. const assistantThreshold = new Date(2019, 6, 1);
  186. const userDateJoined = new Date(user?.dateJoined);
  187. if (!forceShow) {
  188. guideOptions = guideOptions.filter(({seen, dateThreshold}) => {
  189. if (seen) {
  190. return false;
  191. }
  192. if (user?.isSuperuser) {
  193. return true;
  194. }
  195. if (dateThreshold) {
  196. // Show the guide to users who've joined before the date threshold
  197. return userDateJoined < dateThreshold;
  198. }
  199. return userDateJoined > assistantThreshold;
  200. });
  201. }
  202. const nextGuide =
  203. guideOptions.length > 0
  204. ? {
  205. ...guideOptions[0],
  206. steps: guideOptions[0].steps.filter(
  207. step => step.target && anchors.has(step.target)
  208. ),
  209. }
  210. : null;
  211. this.updatePrevGuide(nextGuide);
  212. this.state.currentStep =
  213. this.state.currentGuide &&
  214. nextGuide &&
  215. this.state.currentGuide.guide === nextGuide.guide
  216. ? this.state.currentStep
  217. : 0;
  218. this.state.currentGuide = nextGuide;
  219. this.trigger(this.state);
  220. },
  221. };
  222. const GuideStore = Reflux.createStore(storeConfig) as Reflux.Store & GuideStoreInterface;
  223. export default GuideStore;