import type { Serialized } from '@rocket.chat/core-typings'; import type { Method, OperationParams, OperationResult, PathPattern, UrlParams } from '@rocket.chat/rest-typings'; import { useToastMessageDispatch, useEndpoint } from '@rocket.chat/ui-contexts'; import type { UseMutationOptions, UseMutationResult } from '@tanstack/react-query'; import { useMutation } from '@tanstack/react-query'; type UseEndpointActionOptions = (undefined extends UrlParams ? { keys?: UrlParams; } : { keys: UrlParams; }) & Omit< UseMutationOptions< Serialized>, Error, undefined extends OperationParams ? void : OperationParams >, 'mutationFn' >; export function useEndpointMutation( method: TMethod, pathPattern: TPathPattern, { keys, ...options }: NoInfer> = { keys: {} as UrlParams }, ) { const sendData = useEndpoint(method, pathPattern, keys as UrlParams); const dispatchToastMessage = useToastMessageDispatch(); return useMutation({ mutationFn: sendData, onError: (error) => { dispatchToastMessage({ type: 'error', message: error }); }, ...options, }) as UseMutationResult< Serialized>, Error, undefined extends OperationParams ? void : OperationParams >; }