mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-12 11:43:39 +08:00
Refa: migrate chunk APIs to RESTful routes (#14291)
### What problem does this PR solve? migrate chunk APIs to RESTful routes ### Type of change - [x] Refactoring
This commit is contained in:
@@ -2,6 +2,7 @@ import {
|
||||
KnowledgeRouteKey,
|
||||
KnowledgeSearchParams,
|
||||
} from '@/constants/knowledge';
|
||||
import { Routes } from '@/routes';
|
||||
import { useCallback } from 'react';
|
||||
import { useLocation, useNavigate, useSearchParams } from 'react-router';
|
||||
|
||||
@@ -27,13 +28,16 @@ export const useThirdPathName = () => {
|
||||
|
||||
export const useGetKnowledgeSearchParams = () => {
|
||||
const [currentQueryParameters] = useSearchParams();
|
||||
const { pathname } = useLocation();
|
||||
const isDataflowResultPage = pathname === Routes.DataflowResult;
|
||||
|
||||
return {
|
||||
type: currentQueryParameters.get(KnowledgeSearchParams.Type) || '',
|
||||
documentId:
|
||||
currentQueryParameters.get(KnowledgeSearchParams.DocumentId) || '',
|
||||
knowledgeId:
|
||||
currentQueryParameters.get(KnowledgeSearchParams.KnowledgeId) || '',
|
||||
knowledgeId: isDataflowResultPage
|
||||
? currentQueryParameters.get('knowledgeId') || ''
|
||||
: currentQueryParameters.get(KnowledgeSearchParams.KnowledgeId) || '',
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@ export const useSelectChunkList = () => {
|
||||
export const useDeleteChunk = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const { setPaginationParams } = useSetPaginationParams();
|
||||
const { knowledgeId } = useGetKnowledgeSearchParams();
|
||||
const {
|
||||
data,
|
||||
isPending: loading,
|
||||
@@ -47,7 +48,10 @@ export const useDeleteChunk = () => {
|
||||
} = useMutation({
|
||||
mutationKey: ['deleteChunk'],
|
||||
mutationFn: async (params: { chunkIds: string[]; doc_id: string }) => {
|
||||
const { data } = await kbService.rmChunk(params);
|
||||
const { data } = await kbService.rmChunk({
|
||||
...params,
|
||||
kb_id: knowledgeId,
|
||||
});
|
||||
if (data.code === 0) {
|
||||
setPaginationParams(1);
|
||||
queryClient.invalidateQueries({ queryKey: ['fetchChunkList'] });
|
||||
@@ -62,6 +66,7 @@ export const useDeleteChunk = () => {
|
||||
export const useCreateChunk = () => {
|
||||
const { t } = useTranslation();
|
||||
const queryClient = useQueryClient();
|
||||
const { knowledgeId } = useGetKnowledgeSearchParams();
|
||||
|
||||
const {
|
||||
data,
|
||||
@@ -74,7 +79,10 @@ export const useCreateChunk = () => {
|
||||
if (payload.chunk_id) {
|
||||
service = kbService.setChunk;
|
||||
}
|
||||
const { data } = await service(payload);
|
||||
const { data } = await service({
|
||||
...payload,
|
||||
kb_id: payload.kb_id || knowledgeId,
|
||||
});
|
||||
if (data.code === 0) {
|
||||
message.success(t('message.created'));
|
||||
setTimeout(() => {
|
||||
@@ -88,14 +96,20 @@ export const useCreateChunk = () => {
|
||||
return { data, loading, createChunk: mutateAsync };
|
||||
};
|
||||
|
||||
export const useFetchChunk = (chunkId?: string): ResponseType<any> => {
|
||||
export const useFetchChunk = (
|
||||
chunkId?: string,
|
||||
documentId?: string,
|
||||
): ResponseType<any> => {
|
||||
const { knowledgeId } = useGetKnowledgeSearchParams();
|
||||
const { data } = useQuery({
|
||||
queryKey: ['fetchChunk'],
|
||||
enabled: !!chunkId,
|
||||
queryKey: ['fetchChunk', knowledgeId, documentId, chunkId],
|
||||
enabled: !!chunkId && !!documentId && !!knowledgeId,
|
||||
initialData: {},
|
||||
gcTime: 0,
|
||||
queryFn: async () => {
|
||||
const data = await kbService.getChunk({
|
||||
kb_id: knowledgeId,
|
||||
doc_id: documentId,
|
||||
chunk_id: chunkId,
|
||||
});
|
||||
|
||||
@@ -115,7 +129,7 @@ export const useFetchNextChunkList = (
|
||||
}> &
|
||||
IChunkListResult => {
|
||||
const { pagination, setPagination } = useGetPaginationWithRouter();
|
||||
const { documentId } = useGetKnowledgeSearchParams();
|
||||
const { documentId, knowledgeId } = useGetKnowledgeSearchParams();
|
||||
const { searchString, handleInputChange } = useHandleSearchChange();
|
||||
const [available, setAvailable] = useState<number | undefined>();
|
||||
const debouncedSearchString = useDebounce(searchString, { wait: 500 });
|
||||
@@ -127,6 +141,7 @@ export const useFetchNextChunkList = (
|
||||
} = useQuery({
|
||||
queryKey: [
|
||||
'fetchChunkList',
|
||||
knowledgeId,
|
||||
documentId,
|
||||
pagination.current,
|
||||
pagination.pageSize,
|
||||
@@ -136,9 +151,10 @@ export const useFetchNextChunkList = (
|
||||
placeholderData: (previousData: any) =>
|
||||
previousData ?? { data: [], total: 0, documentInfo: {} }, // https://github.com/TanStack/query/issues/8183
|
||||
gcTime: 0,
|
||||
enabled,
|
||||
enabled: enabled && !!knowledgeId && !!documentId,
|
||||
queryFn: async () => {
|
||||
const { data } = await kbService.chunkList({
|
||||
kb_id: knowledgeId,
|
||||
doc_id: documentId,
|
||||
page: pagination.current,
|
||||
size: pagination.pageSize,
|
||||
@@ -195,6 +211,7 @@ export const useFetchNextChunkList = (
|
||||
|
||||
export const useSwitchChunk = () => {
|
||||
const { t } = useTranslation();
|
||||
const { knowledgeId } = useGetKnowledgeSearchParams();
|
||||
const {
|
||||
data,
|
||||
isPending: loading,
|
||||
@@ -206,7 +223,10 @@ export const useSwitchChunk = () => {
|
||||
available_int?: number;
|
||||
doc_id: string;
|
||||
}) => {
|
||||
const { data } = await kbService.switchChunk(params);
|
||||
const { data } = await kbService.switchChunk({
|
||||
...params,
|
||||
kb_id: knowledgeId,
|
||||
});
|
||||
if (data.code === 0) {
|
||||
message.success(t('message.modified'));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user