mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-25 08:06:48 +08:00
### What problem does this PR solve? Feat: Create a folder #3221 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
@ -1,14 +1,26 @@
|
||||
import { IFolder } from '@/interfaces/database/file-manager';
|
||||
import fileManagerService from '@/services/file-manager-service';
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import { message } from 'antd';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useSearchParams } from 'umi';
|
||||
import { useSetPaginationParams } from './route-hook';
|
||||
|
||||
export const enum FileApiAction {
|
||||
UploadFile = 'uploadFile',
|
||||
FetchFileList = 'fetchFileList',
|
||||
MoveFile = 'moveFile',
|
||||
CreateFolder = 'createFolder',
|
||||
FetchParentFolderList = 'fetchParentFolderList',
|
||||
}
|
||||
|
||||
export const useGetFolderId = () => {
|
||||
const [searchParams] = useSearchParams();
|
||||
const id = searchParams.get('folderId') as string;
|
||||
|
||||
return id ?? '';
|
||||
};
|
||||
|
||||
export const useUploadFile = () => {
|
||||
const { setPaginationParams } = useSetPaginationParams();
|
||||
const { t } = useTranslation();
|
||||
@ -46,3 +58,81 @@ export const useUploadFile = () => {
|
||||
|
||||
return { data, loading, uploadFile: mutateAsync };
|
||||
};
|
||||
|
||||
export interface IMoveFileBody {
|
||||
src_file_ids: string[];
|
||||
dest_file_id: string; // target folder id
|
||||
}
|
||||
|
||||
export const useMoveFile = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const { t } = useTranslation();
|
||||
|
||||
const {
|
||||
data,
|
||||
isPending: loading,
|
||||
mutateAsync,
|
||||
} = useMutation({
|
||||
mutationKey: [FileApiAction.MoveFile],
|
||||
mutationFn: async (params: IMoveFileBody) => {
|
||||
const { data } = await fileManagerService.moveFile(params);
|
||||
if (data.code === 0) {
|
||||
message.success(t('message.operated'));
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: [FileApiAction.FetchFileList],
|
||||
});
|
||||
}
|
||||
return data.code;
|
||||
},
|
||||
});
|
||||
|
||||
return { data, loading, moveFile: mutateAsync };
|
||||
};
|
||||
|
||||
export const useCreateFolder = () => {
|
||||
const { setPaginationParams } = useSetPaginationParams();
|
||||
const queryClient = useQueryClient();
|
||||
const { t } = useTranslation();
|
||||
|
||||
const {
|
||||
data,
|
||||
isPending: loading,
|
||||
mutateAsync,
|
||||
} = useMutation({
|
||||
mutationKey: [FileApiAction.CreateFolder],
|
||||
mutationFn: async (params: { parentId: string; name: string }) => {
|
||||
const { data } = await fileManagerService.createFolder({
|
||||
...params,
|
||||
type: 'folder',
|
||||
});
|
||||
if (data.code === 0) {
|
||||
message.success(t('message.created'));
|
||||
setPaginationParams(1);
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: [FileApiAction.FetchFileList],
|
||||
});
|
||||
}
|
||||
return data.code;
|
||||
},
|
||||
});
|
||||
|
||||
return { data, loading, createFolder: mutateAsync };
|
||||
};
|
||||
|
||||
export const useFetchParentFolderList = () => {
|
||||
const id = useGetFolderId();
|
||||
const { data } = useQuery<IFolder[]>({
|
||||
queryKey: [FileApiAction.FetchParentFolderList, id],
|
||||
initialData: [],
|
||||
enabled: !!id,
|
||||
queryFn: async () => {
|
||||
const { data } = await fileManagerService.getAllParentFolder({
|
||||
fileId: id,
|
||||
});
|
||||
|
||||
return data?.data?.parent_folders?.toReversed() ?? [];
|
||||
},
|
||||
});
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user