savedSearches.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import type {Client} from 'sentry/api';
  2. import {MAX_AUTOCOMPLETE_RECENT_SEARCHES} from 'sentry/constants';
  3. import type {RecentSearch, SavedSearch, SavedSearchType} from 'sentry/types/group';
  4. import {handleXhrErrorResponse} from 'sentry/utils/handleXhrErrorResponse';
  5. import type 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. // Prevent requests that are too long
  53. // 8k is the default max size for a URL in nginx
  54. // Docs - http://nginx.org/en/docs/http/ngx_http_core_module.html#large_client_header_buffers
  55. // 5000 saves us room for other query params and url
  56. // Recent searches stops being useful at a certain point
  57. if (query && query.length > 5000) {
  58. query = query.substring(0, 5000);
  59. }
  60. const promise = api.requestPromise(url, {
  61. query: {
  62. query,
  63. type,
  64. limit: MAX_AUTOCOMPLETE_RECENT_SEARCHES,
  65. },
  66. });
  67. promise.catch((resp: RequestError) => {
  68. if (resp.status !== 401 && resp.status !== 403) {
  69. handleXhrErrorResponse('Unable to fetch recent searches', resp);
  70. }
  71. });
  72. return promise;
  73. }