import { useToast } from './useToast'; import { useMutation } from '@tanstack/react-query'; import type { UseMutationOptions } from '@tanstack/react-query'; import { getErrText } from '@fastgpt/global/common/error/utils'; import { useTranslation } from 'next-i18next'; import { useRequest as ahooksUseRequest } from 'ahooks'; interface Props extends UseMutationOptions { successToast?: string | null; errorToast?: string | null; } export const useRequest = ({ successToast, errorToast, onSuccess, onError, ...props }: Props) => { const { toast } = useToast(); const { t } = useTranslation(); const mutation = useMutation({ ...props, onSuccess(res, variables: void, context: unknown) { onSuccess?.(res, variables, context); successToast && toast({ title: successToast, status: 'success' }); }, onError(err: any, variables: void, context: unknown) { onError?.(err, variables, context); if (errorToast !== undefined) { const errText = t(getErrText(err, errorToast || '') as any); if (errText) { toast({ title: errText, status: 'error' }); } } } }); return mutation; }; type UseRequestFunProps = Parameters< typeof ahooksUseRequest >; export const useRequest2 = ( server: UseRequestFunProps[0], options: UseRequestFunProps[1] & { errorToast?: string; successToast?: string; } = {}, plugin?: UseRequestFunProps[2] ) => { const { t } = useTranslation(); const { errorToast = 'Error', successToast, ...rest } = options || {}; const { toast } = useToast(); const res = ahooksUseRequest( server, { manual: true, ...rest, onError: (err, params) => { rest?.onError?.(err, params); if (errorToast !== '') { const errText = t(getErrText(err, errorToast || '') as any); if (errText) { toast({ title: errText, status: 'error' }); } } }, onSuccess: (res, params) => { rest?.onSuccess?.(res, params); if (successToast) { toast({ title: successToast, status: 'success' }); } } }, plugin ); return res; };