settingsBreadcrumbStore.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import {PlainRoute} from 'react-router';
  2. import {createStore, StoreDefinition} from 'reflux';
  3. import SettingsBreadcrumbActions from 'sentry/actions/settingsBreadcrumbActions';
  4. import getRouteStringFromRoutes from 'sentry/utils/getRouteStringFromRoutes';
  5. import {makeSafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
  6. type UpdateData = {
  7. routes: PlainRoute<any>[];
  8. title: string;
  9. };
  10. interface SettingsBreadcrumbStoreDefinition extends StoreDefinition {
  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: SettingsBreadcrumbStoreDefinition = {
  21. pathMap: {},
  22. unsubscribeListeners: [],
  23. init() {
  24. this.reset();
  25. this.unsubscribeListeners.push(
  26. this.listenTo(SettingsBreadcrumbActions.mapTitle, this.onUpdateRouteMap)
  27. );
  28. this.unsubscribeListeners.push(
  29. this.listenTo(SettingsBreadcrumbActions.trimMappings, this.onTrimMappings)
  30. );
  31. },
  32. reset() {
  33. this.pathMap = {};
  34. },
  35. getPathMap() {
  36. return this.pathMap;
  37. },
  38. onUpdateRouteMap({routes, title}) {
  39. this.pathMap[getRouteStringFromRoutes(routes)] = title;
  40. this.trigger(this.pathMap);
  41. },
  42. onTrimMappings(routes) {
  43. const routePath = getRouteStringFromRoutes(routes);
  44. for (const fullPath in this.pathMap) {
  45. if (!routePath.startsWith(fullPath)) {
  46. delete this.pathMap[fullPath];
  47. }
  48. }
  49. this.trigger(this.pathMap);
  50. },
  51. };
  52. const SettingsBreadcrumbStore = createStore(makeSafeRefluxStore(storeConfig));
  53. export default SettingsBreadcrumbStore;