formSearchStore.tsx 1.4 KB

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