useUnpinSearch.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  2. import {t} from 'sentry/locale';
  3. import type {SavedSearch, SavedSearchType} from 'sentry/types/group';
  4. import type {UseMutationOptions} from 'sentry/utils/queryClient';
  5. import {setApiQueryData, useMutation, useQueryClient} from 'sentry/utils/queryClient';
  6. import type RequestError from 'sentry/utils/requestError/requestError';
  7. import useApi from 'sentry/utils/useApi';
  8. import {makeFetchSavedSearchesForOrgQueryKey} from 'sentry/views/issueList/queries/useFetchSavedSearchesForOrg';
  9. type UnpinSavedSearchVariables = {
  10. orgSlug: string;
  11. type: SavedSearchType;
  12. };
  13. type UnpinSavedSearchResponse = {type: SavedSearchType};
  14. export const useUnpinSearch = (
  15. options: Omit<
  16. UseMutationOptions<UnpinSavedSearchResponse, RequestError, UnpinSavedSearchVariables>,
  17. 'mutationFn'
  18. > = {}
  19. ) => {
  20. const api = useApi();
  21. const queryClient = useQueryClient();
  22. return useMutation<UnpinSavedSearchResponse, RequestError, UnpinSavedSearchVariables>({
  23. ...options,
  24. mutationFn: ({orgSlug, type}) =>
  25. api.requestPromise(`/organizations/${orgSlug}/pinned-searches/`, {
  26. method: 'DELETE',
  27. data: {
  28. type,
  29. },
  30. }),
  31. onSuccess: (savedSearch, variables, context) => {
  32. setApiQueryData<SavedSearch[]>(
  33. queryClient,
  34. makeFetchSavedSearchesForOrgQueryKey({orgSlug: variables.orgSlug}),
  35. oldData => {
  36. if (!Array.isArray(oldData)) {
  37. return oldData;
  38. }
  39. return oldData.filter(search => !search.isPinned);
  40. }
  41. );
  42. addSuccessMessage(t("You'll no longer see this search by default."));
  43. options.onSuccess?.(savedSearch, variables, context);
  44. },
  45. onError: (error, variables, context) => {
  46. addErrorMessage(t('Unable to remove the default search.'));
  47. options.onError?.(error, variables, context);
  48. },
  49. });
  50. };