guideStore.tsx 7.3 KB

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