useUnpinSearch.tsx 1.8 KB

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