savedSearchesStore.tsx 5.3 KB

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