settingsBreadcrumbStore.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import {PlainRoute} from 'react-router';
  2. import {createStore, Store, StoreDefinition} from 'reflux';
  3. import SettingsBreadcrumbActions from 'sentry/actions/settingsBreadcrumbActions';
  4. import getRouteStringFromRoutes from 'sentry/utils/getRouteStringFromRoutes';
  5. import {makeSafeRefluxStore, SafeStoreDefinition} from 'sentry/utils/makeSafeRefluxStore';
  6. type UpdateData = {
  7. routes: PlainRoute<any>[];
  8. title: string;
  9. };
  10. type SettingsBreadcrumbStoreInterface = {
  11. getPathMap(): Internals['pathMap'];
  12. init(): void;
  13. onTrimMappings(routes: PlainRoute<any>[]): void;
  14. onUpdateRouteMap(update: UpdateData): void;
  15. reset(): void;
  16. };
  17. type Internals = {
  18. pathMap: Record<string, string>;
  19. };
  20. const storeConfig: StoreDefinition &
  21. Internals &
  22. SettingsBreadcrumbStoreInterface &
  23. SafeStoreDefinition = {
  24. pathMap: {},
  25. unsubscribeListeners: [],
  26. init() {
  27. this.reset();
  28. this.unsubscribeListeners.push(
  29. this.listenTo(SettingsBreadcrumbActions.mapTitle, this.onUpdateRouteMap)
  30. );
  31. this.unsubscribeListeners.push(
  32. this.listenTo(SettingsBreadcrumbActions.trimMappings, this.onTrimMappings)
  33. );
  34. },
  35. reset() {
  36. this.pathMap = {};
  37. },
  38. getPathMap() {
  39. return this.pathMap;
  40. },
  41. onUpdateRouteMap({routes, title}) {
  42. this.pathMap[getRouteStringFromRoutes(routes)] = title;
  43. this.trigger(this.pathMap);
  44. },
  45. onTrimMappings(routes) {
  46. const routePath = getRouteStringFromRoutes(routes);
  47. for (const fullPath in this.pathMap) {
  48. if (!routePath.startsWith(fullPath)) {
  49. delete this.pathMap[fullPath];
  50. }
  51. }
  52. this.trigger(this.pathMap);
  53. },
  54. };
  55. const SettingsBreadcrumbStore = createStore(makeSafeRefluxStore(storeConfig)) as Store &
  56. SettingsBreadcrumbStoreInterface;
  57. export default SettingsBreadcrumbStore;