Feat: Upload document #3221 (#7209)

### What problem does this PR solve?

Feat: Upload document #3221

### Type of change


- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
balibabu
2025-04-23 10:39:09 +08:00
committed by GitHub
parent 1e91318445
commit b44bbd11b8
15 changed files with 341 additions and 206 deletions

View File

@ -0,0 +1,49 @@
import kbService from '@/services/knowledge-service';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { get } from 'lodash';
import { useParams } from 'umi';
export const enum DocumentApiAction {
UploadDocument = 'uploadDocument',
FetchDocumentList = 'fetchDocumentList',
}
export const useUploadNextDocument = () => {
const queryClient = useQueryClient();
const { id } = useParams();
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: [DocumentApiAction.UploadDocument],
mutationFn: async (fileList: File[]) => {
const formData = new FormData();
formData.append('kb_id', id!);
fileList.forEach((file: any) => {
formData.append('file', file);
});
try {
const ret = await kbService.document_upload(formData);
const code = get(ret, 'data.code');
if (code === 0 || code === 500) {
queryClient.invalidateQueries({
queryKey: [DocumentApiAction.FetchDocumentList],
});
}
return ret?.data;
} catch (error) {
console.warn(error);
return {
code: 500,
message: error + '',
};
}
},
});
return { uploadDocument: mutateAsync, loading, data };
};

View File

@ -0,0 +1,48 @@
import fileManagerService from '@/services/file-manager-service';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { message } from 'antd';
import { useTranslation } from 'react-i18next';
import { useSetPaginationParams } from './route-hook';
export const enum FileApiAction {
UploadFile = 'uploadFile',
FetchFileList = 'fetchFileList',
}
export const useUploadFile = () => {
const { setPaginationParams } = useSetPaginationParams();
const { t } = useTranslation();
const queryClient = useQueryClient();
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: [FileApiAction.UploadFile],
mutationFn: async (params: { fileList: File[]; parentId: string }) => {
const fileList = params.fileList;
const pathList = params.fileList.map(
(file) => (file as any).webkitRelativePath,
);
const formData = new FormData();
formData.append('parent_id', params.parentId);
fileList.forEach((file: any, index: number) => {
formData.append('file', file);
formData.append('path', pathList[index]);
});
try {
const ret = await fileManagerService.uploadFile(formData);
if (ret?.data.code === 0) {
message.success(t('message.uploaded'));
setPaginationParams(1);
queryClient.invalidateQueries({
queryKey: [FileApiAction.FetchFileList],
});
}
return ret?.data?.code;
} catch (error) {}
},
});
return { data, loading, uploadFile: mutateAsync };
};