index.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use client'
  2. import { useTranslation } from 'react-i18next'
  3. import Image from 'next/image'
  4. import { useRef } from 'react'
  5. import { useHover } from 'ahooks'
  6. import { IndexingType } from '../../create/step-two'
  7. import { OptionCard } from '../../create/step-two/option-card'
  8. import { indexMethodIcon } from '../../create/icons'
  9. import classNames from '@/utils/classnames'
  10. import type { DataSet } from '@/models/datasets'
  11. import { ChunkingMode } from '@/models/datasets'
  12. import Badge from '@/app/components/base/badge'
  13. import { PortalToFollowElem, PortalToFollowElemContent, PortalToFollowElemTrigger } from '@/app/components/base/portal-to-follow-elem'
  14. type IIndexMethodRadioProps = {
  15. value?: DataSet['indexing_technique']
  16. onChange: (v?: DataSet['indexing_technique']) => void
  17. disable?: boolean
  18. docForm?: ChunkingMode
  19. currentValue?: DataSet['indexing_technique']
  20. }
  21. const IndexMethodRadio = ({
  22. value,
  23. onChange,
  24. disable,
  25. docForm,
  26. currentValue,
  27. }: IIndexMethodRadioProps) => {
  28. const { t } = useTranslation()
  29. const economyDomRef = useRef<HTMLDivElement>(null)
  30. const isHoveringEconomy = useHover(economyDomRef)
  31. const isEconomyDisabled = currentValue === IndexingType.QUALIFIED
  32. const options = [
  33. {
  34. key: 'high_quality',
  35. text: <div className='flex items-center'>
  36. {t('datasetCreation.stepTwo.qualified')}
  37. <Badge uppercase className='ml-auto border-text-accent-secondary text-text-accent-secondary'>
  38. {t('datasetCreation.stepTwo.recommend')}
  39. </Badge>
  40. </div>,
  41. desc: t('datasetSettings.form.indexMethodHighQualityTip'),
  42. },
  43. {
  44. key: 'economy',
  45. text: t('datasetSettings.form.indexMethodEconomy'),
  46. desc: t('datasetSettings.form.indexMethodEconomyTip'),
  47. },
  48. ]
  49. return (
  50. <div className={classNames('flex justify-between w-full gap-2')}>
  51. {
  52. options.map((option) => {
  53. const isParentChild = docForm === ChunkingMode.parentChild
  54. return (
  55. <PortalToFollowElem
  56. key={option.key}
  57. open={
  58. isHoveringEconomy && option.key === 'economy'
  59. }
  60. placement={'top'}
  61. >
  62. <PortalToFollowElemTrigger>
  63. <OptionCard
  64. disabled={
  65. disable
  66. || (isEconomyDisabled && option.key === IndexingType.ECONOMICAL)
  67. }
  68. isActive={option.key === value}
  69. onSwitched={() => {
  70. if (isParentChild && option.key === IndexingType.ECONOMICAL)
  71. return
  72. if (isEconomyDisabled && option.key === IndexingType.ECONOMICAL)
  73. return
  74. if (!disable)
  75. onChange(option.key as DataSet['indexing_technique'])
  76. } }
  77. icon={
  78. <Image
  79. src={option.key === 'high_quality' ? indexMethodIcon.high_quality : indexMethodIcon.economical}
  80. alt={option.desc}
  81. />
  82. }
  83. title={option.text}
  84. description={option.desc}
  85. ref={option.key === 'economy' ? economyDomRef : undefined}
  86. className={classNames((isEconomyDisabled && option.key === 'economy') && 'cursor-not-allowed')}
  87. >
  88. </OptionCard>
  89. </PortalToFollowElemTrigger>
  90. <PortalToFollowElemContent style={{ zIndex: 60 }}>
  91. <div className='rounded-lg border-components-panel-border bg-components-tooltip-bg p-3 text-xs font-medium text-text-secondary shadow-lg'>
  92. {t('datasetSettings.form.indexMethodChangeToEconomyDisabledTip')}
  93. </div>
  94. </PortalToFollowElemContent>
  95. </PortalToFollowElem>
  96. )
  97. })
  98. }
  99. </div>
  100. )
  101. }
  102. export default IndexMethodRadio