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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import { useSetModalState } from '@/hooks/commonHooks';
  2. import {
  3. useCreateDocument,
  4. useFetchDocumentList,
  5. useSaveDocumentName,
  6. useSetDocumentParser,
  7. } from '@/hooks/documentHooks';
  8. import { useGetKnowledgeSearchParams } from '@/hooks/routeHook';
  9. import { useOneNamespaceEffectsLoading } from '@/hooks/storeHooks';
  10. import { useFetchTenantInfo } from '@/hooks/userSettingHook';
  11. import { Pagination } from '@/interfaces/common';
  12. import { IChangeParserConfigRequestBody } from '@/interfaces/request/document';
  13. import { PaginationProps } from 'antd';
  14. import { useCallback, useEffect, useMemo } from 'react';
  15. import { useDispatch, useNavigate, useSelector } from 'umi';
  16. import { KnowledgeRouteKey } from './constant';
  17. export const useFetchDocumentListOnMount = () => {
  18. const { knowledgeId } = useGetKnowledgeSearchParams();
  19. const fetchDocumentList = useFetchDocumentList();
  20. const dispatch = useDispatch();
  21. useFetchTenantInfo();
  22. useEffect(() => {
  23. if (knowledgeId) {
  24. fetchDocumentList();
  25. dispatch({
  26. type: 'kFModel/pollGetDocumentList-start',
  27. payload: knowledgeId,
  28. });
  29. }
  30. return () => {
  31. dispatch({
  32. type: 'kFModel/pollGetDocumentList-stop',
  33. });
  34. };
  35. }, [knowledgeId, dispatch, fetchDocumentList]);
  36. return { fetchDocumentList };
  37. };
  38. export const useGetPagination = (fetchDocumentList: () => void) => {
  39. const dispatch = useDispatch();
  40. const kFModel = useSelector((state: any) => state.kFModel);
  41. const setPagination = useCallback(
  42. (pageNumber = 1, pageSize?: number) => {
  43. const pagination: Pagination = {
  44. current: pageNumber,
  45. } as Pagination;
  46. if (pageSize) {
  47. pagination.pageSize = pageSize;
  48. }
  49. dispatch({
  50. type: 'kFModel/setPagination',
  51. payload: pagination,
  52. });
  53. },
  54. [dispatch],
  55. );
  56. const onPageChange: PaginationProps['onChange'] = useCallback(
  57. (pageNumber: number, pageSize: number) => {
  58. setPagination(pageNumber, pageSize);
  59. fetchDocumentList();
  60. },
  61. [fetchDocumentList, setPagination],
  62. );
  63. const pagination: PaginationProps = useMemo(() => {
  64. return {
  65. showQuickJumper: true,
  66. total: kFModel.total,
  67. showSizeChanger: true,
  68. current: kFModel.pagination.current,
  69. pageSize: kFModel.pagination.pageSize,
  70. pageSizeOptions: [1, 2, 10, 20, 50, 100],
  71. onChange: onPageChange,
  72. };
  73. }, [kFModel, onPageChange]);
  74. return {
  75. pagination,
  76. setPagination,
  77. total: kFModel.total,
  78. searchString: kFModel.searchString,
  79. };
  80. };
  81. export const useSelectDocumentListLoading = () => {
  82. return useOneNamespaceEffectsLoading('kFModel', [
  83. 'getKfList',
  84. 'updateDocumentStatus',
  85. ]);
  86. };
  87. export const useNavigateToOtherPage = () => {
  88. const navigate = useNavigate();
  89. const { knowledgeId } = useGetKnowledgeSearchParams();
  90. const linkToUploadPage = useCallback(() => {
  91. navigate(`/knowledge/dataset/upload?id=${knowledgeId}`);
  92. }, [navigate, knowledgeId]);
  93. const toChunk = useCallback(
  94. (id: string) => {
  95. navigate(
  96. `/knowledge/${KnowledgeRouteKey.Dataset}/chunk?id=${knowledgeId}&doc_id=${id}`,
  97. );
  98. },
  99. [navigate, knowledgeId],
  100. );
  101. return { linkToUploadPage, toChunk };
  102. };
  103. export const useHandleSearchChange = (setPagination: () => void) => {
  104. const dispatch = useDispatch();
  105. const { knowledgeId } = useGetKnowledgeSearchParams();
  106. const throttledGetDocumentList = useCallback(() => {
  107. dispatch({
  108. type: 'kFModel/throttledGetDocumentList',
  109. payload: knowledgeId,
  110. });
  111. }, [dispatch, knowledgeId]);
  112. const handleInputChange = useCallback(
  113. (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
  114. const value = e.target.value;
  115. dispatch({ type: 'kFModel/setSearchString', payload: value });
  116. setPagination();
  117. throttledGetDocumentList();
  118. },
  119. [setPagination, throttledGetDocumentList, dispatch],
  120. );
  121. return { handleInputChange };
  122. };
  123. export const useRenameDocument = (documentId: string) => {
  124. const saveName = useSaveDocumentName();
  125. const {
  126. visible: renameVisible,
  127. hideModal: hideRenameModal,
  128. showModal: showRenameModal,
  129. } = useSetModalState();
  130. const loading = useOneNamespaceEffectsLoading('kFModel', ['document_rename']);
  131. const onRenameOk = useCallback(
  132. async (name: string) => {
  133. const ret = await saveName(documentId, name);
  134. if (ret === 0) {
  135. hideRenameModal();
  136. }
  137. },
  138. [hideRenameModal, saveName, documentId],
  139. );
  140. return {
  141. renameLoading: loading,
  142. onRenameOk,
  143. renameVisible,
  144. hideRenameModal,
  145. showRenameModal,
  146. };
  147. };
  148. export const useCreateEmptyDocument = () => {
  149. const createDocument = useCreateDocument();
  150. const {
  151. visible: createVisible,
  152. hideModal: hideCreateModal,
  153. showModal: showCreateModal,
  154. } = useSetModalState();
  155. const loading = useOneNamespaceEffectsLoading('kFModel', ['document_create']);
  156. const onCreateOk = useCallback(
  157. async (name: string) => {
  158. const ret = await createDocument(name);
  159. if (ret === 0) {
  160. hideCreateModal();
  161. }
  162. },
  163. [hideCreateModal, createDocument],
  164. );
  165. return {
  166. createLoading: loading,
  167. onCreateOk,
  168. createVisible,
  169. hideCreateModal,
  170. showCreateModal,
  171. };
  172. };
  173. export const useChangeDocumentParser = (documentId: string) => {
  174. const setDocumentParser = useSetDocumentParser();
  175. const {
  176. visible: changeParserVisible,
  177. hideModal: hideChangeParserModal,
  178. showModal: showChangeParserModal,
  179. } = useSetModalState();
  180. const loading = useOneNamespaceEffectsLoading('kFModel', [
  181. 'document_change_parser',
  182. ]);
  183. const onChangeParserOk = useCallback(
  184. async (parserId: string, parserConfig: IChangeParserConfigRequestBody) => {
  185. const ret = await setDocumentParser(parserId, documentId, parserConfig);
  186. if (ret === 0) {
  187. hideChangeParserModal();
  188. }
  189. },
  190. [hideChangeParserModal, setDocumentParser, documentId],
  191. );
  192. return {
  193. changeParserLoading: loading,
  194. onChangeParserOk,
  195. changeParserVisible,
  196. hideChangeParserModal,
  197. showChangeParserModal,
  198. };
  199. };