modify-retrieval-modal.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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-white border-[0.5px] border-gray-200 rounded-xl shadow-xl'
  56. style={{
  57. height: 'calc(100vh - 72px)',
  58. }}
  59. ref={ref}
  60. >
  61. <div className='shrink-0 flex justify-between items-center pl-6 pr-5 h-14 border-b border-b-gray-100'>
  62. <div className='text-base font-semibold text-gray-900'>
  63. <div>{t('datasetSettings.form.retrievalSetting.title')}</div>
  64. <div className='leading-[18px] text-xs font-normal text-gray-500'>
  65. <a target='_blank' rel='noopener noreferrer' href='https://docs.dify.ai/guides/knowledge-base/create-knowledge-and-upload-documents#id-4-retrieval-settings' className='text-text-accent'>{t('datasetSettings.form.retrievalSetting.learnMore')}</a>
  66. {t('datasetSettings.form.retrievalSetting.description')}
  67. </div>
  68. </div>
  69. <div className='flex items-center'>
  70. <div
  71. onClick={onHide}
  72. className='flex justify-center items-center w-6 h-6 cursor-pointer'
  73. >
  74. <RiCloseLine className='w-4 h-4 text-gray-500' />
  75. </div>
  76. </div>
  77. </div>
  78. <div className='p-6 border-b' style={{
  79. borderBottom: 'rgba(0, 0, 0, 0.05)',
  80. }}>
  81. {indexMethod === 'high_quality'
  82. ? (
  83. <RetrievalMethodConfig
  84. value={retrievalConfig}
  85. onChange={setRetrievalConfig}
  86. />
  87. )
  88. : (
  89. <EconomicalRetrievalMethodConfig
  90. value={retrievalConfig}
  91. onChange={setRetrievalConfig}
  92. />
  93. )}
  94. </div>
  95. <div
  96. className='flex justify-end pt-6 px-6 border-t'
  97. style={{
  98. borderColor: 'rgba(0, 0, 0, 0.05)',
  99. }}
  100. >
  101. <Button className='mr-2 flex-shrink-0' onClick={onHide}>{t('common.operation.cancel')}</Button>
  102. <Button variant='primary' className='flex-shrink-0' onClick={handleSave} >{t('common.operation.save')}</Button>
  103. </div>
  104. </div>
  105. )
  106. }
  107. export default React.memo(ModifyRetrievalModal)