You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hooks.ts 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import {
  2. useSetModalState,
  3. useShowDeleteConfirm,
  4. useTranslate,
  5. } from '@/hooks/commonHooks';
  6. import {
  7. useCreateFolder,
  8. useFetchFileList,
  9. useFetchParentFolderList,
  10. useRemoveFile,
  11. useRenameFile,
  12. useSelectFileList,
  13. useSelectParentFolderList,
  14. } from '@/hooks/fileManagerHooks';
  15. import { useOneNamespaceEffectsLoading } from '@/hooks/storeHooks';
  16. import { Pagination } from '@/interfaces/common';
  17. import { IFile } from '@/interfaces/database/file-manager';
  18. import { PaginationProps } from 'antd';
  19. import { useCallback, useEffect, useMemo, useState } from 'react';
  20. import { useDispatch, useNavigate, useSearchParams, useSelector } from 'umi';
  21. export const useGetFolderId = () => {
  22. const [searchParams] = useSearchParams();
  23. const id = searchParams.get('folderId') as string;
  24. return id;
  25. };
  26. export const useFetchDocumentListOnMount = () => {
  27. const fetchDocumentList = useFetchFileList();
  28. const fileList = useSelectFileList();
  29. const id = useGetFolderId();
  30. const dispatch = useDispatch();
  31. useEffect(() => {
  32. fetchDocumentList({ parent_id: id });
  33. }, [dispatch, fetchDocumentList, id]);
  34. return { fetchDocumentList, fileList };
  35. };
  36. export const useGetPagination = (
  37. fetchDocumentList: (payload: IFile) => any,
  38. ) => {
  39. const dispatch = useDispatch();
  40. const kFModel = useSelector((state: any) => state.kFModel);
  41. const { t } = useTranslate('common');
  42. const setPagination = useCallback(
  43. (pageNumber = 1, pageSize?: number) => {
  44. const pagination: Pagination = {
  45. current: pageNumber,
  46. } as Pagination;
  47. if (pageSize) {
  48. pagination.pageSize = pageSize;
  49. }
  50. dispatch({
  51. type: 'kFModel/setPagination',
  52. payload: pagination,
  53. });
  54. },
  55. [dispatch],
  56. );
  57. const onPageChange: PaginationProps['onChange'] = useCallback(
  58. (pageNumber: number, pageSize: number) => {
  59. setPagination(pageNumber, pageSize);
  60. fetchDocumentList();
  61. },
  62. [fetchDocumentList, setPagination],
  63. );
  64. const pagination: PaginationProps = useMemo(() => {
  65. return {
  66. showQuickJumper: true,
  67. total: kFModel.total,
  68. showSizeChanger: true,
  69. current: kFModel.pagination.current,
  70. pageSize: kFModel.pagination.pageSize,
  71. pageSizeOptions: [1, 2, 10, 20, 50, 100],
  72. onChange: onPageChange,
  73. showTotal: (total) => `${t('total')} ${total}`,
  74. };
  75. }, [kFModel, onPageChange, t]);
  76. return {
  77. pagination,
  78. setPagination,
  79. total: kFModel.total,
  80. searchString: kFModel.searchString,
  81. };
  82. };
  83. export const useHandleSearchChange = (setPagination: () => void) => {
  84. const dispatch = useDispatch();
  85. const throttledGetDocumentList = useCallback(() => {
  86. dispatch({
  87. type: 'kFModel/throttledGetDocumentList',
  88. });
  89. }, [dispatch]);
  90. const handleInputChange = useCallback(
  91. (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
  92. const value = e.target.value;
  93. dispatch({ type: 'kFModel/setSearchString', payload: value });
  94. setPagination();
  95. throttledGetDocumentList();
  96. },
  97. [setPagination, throttledGetDocumentList, dispatch],
  98. );
  99. return { handleInputChange };
  100. };
  101. export const useGetRowSelection = () => {
  102. const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
  103. const rowSelection = {
  104. selectedRowKeys,
  105. onChange: (newSelectedRowKeys: React.Key[]) => {
  106. setSelectedRowKeys(newSelectedRowKeys);
  107. },
  108. };
  109. return rowSelection;
  110. };
  111. export const useNavigateToOtherFolder = () => {
  112. const navigate = useNavigate();
  113. const navigateToOtherFolder = useCallback(
  114. (folderId: string) => {
  115. navigate(`/file?folderId=${folderId}`);
  116. },
  117. [navigate],
  118. );
  119. return navigateToOtherFolder;
  120. };
  121. export const useRenameCurrentFile = () => {
  122. const [file, setFile] = useState<IFile>({} as IFile);
  123. const {
  124. visible: fileRenameVisible,
  125. hideModal: hideFileRenameModal,
  126. showModal: showFileRenameModal,
  127. } = useSetModalState();
  128. const renameFile = useRenameFile();
  129. const onFileRenameOk = useCallback(
  130. async (name: string) => {
  131. const ret = await renameFile(file.id, name, file.parent_id);
  132. if (ret === 0) {
  133. hideFileRenameModal();
  134. }
  135. },
  136. [renameFile, file, hideFileRenameModal],
  137. );
  138. const loading = useOneNamespaceEffectsLoading('fileManager', ['renameFile']);
  139. const handleShowFileRenameModal = useCallback(
  140. async (record: IFile) => {
  141. setFile(record);
  142. showFileRenameModal();
  143. },
  144. [showFileRenameModal],
  145. );
  146. return {
  147. fileRenameLoading: loading,
  148. initialFileName: file.name,
  149. onFileRenameOk,
  150. fileRenameVisible,
  151. hideFileRenameModal,
  152. showFileRenameModal: handleShowFileRenameModal,
  153. };
  154. };
  155. export const useSelectBreadcrumbItems = () => {
  156. const parentFolderList = useSelectParentFolderList();
  157. const id = useGetFolderId();
  158. const fetchParentFolderList = useFetchParentFolderList();
  159. useEffect(() => {
  160. if (id) {
  161. fetchParentFolderList(id);
  162. }
  163. }, [id, fetchParentFolderList]);
  164. return parentFolderList.length === 1
  165. ? []
  166. : parentFolderList.map((x) => ({
  167. title: x.name === '/' ? 'root' : x.name,
  168. path: `/file?folderId=${x.id}`,
  169. }));
  170. };
  171. export const useHandleCreateFolder = () => {
  172. const {
  173. visible: folderCreateModalVisible,
  174. hideModal: hideFolderCreateModal,
  175. showModal: showFolderCreateModal,
  176. } = useSetModalState();
  177. const createFolder = useCreateFolder();
  178. const id = useGetFolderId();
  179. const onFolderCreateOk = useCallback(
  180. async (name: string) => {
  181. const ret = await createFolder(id, name);
  182. if (ret === 0) {
  183. hideFolderCreateModal();
  184. }
  185. },
  186. [createFolder, hideFolderCreateModal, id],
  187. );
  188. const loading = useOneNamespaceEffectsLoading('fileManager', [
  189. 'createFolder',
  190. ]);
  191. return {
  192. folderCreateLoading: loading,
  193. onFolderCreateOk,
  194. folderCreateModalVisible,
  195. hideFolderCreateModal,
  196. showFolderCreateModal,
  197. };
  198. };
  199. export const useHandleDeleteFile = (fileIds: string[]) => {
  200. const removeDocument = useRemoveFile();
  201. const showDeleteConfirm = useShowDeleteConfirm();
  202. const parentId = useGetFolderId();
  203. const handleRemoveFile = () => {
  204. showDeleteConfirm({
  205. onOk: () => {
  206. return removeDocument(fileIds, parentId);
  207. },
  208. });
  209. };
  210. return { handleRemoveFile };
  211. };
  212. export const useSelectFileListLoading = () => {
  213. return useOneNamespaceEffectsLoading('fileManager', ['listFile']);
  214. };