savedSearchesStore.tsx 5.2 KB

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