formSearchStore.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import {createStore, Store} 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. type 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 extends SafeStoreDefinition, InternalDefinition {}
  23. /**
  24. * Store for "form" searches, but probably will include more
  25. */
  26. const storeConfig: ExternalIssuesDefinition = {
  27. searchMap: null,
  28. unsubscribeListeners: [],
  29. init() {
  30. this.reset();
  31. this.unsubscribeListeners.push(
  32. this.listenTo(FormSearchActions.loadSearchMap, this.onLoadSearchMap)
  33. );
  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. onLoadSearchMap(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(makeSafeRefluxStore(storeConfig)) as Store &
  55. StoreInterface;
  56. export default FormSearchStore;