Container.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 [tagFilterValue, setTagFilterValue] = useState<string[]>([])
  64. const [tagIDs, setTagIDs] = useState<string[]>([])
  65. const { run: handleTagsUpdate } = useDebounceFn(() => {
  66. setTagIDs(tagFilterValue)
  67. }, { wait: 500 })
  68. const handleTagsChange = (value: string[]) => {
  69. setTagFilterValue(value)
  70. handleTagsUpdate()
  71. }
  72. useEffect(() => {
  73. if (currentWorkspace.role === 'normal')
  74. return router.replace('/apps')
  75. }, [currentWorkspace, router])
  76. const [type, setType] = useState<any>()
  77. const [searchType, setSearchType] = useState('')
  78. const { run: handleTypeUpdate } = useDebounceFn(() => {
  79. setSearchType(type)
  80. }, { wait: 500 })
  81. const [optionsType, setOptionsType] = useState<any>([])
  82. useEffect(() => {
  83. fetchTypes({
  84. url: '/tags/page',
  85. params: {
  86. page: 1,
  87. limit: 99999,
  88. tag_type: 'knowledge_category',
  89. },
  90. }).then((res: any) => {
  91. setOptionsType(res.data.map((v: any) => ({ name: v.name, value: v.id })) || [])
  92. })
  93. }, [])
  94. const [dept, setDept] = useState<any>()
  95. const [optionsDept, setOptionsDept] = useState<any>([])
  96. useEffect(() => {
  97. fetchDepts({
  98. url: '/depts',
  99. }).then((res: any) => {
  100. setOptionsDept(res.data || [])
  101. })
  102. }, [])
  103. const [authType, setAuthType] = useState<any>('')
  104. const optionsAuthType = [
  105. { name: '创建', value: '1' },
  106. { name: '编辑', value: '2' },
  107. { name: '授权编辑', value: '3' },
  108. { name: '授权可见', value: '4' },
  109. ]
  110. return (
  111. <div ref={containerRef} className='scroll-container relative flex grow flex-col overflow-y-auto bg-background-body'>
  112. <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]'>
  113. <TabSliderNew
  114. value={activeTab}
  115. onChange={newActiveTab => setActiveTab(newActiveTab)}
  116. options={options}
  117. />
  118. {activeTab === 'dataset' && (
  119. <div className='flex items-center justify-center gap-2'>
  120. <SimpleSelect
  121. wrapperClassName="h-[32px] w-[200px]"
  122. defaultValue={authType}
  123. onSelect={(i) => {
  124. setAuthType(i.value)
  125. }}
  126. items={optionsAuthType}
  127. allowSearch={false}
  128. placeholder="请选择权限类型"
  129. />
  130. <AntdTreeSelect
  131. showSearch
  132. style={{ width: '200px' }}
  133. value={dept}
  134. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  135. placeholder="请选择部门"
  136. allowClear
  137. treeDefaultExpandAll
  138. onChange={v => setDept(v || '')}
  139. treeData={optionsDept}
  140. fieldNames={{ label: 'dept_name', value: 'dept_id' }}
  141. />
  142. <SimpleSelect
  143. wrapperClassName="h-[32px] w-[200px]"
  144. defaultValue={type}
  145. onSelect={(i) => {
  146. setType(i.value)
  147. handleTypeUpdate()
  148. }}
  149. items={optionsType}
  150. allowSearch={false}
  151. placeholder="请选择类型"
  152. />
  153. {/* {isCurrentWorkspaceOwner && <CheckboxWithLabel */}
  154. {/* isChecked={includeAll} */}
  155. {/* onChange={toggleIncludeAll} */}
  156. {/* label={t('dataset.allKnowledge')} */}
  157. {/* labelClassName='system-md-regular text-text-secondary' */}
  158. {/* className='mr-2' */}
  159. {/* tooltip={t('dataset.allKnowledgeDescription') as string} */}
  160. {/* />} */}
  161. <TagFilter type='knowledge' value={tagFilterValue} onChange={handleTagsChange} />
  162. <Input
  163. showLeftIcon
  164. showClearIcon
  165. wrapperClassName='w-[200px]'
  166. value={keywords}
  167. onChange={e => handleKeywordsChange(e.target.value)}
  168. onClear={() => handleKeywordsChange('')}
  169. />
  170. {/* <div className="h-4 w-[1px] bg-divider-regular" /> */}
  171. {/* <Button */}
  172. {/* className='shadows-shadow-xs gap-0.5' */}
  173. {/* onClick={() => setShowExternalApiPanel(true)} */}
  174. {/* > */}
  175. {/* <ApiConnectionMod className='h-4 w-4 text-components-button-secondary-text' /> */}
  176. {/* <div className='system-sm-medium flex items-center justify-center gap-1 px-0.5 text-components-button-secondary-text'>{t('dataset.externalAPIPanelTitle')}</div> */}
  177. {/* </Button> */}
  178. </div>
  179. )}
  180. {activeTab === 'api' && data && <ApiServer apiBaseUrl={data.api_base_url || ''} />}
  181. </div>
  182. {activeTab === 'dataset' && (
  183. <>
  184. <Datasets containerRef={containerRef} tags={tagIDs} keywords={searchKeywords} includeAll={includeAll} type={searchType} dept={dept} authType={authType} />
  185. <DatasetFooter />
  186. {showTagManagementModal && (
  187. <TagManagementModal type='knowledge' show={showTagManagementModal} />
  188. )}
  189. </>
  190. )}
  191. {activeTab === 'api' && data && <Doc apiBaseUrl={data.api_base_url || ''} />}
  192. {activeTab === 'statistic' && data && <Statistic/>}
  193. {showExternalApiPanel && <ExternalAPIPanel onClose={() => setShowExternalApiPanel(false)} />}
  194. </div>
  195. )
  196. }
  197. export default Container