wwf
2025-05-20 938c3e5a587ce950a94964ea509b9e7f8834dfae
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
'use client'
import { useTranslation } from 'react-i18next'
import Image from 'next/image'
import { useRef } from 'react'
import { useHover } from 'ahooks'
import { IndexingType } from '../../create/step-two'
import { OptionCard } from '../../create/step-two/option-card'
import { indexMethodIcon } from '../../create/icons'
import classNames from '@/utils/classnames'
import type { DataSet } from '@/models/datasets'
import { ChunkingMode } from '@/models/datasets'
import Badge from '@/app/components/base/badge'
import { PortalToFollowElem, PortalToFollowElemContent, PortalToFollowElemTrigger } from '@/app/components/base/portal-to-follow-elem'
 
type IIndexMethodRadioProps = {
  value?: DataSet['indexing_technique']
  onChange: (v?: DataSet['indexing_technique']) => void
  disable?: boolean
  docForm?: ChunkingMode
  currentValue?: DataSet['indexing_technique']
}
 
const IndexMethodRadio = ({
  value,
  onChange,
  disable,
  docForm,
  currentValue,
}: IIndexMethodRadioProps) => {
  const { t } = useTranslation()
  const economyDomRef = useRef<HTMLDivElement>(null)
  const isHoveringEconomy = useHover(economyDomRef)
  const isEconomyDisabled = currentValue === IndexingType.QUALIFIED
  const options = [
    {
      key: 'high_quality',
      text: <div className='flex items-center'>
        {t('datasetCreation.stepTwo.qualified')}
        <Badge uppercase className='ml-auto border-text-accent-secondary text-text-accent-secondary'>
          {t('datasetCreation.stepTwo.recommend')}
        </Badge>
      </div>,
      desc: t('datasetSettings.form.indexMethodHighQualityTip'),
    },
    {
      key: 'economy',
      text: t('datasetSettings.form.indexMethodEconomy'),
      desc: t('datasetSettings.form.indexMethodEconomyTip'),
    },
  ]
 
  return (
    <div className={classNames('flex justify-between w-full gap-2')}>
      {
        options.map((option) => {
          const isParentChild = docForm === ChunkingMode.parentChild
          return (
            <PortalToFollowElem
              key={option.key}
              open={
                isHoveringEconomy && option.key === 'economy'
              }
              placement={'top'}
            >
              <PortalToFollowElemTrigger>
                <OptionCard
                  disabled={
                    disable
                    || (isEconomyDisabled && option.key === IndexingType.ECONOMICAL)
                  }
                  isActive={option.key === value}
                  onSwitched={() => {
                    if (isParentChild && option.key === IndexingType.ECONOMICAL)
                      return
                    if (isEconomyDisabled && option.key === IndexingType.ECONOMICAL)
                      return
                    if (!disable)
                      onChange(option.key as DataSet['indexing_technique'])
                  } }
                  icon={
                    <Image
                      src={option.key === 'high_quality' ? indexMethodIcon.high_quality : indexMethodIcon.economical}
                      alt={option.desc}
                    />
                  }
                  title={option.text}
                  description={option.desc}
                  ref={option.key === 'economy' ? economyDomRef : undefined}
                  className={classNames((isEconomyDisabled && option.key === 'economy') && 'cursor-not-allowed')}
                >
                </OptionCard>
              </PortalToFollowElemTrigger>
              <PortalToFollowElemContent style={{ zIndex: 60 }}>
                <div className='rounded-lg border-components-panel-border bg-components-tooltip-bg p-3 text-xs font-medium text-text-secondary shadow-lg'>
                  {t('datasetSettings.form.indexMethodChangeToEconomyDisabledTip')}
                </div>
              </PortalToFollowElemContent>
            </PortalToFollowElem>
          )
        })
      }
    </div>
  )
}
 
export default IndexMethodRadio