2024-02-05 19:26:03 +08:00
|
|
|
import { getOneNamespaceEffectsLoading } from '@/utils/storeUtil';
|
2024-01-18 18:27:38 +08:00
|
|
|
import type { PaginationProps } from 'antd';
|
2024-02-06 18:45:20 +08:00
|
|
|
import { Button, Input, Pagination, Space, Spin } from 'antd';
|
2024-02-02 18:49:54 +08:00
|
|
|
import { debounce } from 'lodash';
|
2024-01-30 19:26:29 +08:00
|
|
|
import React, { useCallback, useEffect, useState } from 'react';
|
2024-02-02 18:49:54 +08:00
|
|
|
import { useDispatch, useSearchParams, useSelector } from 'umi';
|
2024-01-30 19:26:29 +08:00
|
|
|
import CreateModal from './components/createModal';
|
2024-01-18 18:27:38 +08:00
|
|
|
|
2024-02-06 18:45:20 +08:00
|
|
|
import ChunkCard from './components/chunk-card';
|
2024-02-05 19:26:03 +08:00
|
|
|
import ChunkToolBar from './components/chunk-toolbar';
|
2024-01-30 19:26:29 +08:00
|
|
|
import styles from './index.less';
|
2024-02-06 18:45:20 +08:00
|
|
|
import { ChunkModelState } from './model';
|
2024-01-30 19:26:29 +08:00
|
|
|
|
|
|
|
interface PayloadType {
|
|
|
|
doc_id: string;
|
|
|
|
keywords?: string;
|
|
|
|
}
|
|
|
|
|
2024-02-02 18:49:54 +08:00
|
|
|
const Chunk = () => {
|
2024-01-30 19:26:29 +08:00
|
|
|
const dispatch = useDispatch();
|
2024-02-06 18:45:20 +08:00
|
|
|
const chunkModel: ChunkModelState = useSelector(
|
|
|
|
(state: any) => state.chunkModel,
|
|
|
|
);
|
2024-01-30 19:26:29 +08:00
|
|
|
const [keywords, SetKeywords] = useState('');
|
2024-02-06 18:45:20 +08:00
|
|
|
const [selectedChunkIds, setSelectedChunkIds] = useState<string[]>([]);
|
2024-02-02 18:49:54 +08:00
|
|
|
const [searchParams] = useSearchParams();
|
2024-02-06 18:45:20 +08:00
|
|
|
const {
|
|
|
|
data = [],
|
|
|
|
total,
|
|
|
|
chunk_id,
|
|
|
|
isShowCreateModal,
|
|
|
|
pagination,
|
|
|
|
} = chunkModel;
|
2024-01-30 19:26:29 +08:00
|
|
|
const effects = useSelector((state: any) => state.loading.effects);
|
|
|
|
const loading = getOneNamespaceEffectsLoading('chunkModel', effects, [
|
|
|
|
'create_hunk',
|
|
|
|
'chunk_list',
|
|
|
|
'switch_chunk',
|
|
|
|
]);
|
2024-02-02 18:49:54 +08:00
|
|
|
const documentId: string = searchParams.get('doc_id') || '';
|
2024-01-30 19:26:29 +08:00
|
|
|
|
2024-02-06 18:45:20 +08:00
|
|
|
const getChunkList = () => {
|
2024-01-30 19:26:29 +08:00
|
|
|
const payload: PayloadType = {
|
2024-02-02 18:49:54 +08:00
|
|
|
doc_id: documentId,
|
2024-01-30 19:26:29 +08:00
|
|
|
};
|
2024-02-06 18:45:20 +08:00
|
|
|
|
2024-01-18 18:27:38 +08:00
|
|
|
dispatch({
|
|
|
|
type: 'chunkModel/chunk_list',
|
|
|
|
payload: {
|
|
|
|
...payload,
|
2024-01-30 19:26:29 +08:00
|
|
|
},
|
2024-01-18 18:27:38 +08:00
|
|
|
});
|
2024-01-30 19:26:29 +08:00
|
|
|
};
|
2024-02-06 18:45:20 +08:00
|
|
|
|
2024-01-30 19:26:29 +08:00
|
|
|
const confirm = async (id: string) => {
|
|
|
|
const retcode = await dispatch<any>({
|
2024-01-18 18:27:38 +08:00
|
|
|
type: 'chunkModel/rm_chunk',
|
|
|
|
payload: {
|
2024-01-30 19:26:29 +08:00
|
|
|
chunk_ids: [id],
|
2024-01-18 18:27:38 +08:00
|
|
|
},
|
|
|
|
});
|
2024-01-30 19:26:29 +08:00
|
|
|
|
|
|
|
retcode === 0 && getChunkList();
|
2024-01-18 18:27:38 +08:00
|
|
|
};
|
2024-01-30 19:26:29 +08:00
|
|
|
|
2024-01-18 18:27:38 +08:00
|
|
|
const handleEditchunk = (chunk_id?: string) => {
|
|
|
|
dispatch({
|
|
|
|
type: 'chunkModel/updateState',
|
|
|
|
payload: {
|
|
|
|
isShowCreateModal: true,
|
2024-01-19 18:35:24 +08:00
|
|
|
chunk_id,
|
2024-02-02 18:49:54 +08:00
|
|
|
doc_id: documentId,
|
2024-01-18 18:27:38 +08:00
|
|
|
},
|
|
|
|
});
|
2024-01-30 19:26:29 +08:00
|
|
|
getChunkList();
|
2024-01-18 18:27:38 +08:00
|
|
|
};
|
2024-01-30 19:26:29 +08:00
|
|
|
|
2024-02-06 18:45:20 +08:00
|
|
|
const onPaginationChange: PaginationProps['onShowSizeChange'] = (
|
2024-01-30 19:26:29 +08:00
|
|
|
page,
|
|
|
|
size,
|
|
|
|
) => {
|
2024-02-06 18:45:20 +08:00
|
|
|
setSelectedChunkIds([]);
|
|
|
|
dispatch({
|
|
|
|
type: 'chunkModel/setPagination',
|
2024-01-18 18:27:38 +08:00
|
|
|
payload: {
|
2024-02-06 18:45:20 +08:00
|
|
|
current: page,
|
|
|
|
pageSize: size,
|
2024-01-18 18:27:38 +08:00
|
|
|
},
|
|
|
|
});
|
2024-02-06 18:45:20 +08:00
|
|
|
getChunkList();
|
2024-01-30 19:26:29 +08:00
|
|
|
};
|
2024-01-18 18:27:38 +08:00
|
|
|
|
2024-02-06 18:45:20 +08:00
|
|
|
const selectAllChunk = useCallback(
|
|
|
|
(checked: boolean) => {
|
|
|
|
setSelectedChunkIds(checked ? data.map((x) => x.chunk_id) : []);
|
|
|
|
// setSelectedChunkIds((previousIds) => {
|
|
|
|
// return checked ? [...previousIds, ...data.map((x) => x.chunk_id)] : [];
|
|
|
|
// });
|
|
|
|
},
|
|
|
|
[data],
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleSingleCheckboxClick = useCallback(
|
|
|
|
(chunkId: string, checked: boolean) => {
|
|
|
|
setSelectedChunkIds((previousIds) => {
|
|
|
|
const idx = previousIds.findIndex((x) => x === chunkId);
|
|
|
|
const nextIds = [...previousIds];
|
|
|
|
if (checked && idx === -1) {
|
|
|
|
nextIds.push(chunkId);
|
|
|
|
} else if (!checked && idx !== -1) {
|
|
|
|
nextIds.splice(idx, 1);
|
|
|
|
}
|
|
|
|
return nextIds;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
|
2024-01-18 18:27:38 +08:00
|
|
|
useEffect(() => {
|
2024-01-30 19:26:29 +08:00
|
|
|
getChunkList();
|
2024-02-06 18:45:20 +08:00
|
|
|
return () => {
|
|
|
|
dispatch({
|
|
|
|
type: 'chunkModel/resetFilter', // TODO: need to reset state uniformly
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}, [documentId]);
|
2024-01-30 19:26:29 +08:00
|
|
|
|
|
|
|
const debounceChange = debounce(getChunkList, 300);
|
|
|
|
const debounceCallback = useCallback(
|
|
|
|
(value: string) => debounceChange(value),
|
|
|
|
[],
|
|
|
|
);
|
2024-01-18 18:27:38 +08:00
|
|
|
|
2024-01-30 19:26:29 +08:00
|
|
|
const handleInputChange = (
|
|
|
|
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
|
|
|
|
) => {
|
2024-02-06 18:45:20 +08:00
|
|
|
setSelectedChunkIds([]);
|
2024-01-30 19:26:29 +08:00
|
|
|
const value = e.target.value;
|
|
|
|
SetKeywords(value);
|
|
|
|
debounceCallback(value);
|
|
|
|
};
|
2024-02-06 18:45:20 +08:00
|
|
|
|
2024-01-30 19:26:29 +08:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className={styles.chunkPage}>
|
2024-02-06 18:45:20 +08:00
|
|
|
<ChunkToolBar
|
|
|
|
getChunkList={getChunkList}
|
|
|
|
selectAllChunk={selectAllChunk}
|
|
|
|
checked={selectedChunkIds.length === data.length}
|
|
|
|
></ChunkToolBar>
|
2024-01-30 19:26:29 +08:00
|
|
|
<div className={styles.filter}>
|
|
|
|
<div>
|
|
|
|
<Input
|
|
|
|
placeholder="搜索"
|
|
|
|
style={{ width: 220 }}
|
|
|
|
value={keywords}
|
|
|
|
allowClear
|
|
|
|
onChange={handleInputChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
handleEditchunk();
|
|
|
|
}}
|
|
|
|
type="link"
|
|
|
|
>
|
|
|
|
添加分段
|
|
|
|
</Button>
|
2024-01-18 18:27:38 +08:00
|
|
|
</div>
|
2024-01-30 19:26:29 +08:00
|
|
|
<div className={styles.pageContent}>
|
|
|
|
<Spin spinning={loading} className={styles.spin} size="large">
|
2024-02-06 18:45:20 +08:00
|
|
|
<Space direction="vertical" size={'middle'}>
|
|
|
|
{data.map((item) => (
|
|
|
|
<ChunkCard
|
|
|
|
item={item}
|
|
|
|
key={item.chunk_id}
|
|
|
|
checked={selectedChunkIds.some((x) => x === item.chunk_id)}
|
|
|
|
handleCheckboxClick={handleSingleCheckboxClick}
|
|
|
|
></ChunkCard>
|
|
|
|
))}
|
|
|
|
</Space>
|
2024-01-30 19:26:29 +08:00
|
|
|
</Spin>
|
|
|
|
</div>
|
|
|
|
<div className={styles.pageFooter}>
|
|
|
|
<Pagination
|
|
|
|
responsive
|
|
|
|
showLessItems
|
|
|
|
showQuickJumper
|
|
|
|
showSizeChanger
|
2024-02-06 18:45:20 +08:00
|
|
|
onChange={onPaginationChange}
|
|
|
|
defaultPageSize={10}
|
|
|
|
pageSizeOptions={[10, 30, 60, 90]}
|
|
|
|
defaultCurrent={pagination.current}
|
2024-01-30 19:26:29 +08:00
|
|
|
total={total}
|
|
|
|
/>
|
|
|
|
</div>
|
2024-01-18 18:27:38 +08:00
|
|
|
</div>
|
2024-01-30 19:26:29 +08:00
|
|
|
<CreateModal
|
2024-02-02 18:49:54 +08:00
|
|
|
doc_id={documentId}
|
2024-01-30 19:26:29 +08:00
|
|
|
isShowCreateModal={isShowCreateModal}
|
|
|
|
chunk_id={chunk_id}
|
|
|
|
getChunkList={getChunkList}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
2024-01-18 18:27:38 +08:00
|
|
|
};
|
|
|
|
|
2024-01-30 19:26:29 +08:00
|
|
|
export default Chunk;
|