您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import { useSetModalState, useTranslate } from '@/hooks/commonHooks';
  2. import {
  3. useCreateDocument,
  4. useFetchDocumentList,
  5. useRunDocument,
  6. useSaveDocumentName,
  7. useSelectRunDocumentLoading,
  8. useSetDocumentParser,
  9. useUploadDocument,
  10. } from '@/hooks/documentHooks';
  11. import { useGetKnowledgeSearchParams } from '@/hooks/routeHook';
  12. import { useOneNamespaceEffectsLoading } from '@/hooks/storeHooks';
  13. import { useFetchTenantInfo } from '@/hooks/userSettingHook';
  14. import { Pagination } from '@/interfaces/common';
  15. import { IChangeParserConfigRequestBody } from '@/interfaces/request/document';
  16. import { getUnSupportedFilesCount } from '@/utils/documentUtils';
  17. import { PaginationProps, UploadFile } from 'antd';
  18. import { useCallback, useEffect, useMemo, useState } from 'react';
  19. import { useDispatch, useNavigate, useSelector } from 'umi';
  20. import { KnowledgeRouteKey } from './constant';
  21. export const useFetchDocumentListOnMount = () => {
  22. const { knowledgeId } = useGetKnowledgeSearchParams();
  23. const fetchDocumentList = useFetchDocumentList();
  24. const dispatch = useDispatch();
  25. useFetchTenantInfo();
  26. useEffect(() => {
  27. if (knowledgeId) {
  28. fetchDocumentList();
  29. dispatch({
  30. type: 'kFModel/pollGetDocumentList-start',
  31. payload: knowledgeId,
  32. });
  33. }
  34. return () => {
  35. dispatch({
  36. type: 'kFModel/pollGetDocumentList-stop',
  37. });
  38. };
  39. }, [knowledgeId, dispatch, fetchDocumentList]);
  40. return { fetchDocumentList };
  41. };
  42. export const useGetPagination = (fetchDocumentList: () => void) => {
  43. const dispatch = useDispatch();
  44. const kFModel = useSelector((state: any) => state.kFModel);
  45. const { t } = useTranslate('common');
  46. const setPagination = useCallback(
  47. (pageNumber = 1, pageSize?: number) => {
  48. const pagination: Pagination = {
  49. current: pageNumber,
  50. } as Pagination;
  51. if (pageSize) {
  52. pagination.pageSize = pageSize;
  53. }
  54. dispatch({
  55. type: 'kFModel/setPagination',
  56. payload: pagination,
  57. });
  58. },
  59. [dispatch],
  60. );
  61. const onPageChange: PaginationProps['onChange'] = useCallback(
  62. (pageNumber: number, pageSize: number) => {
  63. setPagination(pageNumber, pageSize);
  64. fetchDocumentList();
  65. },
  66. [fetchDocumentList, setPagination],
  67. );
  68. const pagination: PaginationProps = useMemo(() => {
  69. return {
  70. showQuickJumper: true,
  71. total: kFModel.total,
  72. showSizeChanger: true,
  73. current: kFModel.pagination.current,
  74. pageSize: kFModel.pagination.pageSize,
  75. pageSizeOptions: [1, 2, 10, 20, 50, 100],
  76. onChange: onPageChange,
  77. showTotal: (total) => `${t('total')} ${total}`,
  78. };
  79. }, [kFModel, onPageChange, t]);
  80. return {
  81. pagination,
  82. setPagination,
  83. total: kFModel.total,
  84. searchString: kFModel.searchString,
  85. };
  86. };
  87. export const useSelectDocumentListLoading = () => {
  88. return useOneNamespaceEffectsLoading('kFModel', [
  89. 'getKfList',
  90. 'updateDocumentStatus',
  91. ]);
  92. };
  93. export const useNavigateToOtherPage = () => {
  94. const navigate = useNavigate();
  95. const { knowledgeId } = useGetKnowledgeSearchParams();
  96. const linkToUploadPage = useCallback(() => {
  97. navigate(`/knowledge/dataset/upload?id=${knowledgeId}`);
  98. }, [navigate, knowledgeId]);
  99. const toChunk = useCallback(
  100. (id: string) => {
  101. navigate(
  102. `/knowledge/${KnowledgeRouteKey.Dataset}/chunk?id=${knowledgeId}&doc_id=${id}`,
  103. );
  104. },
  105. [navigate, knowledgeId],
  106. );
  107. return { linkToUploadPage, toChunk };
  108. };
  109. export const useHandleSearchChange = (setPagination: () => void) => {
  110. const dispatch = useDispatch();
  111. const { knowledgeId } = useGetKnowledgeSearchParams();
  112. const throttledGetDocumentList = useCallback(() => {
  113. dispatch({
  114. type: 'kFModel/throttledGetDocumentList',
  115. payload: knowledgeId,
  116. });
  117. }, [dispatch, knowledgeId]);
  118. const handleInputChange = useCallback(
  119. (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
  120. const value = e.target.value;
  121. dispatch({ type: 'kFModel/setSearchString', payload: value });
  122. setPagination();
  123. throttledGetDocumentList();
  124. },
  125. [setPagination, throttledGetDocumentList, dispatch],
  126. );
  127. return { handleInputChange };
  128. };
  129. export const useRenameDocument = (documentId: string) => {
  130. const saveName = useSaveDocumentName();
  131. const {
  132. visible: renameVisible,
  133. hideModal: hideRenameModal,
  134. showModal: showRenameModal,
  135. } = useSetModalState();
  136. const loading = useOneNamespaceEffectsLoading('kFModel', ['document_rename']);
  137. const onRenameOk = useCallback(
  138. async (name: string) => {
  139. const ret = await saveName(documentId, name);
  140. if (ret === 0) {
  141. hideRenameModal();
  142. }
  143. },
  144. [hideRenameModal, saveName, documentId],
  145. );
  146. return {
  147. renameLoading: loading,
  148. onRenameOk,
  149. renameVisible,
  150. hideRenameModal,
  151. showRenameModal,
  152. };
  153. };
  154. export const useCreateEmptyDocument = () => {
  155. const createDocument = useCreateDocument();
  156. const {
  157. visible: createVisible,
  158. hideModal: hideCreateModal,
  159. showModal: showCreateModal,
  160. } = useSetModalState();
  161. const loading = useOneNamespaceEffectsLoading('kFModel', ['document_create']);
  162. const onCreateOk = useCallback(
  163. async (name: string) => {
  164. const ret = await createDocument(name);
  165. if (ret === 0) {
  166. hideCreateModal();
  167. }
  168. },
  169. [hideCreateModal, createDocument],
  170. );
  171. return {
  172. createLoading: loading,
  173. onCreateOk,
  174. createVisible,
  175. hideCreateModal,
  176. showCreateModal,
  177. };
  178. };
  179. export const useChangeDocumentParser = (documentId: string) => {
  180. const setDocumentParser = useSetDocumentParser();
  181. const {
  182. visible: changeParserVisible,
  183. hideModal: hideChangeParserModal,
  184. showModal: showChangeParserModal,
  185. } = useSetModalState();
  186. const loading = useOneNamespaceEffectsLoading('kFModel', [
  187. 'document_change_parser',
  188. ]);
  189. const onChangeParserOk = useCallback(
  190. async (parserId: string, parserConfig: IChangeParserConfigRequestBody) => {
  191. const ret = await setDocumentParser(parserId, documentId, parserConfig);
  192. if (ret === 0) {
  193. hideChangeParserModal();
  194. }
  195. },
  196. [hideChangeParserModal, setDocumentParser, documentId],
  197. );
  198. return {
  199. changeParserLoading: loading,
  200. onChangeParserOk,
  201. changeParserVisible,
  202. hideChangeParserModal,
  203. showChangeParserModal,
  204. };
  205. };
  206. export const useGetRowSelection = () => {
  207. const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
  208. const rowSelection = {
  209. selectedRowKeys,
  210. onChange: (newSelectedRowKeys: React.Key[]) => {
  211. setSelectedRowKeys(newSelectedRowKeys);
  212. },
  213. };
  214. return rowSelection;
  215. };
  216. export const useHandleUploadDocument = () => {
  217. const {
  218. visible: documentUploadVisible,
  219. hideModal: hideDocumentUploadModal,
  220. showModal: showDocumentUploadModal,
  221. } = useSetModalState();
  222. const uploadDocument = useUploadDocument();
  223. const onDocumentUploadOk = useCallback(
  224. async (fileList: UploadFile[]): Promise<number | undefined> => {
  225. if (fileList.length > 0) {
  226. const ret: any = await uploadDocument(fileList);
  227. const count = getUnSupportedFilesCount(ret.retmsg);
  228. /// 500 error code indicates that some file types are not supported
  229. let retcode = ret.retcode;
  230. if (
  231. ret.retcode === 0 ||
  232. (ret.retcode === 500 && count !== fileList.length) // Some files were not uploaded successfully, but some were uploaded successfully.
  233. ) {
  234. retcode = 0;
  235. hideDocumentUploadModal();
  236. }
  237. return retcode;
  238. }
  239. },
  240. [uploadDocument, hideDocumentUploadModal],
  241. );
  242. const loading = useOneNamespaceEffectsLoading('kFModel', ['upload_document']);
  243. return {
  244. documentUploadLoading: loading,
  245. onDocumentUploadOk,
  246. documentUploadVisible,
  247. hideDocumentUploadModal,
  248. showDocumentUploadModal,
  249. };
  250. };
  251. export const useHandleRunDocumentByIds = (id: string) => {
  252. const loading = useSelectRunDocumentLoading();
  253. const runDocumentByIds = useRunDocument();
  254. const [currentId, setCurrentId] = useState<string>('');
  255. const isLoading = loading && currentId !== '' && currentId === id;
  256. const handleRunDocumentByIds = async (
  257. documentId: string,
  258. knowledgeBaseId: string,
  259. isRunning: boolean,
  260. ) => {
  261. if (isLoading) {
  262. return;
  263. }
  264. setCurrentId(documentId);
  265. try {
  266. await runDocumentByIds({
  267. doc_ids: [documentId],
  268. run: isRunning ? 2 : 1,
  269. knowledgeBaseId,
  270. });
  271. setCurrentId('');
  272. } catch (error) {
  273. setCurrentId('');
  274. }
  275. };
  276. return {
  277. handleRunDocumentByIds,
  278. loading: isLoading,
  279. };
  280. };