index.tsx 4.1 KB

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