useUnpinSearch.tsx 1.8 KB

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