index.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use client'
  2. import type { MouseEventHandler } from 'react'
  3. import cn from 'classnames'
  4. import { useState } from 'react'
  5. import { BookOpenIcon } from '@heroicons/react/24/outline'
  6. import { useContext } from 'use-context-selector'
  7. import { useTranslation } from 'react-i18next'
  8. import Button from '@/app/components/base/button'
  9. import Modal from '@/app/components/base/modal'
  10. import { ToastContext } from '@/app/components/base/toast'
  11. import { XClose } from '@/app/components/base/icons/src/vender/line/general'
  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. wrapperClassName='z-20'
  55. className='px-8 py-6 max-w-[520px] w-[520px] rounded-xl'
  56. isShow={show}
  57. onClose={() => {}}
  58. >
  59. <div className='relative pb-2 text-xl font-medium leading-[30px] text-gray-900'>{t('datasetSettings.title')}</div>
  60. <div className='absolute right-4 top-4 p-2 cursor-pointer' onClick={onClose}>
  61. <XClose className='w-4 h-4 text-gray-500' />
  62. </div>
  63. <div>
  64. <div className={cn('flex justify-between py-4 flex-wrap items-center')}>
  65. <div className='shrink-0 py-2 text-sm font-medium leading-[20px] text-gray-900'>
  66. {t('datasetSettings.form.name')}
  67. </div>
  68. <input
  69. value={name}
  70. onChange={e => setName(e.target.value)}
  71. className='block px-3 w-full h-9 bg-gray-100 rounded-lg text-sm text-gray-900 outline-none appearance-none'
  72. placeholder={t('datasetSettings.form.namePlaceholder') || ''}
  73. />
  74. </div>
  75. <div className={cn('flex justify-between py-4 flex-wrap items-center')}>
  76. <div className='shrink-0 py-2 text-sm font-medium leading-[20px] text-gray-900'>
  77. {t('datasetSettings.form.desc')}
  78. </div>
  79. <div className='w-full'>
  80. <textarea
  81. value={description}
  82. onChange={e => setDescription(e.target.value)}
  83. className='block px-3 py-2 w-full h-[88px] rounded-lg bg-gray-100 text-sm outline-none appearance-none resize-none'
  84. placeholder={t('datasetSettings.form.descPlaceholder') || ''}
  85. />
  86. <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'>
  87. <BookOpenIcon className='w-3 h-[18px] mr-1' />
  88. {t('datasetSettings.form.descWrite')}
  89. </a>
  90. </div>
  91. </div>
  92. </div>
  93. <div className='pt-6 flex justify-end'>
  94. <Button className='mr-2 text-gray-700 text-sm font-medium' onClick={onClose}>{t('common.operation.cancel')}</Button>
  95. <Button className='text-sm font-medium' disabled={loading} type="primary" onClick={onConfirm}>{t('common.operation.save')}</Button>
  96. </div>
  97. </Modal>
  98. )
  99. }
  100. export default RenameDatasetModal