import type { FC } from 'react'
import React, { useState } from 'react'
import { ArrowUpRightIcon } from '@heroicons/react/24/outline'
import { useTranslation } from 'react-i18next'
import {
RiDeleteBinLine,
} from '@remixicon/react'
import { StatusItem } from '../../list'
import { DocumentTitle } from '../index'
import s from './style.module.css'
import { SegmentIndexTag } from './index'
import cn from '@/utils/classnames'
import Confirm from '@/app/components/base/confirm'
import Switch from '@/app/components/base/switch'
import Divider from '@/app/components/base/divider'
import Indicator from '@/app/components/header/indicator'
import { formatNumber } from '@/utils/format'
import type { SegmentDetailModel } from '@/models/datasets'
const ProgressBar: FC<{ percent: number; loading: boolean }> = ({ percent, loading }) => {
return (
{loading ? null : percent.toFixed(2)}
)
}
export type UsageScene = 'doc' | 'hitTesting'
type ISegmentCardProps = {
loading: boolean
detail?: SegmentDetailModel & { document: { name: string } }
contentExternal?: string
refSource?: {
title: string
uri: string
}
isExternal?: boolean
score?: number
onClick?: () => void
onChangeSwitch?: (segId: string, enabled: boolean) => Promise
onDelete?: (segId: string) => Promise
scene?: UsageScene
className?: string
archived?: boolean
embeddingAvailable?: boolean
}
const SegmentCard: FC = ({
detail = {},
contentExternal,
isExternal,
refSource,
score,
onClick,
onChangeSwitch,
onDelete,
loading = true,
scene = 'doc',
className = '',
archived,
embeddingAvailable,
}) => {
const { t } = useTranslation()
const {
id,
position,
enabled,
content,
word_count,
hit_count,
index_node_hash,
answer,
} = detail as Required['detail']
const isDocScene = scene === 'doc'
const [showModal, setShowModal] = useState(false)
const renderContent = () => {
if (answer) {
return (
<>
>
)
}
if (contentExternal)
return contentExternal
return content
}
return (
onClick?.()}
>
{isDocScene
? <>
{loading
? (
)
: (
<>
{embeddingAvailable && (
) =>
e.stopPropagation()
}
className="inline-flex items-center"
>
{
await onChangeSwitch?.(id, val)
}}
/>
)}
>
)}
>
: (
score !== null
? (
)
: null
)}
{loading
? (
)
: (
isDocScene
? <>
{renderContent()}
{formatNumber(word_count)}
{formatNumber(hit_count)}
{!archived && embeddingAvailable && (
{
e.stopPropagation()
setShowModal(true)
}}>
)}
>
: <>
{renderContent()}
{isExternal ? t('datasetHitTesting.viewDetail') : t('datasetHitTesting.viewChart')}
>
)}
{showModal
&&
{ await onDelete?.(id) }}
onCancel={() => setShowModal(false)}
/>
}
)
}
export default SegmentCard