### What problem does this PR solve? Put the knowledge base list related hooks into use-knowledge-request.ts #3221 ### Type of change - [x] New Feature (non-breaking change which adds functionality)tags/v0.18.0
| @@ -2,7 +2,6 @@ import { ResponsePostType } from '@/interfaces/database/base'; | |||
| import { | |||
| IKnowledge, | |||
| IKnowledgeGraph, | |||
| IKnowledgeResult, | |||
| IRenameTag, | |||
| ITestingResult, | |||
| } from '@/interfaces/database/knowledge'; | |||
| @@ -25,12 +24,9 @@ import { | |||
| } from '@tanstack/react-query'; | |||
| import { useDebounce } from 'ahooks'; | |||
| import { message } from 'antd'; | |||
| import { useCallback, useState } from 'react'; | |||
| import { useState } from 'react'; | |||
| import { useSearchParams } from 'umi'; | |||
| import { | |||
| useGetPaginationWithRouter, | |||
| useHandleSearchChange, | |||
| } from './logic-hooks'; | |||
| import { useHandleSearchChange } from './logic-hooks'; | |||
| import { useSetPaginationParams } from './route-hook'; | |||
| export const useKnowledgeBaseId = (): string => { | |||
| @@ -138,67 +134,6 @@ export const useInfiniteFetchKnowledgeList = () => { | |||
| }; | |||
| }; | |||
| export const useFetchNextKnowledgeListByPage = () => { | |||
| const { searchString, handleInputChange } = useHandleSearchChange(); | |||
| const { pagination, setPagination } = useGetPaginationWithRouter(); | |||
| const [ownerIds, setOwnerIds] = useState<string[]>([]); | |||
| const debouncedSearchString = useDebounce(searchString, { wait: 500 }); | |||
| const { data, isFetching: loading } = useQuery<IKnowledgeResult>({ | |||
| queryKey: [ | |||
| 'fetchKnowledgeListByPage', | |||
| { | |||
| debouncedSearchString, | |||
| ...pagination, | |||
| ownerIds, | |||
| }, | |||
| ], | |||
| initialData: { | |||
| kbs: [], | |||
| total: 0, | |||
| }, | |||
| gcTime: 0, | |||
| queryFn: async () => { | |||
| const { data } = await listDataset( | |||
| { | |||
| keywords: debouncedSearchString, | |||
| page_size: pagination.pageSize, | |||
| page: pagination.current, | |||
| }, | |||
| { | |||
| owner_ids: ownerIds, | |||
| }, | |||
| ); | |||
| return data?.data; | |||
| }, | |||
| }); | |||
| const onInputChange: React.ChangeEventHandler<HTMLInputElement> = useCallback( | |||
| (e) => { | |||
| // setPagination({ page: 1 }); // TODO: 这里导致重复请求 | |||
| handleInputChange(e); | |||
| }, | |||
| [handleInputChange], | |||
| ); | |||
| const handleOwnerIdsChange = useCallback((ids: string[]) => { | |||
| // setPagination({ page: 1 }); // TODO: 这里导致重复请求 | |||
| setOwnerIds(ids); | |||
| }, []); | |||
| return { | |||
| ...data, | |||
| searchString, | |||
| handleInputChange: onInputChange, | |||
| pagination: { ...pagination, total: data?.total }, | |||
| setPagination, | |||
| loading, | |||
| setOwnerIds: handleOwnerIdsChange, | |||
| ownerIds, | |||
| }; | |||
| }; | |||
| export const useCreateKnowledge = () => { | |||
| const queryClient = useQueryClient(); | |||
| const { | |||
| @@ -206,7 +141,7 @@ export const useCreateKnowledge = () => { | |||
| isPending: loading, | |||
| mutateAsync, | |||
| } = useMutation({ | |||
| mutationKey: ['createKnowledge'], | |||
| mutationKey: ['infiniteFetchKnowledgeList'], | |||
| mutationFn: async (params: { id?: string; name: string }) => { | |||
| const { data = {} } = await kbService.createKb(params); | |||
| if (data.code === 0) { | |||
| @@ -1,13 +1,27 @@ | |||
| import { INextTestingResult } from '@/interfaces/database/knowledge'; | |||
| import { | |||
| IKnowledgeResult, | |||
| INextTestingResult, | |||
| } from '@/interfaces/database/knowledge'; | |||
| import { ITestRetrievalRequestBody } from '@/interfaces/request/knowledge'; | |||
| import kbService from '@/services/knowledge-service'; | |||
| import { useQuery } from '@tanstack/react-query'; | |||
| import { useMemo, useState } from 'react'; | |||
| import i18n from '@/locales/config'; | |||
| import kbService, { listDataset } from '@/services/knowledge-service'; | |||
| import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; | |||
| import { useDebounce } from 'ahooks'; | |||
| import { message } from 'antd'; | |||
| import { useCallback, useMemo, useState } from 'react'; | |||
| import { useParams } from 'umi'; | |||
| import { | |||
| useGetPaginationWithRouter, | |||
| useHandleSearchChange, | |||
| } from './logic-hooks'; | |||
| import { useSetPaginationParams } from './route-hook'; | |||
| export const enum KnowledgeApiAction { | |||
| TestRetrieval = 'testRetrieval', | |||
| FetchKnowledgeListByPage = 'fetchKnowledgeListByPage', | |||
| CreateKnowledge = 'createKnowledge', | |||
| DeleteKnowledge = 'deleteKnowledge', | |||
| SaveKnowledge = 'saveKnowledge', | |||
| } | |||
| export const useKnowledgeBaseId = () => { | |||
| @@ -52,3 +66,141 @@ export const useTestRetrieval = () => { | |||
| return { data, loading, setValues, refetch }; | |||
| }; | |||
| export const useFetchNextKnowledgeListByPage = () => { | |||
| const { searchString, handleInputChange } = useHandleSearchChange(); | |||
| const { pagination, setPagination } = useGetPaginationWithRouter(); | |||
| const [ownerIds, setOwnerIds] = useState<string[]>([]); | |||
| const debouncedSearchString = useDebounce(searchString, { wait: 500 }); | |||
| const { data, isFetching: loading } = useQuery<IKnowledgeResult>({ | |||
| queryKey: [ | |||
| KnowledgeApiAction.FetchKnowledgeListByPage, | |||
| { | |||
| debouncedSearchString, | |||
| ...pagination, | |||
| ownerIds, | |||
| }, | |||
| ], | |||
| initialData: { | |||
| kbs: [], | |||
| total: 0, | |||
| }, | |||
| gcTime: 0, | |||
| queryFn: async () => { | |||
| const { data } = await listDataset( | |||
| { | |||
| keywords: debouncedSearchString, | |||
| page_size: pagination.pageSize, | |||
| page: pagination.current, | |||
| }, | |||
| { | |||
| owner_ids: ownerIds, | |||
| }, | |||
| ); | |||
| return data?.data; | |||
| }, | |||
| }); | |||
| const onInputChange: React.ChangeEventHandler<HTMLInputElement> = useCallback( | |||
| (e) => { | |||
| // setPagination({ page: 1 }); // TODO: 这里导致重复请求 | |||
| handleInputChange(e); | |||
| }, | |||
| [handleInputChange], | |||
| ); | |||
| const handleOwnerIdsChange = useCallback((ids: string[]) => { | |||
| // setPagination({ page: 1 }); // TODO: 这里导致重复请求 | |||
| setOwnerIds(ids); | |||
| }, []); | |||
| return { | |||
| ...data, | |||
| searchString, | |||
| handleInputChange: onInputChange, | |||
| pagination: { ...pagination, total: data?.total }, | |||
| setPagination, | |||
| loading, | |||
| setOwnerIds: handleOwnerIdsChange, | |||
| ownerIds, | |||
| }; | |||
| }; | |||
| export const useCreateKnowledge = () => { | |||
| const queryClient = useQueryClient(); | |||
| const { | |||
| data, | |||
| isPending: loading, | |||
| mutateAsync, | |||
| } = useMutation({ | |||
| mutationKey: [KnowledgeApiAction.CreateKnowledge], | |||
| mutationFn: async (params: { id?: string; name: string }) => { | |||
| const { data = {} } = await kbService.createKb(params); | |||
| if (data.code === 0) { | |||
| message.success( | |||
| i18n.t(`message.${params?.id ? 'modified' : 'created'}`), | |||
| ); | |||
| queryClient.invalidateQueries({ queryKey: ['fetchKnowledgeList'] }); | |||
| } | |||
| return data; | |||
| }, | |||
| }); | |||
| return { data, loading, createKnowledge: mutateAsync }; | |||
| }; | |||
| export const useDeleteKnowledge = () => { | |||
| const queryClient = useQueryClient(); | |||
| const { | |||
| data, | |||
| isPending: loading, | |||
| mutateAsync, | |||
| } = useMutation({ | |||
| mutationKey: [KnowledgeApiAction.DeleteKnowledge], | |||
| mutationFn: async (id: string) => { | |||
| const { data } = await kbService.rmKb({ kb_id: id }); | |||
| if (data.code === 0) { | |||
| message.success(i18n.t(`message.deleted`)); | |||
| queryClient.invalidateQueries({ | |||
| queryKey: [KnowledgeApiAction.FetchKnowledgeListByPage], | |||
| }); | |||
| } | |||
| return data?.data ?? []; | |||
| }, | |||
| }); | |||
| return { data, loading, deleteKnowledge: mutateAsync }; | |||
| }; | |||
| export const useUpdateKnowledge = (shouldFetchList = false) => { | |||
| const knowledgeBaseId = useKnowledgeBaseId(); | |||
| const queryClient = useQueryClient(); | |||
| const { | |||
| data, | |||
| isPending: loading, | |||
| mutateAsync, | |||
| } = useMutation({ | |||
| mutationKey: [KnowledgeApiAction.SaveKnowledge], | |||
| mutationFn: async (params: Record<string, any>) => { | |||
| const { data = {} } = await kbService.updateKb({ | |||
| kb_id: params?.kb_id ? params?.kb_id : knowledgeBaseId, | |||
| ...params, | |||
| }); | |||
| if (data.code === 0) { | |||
| message.success(i18n.t(`message.updated`)); | |||
| if (shouldFetchList) { | |||
| queryClient.invalidateQueries({ | |||
| queryKey: [KnowledgeApiAction.FetchKnowledgeListByPage], | |||
| }); | |||
| } else { | |||
| queryClient.invalidateQueries({ queryKey: ['fetchKnowledgeDetail'] }); | |||
| } | |||
| } | |||
| return data; | |||
| }, | |||
| }); | |||
| return { data, loading, saveKnowledgeConfiguration: mutateAsync }; | |||
| }; | |||
| @@ -6,7 +6,7 @@ import { | |||
| DropdownMenuSeparator, | |||
| DropdownMenuTrigger, | |||
| } from '@/components/ui/dropdown-menu'; | |||
| import { useDeleteKnowledge } from '@/hooks/knowledge-hooks'; | |||
| import { useDeleteKnowledge } from '@/hooks/use-knowledge-request'; | |||
| import { IKnowledge } from '@/interfaces/database/knowledge'; | |||
| import { PenLine, Trash2 } from 'lucide-react'; | |||
| import { PropsWithChildren, useCallback } from 'react'; | |||
| @@ -18,7 +18,7 @@ import { | |||
| FormLabel, | |||
| FormMessage, | |||
| } from '@/components/ui/form'; | |||
| import { useFetchNextKnowledgeListByPage } from '@/hooks/knowledge-hooks'; | |||
| import { useFetchNextKnowledgeListByPage } from '@/hooks/use-knowledge-request'; | |||
| import { useSelectOwners } from './use-select-owners'; | |||
| const FormSchema = z.object({ | |||
| @@ -1,6 +1,6 @@ | |||
| import { useSetModalState } from '@/hooks/common-hooks'; | |||
| import { useCreateKnowledge } from '@/hooks/knowledge-hooks'; | |||
| import { useNavigatePage } from '@/hooks/logic-hooks/navigate-hooks'; | |||
| import { useCreateKnowledge } from '@/hooks/use-knowledge-request'; | |||
| import { useCallback, useState } from 'react'; | |||
| export const useSearchKnowledge = () => { | |||
| @@ -3,8 +3,8 @@ import { RenameDialog } from '@/components/rename-dialog'; | |||
| import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; | |||
| import { Button } from '@/components/ui/button'; | |||
| import { Card, CardContent } from '@/components/ui/card'; | |||
| import { useFetchNextKnowledgeListByPage } from '@/hooks/knowledge-hooks'; | |||
| import { useNavigatePage } from '@/hooks/logic-hooks/navigate-hooks'; | |||
| import { useFetchNextKnowledgeListByPage } from '@/hooks/use-knowledge-request'; | |||
| import { formatDate } from '@/utils/date'; | |||
| import { pick } from 'lodash'; | |||
| import { ChevronRight, Ellipsis, Plus } from 'lucide-react'; | |||
| @@ -1,5 +1,5 @@ | |||
| import { useSetModalState } from '@/hooks/common-hooks'; | |||
| import { useUpdateKnowledge } from '@/hooks/knowledge-hooks'; | |||
| import { useUpdateKnowledge } from '@/hooks/use-knowledge-request'; | |||
| import { IKnowledge } from '@/interfaces/database/knowledge'; | |||
| import { omit } from 'lodash'; | |||
| import { useCallback, useState } from 'react'; | |||