savedSearches.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import {Client} from 'sentry/api';
  2. import {MAX_AUTOCOMPLETE_RECENT_SEARCHES} from 'sentry/constants';
  3. import {RecentSearch, SavedSearch, SavedSearchType} from 'sentry/types';
  4. import handleXhrErrorResponse from 'sentry/utils/handleXhrErrorResponse';
  5. const getRecentSearchUrl = (orgSlug: string): string =>
  6. `/organizations/${orgSlug}/recent-searches/`;
  7. /**
  8. * Saves search term for `user` + `orgSlug`
  9. *
  10. * @param api API client
  11. * @param orgSlug Organization slug
  12. * @param type Context for where search happened, 0 for issue, 1 for event
  13. * @param query The search term that was used
  14. */
  15. export function saveRecentSearch(
  16. api: Client,
  17. orgSlug: string,
  18. type: SavedSearchType,
  19. query: string
  20. ): Promise<SavedSearch> {
  21. const url = getRecentSearchUrl(orgSlug);
  22. const promise = api.requestPromise(url, {
  23. method: 'POST',
  24. data: {
  25. query,
  26. type,
  27. },
  28. });
  29. promise.catch(handleXhrErrorResponse('Unable to save a recent search'));
  30. return promise;
  31. }
  32. /**
  33. * Fetches a list of recent search terms conducted by `user` for `orgSlug`
  34. *
  35. * @param api API client
  36. * @param orgSlug Organization slug
  37. * @param type Context for where search happened, 0 for issue, 1 for event
  38. * @param query A query term used to filter results
  39. *
  40. * @return Returns a list of objects of recent search queries performed by user
  41. */
  42. export function fetchRecentSearches(
  43. api: Client,
  44. orgSlug: string,
  45. type: SavedSearchType,
  46. query?: string
  47. ): Promise<RecentSearch[]> {
  48. const url = getRecentSearchUrl(orgSlug);
  49. const promise = api.requestPromise(url, {
  50. query: {
  51. query,
  52. type,
  53. limit: MAX_AUTOCOMPLETE_RECENT_SEARCHES,
  54. },
  55. });
  56. promise.catch(resp => {
  57. if (resp.status !== 401 && resp.status !== 403) {
  58. handleXhrErrorResponse('Unable to fetch recent searches')(resp);
  59. }
  60. });
  61. return promise;
  62. }