index.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 { SimpleSelect } from '@/app/components/base/select'
  12. import Modal from '@/app/components/base/modal'
  13. import { ToastContext } from '@/app/components/base/toast'
  14. import type { DataSet } from '@/models/datasets'
  15. import { updateDatasetSetting } from '@/service/datasets'
  16. type RenameDatasetModalProps = {
  17. show: boolean
  18. dataset: DataSet
  19. onSuccess?: () => void
  20. onClose: () => void
  21. }
  22. const RenameDatasetModal = ({ show, dataset, onSuccess, onClose }: RenameDatasetModalProps) => {
  23. const { t } = useTranslation()
  24. const { notify } = useContext(ToastContext)
  25. const [loading, setLoading] = useState(false)
  26. const [name, setName] = useState<string>(dataset.name)
  27. const [description, setDescription] = useState<string>(dataset.description)
  28. const [externalKnowledgeId, setExternalKnowledgeId] = useState<string>(dataset.external_knowledge_info.external_knowledge_id)
  29. const [externalKnowledgeApiId, setExternalKnowledgeApiId] = useState<string>(dataset.external_knowledge_info.external_knowledge_api_id)
  30. const [type, setType] = useState<any>(dataset.type)
  31. const [options, setOptions] = useState<any>([])
  32. setTimeout(() => {
  33. const arr = []
  34. for (let i = 1; i < 10; i++)
  35. arr.push({ name: `选项${i}`, value: i })
  36. setOptions(arr)
  37. }, 1000)
  38. const onConfirm: MouseEventHandler = async () => {
  39. if (!name.trim()) {
  40. notify({ type: 'error', message: t('datasetSettings.form.nameError') })
  41. return
  42. }
  43. try {
  44. setLoading(true)
  45. const body: Partial<DataSet> & { external_knowledge_id?: string; external_knowledge_api_id?: string } = {
  46. name,
  47. description,
  48. type,
  49. }
  50. if (externalKnowledgeId && externalKnowledgeApiId) {
  51. body.external_knowledge_id = externalKnowledgeId
  52. body.external_knowledge_api_id = externalKnowledgeApiId
  53. }
  54. await updateDatasetSetting({
  55. datasetId: dataset.id,
  56. body,
  57. })
  58. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  59. if (onSuccess)
  60. onSuccess()
  61. onClose()
  62. }
  63. catch (e) {
  64. notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') })
  65. }
  66. finally {
  67. setLoading(false)
  68. }
  69. }
  70. return (
  71. <Modal
  72. className='w-[520px] max-w-[520px] rounded-xl px-8 py-6'
  73. isShow={show}
  74. onClose={() => { }}
  75. >
  76. <div className='relative pb-2 text-xl font-medium leading-[30px] text-text-primary'>{t('datasetSettings.title')}</div>
  77. <div className='absolute right-4 top-4 cursor-pointer p-2' onClick={onClose}>
  78. <RiCloseLine className='h-4 w-4 text-text-tertiary' />
  79. </div>
  80. <div>
  81. <div className={cn('flex flex-wrap items-center justify-between py-4')}>
  82. <div className='flex shrink-0 items-center py-2 text-sm font-medium leading-[20px] text-text-primary'>
  83. 分类管理
  84. <div style={{
  85. color: '#1E98D7',
  86. }} className='ml-3 cursor-pointer hover:opacity-75'>设置</div>
  87. </div>
  88. <div className='w-full'>
  89. <SimpleSelect
  90. defaultValue={type}
  91. onSelect={(i) => { setType(i.value) }}
  92. items={options}
  93. allowSearch={false}
  94. />
  95. </div>
  96. </div>
  97. <div className={cn('flex flex-wrap items-center justify-between py-4')}>
  98. <div className='shrink-0 py-2 text-sm font-medium leading-[20px] text-text-primary'>
  99. {t('datasetSettings.form.name')}
  100. </div>
  101. <Input
  102. value={name}
  103. onChange={e => setName(e.target.value)}
  104. className='h-9'
  105. placeholder={t('datasetSettings.form.namePlaceholder') || ''}
  106. />
  107. </div>
  108. <div className={cn('flex flex-wrap items-center justify-between py-4')}>
  109. <div className='shrink-0 py-2 text-sm font-medium leading-[20px] text-text-primary'>
  110. {t('datasetSettings.form.desc')}
  111. </div>
  112. <div className='w-full'>
  113. <Textarea
  114. value={description}
  115. onChange={e => setDescription(e.target.value)}
  116. className='resize-none'
  117. placeholder={t('datasetSettings.form.descPlaceholder') || ''}
  118. />
  119. </div>
  120. </div>
  121. </div>
  122. <div className='flex justify-end pt-6'>
  123. <Button className='mr-2' onClick={onClose}>{t('common.operation.cancel')}</Button>
  124. <Button disabled={loading} variant="primary" onClick={onConfirm}>{t('common.operation.save')}</Button>
  125. </div>
  126. </Modal>
  127. )
  128. }
  129. export default RenameDatasetModal