From c1d1960215fd45d55bbb1dc15dc52b623a3ebd2d Mon Sep 17 00:00:00 2001 From: twwu Date: Thu, 12 Dec 2024 14:45:00 +0800 Subject: [PATCH] feat: enhance time formatting and add child segment detail component --- .../detail/completed/child-segment-detail.tsx | 118 ++++++++++++++++++ .../completed/common/action-buttons.tsx | 4 +- .../documents/detail/completed/common/dot.tsx | 2 +- .../documents/detail/completed/index.tsx | 83 +++++++++++- .../detail/completed/new-child-segment.tsx | 5 +- .../detail/completed/segment-detail.tsx | 3 +- .../datasets/documents/detail/new-segment.tsx | 3 +- web/utils/time.ts | 7 ++ 8 files changed, 215 insertions(+), 10 deletions(-) create mode 100644 web/app/components/datasets/documents/detail/completed/child-segment-detail.tsx diff --git a/web/app/components/datasets/documents/detail/completed/child-segment-detail.tsx b/web/app/components/datasets/documents/detail/completed/child-segment-detail.tsx new file mode 100644 index 0000000000..a305c48feb --- /dev/null +++ b/web/app/components/datasets/documents/detail/completed/child-segment-detail.tsx @@ -0,0 +1,118 @@ +import React, { type FC, useState } from 'react' +import { useTranslation } from 'react-i18next' +import { + RiCloseLine, + RiExpandDiagonalLine, +} from '@remixicon/react' +import ActionButtons from './common/action-buttons' +import ChunkContent from './common/chunk-content' +import Dot from './common/dot' +import { SegmentIndexTag } from './common/segment-index-tag' +import { useSegmentListContext } from './index' +import type { ChildChunkDetail } from '@/models/datasets' +import { useEventEmitterContextContext } from '@/context/event-emitter' +import { formatNumber } from '@/utils/format' +import classNames from '@/utils/classnames' +import Divider from '@/app/components/base/divider' +import { formatTime } from '@/utils/time' + +type IChildSegmentDetailProps = { + chunkId: string + childChunkInfo?: Partial & { id: string } + onUpdate: (segmentId: string, childChunkId: string, content: string) => void + onCancel: () => void + docForm: string +} + +/** + * Show all the contents of the segment + */ +const ChildSegmentDetail: FC = ({ + chunkId, + childChunkInfo, + onUpdate, + onCancel, + docForm, +}) => { + const { t } = useTranslation() + const [content, setContent] = useState(childChunkInfo?.content || '') + const { eventEmitter } = useEventEmitterContextContext() + const [loading, setLoading] = useState(false) + const [fullScreen, toggleFullScreen] = useSegmentListContext(s => [s.fullScreen, s.toggleFullScreen]) + + eventEmitter?.useSubscription((v) => { + if (v === 'update-child-segment') + setLoading(true) + if (v === 'update-child-segment-done') + setLoading(false) + }) + + const handleCancel = () => { + onCancel() + setContent(childChunkInfo?.content || '') + } + + const handleSave = () => { + onUpdate(chunkId, childChunkInfo?.id || '', content) + } + + return ( +
+
+
+
{'Edit Child Chunk'}
+
+ + + {formatNumber(content.length)} {t('datasetDocuments.segment.characters')} + + + {`Edited at ${formatTime({ date: (childChunkInfo?.created_at ?? 0) * 1000, dateFormat: 'MM/DD/YYYY h:mm:ss' })}`} + +
+
+
+ {fullScreen && ( + <> + + + + )} +
+ +
+
+ +
+
+
+
+
+ setContent(content)} + isEditMode={true} + /> +
+
+ {!fullScreen && ( +
+ +
+ )} +
+ ) +} + +export default React.memo(ChildSegmentDetail) diff --git a/web/app/components/datasets/documents/detail/completed/common/action-buttons.tsx b/web/app/components/datasets/documents/detail/completed/common/action-buttons.tsx index 04c829dfda..5ade8267c2 100644 --- a/web/app/components/datasets/documents/detail/completed/common/action-buttons.tsx +++ b/web/app/components/datasets/documents/detail/completed/common/action-buttons.tsx @@ -11,6 +11,7 @@ type IActionButtonsProps = { loading: boolean actionType?: 'edit' | 'add' handleRegeneration?: () => void + isChildChunk?: boolean } const ActionButtons: FC = ({ @@ -19,6 +20,7 @@ const ActionButtons: FC = ({ loading, actionType = 'edit', handleRegeneration, + isChildChunk = false, }) => { const { t } = useTranslation() const [mode, parentMode] = useDocumentContext(s => [s.mode, s.parentMode]) @@ -50,7 +52,7 @@ const ActionButtons: FC = ({ ESC - {(isParentChildParagraphMode && actionType === 'edit') + {(isParentChildParagraphMode && actionType === 'edit' && !isChildChunk) ?