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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import { useSetModalState, useTranslate } 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, useState } 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 { 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 useSelectDocumentListLoading = () => {
  84. return useOneNamespaceEffectsLoading('kFModel', [
  85. 'getKfList',
  86. 'updateDocumentStatus',
  87. ]);
  88. };
  89. export const useNavigateToOtherPage = () => {
  90. const navigate = useNavigate();
  91. const { knowledgeId } = useGetKnowledgeSearchParams();
  92. const linkToUploadPage = useCallback(() => {
  93. navigate(`/knowledge/dataset/upload?id=${knowledgeId}`);
  94. }, [navigate, knowledgeId]);
  95. const toChunk = useCallback(
  96. (id: string) => {
  97. navigate(
  98. `/knowledge/${KnowledgeRouteKey.Dataset}/chunk?id=${knowledgeId}&doc_id=${id}`,
  99. );
  100. },
  101. [navigate, knowledgeId],
  102. );
  103. return { linkToUploadPage, toChunk };
  104. };
  105. export const useHandleSearchChange = (setPagination: () => void) => {
  106. const dispatch = useDispatch();
  107. const { knowledgeId } = useGetKnowledgeSearchParams();
  108. const throttledGetDocumentList = useCallback(() => {
  109. dispatch({
  110. type: 'kFModel/throttledGetDocumentList',
  111. payload: knowledgeId,
  112. });
  113. }, [dispatch, knowledgeId]);
  114. const handleInputChange = useCallback(
  115. (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
  116. const value = e.target.value;
  117. dispatch({ type: 'kFModel/setSearchString', payload: value });
  118. setPagination();
  119. throttledGetDocumentList();
  120. },
  121. [setPagination, throttledGetDocumentList, dispatch],
  122. );
  123. return { handleInputChange };
  124. };
  125. export const useRenameDocument = (documentId: string) => {
  126. const saveName = useSaveDocumentName();
  127. const {
  128. visible: renameVisible,
  129. hideModal: hideRenameModal,
  130. showModal: showRenameModal,
  131. } = useSetModalState();
  132. const loading = useOneNamespaceEffectsLoading('kFModel', ['document_rename']);
  133. const onRenameOk = useCallback(
  134. async (name: string) => {
  135. const ret = await saveName(documentId, name);
  136. if (ret === 0) {
  137. hideRenameModal();
  138. }
  139. },
  140. [hideRenameModal, saveName, documentId],
  141. );
  142. return {
  143. renameLoading: loading,
  144. onRenameOk,
  145. renameVisible,
  146. hideRenameModal,
  147. showRenameModal,
  148. };
  149. };
  150. export const useCreateEmptyDocument = () => {
  151. const createDocument = useCreateDocument();
  152. const {
  153. visible: createVisible,
  154. hideModal: hideCreateModal,
  155. showModal: showCreateModal,
  156. } = useSetModalState();
  157. const loading = useOneNamespaceEffectsLoading('kFModel', ['document_create']);
  158. const onCreateOk = useCallback(
  159. async (name: string) => {
  160. const ret = await createDocument(name);
  161. if (ret === 0) {
  162. hideCreateModal();
  163. }
  164. },
  165. [hideCreateModal, createDocument],
  166. );
  167. return {
  168. createLoading: loading,
  169. onCreateOk,
  170. createVisible,
  171. hideCreateModal,
  172. showCreateModal,
  173. };
  174. };
  175. export const useChangeDocumentParser = (documentId: string) => {
  176. const setDocumentParser = useSetDocumentParser();
  177. const {
  178. visible: changeParserVisible,
  179. hideModal: hideChangeParserModal,
  180. showModal: showChangeParserModal,
  181. } = useSetModalState();
  182. const loading = useOneNamespaceEffectsLoading('kFModel', [
  183. 'document_change_parser',
  184. ]);
  185. const onChangeParserOk = useCallback(
  186. async (parserId: string, parserConfig: IChangeParserConfigRequestBody) => {
  187. const ret = await setDocumentParser(parserId, documentId, parserConfig);
  188. if (ret === 0) {
  189. hideChangeParserModal();
  190. }
  191. },
  192. [hideChangeParserModal, setDocumentParser, documentId],
  193. );
  194. return {
  195. changeParserLoading: loading,
  196. onChangeParserOk,
  197. changeParserVisible,
  198. hideChangeParserModal,
  199. showChangeParserModal,
  200. };
  201. };
  202. export const useGetRowSelection = () => {
  203. const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
  204. const rowSelection = {
  205. selectedRowKeys,
  206. onChange: (newSelectedRowKeys: React.Key[]) => {
  207. setSelectedRowKeys(newSelectedRowKeys);
  208. },
  209. };
  210. return rowSelection;
  211. };