mirror of
https://github.com/hanzoai/sentinel.git
synced 2026-09-29 22:34:33 +00:00
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import type {SavedSearch, SavedSearchType, SavedSearchVisibility} from 'sentry/types';
|
|
import type {UseMutationOptions} from 'sentry/utils/queryClient';
|
|
import {setApiQueryData, useMutation, useQueryClient} from 'sentry/utils/queryClient';
|
|
import type RequestError from 'sentry/utils/requestError/requestError';
|
|
import useApi from 'sentry/utils/useApi';
|
|
import {makeFetchSavedSearchesForOrgQueryKey} from 'sentry/views/issueList/queries/useFetchSavedSearchesForOrg';
|
|
|
|
type ModifySavedSearchVariables = {
|
|
id: string;
|
|
name: string;
|
|
orgSlug: string;
|
|
query: string;
|
|
sort: string | null;
|
|
type: SavedSearchType;
|
|
visibility: SavedSearchVisibility;
|
|
};
|
|
|
|
type ModifySavedSearchResponse = SavedSearch;
|
|
|
|
export const useModifySavedSearch = (
|
|
options: Omit<
|
|
UseMutationOptions<
|
|
ModifySavedSearchResponse,
|
|
RequestError,
|
|
ModifySavedSearchVariables
|
|
>,
|
|
'mutationFn'
|
|
> = {}
|
|
) => {
|
|
const api = useApi();
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation<ModifySavedSearchResponse, RequestError, ModifySavedSearchVariables>(
|
|
{
|
|
...options,
|
|
mutationFn: ({id, orgSlug, ...data}: ModifySavedSearchVariables) =>
|
|
api.requestPromise(`/organizations/${orgSlug}/searches/${id}/`, {
|
|
method: 'PUT',
|
|
data,
|
|
}),
|
|
onSuccess: (savedSearch, parameters, context) => {
|
|
setApiQueryData<SavedSearch[]>(
|
|
queryClient,
|
|
makeFetchSavedSearchesForOrgQueryKey({orgSlug: parameters.orgSlug}),
|
|
oldData => {
|
|
if (!Array.isArray(oldData)) {
|
|
return oldData;
|
|
}
|
|
|
|
return oldData.map(search =>
|
|
search?.id === parameters.id ? savedSearch : search
|
|
);
|
|
}
|
|
);
|
|
options.onSuccess?.(savedSearch, parameters, context);
|
|
},
|
|
}
|
|
);
|
|
};
|