guideStore.tsx 7.6 KB

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