2024-04-25 08:46:18 +08:00
|
|
|
import {
|
|
|
|
|
IConnectRequestBody,
|
|
|
|
|
IFileListRequestBody,
|
|
|
|
|
} from '@/interfaces/request/file-manager';
|
|
|
|
|
import { UploadFile } from 'antd';
|
2024-04-23 17:46:56 +08:00
|
|
|
import { useCallback } from 'react';
|
|
|
|
|
import { useDispatch, useSelector } from 'umi';
|
|
|
|
|
|
|
|
|
|
export const useFetchFileList = () => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
|
|
const fetchFileList = useCallback(
|
|
|
|
|
(payload: IFileListRequestBody) => {
|
|
|
|
|
return dispatch<any>({
|
|
|
|
|
type: 'fileManager/listFile',
|
|
|
|
|
payload,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
[dispatch],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return fetchFileList;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useRemoveFile = () => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
|
|
const removeFile = useCallback(
|
2024-04-24 11:07:22 +08:00
|
|
|
(fileIds: string[], parentId: string) => {
|
2024-04-23 17:46:56 +08:00
|
|
|
return dispatch<any>({
|
|
|
|
|
type: 'fileManager/removeFile',
|
2024-04-24 11:07:22 +08:00
|
|
|
payload: { fileIds, parentId },
|
2024-04-23 17:46:56 +08:00
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
[dispatch],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return removeFile;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useRenameFile = () => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
|
|
const renameFile = useCallback(
|
2024-04-24 11:07:22 +08:00
|
|
|
(fileId: string, name: string, parentId: string) => {
|
2024-04-23 17:46:56 +08:00
|
|
|
return dispatch<any>({
|
|
|
|
|
type: 'fileManager/renameFile',
|
2024-04-24 11:07:22 +08:00
|
|
|
payload: { fileId, name, parentId },
|
2024-04-23 17:46:56 +08:00
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
[dispatch],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return renameFile;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useFetchParentFolderList = () => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
|
|
const fetchParentFolderList = useCallback(
|
|
|
|
|
(fileId: string) => {
|
|
|
|
|
return dispatch<any>({
|
|
|
|
|
type: 'fileManager/getAllParentFolder',
|
|
|
|
|
payload: { fileId },
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
[dispatch],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return fetchParentFolderList;
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-24 11:07:22 +08:00
|
|
|
export const useCreateFolder = () => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
|
|
const createFolder = useCallback(
|
|
|
|
|
(parentId: string, name: string) => {
|
|
|
|
|
return dispatch<any>({
|
|
|
|
|
type: 'fileManager/createFolder',
|
|
|
|
|
payload: { parentId, name, type: 'folder' },
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
[dispatch],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return createFolder;
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-23 17:46:56 +08:00
|
|
|
export const useSelectFileList = () => {
|
|
|
|
|
const fileList = useSelector((state) => state.fileManager.fileList);
|
|
|
|
|
|
|
|
|
|
return fileList;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useSelectParentFolderList = () => {
|
|
|
|
|
const parentFolderList = useSelector(
|
|
|
|
|
(state) => state.fileManager.parentFolderList,
|
|
|
|
|
);
|
|
|
|
|
return parentFolderList.toReversed();
|
|
|
|
|
};
|
2024-04-25 08:46:18 +08:00
|
|
|
|
|
|
|
|
export const useUploadFile = () => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
|
|
const uploadFile = useCallback(
|
|
|
|
|
(file: UploadFile, parentId: string, path: string) => {
|
|
|
|
|
try {
|
|
|
|
|
return dispatch<any>({
|
|
|
|
|
type: 'fileManager/uploadFile',
|
|
|
|
|
payload: {
|
|
|
|
|
file,
|
|
|
|
|
parentId,
|
|
|
|
|
path,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch (errorInfo) {
|
|
|
|
|
console.log('Failed:', errorInfo);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[dispatch],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return uploadFile;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useConnectToKnowledge = () => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
|
|
const uploadFile = useCallback(
|
|
|
|
|
(payload: IConnectRequestBody) => {
|
|
|
|
|
try {
|
|
|
|
|
return dispatch<any>({
|
|
|
|
|
type: 'fileManager/connectFileToKnowledge',
|
|
|
|
|
payload,
|
|
|
|
|
});
|
|
|
|
|
} catch (errorInfo) {
|
|
|
|
|
console.log('Failed:', errorInfo);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[dispatch],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return uploadFile;
|
|
|
|
|
};
|