feat: enhance child segment list with empty state handling and search result display

This commit is contained in:
twwu 2024-12-16 11:20:37 +08:00
parent 6a02076c54
commit cea24acb0a
2 changed files with 47 additions and 29 deletions

View File

@ -3,6 +3,7 @@ import { RiArrowDownSLine, RiArrowRightSLine } from '@remixicon/react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { EditSlice } from '../../../formatted-text/flavours/edit-slice' import { EditSlice } from '../../../formatted-text/flavours/edit-slice'
import { useDocumentContext } from '../index' import { useDocumentContext } from '../index'
import Empty from './common/empty'
import type { ChildChunkDetail } from '@/models/datasets' import type { ChildChunkDetail } from '@/models/datasets'
import Input from '@/app/components/base/input' import Input from '@/app/components/base/input'
import classNames from '@/utils/classnames' import classNames from '@/utils/classnames'
@ -19,6 +20,7 @@ type IChildSegmentCardProps = {
onClickSlice?: (childChunk: ChildChunkDetail) => void onClickSlice?: (childChunk: ChildChunkDetail) => void
total?: number total?: number
inputValue?: string inputValue?: string
onClearFilter?: () => void
} }
const ChildSegmentList: FC<IChildSegmentCardProps> = ({ const ChildSegmentList: FC<IChildSegmentCardProps> = ({
@ -31,6 +33,7 @@ const ChildSegmentList: FC<IChildSegmentCardProps> = ({
onClickSlice, onClickSlice,
total, total,
inputValue, inputValue,
onClearFilter,
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
const parentMode = useDocumentContext(s => s.parentMode) const parentMode = useDocumentContext(s => s.parentMode)
@ -54,24 +57,33 @@ const ChildSegmentList: FC<IChildSegmentCardProps> = ({
}, [enabled]) }, [enabled])
const totalText = useMemo(() => { const totalText = useMemo(() => {
const text = isFullDocMode const isSearch = inputValue !== ''
? !total if (!isSearch) {
? '--' const text = isFullDocMode
: formatNumber(total) ? !total
: formatNumber(childChunks.length) ? '--'
const count = isFullDocMode : formatNumber(total)
? text === '--' : formatNumber(childChunks.length)
? 0 const count = isFullDocMode
: total ? text === '--'
: childChunks.length ? 0
return `${isFullDocMode ? count : childChunks.length} ${t('datasetDocuments.segment.childChunks', { count })}` : total
: childChunks.length
return `${isFullDocMode ? count : childChunks.length} ${t('datasetDocuments.segment.childChunks', { count })}`
}
else {
const text = !total ? '--' : formatNumber(total)
const count = text === '--' ? 0 : total
return `${count} ${t('datasetDocuments.segment.searchResults', { count })}`
}
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [isFullDocMode, total, childChunks.length]) }, [isFullDocMode, total, childChunks.length])
return ( return (
<div className={classNames('flex flex-col', contentOpacity, isParagraphMode ? 'p-1 pb-2' : 'px-3 grow')}> <div className={classNames('flex flex-col', contentOpacity, isParagraphMode ? 'p-1 pb-2' : 'px-3 grow')}>
{isFullDocMode ? <Divider type='horizontal' className='h-[1px] bg-divider-subtle my-1' /> : null} {isFullDocMode ? <Divider type='horizontal' className='h-[1px] bg-divider-subtle my-1' /> : null}
<div className={classNames('flex items-center justify-between', isFullDocMode ? 'pt-2 pb-3 sticky top-0 left-0 bg-components-panel-bg' : '')}> <div className={classNames('flex items-center justify-between', isFullDocMode ? 'pt-2 pb-3 sticky -top-2 left-0 bg-components-panel-bg' : '')}>
<div className={classNames('h-7 flex items-center pl-1 pr-3 rounded-lg', (isParagraphMode && collapsed) ? 'bg-dataset-child-chunk-expand-btn-bg' : '')} onClick={(event) => { <div className={classNames('h-7 flex items-center pl-1 pr-3 rounded-lg', (isParagraphMode && collapsed) ? 'bg-dataset-child-chunk-expand-btn-bg' : '')} onClick={(event) => {
event.stopPropagation() event.stopPropagation()
toggleCollapse() toggleCollapse()
@ -111,22 +123,27 @@ const ChildSegmentList: FC<IChildSegmentCardProps> = ({
{(isFullDocMode || !collapsed) {(isFullDocMode || !collapsed)
? <div className={classNames('flex gap-x-0.5', isFullDocMode ? 'grow' : '')}> ? <div className={classNames('flex gap-x-0.5', isFullDocMode ? 'grow' : '')}>
{isParagraphMode && <Divider type='vertical' className='h-auto w-[2px] mx-[7px] bg-text-accent-secondary' />} {isParagraphMode && <Divider type='vertical' className='h-auto w-[2px] mx-[7px] bg-text-accent-secondary' />}
<div className={classNames('w-full !leading-5 flex flex-col', isParagraphMode ? 'gap-y-2' : 'gap-y-3')}> {childChunks.length > 0
{childChunks.map((childChunk) => { ? <div className={classNames('w-full !leading-5 flex flex-col', isParagraphMode ? 'gap-y-2' : 'gap-y-3')}>
const edited = childChunk.updated_at !== childChunk.created_at {childChunks.map((childChunk) => {
return <EditSlice const edited = childChunk.updated_at !== childChunk.created_at
key={childChunk.id} return <EditSlice
label={`C-${childChunk.position}${edited ? ` · ${t('datasetDocuments.segment.edited')}` : ''}`} key={childChunk.id}
text={childChunk.content} label={`C-${childChunk.position}${edited ? ` · ${t('datasetDocuments.segment.edited')}` : ''}`}
onDelete={() => onDelete?.(childChunk.segment_id, childChunk.id)} text={childChunk.content}
className='line-clamp-3' onDelete={() => onDelete?.(childChunk.segment_id, childChunk.id)}
onClick={(e) => { className='line-clamp-3'
e.stopPropagation() onClick={(e) => {
onClickSlice?.(childChunk) e.stopPropagation()
}} onClickSlice?.(childChunk)
/> }}
})} />
</div> })}
</div>
: <div className='h-full w-full'>
<Empty onClearFilter={onClearFilter!} />
</div>
}
</div> </div>
: null} : null}
</div> </div>

View File

@ -533,7 +533,7 @@ const Completed: FC<ICompletedProps> = ({
{/* Segment list */} {/* Segment list */}
{ {
isFullDocMode isFullDocMode
? <div className='grow relative overflow-x-hidden overflow-y-auto'> ? <div className='flex flex-col grow relative overflow-x-hidden overflow-y-auto'>
<SegmentCard <SegmentCard
detail={segments[0]} detail={segments[0]}
onClick={() => onClickCard(segments[0])} onClick={() => onClickCard(segments[0])}
@ -549,6 +549,7 @@ const Completed: FC<ICompletedProps> = ({
enabled={!archived} enabled={!archived}
total={childChunkListData?.total || 0} total={childChunkListData?.total || 0}
inputValue={inputValue} inputValue={inputValue}
onClearFilter={onClearFilter}
/> />
</div> </div>
: <SegmentList : <SegmentList