formSearchStore.tsx 1.5 KB

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