settingsBreadcrumbStore.tsx 1.5 KB

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