settingsBreadcrumbStore.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import {PlainRoute} from 'react-router';
  2. import Reflux from 'reflux';
  3. import SettingsBreadcrumbActions from 'app/actions/settingsBreadcrumbActions';
  4. import getRouteStringFromRoutes from 'app/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. };
  15. type Internals = {
  16. pathMap: Record<string, string>;
  17. };
  18. const storeConfig: Reflux.StoreDefinition &
  19. SettingsBreadcrumbStoreInterface &
  20. Internals = {
  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. onUpdateRouteMap({routes, title}) {
  31. this.pathMap[getRouteStringFromRoutes(routes)] = title;
  32. this.trigger(this.pathMap);
  33. },
  34. onTrimMappings(routes) {
  35. const routePath = getRouteStringFromRoutes(routes);
  36. for (const fullPath in this.pathMap) {
  37. if (!routePath.startsWith(fullPath)) {
  38. delete this.pathMap[fullPath];
  39. }
  40. }
  41. this.trigger(this.pathMap);
  42. },
  43. };
  44. type SettingsBreadcrumbStore = Reflux.Store & SettingsBreadcrumbStoreInterface;
  45. const SettingsBreadcrumbStore = Reflux.createStore(
  46. storeConfig
  47. ) as SettingsBreadcrumbStore;
  48. export default SettingsBreadcrumbStore;