guideStore.tsx 7.7 KB

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