formSearchStore.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import {createStore, StoreDefinition} from 'reflux';
  2. import {FieldObject} from 'sentry/components/forms/types';
  3. /**
  4. * Processed form field metadata.
  5. */
  6. export type FormSearchField = {
  7. description: React.ReactNode;
  8. field: FieldObject;
  9. route: string;
  10. title: React.ReactNode;
  11. };
  12. interface StoreInterface {
  13. get(): InternalDefinition['searchMap'];
  14. reset(): void;
  15. }
  16. type InternalDefinition = {
  17. loadSearchMap: (searchMap: null | FormSearchField[]) => void;
  18. searchMap: null | FormSearchField[];
  19. };
  20. interface ExternalIssuesDefinition
  21. extends StoreDefinition,
  22. InternalDefinition,
  23. StoreInterface {}
  24. /**
  25. * Store for "form" searches, but probably will include more
  26. */
  27. const storeConfig: ExternalIssuesDefinition = {
  28. searchMap: null,
  29. init() {
  30. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  31. // listeners due to their leaky nature in tests.
  32. this.reset();
  33. },
  34. get() {
  35. return this.searchMap;
  36. },
  37. reset() {
  38. // `null` means it hasn't been loaded yet
  39. this.searchMap = null;
  40. },
  41. /**
  42. * Adds to search map
  43. */
  44. loadSearchMap(searchMap) {
  45. // Only load once
  46. if (this.searchMap !== null) {
  47. return;
  48. }
  49. this.searchMap = searchMap;
  50. this.trigger(this.searchMap);
  51. },
  52. };
  53. const FormSearchStore = createStore(storeConfig);
  54. export default FormSearchStore;