mirror of
https://github.com/labring/FastGPT.git
synced 2026-02-27 01:02:22 +08:00
refactor: useRequest2 -> useRequest (#6333)
This commit is contained in:
@@ -2,7 +2,7 @@ import React, { useMemo } from 'react';
|
||||
import { ModalFooter, ModalBody, Input, Button, Box, Textarea } from '@chakra-ui/react';
|
||||
import MyModal from './index';
|
||||
import { useTranslation } from 'next-i18next';
|
||||
import { useRequest2 } from '../../../hooks/useRequest';
|
||||
import { useRequest } from '../../../hooks/useRequest';
|
||||
import FormLabel from '../MyBox/FormLabel';
|
||||
import { useForm } from 'react-hook-form';
|
||||
|
||||
@@ -49,7 +49,7 @@ const EditFolderModal = ({
|
||||
[isEdit, t]
|
||||
);
|
||||
|
||||
const { run: onSave, loading } = useRequest2(
|
||||
const { run: onSave, loading } = useRequest(
|
||||
({ name = '', intro }: EditFolderFormType) => {
|
||||
if (!name) return;
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import React from 'react';
|
||||
import { useTranslation } from 'next-i18next';
|
||||
import MyIcon from '../Icon';
|
||||
import { useRequest2 } from '../../../hooks/useRequest';
|
||||
import { useRequest } from '../../../hooks/useRequest';
|
||||
import {
|
||||
Popover,
|
||||
PopoverTrigger,
|
||||
@@ -56,7 +56,7 @@ const PopoverConfirm = ({
|
||||
const firstFieldRef = React.useRef(null);
|
||||
const { onOpen, onClose, isOpen } = useDisclosure();
|
||||
|
||||
const { runAsync: onclickConfirm, loading } = useRequest2(async () => onConfirm(), {
|
||||
const { runAsync: onclickConfirm, loading } = useRequest(async () => onConfirm(), {
|
||||
onSuccess: onClose
|
||||
});
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ import {
|
||||
} from '@chakra-ui/react';
|
||||
import type { ButtonProps, MenuItemProps } from '@chakra-ui/react';
|
||||
import MyIcon from '../Icon';
|
||||
import { useRequest2 } from '../../../hooks/useRequest';
|
||||
import { useRequest } from '../../../hooks/useRequest';
|
||||
import MyDivider from '../MyDivider';
|
||||
import type { useScrollPagination } from '../../../hooks/useScrollPagination';
|
||||
import Avatar from '../Avatar';
|
||||
@@ -143,7 +143,7 @@ const MySelect = <T = any,>(
|
||||
}
|
||||
}, [isSearch, isOpen]);
|
||||
|
||||
const { runAsync: onClickChange, loading } = useRequest2((val: T) => onChange?.(val));
|
||||
const { runAsync: onClickChange, loading } = useRequest((val: T) => onChange?.(val));
|
||||
|
||||
const ListRender = useMemo(() => {
|
||||
return (
|
||||
|
||||
@@ -4,7 +4,7 @@ import { Box, type BoxProps } from '@chakra-ui/react';
|
||||
import { useTranslation } from 'next-i18next';
|
||||
import { useScroll, useMemoizedFn, useDebounceEffect } from 'ahooks';
|
||||
import MyBox from '../components/common/MyBox';
|
||||
import { useRequest2 } from './useRequest';
|
||||
import { useRequest } from './useRequest';
|
||||
|
||||
const threshold = 200;
|
||||
|
||||
@@ -70,10 +70,10 @@ export function useLinkedScroll<
|
||||
[dataList, defaultScroll]
|
||||
);
|
||||
|
||||
const { runAsync: callApi, loading: isLoading } = useRequest2(api);
|
||||
const { runAsync: callApi, loading: isLoading } = useRequest(api);
|
||||
|
||||
let scrollSign = useRef(false);
|
||||
const { runAsync: loadInitData } = useRequest2(
|
||||
const { runAsync: loadInitData } = useRequest(
|
||||
async ({ scrollWhenFinish, refresh } = { scrollWhenFinish: true, refresh: false }) => {
|
||||
if (isLoading) return;
|
||||
|
||||
@@ -122,7 +122,7 @@ export function useLinkedScroll<
|
||||
}
|
||||
}, [dataList]);
|
||||
|
||||
const { runAsync: loadPrevData, loading: prevLoading } = useRequest2(
|
||||
const { runAsync: loadPrevData, loading: prevLoading } = useRequest(
|
||||
async (scrollRef = containerRef) => {
|
||||
if (!anchorRef.current.top || !hasMorePrev || isLoading) return;
|
||||
|
||||
@@ -161,7 +161,7 @@ export function useLinkedScroll<
|
||||
}
|
||||
);
|
||||
|
||||
const { runAsync: loadNextData, loading: nextLoading } = useRequest2(
|
||||
const { runAsync: loadNextData, loading: nextLoading } = useRequest(
|
||||
async (scrollRef = containerRef) => {
|
||||
if (!anchorRef.current.bottom || !hasMoreNext || isLoading) return;
|
||||
|
||||
|
||||
@@ -10,41 +10,11 @@ interface Props extends UseMutationOptions<any, any, any, any> {
|
||||
errorToast?: string | null;
|
||||
}
|
||||
|
||||
export const useRequest = ({ successToast, errorToast, onSuccess, onError, ...props }: Props) => {
|
||||
const { toast } = useToast();
|
||||
const { t } = useTranslation();
|
||||
const mutation = useMutation<unknown, unknown, any, unknown>({
|
||||
...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<TData, TParams extends any[]> = Parameters<
|
||||
typeof ahooksUseRequest<TData, TParams>
|
||||
>;
|
||||
export const useRequest2 = <TData, TParams extends any[]>(
|
||||
|
||||
export const useRequest = <TData, TParams extends any[]>(
|
||||
server: UseRequestFunProps<TData, TParams>[0],
|
||||
options: UseRequestFunProps<TData, TParams>[1] & {
|
||||
errorToast?: string;
|
||||
|
||||
@@ -14,7 +14,6 @@ import {
|
||||
} from 'ahooks';
|
||||
import MyBox from '../components/common/MyBox';
|
||||
import { useTranslation } from 'next-i18next';
|
||||
import { useRequest2 } from './useRequest';
|
||||
import type { PaginationType, PaginationResponseType } from '../../global/openapi/api';
|
||||
|
||||
type ItemHeight<T> = (index: number, data: T) => number;
|
||||
@@ -202,7 +201,7 @@ export function useScrollPagination<
|
||||
EmptyTip?: React.JSX.Element;
|
||||
showErrorToast?: boolean;
|
||||
disabled?: boolean;
|
||||
} & Parameters<typeof useRequest2>[1]
|
||||
} & Parameters<typeof useRequest>[1]
|
||||
) {
|
||||
const { t } = useTranslation();
|
||||
const { toast } = useToast();
|
||||
@@ -356,7 +355,7 @@ export function useScrollPagination<
|
||||
);
|
||||
|
||||
// Reload data
|
||||
useRequest2(
|
||||
useRequest(
|
||||
async () => {
|
||||
if (disabled) return;
|
||||
loadData({ init: true });
|
||||
|
||||
Reference in New Issue
Block a user