guideStore.tsx 7.5 KB

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