modify-retrieval-modal.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useRef, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import { RiCloseLine } from '@remixicon/react'
  6. import Toast from '../../base/toast'
  7. import { ModelTypeEnum } from '../../header/account-setting/model-provider-page/declarations'
  8. import type { RetrievalConfig } from '@/types/app'
  9. import RetrievalMethodConfig from '@/app/components/datasets/common/retrieval-method-config'
  10. import EconomicalRetrievalMethodConfig from '@/app/components/datasets/common/economical-retrieval-method-config'
  11. import Button from '@/app/components/base/button'
  12. import { isReRankModelSelected } from '@/app/components/datasets/common/check-rerank-model'
  13. import { useModelListAndDefaultModelAndCurrentProviderAndModel } from '@/app/components/header/account-setting/model-provider-page/hooks'
  14. type Props = {
  15. indexMethod: string
  16. value: RetrievalConfig
  17. isShow: boolean
  18. onHide: () => void
  19. onSave: (value: RetrievalConfig) => void
  20. }
  21. const ModifyRetrievalModal: FC<Props> = ({
  22. indexMethod,
  23. value,
  24. isShow,
  25. onHide,
  26. onSave,
  27. }) => {
  28. const ref = useRef(null)
  29. const { t } = useTranslation()
  30. const [retrievalConfig, setRetrievalConfig] = useState(value)
  31. // useClickAway(() => {
  32. // if (ref)
  33. // onHide()
  34. // }, ref)
  35. const {
  36. modelList: rerankModelList,
  37. } = useModelListAndDefaultModelAndCurrentProviderAndModel(ModelTypeEnum.rerank)
  38. const handleSave = () => {
  39. if (
  40. !isReRankModelSelected({
  41. rerankModelList,
  42. retrievalConfig,
  43. indexMethod,
  44. })
  45. ) {
  46. Toast.notify({ type: 'error', message: t('appDebug.datasetConfig.rerankModelRequired') })
  47. return
  48. }
  49. onSave(retrievalConfig)
  50. }
  51. if (!isShow)
  52. return null
  53. return (
  54. <div
  55. className='w-full flex flex-col bg-components-panel-bg border-[0.5px] border-components-panel-border rounded-2xl shadow-2xl shadow-shadow-shadow-9'
  56. style={{
  57. height: 'calc(100vh - 72px)',
  58. }}
  59. ref={ref}
  60. >
  61. <div className='shrink-0 flex justify-between pt-3.5 pb-1 px-3 h-15'>
  62. <div className='text-base font-semibold text-text-primary'>
  63. <div>{t('datasetSettings.form.retrievalSetting.title')}</div>
  64. <div className='leading-[18px] text-xs font-normal text-text-tertiary'>
  65. <a
  66. target='_blank'
  67. rel='noopener noreferrer'
  68. href='https://docs.dify.ai/guides/knowledge-base/create-knowledge-and-upload-documents#id-4-retrieval-settings'
  69. className='text-text-accent'
  70. >
  71. {t('datasetSettings.form.retrievalSetting.learnMore')}
  72. </a>
  73. {t('datasetSettings.form.retrievalSetting.description')}
  74. </div>
  75. </div>
  76. <div className='flex'>
  77. <div
  78. onClick={onHide}
  79. className='flex justify-center items-center w-8 h-8 cursor-pointer'
  80. >
  81. <RiCloseLine className='w-4 h-4 text-text-tertiary' />
  82. </div>
  83. </div>
  84. </div>
  85. <div className='px-4 py-2'>
  86. <div className='mb-1 text-text-secondary text-[13px] leading-6 font-semibold'>
  87. {t('datasetSettings.form.retrievalSetting.method')}
  88. </div>
  89. {indexMethod === 'high_quality'
  90. ? (
  91. <RetrievalMethodConfig
  92. value={retrievalConfig}
  93. onChange={setRetrievalConfig}
  94. />
  95. )
  96. : (
  97. <EconomicalRetrievalMethodConfig
  98. value={retrievalConfig}
  99. onChange={setRetrievalConfig}
  100. />
  101. )}
  102. </div>
  103. <div className='flex justify-end p-4 pt-2'>
  104. <Button className='mr-2 flex-shrink-0' onClick={onHide}>{t('common.operation.cancel')}</Button>
  105. <Button variant='primary' className='flex-shrink-0' onClick={handleSave} >{t('common.operation.save')}</Button>
  106. </div>
  107. </div>
  108. )
  109. }
  110. export default React.memo(ModifyRetrievalModal)