index.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use client'
  2. import type { MouseEventHandler } from 'react'
  3. import cn from 'classnames'
  4. import { useState } from 'react'
  5. import { RiCloseLine } from '@remixicon/react'
  6. import { BookOpenIcon } from '@heroicons/react/24/outline'
  7. import { useContext } from 'use-context-selector'
  8. import { useTranslation } from 'react-i18next'
  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 onConfirm: MouseEventHandler = async () => {
  27. if (!name.trim()) {
  28. notify({ type: 'error', message: t('datasetSettings.form.nameError') })
  29. return
  30. }
  31. try {
  32. setLoading(true)
  33. await updateDatasetSetting({
  34. datasetId: dataset.id,
  35. body: {
  36. name,
  37. description,
  38. },
  39. })
  40. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  41. if (onSuccess)
  42. onSuccess()
  43. onClose()
  44. }
  45. catch (e) {
  46. notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') })
  47. }
  48. finally {
  49. setLoading(false)
  50. }
  51. }
  52. return (
  53. <Modal
  54. className='px-8 py-6 max-w-[520px] w-[520px] rounded-xl'
  55. isShow={show}
  56. onClose={() => { }}
  57. >
  58. <div className='relative pb-2 text-xl font-medium leading-[30px] text-gray-900'>{t('datasetSettings.title')}</div>
  59. <div className='absolute right-4 top-4 p-2 cursor-pointer' onClick={onClose}>
  60. <RiCloseLine className='w-4 h-4 text-gray-500' />
  61. </div>
  62. <div>
  63. <div className={cn('flex justify-between py-4 flex-wrap items-center')}>
  64. <div className='shrink-0 py-2 text-sm font-medium leading-[20px] text-gray-900'>
  65. {t('datasetSettings.form.name')}
  66. </div>
  67. <input
  68. value={name}
  69. onChange={e => setName(e.target.value)}
  70. className='block px-3 w-full h-9 bg-gray-100 rounded-lg text-sm text-gray-900 outline-none appearance-none'
  71. placeholder={t('datasetSettings.form.namePlaceholder') || ''}
  72. />
  73. </div>
  74. <div className={cn('flex justify-between py-4 flex-wrap items-center')}>
  75. <div className='shrink-0 py-2 text-sm font-medium leading-[20px] text-gray-900'>
  76. {t('datasetSettings.form.desc')}
  77. </div>
  78. <div className='w-full'>
  79. <textarea
  80. value={description}
  81. onChange={e => setDescription(e.target.value)}
  82. className='block px-3 py-2 w-full h-[88px] rounded-lg bg-gray-100 text-sm outline-none appearance-none resize-none'
  83. placeholder={t('datasetSettings.form.descPlaceholder') || ''}
  84. />
  85. <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'>
  86. <BookOpenIcon className='w-3 h-[18px] mr-1' />
  87. {t('datasetSettings.form.descWrite')}
  88. </a>
  89. </div>
  90. </div>
  91. </div>
  92. <div className='pt-6 flex justify-end'>
  93. <Button className='mr-2' onClick={onClose}>{t('common.operation.cancel')}</Button>
  94. <Button disabled={loading} variant="primary" onClick={onConfirm}>{t('common.operation.save')}</Button>
  95. </div>
  96. </Modal>
  97. )
  98. }
  99. export default RenameDatasetModal