index.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 'use client'
  2. import type { MouseEventHandler } from 'react'
  3. import { useState } from 'react'
  4. import { RiCloseLine } from '@remixicon/react'
  5. import { BookOpenIcon } from '@heroicons/react/24/outline'
  6. import { useContext } from 'use-context-selector'
  7. import { useTranslation } from 'react-i18next'
  8. import cn from '@/utils/classnames'
  9. import Button from '@/app/components/base/button'
  10. import Modal from '@/app/components/base/modal'
  11. import { ToastContext } from '@/app/components/base/toast'
  12. import type { DataSet } from '@/models/datasets'
  13. import { updateDatasetSetting } from '@/service/datasets'
  14. type RenameDatasetModalProps = {
  15. show: boolean
  16. dataset: DataSet
  17. onSuccess?: () => void
  18. onClose: () => void
  19. }
  20. const RenameDatasetModal = ({ show, dataset, onSuccess, onClose }: RenameDatasetModalProps) => {
  21. const { t } = useTranslation()
  22. const { notify } = useContext(ToastContext)
  23. const [loading, setLoading] = useState(false)
  24. const [name, setName] = useState<string>(dataset.name)
  25. const [description, setDescription] = useState<string>(dataset.description)
  26. const [externalKnowledgeId, setExternalKnowledgeId] = useState<string>(dataset.external_knowledge_info.external_knowledge_id)
  27. const [externalKnowledgeApiId, setExternalKnowledgeApiId] = useState<string>(dataset.external_knowledge_info.external_knowledge_api_id)
  28. const onConfirm: MouseEventHandler = async () => {
  29. if (!name.trim()) {
  30. notify({ type: 'error', message: t('datasetSettings.form.nameError') })
  31. return
  32. }
  33. try {
  34. setLoading(true)
  35. const body: Partial<DataSet> & { external_knowledge_id?: string; external_knowledge_api_id?: string } = {
  36. name,
  37. description,
  38. }
  39. if (externalKnowledgeId && externalKnowledgeApiId) {
  40. body.external_knowledge_id = externalKnowledgeId
  41. body.external_knowledge_api_id = externalKnowledgeApiId
  42. }
  43. await updateDatasetSetting({
  44. datasetId: dataset.id,
  45. body,
  46. })
  47. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  48. if (onSuccess)
  49. onSuccess()
  50. onClose()
  51. }
  52. catch (e) {
  53. notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') })
  54. }
  55. finally {
  56. setLoading(false)
  57. }
  58. }
  59. return (
  60. <Modal
  61. className='px-8 py-6 max-w-[520px] w-[520px] rounded-xl'
  62. isShow={show}
  63. onClose={() => { }}
  64. >
  65. <div className='relative pb-2 text-xl font-medium leading-[30px] text-gray-900'>{t('datasetSettings.title')}</div>
  66. <div className='absolute right-4 top-4 p-2 cursor-pointer' onClick={onClose}>
  67. <RiCloseLine className='w-4 h-4 text-gray-500' />
  68. </div>
  69. <div>
  70. <div className={cn('flex justify-between py-4 flex-wrap items-center')}>
  71. <div className='shrink-0 py-2 text-sm font-medium leading-[20px] text-gray-900'>
  72. {t('datasetSettings.form.name')}
  73. </div>
  74. <input
  75. value={name}
  76. onChange={e => setName(e.target.value)}
  77. className='block px-3 w-full h-9 bg-gray-100 rounded-lg text-sm text-gray-900 outline-none appearance-none'
  78. placeholder={t('datasetSettings.form.namePlaceholder') || ''}
  79. />
  80. </div>
  81. <div className={cn('flex justify-between py-4 flex-wrap items-center')}>
  82. <div className='shrink-0 py-2 text-sm font-medium leading-[20px] text-gray-900'>
  83. {t('datasetSettings.form.desc')}
  84. </div>
  85. <div className='w-full'>
  86. <textarea
  87. value={description}
  88. onChange={e => setDescription(e.target.value)}
  89. className='block px-3 py-2 w-full h-[88px] rounded-lg bg-gray-100 text-sm outline-none appearance-none resize-none'
  90. placeholder={t('datasetSettings.form.descPlaceholder') || ''}
  91. />
  92. <a className='mt-2 flex items-center h-[18px] px-3 text-xs text-gray-500 hover:text-primary-600' href="https://docs.dify.ai/features/datasets#how-to-write-a-good-dataset-description" target='_blank' rel='noopener noreferrer'>
  93. <BookOpenIcon className='w-3 h-[18px] mr-1' />
  94. {t('datasetSettings.form.descWrite')}
  95. </a>
  96. </div>
  97. </div>
  98. </div>
  99. <div className='pt-6 flex justify-end'>
  100. <Button className='mr-2' onClick={onClose}>{t('common.operation.cancel')}</Button>
  101. <Button disabled={loading} variant="primary" onClick={onConfirm}>{t('common.operation.save')}</Button>
  102. </div>
  103. </Modal>
  104. )
  105. }
  106. export default RenameDatasetModal