2025-04-29 09:50:54 +08:00
|
|
|
import { IFlow } from '@/interfaces/database/flow';
|
|
|
|
|
import flowService from '@/services/flow-service';
|
2025-05-20 19:13:19 +08:00
|
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
2025-05-20 16:03:55 +08:00
|
|
|
import { useDebounce } from 'ahooks';
|
2025-05-20 19:13:19 +08:00
|
|
|
import { message } from 'antd';
|
2025-05-20 16:03:55 +08:00
|
|
|
import { useCallback } from 'react';
|
|
|
|
|
import {
|
|
|
|
|
useGetPaginationWithRouter,
|
|
|
|
|
useHandleSearchChange,
|
|
|
|
|
} from './logic-hooks';
|
2025-04-29 09:50:54 +08:00
|
|
|
|
|
|
|
|
export const enum AgentApiAction {
|
|
|
|
|
FetchAgentList = 'fetchAgentList',
|
2025-05-20 19:13:19 +08:00
|
|
|
UpdateAgentSetting = 'updateAgentSetting',
|
|
|
|
|
DeleteAgent = 'deleteAgent',
|
2025-04-29 09:50:54 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-20 16:03:55 +08:00
|
|
|
export const useFetchAgentListByPage = () => {
|
|
|
|
|
const { searchString, handleInputChange } = useHandleSearchChange();
|
|
|
|
|
const { pagination, setPagination } = useGetPaginationWithRouter();
|
|
|
|
|
const debouncedSearchString = useDebounce(searchString, { wait: 500 });
|
|
|
|
|
|
|
|
|
|
const { data, isFetching: loading } = useQuery<{
|
|
|
|
|
kbs: IFlow[];
|
|
|
|
|
total: number;
|
|
|
|
|
}>({
|
|
|
|
|
queryKey: [
|
|
|
|
|
AgentApiAction.FetchAgentList,
|
|
|
|
|
{
|
|
|
|
|
debouncedSearchString,
|
|
|
|
|
...pagination,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
initialData: { kbs: [], total: 0 },
|
2025-04-29 09:50:54 +08:00
|
|
|
gcTime: 0,
|
|
|
|
|
queryFn: async () => {
|
2025-05-20 16:03:55 +08:00
|
|
|
const { data } = await flowService.listCanvasTeam({
|
|
|
|
|
keywords: debouncedSearchString,
|
|
|
|
|
page_size: pagination.pageSize,
|
|
|
|
|
page: pagination.current,
|
|
|
|
|
});
|
2025-04-29 09:50:54 +08:00
|
|
|
|
|
|
|
|
return data?.data ?? [];
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-05-20 16:03:55 +08:00
|
|
|
const onInputChange: React.ChangeEventHandler<HTMLInputElement> = useCallback(
|
|
|
|
|
(e) => {
|
|
|
|
|
// setPagination({ page: 1 }); // TODO: 这里导致重复请求
|
|
|
|
|
handleInputChange(e);
|
|
|
|
|
},
|
|
|
|
|
[handleInputChange],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
data: data.kbs,
|
|
|
|
|
loading,
|
|
|
|
|
searchString,
|
|
|
|
|
handleInputChange: onInputChange,
|
|
|
|
|
pagination: { ...pagination, total: data?.total },
|
|
|
|
|
setPagination,
|
|
|
|
|
};
|
2025-04-29 09:50:54 +08:00
|
|
|
};
|
2025-05-20 19:13:19 +08:00
|
|
|
|
|
|
|
|
export const useUpdateAgentSetting = () => {
|
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
data,
|
|
|
|
|
isPending: loading,
|
|
|
|
|
mutateAsync,
|
|
|
|
|
} = useMutation({
|
|
|
|
|
mutationKey: [AgentApiAction.UpdateAgentSetting],
|
|
|
|
|
mutationFn: async (params: any) => {
|
|
|
|
|
const ret = await flowService.settingCanvas(params);
|
|
|
|
|
if (ret?.data?.code === 0) {
|
|
|
|
|
message.success('success');
|
|
|
|
|
queryClient.invalidateQueries({
|
|
|
|
|
queryKey: [AgentApiAction.FetchAgentList],
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
message.error(ret?.data?.data);
|
|
|
|
|
}
|
|
|
|
|
return ret?.data?.code;
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return { data, loading, updateAgentSetting: mutateAsync };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useDeleteAgent = () => {
|
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
const {
|
|
|
|
|
data,
|
|
|
|
|
isPending: loading,
|
|
|
|
|
mutateAsync,
|
|
|
|
|
} = useMutation({
|
|
|
|
|
mutationKey: [AgentApiAction.DeleteAgent],
|
|
|
|
|
mutationFn: async (canvasIds: string[]) => {
|
|
|
|
|
const { data } = await flowService.removeCanvas({ canvasIds });
|
|
|
|
|
if (data.code === 0) {
|
|
|
|
|
queryClient.invalidateQueries({
|
|
|
|
|
queryKey: [AgentApiAction.FetchAgentList],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return data?.data ?? [];
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return { data, loading, deleteAgent: mutateAsync };
|
|
|
|
|
};
|