index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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.accounts?.length > 0)
  143. v.treeChildren = deep(v.accounts)
  144. return v
  145. })
  146. }
  147. setOptionsDeptUser(deep(res.data) || [])
  148. })
  149. }, [])
  150. useEffect(() => {
  151. fetchDeptUserTree({
  152. url: '/dept/dept-accounts',
  153. }).then((res: any) => {
  154. const deep = (arr: any) => {
  155. return arr.map((v: any) => {
  156. v.treeId = v.dept_id || v.account_id
  157. v.treeName = v.dept_name || v.email
  158. if (v.children?.length > 0)
  159. v.treeChildren = deep(v.children)
  160. else if (v.accounts?.length > 0)
  161. v.treeChildren = deep(v.accounts)
  162. return v
  163. })
  164. }
  165. setOptionsDeptUserEdit(deep(res.data) || [])
  166. })
  167. }, [])
  168. return (
  169. <Modal
  170. className='w-[520px] max-w-[520px] rounded-xl px-8 py-6'
  171. isShow={show}
  172. onClose={() => { }}
  173. >
  174. <div className='relative pb-2 text-xl font-medium leading-[30px] text-text-primary'>{t('datasetSettings.title')}</div>
  175. <div className='absolute right-4 top-4 cursor-pointer p-2' onClick={onClose}>
  176. <RiCloseLine className='h-4 w-4 text-text-tertiary' />
  177. </div>
  178. <div>
  179. <div className={cn('flex flex-wrap items-center justify-between py-4')}>
  180. <div className='flex shrink-0 items-center py-2 text-sm font-medium leading-[20px] text-text-primary'>
  181. 分类管理
  182. <div style={{
  183. color: '#1E98D7',
  184. }} className='ml-3 cursor-pointer hover:opacity-75'
  185. onClick={() => setShowAccountSettingModal({ payload: 'type' })}>设置</div>
  186. </div>
  187. <div className='w-full'>
  188. <SimpleSelect
  189. defaultValue={type}
  190. onSelect={(i) => { setType(i.value) }}
  191. items={options}
  192. allowSearch={false}
  193. />
  194. </div>
  195. </div>
  196. <div className={cn('flex flex-wrap items-center justify-between py-4')}>
  197. <div className='shrink-0 py-2 text-sm font-medium leading-[20px] text-text-primary'>
  198. {t('datasetSettings.form.name')}
  199. </div>
  200. <Input
  201. value={name}
  202. onChange={e => setName(e.target.value)}
  203. className='h-9'
  204. placeholder={t('datasetSettings.form.namePlaceholder') || ''}
  205. />
  206. </div>
  207. <div className={cn('flex flex-wrap items-center justify-between py-4')}>
  208. <div className='shrink-0 py-2 text-sm font-medium leading-[20px] text-text-primary'>
  209. {t('datasetSettings.form.desc')}
  210. </div>
  211. <div className='w-full'>
  212. <Textarea
  213. value={description}
  214. onChange={e => setDescription(e.target.value)}
  215. className='resize-none'
  216. placeholder={t('datasetSettings.form.descPlaceholder') || ''}
  217. />
  218. </div>
  219. </div>
  220. {
  221. isCreate && (
  222. <div className='pt-2'>
  223. <div className='py-2 text-sm font-medium leading-[20px] text-text-primary'>编辑权限</div>
  224. <div className="h-[32px]">
  225. <SimpleSelect
  226. className="h-[32px]"
  227. defaultValue={editAuth}
  228. onSelect={(i: any) => {
  229. setEditAuth(i.value)
  230. }}
  231. items={optionsEditAuth}
  232. allowSearch={false}
  233. placeholder="请选择编辑权限"
  234. notClearable={true}
  235. />
  236. </div>
  237. </div>
  238. )
  239. }
  240. <div className='pt-2'>
  241. <div className='py-2 text-sm font-medium leading-[20px] text-text-primary'>编辑授权</div>
  242. <AntdTreeSelect
  243. showSearch
  244. style={{ width: '100%' }}
  245. value={editUserIds}
  246. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  247. placeholder="请选择编辑授权"
  248. allowClear
  249. treeDefaultExpandAll
  250. onChange={v => setEditUserIds(v)}
  251. treeData={optionsDeptUserEdit}
  252. fieldNames={{ label: 'treeName', value: 'treeId', children: 'treeChildren' }}
  253. multiple={true}
  254. treeCheckable={true}
  255. />
  256. </div>
  257. <div className='pt-2'>
  258. <div className='py-2 text-sm font-medium leading-[20px] text-text-primary'>可见授权</div>
  259. <AntdTreeSelect
  260. showSearch
  261. style={{ width: '100%' }}
  262. value={lookUserIds}
  263. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  264. placeholder="请选择可见授权"
  265. allowClear
  266. treeDefaultExpandAll
  267. onChange={v => setLookUserIds(v)}
  268. treeData={optionsDeptUser}
  269. fieldNames={{ label: 'treeName', value: 'treeId', children: 'treeChildren' }}
  270. multiple={true}
  271. treeCheckable={true}
  272. />
  273. </div>
  274. </div>
  275. <div className='flex justify-end pt-6'>
  276. <Button className='mr-2' onClick={onClose}>{t('common.operation.cancel')}</Button>
  277. <Button disabled={loading} variant="primary" onClick={onConfirm}>{t('common.operation.save')}</Button>
  278. </div>
  279. </Modal>
  280. )
  281. }
  282. export default RenameDatasetModal