File size: 6,708 Bytes
3060a10 68ed806 fad2ec7 e4e6a45 e55650e eb8254e 3060a10 97d4387 b40a87c eb38196 f422a06 e55650e 8e109c7 3060a10 e55650e 362ec6c 88e5a61 b40a87c 362ec6c 7b71fb2 362ec6c 3060a10 97d4387 7b71fb2 3060a10 7b71fb2 eb8254e 3060a10 f422a06 3060a10 88e5a61 8e109c7 362ec6c eb8254e 362ec6c eb8254e 362ec6c 97d4387 362ec6c 97d4387 3060a10 362ec6c fad2ec7 97d4387 8e109c7 88e5a61 8e109c7 eb8254e 8e109c7 eb8254e 3060a10 eb8254e 8e109c7 3060a10 8e109c7 eb8254e 97d4387 3060a10 fad2ec7 3060a10 97d4387 3060a10 97d4387 362ec6c 97d4387 eb8254e 97d4387 eb8254e 8e109c7 3060a10 97d4387 eb8254e e4e6a45 b40a87c e55650e e4e6a45 b40a87c e4e6a45 f422a06 8e109c7 e4e6a45 e55650e e4e6a45 3060a10 e4e6a45 3060a10 e4e6a45 3060a10 e4e6a45 f422a06 3060a10 f422a06 e4e6a45 3060a10 e4e6a45 fad2ec7 eb8254e 362ec6c fad2ec7 362ec6c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
import { useFetchNextChunkList } from '@/hooks/chunk-hooks';
import { useDeleteChunkByIds } from '@/hooks/knowledge-hooks';
import type { PaginationProps } from 'antd';
import { Divider, Flex, Pagination, Space, Spin, message } from 'antd';
import classNames from 'classnames';
import { useCallback, useEffect, useState } from 'react';
import { useDispatch, useSearchParams } from 'umi';
import ChunkCard from './components/chunk-card';
import CreatingModal from './components/chunk-creating-modal';
import ChunkToolBar from './components/chunk-toolbar';
import DocumentPreview from './components/document-preview/preview';
import {
useChangeChunkTextMode,
useGetChunkHighlights,
useHandleChunkCardClick,
} from './hooks';
import { useTranslation } from 'react-i18next';
import styles from './index.less';
const Chunk = () => {
const dispatch = useDispatch();
const [selectedChunkIds, setSelectedChunkIds] = useState<string[]>([]);
const [searchParams] = useSearchParams();
// const loading = useSelectChunkListLoading();
const documentId: string = searchParams.get('doc_id') || '';
const [chunkId, setChunkId] = useState<string | undefined>();
const { removeChunk } = useDeleteChunkByIds();
const {
data: { documentInfo, data = [], total },
pagination,
loading,
searchString,
handleInputChange,
available,
handleSetAvailable,
} = useFetchNextChunkList();
const { handleChunkCardClick, selectedChunkId } = useHandleChunkCardClick();
const isPdf = documentInfo?.type === 'pdf';
const { t } = useTranslation();
const { changeChunkTextMode, textMode } = useChangeChunkTextMode();
const handleEditChunk = useCallback(
(chunk_id?: string) => {
setChunkId(chunk_id);
dispatch({
type: 'chunkModel/setIsShowCreateModal',
payload: true,
});
},
[dispatch],
);
const onPaginationChange: PaginationProps['onShowSizeChange'] = (
page,
size,
) => {
setSelectedChunkIds([]);
pagination.onChange?.(page, size);
// getChunkList();
};
const selectAllChunk = useCallback(
(checked: boolean) => {
setSelectedChunkIds(checked ? 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;
});
},
[],
);
const showSelectedChunkWarning = useCallback(() => {
message.warning(t('message.pleaseSelectChunk'));
}, [t]);
const handleRemoveChunk = useCallback(async () => {
if (selectedChunkIds.length > 0) {
const resCode: number = await removeChunk(selectedChunkIds, documentId);
if (resCode === 0) {
setSelectedChunkIds([]);
}
} else {
showSelectedChunkWarning();
}
}, [selectedChunkIds, documentId, removeChunk, showSelectedChunkWarning]);
const switchChunk = useCallback(
async (available?: number, chunkIds?: string[]) => {
let ids = chunkIds;
if (!chunkIds) {
ids = selectedChunkIds;
if (selectedChunkIds.length === 0) {
showSelectedChunkWarning();
return;
}
}
const resCode: number = await dispatch<any>({
type: 'chunkModel/switch_chunk',
payload: {
chunk_ids: ids,
available_int: available,
doc_id: documentId,
},
});
if (!chunkIds && resCode === 0) {
// getChunkList();
}
},
[
dispatch,
documentId,
// getChunkList,
selectedChunkIds,
showSelectedChunkWarning,
],
);
const { highlights, setWidthAndHeight } =
useGetChunkHighlights(selectedChunkId);
useEffect(() => {
// getChunkList();
return () => {
dispatch({
type: 'chunkModel/resetFilter', // TODO: need to reset state uniformly
});
};
}, [dispatch]);
return (
<>
<div className={styles.chunkPage}>
<ChunkToolBar
selectAllChunk={selectAllChunk}
createChunk={handleEditChunk}
removeChunk={handleRemoveChunk}
checked={selectedChunkIds.length === data.length}
switchChunk={switchChunk}
changeChunkTextMode={changeChunkTextMode}
searchString={searchString}
handleInputChange={handleInputChange}
available={available}
handleSetAvailable={handleSetAvailable}
></ChunkToolBar>
<Divider></Divider>
<Flex flex={1} gap={'middle'}>
<Flex
vertical
className={isPdf ? styles.pagePdfWrapper : styles.pageWrapper}
>
<Spin spinning={loading} className={styles.spin} size="large">
<div className={styles.pageContent}>
<Space
direction="vertical"
size={'middle'}
className={classNames(styles.chunkContainer, {
[styles.chunkOtherContainer]: !isPdf,
})}
>
{data.map((item) => (
<ChunkCard
item={item}
key={item.chunk_id}
editChunk={handleEditChunk}
checked={selectedChunkIds.some(
(x) => x === item.chunk_id,
)}
handleCheckboxClick={handleSingleCheckboxClick}
switchChunk={switchChunk}
clickChunkCard={handleChunkCardClick}
selected={item.chunk_id === selectedChunkId}
textMode={textMode}
></ChunkCard>
))}
</Space>
</div>
</Spin>
<div className={styles.pageFooter}>
<Pagination
{...pagination}
total={total}
size={'small'}
onChange={onPaginationChange}
/>
</div>
</Flex>
{
<section className={styles.documentPreview}>
<DocumentPreview
highlights={highlights}
setWidthAndHeight={setWidthAndHeight}
></DocumentPreview>
</section>
}
</Flex>
</div>
<CreatingModal doc_id={documentId} chunkId={chunkId} />
</>
);
};
export default Chunk;
|