Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

index.tsx 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { getOneNamespaceEffectsLoading } from '@/utils/storeUtil';
  2. import type { PaginationProps } from 'antd';
  3. import { Divider, Pagination, Space, Spin, message } from 'antd';
  4. import { useCallback, useEffect, useState } from 'react';
  5. import { useDispatch, useSearchParams, useSelector } from 'umi';
  6. import CreatingModal from './components/chunk-creating-modal';
  7. import { useDeleteChunkByIds } from '@/hooks/knowledgeHook';
  8. import ChunkCard from './components/chunk-card';
  9. import ChunkToolBar from './components/chunk-toolbar';
  10. import styles from './index.less';
  11. import { ChunkModelState } from './model';
  12. interface PayloadType {
  13. doc_id: string;
  14. keywords?: string;
  15. }
  16. const Chunk = () => {
  17. const dispatch = useDispatch();
  18. const chunkModel: ChunkModelState = useSelector(
  19. (state: any) => state.chunkModel,
  20. );
  21. const [selectedChunkIds, setSelectedChunkIds] = useState<string[]>([]);
  22. const [searchParams] = useSearchParams();
  23. const { data = [], total, pagination } = chunkModel;
  24. const effects = useSelector((state: any) => state.loading.effects);
  25. const loading = getOneNamespaceEffectsLoading('chunkModel', effects, [
  26. 'create_hunk',
  27. 'chunk_list',
  28. 'switch_chunk',
  29. ]);
  30. const documentId: string = searchParams.get('doc_id') || '';
  31. const [chunkId, setChunkId] = useState<string | undefined>();
  32. const { removeChunk } = useDeleteChunkByIds();
  33. const getChunkList = useCallback(() => {
  34. const payload: PayloadType = {
  35. doc_id: documentId,
  36. };
  37. dispatch({
  38. type: 'chunkModel/chunk_list',
  39. payload: {
  40. ...payload,
  41. },
  42. });
  43. }, [dispatch, documentId]);
  44. const handleEditChunk = useCallback(
  45. (chunk_id?: string) => {
  46. setChunkId(chunk_id);
  47. dispatch({
  48. type: 'chunkModel/setIsShowCreateModal',
  49. payload: true,
  50. });
  51. },
  52. [dispatch],
  53. );
  54. const onPaginationChange: PaginationProps['onShowSizeChange'] = (
  55. page,
  56. size,
  57. ) => {
  58. setSelectedChunkIds([]);
  59. dispatch({
  60. type: 'chunkModel/setPagination',
  61. payload: {
  62. current: page,
  63. pageSize: size,
  64. },
  65. });
  66. getChunkList();
  67. };
  68. const selectAllChunk = useCallback(
  69. (checked: boolean) => {
  70. setSelectedChunkIds(checked ? data.map((x) => x.chunk_id) : []);
  71. },
  72. [data],
  73. );
  74. const handleSingleCheckboxClick = useCallback(
  75. (chunkId: string, checked: boolean) => {
  76. setSelectedChunkIds((previousIds) => {
  77. const idx = previousIds.findIndex((x) => x === chunkId);
  78. const nextIds = [...previousIds];
  79. if (checked && idx === -1) {
  80. nextIds.push(chunkId);
  81. } else if (!checked && idx !== -1) {
  82. nextIds.splice(idx, 1);
  83. }
  84. return nextIds;
  85. });
  86. },
  87. [],
  88. );
  89. const showSelectedChunkWarning = () => {
  90. message.warning('Please select chunk!');
  91. };
  92. const handleRemoveChunk = useCallback(async () => {
  93. if (selectedChunkIds.length > 0) {
  94. const resCode: number = await removeChunk(selectedChunkIds, documentId);
  95. if (resCode === 0) {
  96. setSelectedChunkIds([]);
  97. }
  98. } else {
  99. showSelectedChunkWarning();
  100. }
  101. }, [selectedChunkIds, documentId, removeChunk]);
  102. const switchChunk = useCallback(
  103. async (available?: number, chunkIds?: string[]) => {
  104. let ids = chunkIds;
  105. if (!chunkIds) {
  106. ids = selectedChunkIds;
  107. if (selectedChunkIds.length === 0) {
  108. showSelectedChunkWarning();
  109. return;
  110. }
  111. }
  112. const resCode: number = await dispatch<any>({
  113. type: 'chunkModel/switch_chunk',
  114. payload: {
  115. chunk_ids: ids,
  116. available_int: available,
  117. doc_id: documentId,
  118. },
  119. });
  120. if (!chunkIds && resCode === 0) {
  121. getChunkList();
  122. }
  123. },
  124. [dispatch, documentId, getChunkList, selectedChunkIds],
  125. );
  126. useEffect(() => {
  127. getChunkList();
  128. return () => {
  129. dispatch({
  130. type: 'chunkModel/resetFilter', // TODO: need to reset state uniformly
  131. });
  132. };
  133. }, [dispatch, getChunkList]);
  134. return (
  135. <>
  136. <div className={styles.chunkPage}>
  137. <ChunkToolBar
  138. getChunkList={getChunkList}
  139. selectAllChunk={selectAllChunk}
  140. createChunk={handleEditChunk}
  141. removeChunk={handleRemoveChunk}
  142. checked={selectedChunkIds.length === data.length}
  143. switchChunk={switchChunk}
  144. ></ChunkToolBar>
  145. <Divider></Divider>
  146. <div className={styles.pageContent}>
  147. <Spin spinning={loading} className={styles.spin} size="large">
  148. <Space
  149. direction="vertical"
  150. size={'middle'}
  151. className={styles.chunkContainer}
  152. >
  153. {data.map((item) => (
  154. <ChunkCard
  155. item={item}
  156. key={item.chunk_id}
  157. editChunk={handleEditChunk}
  158. checked={selectedChunkIds.some((x) => x === item.chunk_id)}
  159. handleCheckboxClick={handleSingleCheckboxClick}
  160. switchChunk={switchChunk}
  161. ></ChunkCard>
  162. ))}
  163. </Space>
  164. </Spin>
  165. </div>
  166. <div className={styles.pageFooter}>
  167. <Pagination
  168. responsive
  169. showLessItems
  170. showQuickJumper
  171. showSizeChanger
  172. onChange={onPaginationChange}
  173. pageSize={pagination.pageSize}
  174. pageSizeOptions={[10, 30, 60, 90]}
  175. current={pagination.current}
  176. total={total}
  177. />
  178. </div>
  179. </div>
  180. <CreatingModal doc_id={documentId} chunkId={chunkId} />
  181. </>
  182. );
  183. };
  184. export default Chunk;