savedSearchesStore.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import findIndex from 'lodash/findIndex';
  2. import {createStore, StoreDefinition} from 'reflux';
  3. import {SavedSearch, SavedSearchType} from 'sentry/types';
  4. type State = {
  5. hasError: boolean;
  6. isLoading: boolean;
  7. savedSearches: SavedSearch[];
  8. };
  9. interface SavedSearchesStoreDefinition extends StoreDefinition {
  10. findByQuery(query: string, sort?: string): SavedSearch | undefined;
  11. get(): State;
  12. getFilteredSearches(type: SavedSearchType, id?: string): SavedSearch[];
  13. onCreateSavedSearchSuccess: (data) => void;
  14. onDeleteSavedSearchSuccess: (search: SavedSearch) => void;
  15. onFetchSavedSearchesError: (data) => void;
  16. onFetchSavedSearchesSuccess: (data) => void;
  17. onPinSearch(type: SavedSearchType, query: string, sort?: string): void;
  18. onPinSearchSuccess: (data) => void;
  19. onReset: () => void;
  20. onStartFetchSavedSearches: () => void;
  21. onUnpinSearch: (type: SavedSearchType) => void;
  22. reset(): void;
  23. updateExistingSearch(id: string, changes: Partial<SavedSearch>): SavedSearch;
  24. }
  25. const storeConfig: SavedSearchesStoreDefinition = {
  26. state: {
  27. savedSearches: [],
  28. hasError: false,
  29. isLoading: true,
  30. },
  31. init() {
  32. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  33. // listeners due to their leaky nature in tests.
  34. this.reset();
  35. },
  36. reset() {
  37. this.state = {
  38. savedSearches: [],
  39. hasError: false,
  40. isLoading: true,
  41. };
  42. },
  43. get() {
  44. return this.state;
  45. },
  46. /**
  47. * If pinned search, remove from list if user created pin (e.g. not org saved search and not global)
  48. * Otherwise change `isPinned` to false (e.g. if it's default or org saved search)
  49. */
  50. getFilteredSearches(type, existingSearchId) {
  51. return this.state.savedSearches
  52. .filter(
  53. (savedSearch: SavedSearch) =>
  54. !(
  55. savedSearch.isPinned &&
  56. savedSearch.type === type &&
  57. !savedSearch.isGlobal &&
  58. savedSearch.id !== existingSearchId
  59. )
  60. )
  61. .map((savedSearch: SavedSearch) => {
  62. if (
  63. typeof existingSearchId !== 'undefined' &&
  64. existingSearchId === savedSearch.id
  65. ) {
  66. // Do not update existing search
  67. return savedSearch;
  68. }
  69. return {...savedSearch, isPinned: false};
  70. });
  71. },
  72. updateExistingSearch(id, updateObj) {
  73. const index = findIndex(
  74. this.state.savedSearches,
  75. (savedSearch: SavedSearch) => savedSearch.id === id
  76. );
  77. if (index === -1) {
  78. return null;
  79. }
  80. const existingSavedSearch = this.state.savedSearches[index];
  81. const newSavedSearch = {
  82. ...existingSavedSearch,
  83. ...updateObj,
  84. };
  85. this.state.savedSearches[index] = newSavedSearch;
  86. return newSavedSearch;
  87. },
  88. /**
  89. * Find saved search by query string
  90. */
  91. findByQuery(query, sort) {
  92. return this.state.savedSearches.find(
  93. savedSearch => query === savedSearch.query && sort === savedSearch.sort
  94. );
  95. },
  96. /**
  97. * Reset store to initial state
  98. */
  99. onReset() {
  100. this.reset();
  101. this.trigger(this.state);
  102. },
  103. onStartFetchSavedSearches() {
  104. this.state = {
  105. ...this.state,
  106. isLoading: true,
  107. };
  108. this.trigger(this.state);
  109. },
  110. onFetchSavedSearchesSuccess(data) {
  111. if (!Array.isArray(data)) {
  112. data = [];
  113. }
  114. this.state = {
  115. ...this.state,
  116. savedSearches: data,
  117. isLoading: false,
  118. };
  119. this.trigger(this.state);
  120. },
  121. onFetchSavedSearchesError(_resp) {
  122. this.state = {
  123. ...this.state,
  124. savedSearches: [],
  125. isLoading: false,
  126. hasError: true,
  127. };
  128. this.trigger(this.state);
  129. },
  130. onCreateSavedSearchSuccess(data) {
  131. this.state = {
  132. ...this.state,
  133. savedSearches: [...this.state.savedSearches, data],
  134. };
  135. this.trigger(this.state);
  136. },
  137. onDeleteSavedSearchSuccess(search) {
  138. this.state = {
  139. ...this.state,
  140. savedSearches: this.state.savedSearches.filter(item => item.id !== search.id),
  141. };
  142. this.trigger(this.state);
  143. },
  144. onPinSearch(type, query, sort) {
  145. const existingSearch = this.findByQuery(query, sort);
  146. if (existingSearch) {
  147. this.updateExistingSearch(existingSearch.id, {isPinned: true});
  148. }
  149. const newPinnedSearch = !existingSearch
  150. ? [
  151. {
  152. id: null,
  153. name: 'My Pinned Search',
  154. type,
  155. query,
  156. sort,
  157. isPinned: true,
  158. },
  159. ]
  160. : [];
  161. this.state = {
  162. ...this.state,
  163. savedSearches: [
  164. ...newPinnedSearch,
  165. // There can only be 1 pinned search, so the rest must be unpinned
  166. // Also if we are pinning an existing search, then filter that out too
  167. ...this.getFilteredSearches(type, existingSearch && existingSearch.id),
  168. ],
  169. };
  170. this.trigger(this.state);
  171. },
  172. onPinSearchSuccess(data) {
  173. const existingSearch = this.findByQuery(data.query, data.sort);
  174. if (existingSearch) {
  175. this.updateExistingSearch(existingSearch.id, data);
  176. }
  177. this.trigger(this.state);
  178. },
  179. onUnpinSearch(type) {
  180. this.state = {
  181. ...this.state,
  182. // Design decision that there can only be 1 pinned search per `type`
  183. savedSearches: this.getFilteredSearches(type),
  184. };
  185. this.trigger(this.state);
  186. },
  187. };
  188. const SavedSearchesStore = createStore(storeConfig);
  189. export default SavedSearchesStore;