index.tsx 10 KB

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