index.tsx 9.9 KB

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