formSearchStore.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import {createStore, StoreDefinition} from 'reflux';
  2. import {FieldObject} from 'sentry/components/forms/type';
  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. this.reset();
  31. },
  32. get() {
  33. return this.searchMap;
  34. },
  35. reset() {
  36. // `null` means it hasn't been loaded yet
  37. this.searchMap = null;
  38. },
  39. /**
  40. * Adds to search map
  41. */
  42. loadSearchMap(searchMap) {
  43. // Only load once
  44. if (this.searchMap !== null) {
  45. return;
  46. }
  47. this.searchMap = searchMap;
  48. this.trigger(this.searchMap);
  49. },
  50. };
  51. const FormSearchStore = createStore(storeConfig);
  52. export default FormSearchStore;