index.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. 'use client'
  2. import type { MouseEventHandler } from 'react'
  3. import React from 'react'
  4. import { useEffect } from 'react'
  5. import { useState } from 'react'
  6. import { RiCloseLine } from '@remixicon/react'
  7. import { useContext } from 'use-context-selector'
  8. import { useTranslation } from 'react-i18next'
  9. import cn from '@/utils/classnames'
  10. import Button from '@/app/components/base/button'
  11. import Input from '@/app/components/base/input'
  12. import Textarea from '@/app/components/base/textarea'
  13. import { SimpleSelect } from '@/app/components/base/select'
  14. import Modal from '@/app/components/base/modal'
  15. import { ToastContext } from '@/app/components/base/toast'
  16. import type { DataSet } from '@/models/datasets'
  17. import { tagBindingsCreate, tagBindingsRemove, updateDatasetSetting } from '@/service/datasets'
  18. import { useModalContext } from '@/context/modal-context'
  19. import { fetchDeptUsers, fetchTypes } from '@/service/common'
  20. import { TreeSelect as AntdTreeSelect } from 'antd'
  21. type RenameDatasetModalProps = {
  22. show: boolean
  23. dataset: DataSet
  24. onSuccess?: () => void
  25. onClose: () => void
  26. }
  27. const RenameDatasetModal = ({ show, dataset, onSuccess, onClose }: RenameDatasetModalProps) => {
  28. const { t } = useTranslation()
  29. const { notify } = useContext(ToastContext)
  30. const [loading, setLoading] = useState(false)
  31. const [name, setName] = useState<string>(dataset.name)
  32. const [description, setDescription] = useState<string>(dataset.description)
  33. const [externalKnowledgeId, setExternalKnowledgeId] = useState<string>(dataset.external_knowledge_info.external_knowledge_id)
  34. const [externalKnowledgeApiId, setExternalKnowledgeApiId] = useState<string>(dataset.external_knowledge_info.external_knowledge_api_id)
  35. const [type, setType] = useState<any>(dataset.categories[0]?.id)
  36. const [options, setOptions] = useState<any>([])
  37. useEffect(() => {
  38. fetchTypes({
  39. url: '/tags/page',
  40. params: {
  41. page: 1,
  42. limit: 1000,
  43. tag_type: 'knowledge_category',
  44. },
  45. }).then((res: any) => {
  46. setOptions(res.data.map((v: any) => ({ name: v.name, value: v.id })) || [])
  47. })
  48. }, [])
  49. const onConfirm: MouseEventHandler = async () => {
  50. if (!name.trim()) {
  51. notify({ type: 'error', message: t('datasetSettings.form.nameError') })
  52. return
  53. }
  54. try {
  55. setLoading(true)
  56. const body: Partial<DataSet> & { external_knowledge_id?: string; external_knowledge_api_id?: string } = {
  57. name,
  58. description,
  59. }
  60. if (externalKnowledgeId && externalKnowledgeApiId) {
  61. body.external_knowledge_id = externalKnowledgeId
  62. body.external_knowledge_api_id = externalKnowledgeApiId
  63. }
  64. await updateDatasetSetting({
  65. datasetId: dataset.id,
  66. body,
  67. })
  68. if (type) {
  69. if (dataset.categories[0]) {
  70. await tagBindingsRemove({
  71. body: {
  72. tag_id: dataset.categories[0].id,
  73. target_id: dataset.id,
  74. type: 'knowledge_category',
  75. },
  76. })
  77. }
  78. await tagBindingsCreate({
  79. body: {
  80. tag_ids: [type],
  81. target_id: dataset.id,
  82. type: 'knowledge_category',
  83. },
  84. })
  85. }
  86. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  87. if (onSuccess)
  88. onSuccess()
  89. onClose()
  90. }
  91. catch (e) {
  92. notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') })
  93. }
  94. finally {
  95. setLoading(false)
  96. }
  97. }
  98. const { setShowAccountSettingModal } = useModalContext()
  99. const optionsEditAuth = [
  100. { name: '本账号', value: 1 },
  101. { name: '本部门', value: 2 },
  102. ]
  103. const [editAuth, setEditAuth] = useState()
  104. const [editUserIds, setEditUserIds] = useState([])
  105. const [lookUserIds, setLookUserIds] = useState([])
  106. const [optionsDeptUser, setOptionsDeptUser] = useState<any>([])
  107. const [optionsDeptUserEdit, setOptionsDeptUserEdit] = useState<any>([])
  108. useEffect(() => {
  109. fetchDeptUsers({
  110. url: '/xxx',
  111. params: {
  112. page: 1,
  113. limit: 1000,
  114. },
  115. }).then((res: any) => {
  116. setOptionsDeptUser(res.data || [])
  117. })
  118. }, [])
  119. useEffect(() => {
  120. fetchDeptUsers({
  121. url: '/xxx',
  122. params: {
  123. page: 1,
  124. limit: 1000,
  125. },
  126. }).then((res: any) => {
  127. setOptionsDeptUserEdit(res.data || [])
  128. })
  129. }, [])
  130. return (
  131. <Modal
  132. className='w-[520px] max-w-[520px] rounded-xl px-8 py-6'
  133. isShow={show}
  134. onClose={() => { }}
  135. >
  136. <div className='relative pb-2 text-xl font-medium leading-[30px] text-text-primary'>{t('datasetSettings.title')}</div>
  137. <div className='absolute right-4 top-4 cursor-pointer p-2' onClick={onClose}>
  138. <RiCloseLine className='h-4 w-4 text-text-tertiary' />
  139. </div>
  140. <div>
  141. <div className={cn('flex flex-wrap items-center justify-between py-4')}>
  142. <div className='flex shrink-0 items-center py-2 text-sm font-medium leading-[20px] text-text-primary'>
  143. 分类管理
  144. <div style={{
  145. color: '#1E98D7',
  146. }} className='ml-3 cursor-pointer hover:opacity-75'
  147. onClick={() => setShowAccountSettingModal({ payload: 'type' })}>设置</div>
  148. </div>
  149. <div className='w-full'>
  150. <SimpleSelect
  151. defaultValue={type}
  152. onSelect={(i) => { setType(i.value) }}
  153. items={options}
  154. allowSearch={false}
  155. />
  156. </div>
  157. </div>
  158. <div className={cn('flex flex-wrap items-center justify-between py-4')}>
  159. <div className='shrink-0 py-2 text-sm font-medium leading-[20px] text-text-primary'>
  160. {t('datasetSettings.form.name')}
  161. </div>
  162. <Input
  163. value={name}
  164. onChange={e => setName(e.target.value)}
  165. className='h-9'
  166. placeholder={t('datasetSettings.form.namePlaceholder') || ''}
  167. />
  168. </div>
  169. <div className={cn('flex flex-wrap items-center justify-between py-4')}>
  170. <div className='shrink-0 py-2 text-sm font-medium leading-[20px] text-text-primary'>
  171. {t('datasetSettings.form.desc')}
  172. </div>
  173. <div className='w-full'>
  174. <Textarea
  175. value={description}
  176. onChange={e => setDescription(e.target.value)}
  177. className='resize-none'
  178. placeholder={t('datasetSettings.form.descPlaceholder') || ''}
  179. />
  180. </div>
  181. </div>
  182. <div className='pt-2'>
  183. <div className='py-2 text-sm font-medium leading-[20px] text-text-primary'>编辑权限</div>
  184. <div className="h-[32px]">
  185. <SimpleSelect
  186. className="h-[32px]"
  187. defaultValue={editAuth}
  188. onSelect={(i) => {
  189. setEditAuth(i.value)
  190. }}
  191. items={optionsEditAuth}
  192. allowSearch={false}
  193. placeholder="请选择编辑权限"
  194. />
  195. </div>
  196. </div>
  197. <div className='pt-2'>
  198. <div className='py-2 text-sm font-medium leading-[20px] text-text-primary'>编辑授权</div>
  199. <AntdTreeSelect
  200. showSearch
  201. style={{ width: '100%' }}
  202. value={editUserIds}
  203. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  204. placeholder="请选择编辑授权"
  205. allowClear
  206. treeDefaultExpandAll
  207. onChange={v => setEditUserIds(v)}
  208. treeData={optionsDeptUserEdit}
  209. fieldNames={{ label: 'name', value: 'id' }}
  210. multiple={true}
  211. treeCheckable={true}
  212. />
  213. </div>
  214. <div className='pt-2'>
  215. <div className='py-2 text-sm font-medium leading-[20px] text-text-primary'>可见授权</div>
  216. <AntdTreeSelect
  217. showSearch
  218. style={{ width: '100%' }}
  219. value={lookUserIds}
  220. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  221. placeholder="请选择可见授权"
  222. allowClear
  223. treeDefaultExpandAll
  224. onChange={v => setLookUserIds(v)}
  225. treeData={optionsDeptUser}
  226. fieldNames={{ label: 'name', value: 'id' }}
  227. multiple={true}
  228. treeCheckable={true}
  229. />
  230. </div>
  231. </div>
  232. <div className='flex justify-end pt-6'>
  233. <Button className='mr-2' onClick={onClose}>{t('common.operation.cancel')}</Button>
  234. <Button disabled={loading} variant="primary" onClick={onConfirm}>{t('common.operation.save')}</Button>
  235. </div>
  236. </Modal>
  237. )
  238. }
  239. export default RenameDatasetModal