formSearchStore.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import {createStore} from 'reflux';
  2. import {FieldObject} from 'sentry/components/forms/type';
  3. import {makeSafeRefluxStore, SafeStoreDefinition} from 'sentry/utils/makeSafeRefluxStore';
  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 SafeStoreDefinition,
  23. InternalDefinition,
  24. StoreInterface {}
  25. /**
  26. * Store for "form" searches, but probably will include more
  27. */
  28. const storeConfig: ExternalIssuesDefinition = {
  29. searchMap: null,
  30. unsubscribeListeners: [],
  31. init() {
  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(makeSafeRefluxStore(storeConfig));
  54. export default FormSearchStore;