formSearchStore.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import Reflux from 'reflux';
  2. import FormSearchActions from 'sentry/actions/formSearchActions';
  3. import {FieldObject} from 'sentry/views/settings/components/forms/type';
  4. /**
  5. * Processed form field metadata.
  6. */
  7. export type FormSearchField = {
  8. route: string;
  9. title: React.ReactNode;
  10. description: React.ReactNode;
  11. field: FieldObject;
  12. };
  13. type StoreInterface = {
  14. reset(): void;
  15. get(): Internals['searchMap'];
  16. };
  17. type Internals = {
  18. searchMap: null | FormSearchField[];
  19. onLoadSearchMap: (searchMap: null | FormSearchField[]) => void;
  20. };
  21. /**
  22. * Store for "form" searches, but probably will include more
  23. */
  24. const storeConfig: Reflux.StoreDefinition & Internals & StoreInterface = {
  25. searchMap: null,
  26. init() {
  27. this.reset();
  28. this.listenTo(FormSearchActions.loadSearchMap, this.onLoadSearchMap);
  29. },
  30. get() {
  31. return this.searchMap;
  32. },
  33. reset() {
  34. // `null` means it hasn't been loaded yet
  35. this.searchMap = null;
  36. },
  37. /**
  38. * Adds to search map
  39. */
  40. onLoadSearchMap(searchMap) {
  41. // Only load once
  42. if (this.searchMap !== null) {
  43. return;
  44. }
  45. this.searchMap = searchMap;
  46. this.trigger(this.searchMap);
  47. },
  48. };
  49. const FormSearchStore = Reflux.createStore(storeConfig) as Reflux.Store & StoreInterface;
  50. export default FormSearchStore;