savedSearches.tsx 1.9 KB

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