### 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
| import { | import { | ||||
| IKnowledge, | IKnowledge, | ||||
| IKnowledgeGraph, | IKnowledgeGraph, | ||||
| IKnowledgeResult, | |||||
| IRenameTag, | IRenameTag, | ||||
| ITestingResult, | ITestingResult, | ||||
| } from '@/interfaces/database/knowledge'; | } from '@/interfaces/database/knowledge'; | ||||
| } from '@tanstack/react-query'; | } from '@tanstack/react-query'; | ||||
| import { useDebounce } from 'ahooks'; | import { useDebounce } from 'ahooks'; | ||||
| import { message } from 'antd'; | import { message } from 'antd'; | ||||
| import { useCallback, useState } from 'react'; | |||||
| import { useState } from 'react'; | |||||
| import { useSearchParams } from 'umi'; | import { useSearchParams } from 'umi'; | ||||
| import { | |||||
| useGetPaginationWithRouter, | |||||
| useHandleSearchChange, | |||||
| } from './logic-hooks'; | |||||
| import { useHandleSearchChange } from './logic-hooks'; | |||||
| import { useSetPaginationParams } from './route-hook'; | import { useSetPaginationParams } from './route-hook'; | ||||
| export const useKnowledgeBaseId = (): string => { | export const useKnowledgeBaseId = (): string => { | ||||
| }; | }; | ||||
| }; | }; | ||||
| 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 = () => { | export const useCreateKnowledge = () => { | ||||
| const queryClient = useQueryClient(); | const queryClient = useQueryClient(); | ||||
| const { | const { | ||||
| isPending: loading, | isPending: loading, | ||||
| mutateAsync, | mutateAsync, | ||||
| } = useMutation({ | } = useMutation({ | ||||
| mutationKey: ['createKnowledge'], | |||||
| mutationKey: ['infiniteFetchKnowledgeList'], | |||||
| mutationFn: async (params: { id?: string; name: string }) => { | mutationFn: async (params: { id?: string; name: string }) => { | ||||
| const { data = {} } = await kbService.createKb(params); | const { data = {} } = await kbService.createKb(params); | ||||
| if (data.code === 0) { | if (data.code === 0) { |
| import { INextTestingResult } from '@/interfaces/database/knowledge'; | |||||
| import { | |||||
| IKnowledgeResult, | |||||
| INextTestingResult, | |||||
| } from '@/interfaces/database/knowledge'; | |||||
| import { ITestRetrievalRequestBody } from '@/interfaces/request/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 { useParams } from 'umi'; | ||||
| import { | |||||
| useGetPaginationWithRouter, | |||||
| useHandleSearchChange, | |||||
| } from './logic-hooks'; | |||||
| import { useSetPaginationParams } from './route-hook'; | import { useSetPaginationParams } from './route-hook'; | ||||
| export const enum KnowledgeApiAction { | export const enum KnowledgeApiAction { | ||||
| TestRetrieval = 'testRetrieval', | TestRetrieval = 'testRetrieval', | ||||
| FetchKnowledgeListByPage = 'fetchKnowledgeListByPage', | |||||
| CreateKnowledge = 'createKnowledge', | |||||
| DeleteKnowledge = 'deleteKnowledge', | |||||
| SaveKnowledge = 'saveKnowledge', | |||||
| } | } | ||||
| export const useKnowledgeBaseId = () => { | export const useKnowledgeBaseId = () => { | ||||
| return { data, loading, setValues, refetch }; | 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 }; | |||||
| }; |
| DropdownMenuSeparator, | DropdownMenuSeparator, | ||||
| DropdownMenuTrigger, | DropdownMenuTrigger, | ||||
| } from '@/components/ui/dropdown-menu'; | } 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 { IKnowledge } from '@/interfaces/database/knowledge'; | ||||
| import { PenLine, Trash2 } from 'lucide-react'; | import { PenLine, Trash2 } from 'lucide-react'; | ||||
| import { PropsWithChildren, useCallback } from 'react'; | import { PropsWithChildren, useCallback } from 'react'; |
| FormLabel, | FormLabel, | ||||
| FormMessage, | FormMessage, | ||||
| } from '@/components/ui/form'; | } from '@/components/ui/form'; | ||||
| import { useFetchNextKnowledgeListByPage } from '@/hooks/knowledge-hooks'; | |||||
| import { useFetchNextKnowledgeListByPage } from '@/hooks/use-knowledge-request'; | |||||
| import { useSelectOwners } from './use-select-owners'; | import { useSelectOwners } from './use-select-owners'; | ||||
| const FormSchema = z.object({ | const FormSchema = z.object({ |
| import { useSetModalState } from '@/hooks/common-hooks'; | import { useSetModalState } from '@/hooks/common-hooks'; | ||||
| import { useCreateKnowledge } from '@/hooks/knowledge-hooks'; | |||||
| import { useNavigatePage } from '@/hooks/logic-hooks/navigate-hooks'; | import { useNavigatePage } from '@/hooks/logic-hooks/navigate-hooks'; | ||||
| import { useCreateKnowledge } from '@/hooks/use-knowledge-request'; | |||||
| import { useCallback, useState } from 'react'; | import { useCallback, useState } from 'react'; | ||||
| export const useSearchKnowledge = () => { | export const useSearchKnowledge = () => { |
| import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; | import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; | ||||
| import { Button } from '@/components/ui/button'; | import { Button } from '@/components/ui/button'; | ||||
| import { Card, CardContent } from '@/components/ui/card'; | import { Card, CardContent } from '@/components/ui/card'; | ||||
| import { useFetchNextKnowledgeListByPage } from '@/hooks/knowledge-hooks'; | |||||
| import { useNavigatePage } from '@/hooks/logic-hooks/navigate-hooks'; | import { useNavigatePage } from '@/hooks/logic-hooks/navigate-hooks'; | ||||
| import { useFetchNextKnowledgeListByPage } from '@/hooks/use-knowledge-request'; | |||||
| import { formatDate } from '@/utils/date'; | import { formatDate } from '@/utils/date'; | ||||
| import { pick } from 'lodash'; | import { pick } from 'lodash'; | ||||
| import { ChevronRight, Ellipsis, Plus } from 'lucide-react'; | import { ChevronRight, Ellipsis, Plus } from 'lucide-react'; |
| import { useSetModalState } from '@/hooks/common-hooks'; | 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 { IKnowledge } from '@/interfaces/database/knowledge'; | ||||
| import { omit } from 'lodash'; | import { omit } from 'lodash'; | ||||
| import { useCallback, useState } from 'react'; | import { useCallback, useState } from 'react'; |