2025-04-23 15:21:09 +08:00
|
|
|
import { IFolder } from '@/interfaces/database/file-manager';
|
2025-04-23 10:39:09 +08:00
|
|
|
import fileManagerService from '@/services/file-manager-service';
|
2025-04-23 15:21:09 +08:00
|
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
2025-04-23 10:39:09 +08:00
|
|
|
import { message } from 'antd';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
2025-04-23 15:21:09 +08:00
|
|
|
import { useSearchParams } from 'umi';
|
2025-04-23 10:39:09 +08:00
|
|
|
import { useSetPaginationParams } from './route-hook';
|
|
|
|
|
|
|
|
|
|
export const enum FileApiAction {
|
|
|
|
|
UploadFile = 'uploadFile',
|
|
|
|
|
FetchFileList = 'fetchFileList',
|
2025-04-23 15:21:09 +08:00
|
|
|
MoveFile = 'moveFile',
|
|
|
|
|
CreateFolder = 'createFolder',
|
|
|
|
|
FetchParentFolderList = 'fetchParentFolderList',
|
2025-04-23 10:39:09 +08:00
|
|
|
}
|
|
|
|
|
|
2025-04-23 15:21:09 +08:00
|
|
|
export const useGetFolderId = () => {
|
|
|
|
|
const [searchParams] = useSearchParams();
|
|
|
|
|
const id = searchParams.get('folderId') as string;
|
|
|
|
|
|
|
|
|
|
return id ?? '';
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-23 10:39:09 +08:00
|
|
|
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 };
|
|
|
|
|
};
|
2025-04-23 15:21:09 +08:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
};
|