Container.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. 'use client'
  2. // Libraries
  3. import { useEffect, useMemo, useRef, useState } from 'react'
  4. import { useRouter } from 'next/navigation'
  5. import { useTranslation } from 'react-i18next'
  6. import { useBoolean, useDebounceFn } from 'ahooks'
  7. import { useQuery } from '@tanstack/react-query'
  8. // Components
  9. import ExternalAPIPanel from '../../components/datasets/external-api/external-api-panel'
  10. import Datasets from './Datasets'
  11. import DatasetFooter from './DatasetFooter'
  12. import ApiServer from './ApiServer'
  13. import Doc from './Doc'
  14. import TabSliderNew from '@/app/components/base/tab-slider-new'
  15. import TagManagementModal from '@/app/components/base/tag-management'
  16. import TagFilter from '@/app/components/base/tag-management/filter'
  17. import Input from '@/app/components/base/input'
  18. // Services
  19. import { fetchDatasetApiBaseUrl } from '@/service/datasets'
  20. // Hooks
  21. import { useTabSearchParams } from '@/hooks/use-tab-searchparams'
  22. import { useStore as useTagStore } from '@/app/components/base/tag-management/store'
  23. import { useAppContext } from '@/context/app-context'
  24. import { useExternalApiPanel } from '@/context/external-api-panel-context'
  25. import { SimpleSelect } from '@/app/components/base/select'
  26. import { fetchDepts, fetchTypes } from '@/service/common'
  27. import Statistic from '@/app/(commonLayout)/datasets/Statistic'
  28. import { TreeSelect as AntdTreeSelect } from 'antd'
  29. const Container = () => {
  30. const { t } = useTranslation()
  31. const router = useRouter()
  32. const { currentWorkspace, isCurrentWorkspaceOwner } = useAppContext()
  33. const showTagManagementModal = useTagStore(s => s.showTagManagementModal)
  34. const { showExternalApiPanel, setShowExternalApiPanel } = useExternalApiPanel()
  35. const [includeAll, { toggle: toggleIncludeAll }] = useBoolean(false)
  36. const options = useMemo(() => {
  37. return [
  38. { value: 'dataset', text: t('dataset.datasets') },
  39. ...(currentWorkspace.role === 'dataset_operator' ? [] : [{ value: 'api', text: t('dataset.datasetsApi') }]),
  40. { value: 'statistic', text: '知识总览' },
  41. ]
  42. }, [currentWorkspace.role, t])
  43. const [activeTab, setActiveTab] = useTabSearchParams({
  44. defaultTab: 'dataset',
  45. })
  46. const containerRef = useRef<HTMLDivElement>(null)
  47. const { data } = useQuery(
  48. {
  49. queryKey: ['datasetApiBaseInfo'],
  50. queryFn: () => fetchDatasetApiBaseUrl('/datasets/api-base-info'),
  51. enabled: activeTab !== 'dataset',
  52. },
  53. )
  54. const [keywords, setKeywords] = useState('')
  55. const [searchKeywords, setSearchKeywords] = useState('')
  56. const { run: handleSearch } = useDebounceFn(() => {
  57. setSearchKeywords(keywords)
  58. }, { wait: 500 })
  59. const handleKeywordsChange = (value: string) => {
  60. setKeywords(value)
  61. handleSearch()
  62. }
  63. const [creator, setCreator] = useState('')
  64. const [searchCreator, setSearchCreator] = useState('')
  65. const { run: handleSearchCreator } = useDebounceFn(() => {
  66. setSearchCreator(creator)
  67. }, { wait: 500 })
  68. const handleCreatorChange = (value: string) => {
  69. setCreator(value)
  70. handleSearchCreator()
  71. }
  72. const [tagFilterValue, setTagFilterValue] = useState<string[]>([])
  73. const [tagIDs, setTagIDs] = useState<string[]>([])
  74. const { run: handleTagsUpdate } = useDebounceFn(() => {
  75. setTagIDs(tagFilterValue)
  76. }, { wait: 500 })
  77. const handleTagsChange = (value: string[]) => {
  78. setTagFilterValue(value)
  79. handleTagsUpdate()
  80. }
  81. // useEffect(() => {
  82. // if (currentWorkspace.role === 'normal')
  83. // return router.replace('/apps')
  84. // }, [currentWorkspace, router])
  85. const [type, setType] = useState<any>()
  86. const [searchType, setSearchType] = useState('')
  87. const { run: handleTypeUpdate } = useDebounceFn(() => {
  88. setSearchType(type)
  89. }, { wait: 500 })
  90. const [optionsType, setOptionsType] = useState<any>([])
  91. useEffect(() => {
  92. fetchTypes({
  93. url: '/tags/page',
  94. params: {
  95. page: 1,
  96. limit: 99999,
  97. tag_type: 'knowledge_category',
  98. },
  99. }).then((res: any) => {
  100. setOptionsType(res.data.map((v: any) => ({ name: v.name, value: v.id })) || [])
  101. })
  102. }, [])
  103. const [dept, setDept] = useState<any>()
  104. const [optionsDept, setOptionsDept] = useState<any>([])
  105. useEffect(() => {
  106. fetchDepts({
  107. url: '/depts',
  108. }).then((res: any) => {
  109. setOptionsDept(res.data || [])
  110. })
  111. }, [])
  112. const [authType, setAuthType] = useState<any>('')
  113. const optionsAuthType = [
  114. { name: '创建', value: '1' },
  115. { name: '编辑', value: '2' },
  116. { name: '授权编辑', value: '3' },
  117. { name: '授权可见', value: '4' },
  118. ]
  119. return (
  120. <div ref={containerRef} className='scroll-container relative flex grow flex-col overflow-y-auto bg-background-body'>
  121. <div className='sticky top-0 z-10 flex flex-wrap justify-between gap-y-2 bg-background-body px-12 pb-2 pt-4 leading-[56px]'>
  122. <TabSliderNew
  123. value={activeTab}
  124. onChange={newActiveTab => setActiveTab(newActiveTab)}
  125. options={options}
  126. />
  127. {activeTab === 'dataset' && (
  128. <div className='flex items-center justify-center gap-2'>
  129. <Input
  130. showLeftIcon
  131. showClearIcon
  132. wrapperClassName='w-[200px]'
  133. value={creator}
  134. onChange={e => handleCreatorChange(e.target.value)}
  135. onClear={() => handleCreatorChange('')}
  136. placeholder="请输入创建人"
  137. />
  138. <SimpleSelect
  139. wrapperClassName="h-[32px] w-[200px]"
  140. defaultValue={authType}
  141. onSelect={(i) => {
  142. setAuthType(i.value)
  143. }}
  144. items={optionsAuthType}
  145. allowSearch={false}
  146. placeholder="请选择权限类型"
  147. />
  148. <AntdTreeSelect
  149. showSearch
  150. style={{ width: '200px' }}
  151. value={dept}
  152. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  153. placeholder="请选择部门"
  154. allowClear
  155. treeDefaultExpandAll
  156. onChange={v => setDept(v || '')}
  157. treeData={optionsDept}
  158. fieldNames={{ label: 'dept_name', value: 'dept_id' }}
  159. />
  160. <SimpleSelect
  161. wrapperClassName="h-[32px] w-[200px]"
  162. defaultValue={type}
  163. onSelect={(i) => {
  164. setType(i.value)
  165. handleTypeUpdate()
  166. }}
  167. items={optionsType}
  168. allowSearch={false}
  169. placeholder="请选择类型"
  170. />
  171. {/* {isCurrentWorkspaceOwner && <CheckboxWithLabel */}
  172. {/* isChecked={includeAll} */}
  173. {/* onChange={toggleIncludeAll} */}
  174. {/* label={t('dataset.allKnowledge')} */}
  175. {/* labelClassName='system-md-regular text-text-secondary' */}
  176. {/* className='mr-2' */}
  177. {/* tooltip={t('dataset.allKnowledgeDescription') as string} */}
  178. {/* />} */}
  179. <TagFilter type='knowledge' value={tagFilterValue} onChange={handleTagsChange} />
  180. <Input
  181. showLeftIcon
  182. showClearIcon
  183. wrapperClassName='w-[200px]'
  184. value={keywords}
  185. onChange={e => handleKeywordsChange(e.target.value)}
  186. onClear={() => handleKeywordsChange('')}
  187. />
  188. {/* <div className="h-4 w-[1px] bg-divider-regular" /> */}
  189. {/* <Button */}
  190. {/* className='shadows-shadow-xs gap-0.5' */}
  191. {/* onClick={() => setShowExternalApiPanel(true)} */}
  192. {/* > */}
  193. {/* <ApiConnectionMod className='h-4 w-4 text-components-button-secondary-text' /> */}
  194. {/* <div className='system-sm-medium flex items-center justify-center gap-1 px-0.5 text-components-button-secondary-text'>{t('dataset.externalAPIPanelTitle')}</div> */}
  195. {/* </Button> */}
  196. </div>
  197. )}
  198. {activeTab === 'api' && data && <ApiServer apiBaseUrl={data.api_base_url || ''} />}
  199. </div>
  200. {activeTab === 'dataset' && (
  201. <>
  202. <Datasets containerRef={containerRef} tags={tagIDs} keywords={searchKeywords} includeAll={includeAll} type={searchType} dept={dept} authType={authType} creator={searchCreator} />
  203. <DatasetFooter />
  204. {showTagManagementModal && (
  205. <TagManagementModal type='knowledge' show={showTagManagementModal} />
  206. )}
  207. </>
  208. )}
  209. {activeTab === 'api' && data && <Doc apiBaseUrl={data.api_base_url || ''} />}
  210. {activeTab === 'statistic' && data && <Statistic/>}
  211. {showExternalApiPanel && <ExternalAPIPanel onClose={() => setShowExternalApiPanel(false)} />}
  212. </div>
  213. )
  214. }
  215. export default Container
  216. export const GetDatasetAuth = (row: any) => {
  217. const { currentWorkspace, userProfile } = useAppContext()
  218. let isCreate = false
  219. let isEdit = false
  220. let isOperation = false
  221. if (row) {
  222. isCreate = currentWorkspace.role === 'owner' || row.created_by === userProfile.id
  223. isEdit = isCreate || currentWorkspace.role === 'admin' || (row.edit_auth === 2 && currentWorkspace.role === 'leader' && row.dept_id === userProfile.dept_id)
  224. isOperation = isEdit || row.has_edit_permission || (row.edit_auth === 2 && row.dept_id === userProfile.dept_id)
  225. }
  226. return {
  227. isCreate,
  228. isEdit,
  229. isOperation,
  230. }
  231. }