index.tsx 4.0 KB

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