useModifySavedSearch.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import type {SavedSearch, SavedSearchType, SavedSearchVisibility} from 'sentry/types';
  2. import type {UseMutationOptions} from 'sentry/utils/queryClient';
  3. import {setApiQueryData, useMutation, useQueryClient} from 'sentry/utils/queryClient';
  4. import type RequestError from 'sentry/utils/requestError/requestError';
  5. import useApi from 'sentry/utils/useApi';
  6. import {makeFetchSavedSearchesForOrgQueryKey} from 'sentry/views/issueList/queries/useFetchSavedSearchesForOrg';
  7. type ModifySavedSearchVariables = {
  8. id: string;
  9. name: string;
  10. orgSlug: string;
  11. query: string;
  12. sort: string | null;
  13. type: SavedSearchType;
  14. visibility: SavedSearchVisibility;
  15. };
  16. type ModifySavedSearchResponse = SavedSearch;
  17. export const useModifySavedSearch = (
  18. options: Omit<
  19. UseMutationOptions<
  20. ModifySavedSearchResponse,
  21. RequestError,
  22. ModifySavedSearchVariables
  23. >,
  24. 'mutationFn'
  25. > = {}
  26. ) => {
  27. const api = useApi();
  28. const queryClient = useQueryClient();
  29. return useMutation<ModifySavedSearchResponse, RequestError, ModifySavedSearchVariables>(
  30. {
  31. ...options,
  32. mutationFn: ({id, orgSlug, ...data}: ModifySavedSearchVariables) =>
  33. api.requestPromise(`/organizations/${orgSlug}/searches/${id}/`, {
  34. method: 'PUT',
  35. data,
  36. }),
  37. onSuccess: (savedSearch, parameters, context) => {
  38. setApiQueryData<SavedSearch[]>(
  39. queryClient,
  40. makeFetchSavedSearchesForOrgQueryKey({orgSlug: parameters.orgSlug}),
  41. oldData => {
  42. if (!Array.isArray(oldData)) {
  43. return oldData;
  44. }
  45. return oldData.map(search =>
  46. search?.id === parameters.id ? savedSearch : search
  47. );
  48. }
  49. );
  50. options.onSuccess?.(savedSearch, parameters, context);
  51. },
  52. }
  53. );
  54. };