Container.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 { useDebounceFn } from 'ahooks'
  7. // Components
  8. import ExternalAPIPanel from '@/app/components/datasets/external-api/external-api-panel'
  9. import Datasets from './Datasets'
  10. import Database from './Database'
  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 Button from '@/app/components/base/button'
  18. import { ApiConnectionMod } from '@/app/components/base/icons/src/vender/solid/development'
  19. import SearchInput from '@/app/components/base/search-input'
  20. // Services
  21. import { fetchDatasetApiBaseUrl } from '@/service/datasets'
  22. // Hooks
  23. import { useTabSearchParams } from '@/hooks/use-tab-searchparams'
  24. import { useStore as useTagStore } from '@/app/components/base/tag-management/store'
  25. import { useAppContext } from '@/context/app-context'
  26. import { useExternalApiPanel } from '@/context/external-api-panel-context'
  27. // eslint-disable-next-line import/order
  28. import { useQuery } from '@tanstack/react-query'
  29. const Container = () => {
  30. const { t } = useTranslation()
  31. const router = useRouter()
  32. const { currentWorkspace } = useAppContext()
  33. const showTagManagementModal = useTagStore(s => s.showTagManagementModal)
  34. const { showExternalApiPanel, setShowExternalApiPanel } = useExternalApiPanel()
  35. const options = useMemo(() => {
  36. return [
  37. { value: 'dataset', text: t('dataset.database') },
  38. // ...(currentWorkspace.role === 'dataset_operator' ? [] : [{ value: 'api', text: t('dataset.datasetsApi') }]),
  39. ]
  40. }, [currentWorkspace.role, t])
  41. const [activeTab, setActiveTab] = useTabSearchParams({
  42. defaultTab: 'dataset',
  43. })
  44. const containerRef = useRef<HTMLDivElement>(null)
  45. const { data } = useQuery(
  46. {
  47. queryKey: ['datasetApiBaseInfo'],
  48. queryFn: () => fetchDatasetApiBaseUrl('/datasets/api-base-info'),
  49. enabled: activeTab !== 'dataset',
  50. },
  51. )
  52. const [keywords, setKeywords] = useState('')
  53. const [searchKeywords, setSearchKeywords] = useState('')
  54. const { run: handleSearch } = useDebounceFn(() => {
  55. setSearchKeywords(keywords)
  56. }, { wait: 500 })
  57. const handleKeywordsChange = (value: string) => {
  58. setKeywords(value)
  59. handleSearch()
  60. }
  61. const [tagFilterValue, setTagFilterValue] = useState<string[]>([])
  62. const [tagIDs, setTagIDs] = useState<string[]>([])
  63. const { run: handleTagsUpdate } = useDebounceFn(() => {
  64. setTagIDs(tagFilterValue)
  65. }, { wait: 500 })
  66. const handleTagsChange = (value: string[]) => {
  67. setTagFilterValue(value)
  68. handleTagsUpdate()
  69. }
  70. useEffect(() => {
  71. if (currentWorkspace.role === 'normal')
  72. return router.replace('/apps')
  73. }, [currentWorkspace, router])
  74. return (
  75. // 数据库主页面
  76. <div ref={containerRef} className='grow relative flex flex-col overflow-y-auto'>
  77. <div className='sticky top-0 flex justify-between pt-4 px-12 pb-2 leading-[56px] z-10 flex-wrap gap-y-2'>
  78. <TabSliderNew
  79. value={activeTab}
  80. onChange={newActiveTab => setActiveTab(newActiveTab)}
  81. options={options}
  82. />
  83. {activeTab === 'dataset' && (
  84. <div className='flex items-center gap-2'>
  85. <TagFilter type='knowledge' value={tagFilterValue} onChange={handleTagsChange} />
  86. <SearchInput className='w-[200px]' value={keywords} onChange={handleKeywordsChange} />
  87. <div className="w-[1px] h-4 bg-divider-regular" />
  88. {/* <Button
  89. className='gap-0.5 shadows-shadow-xs'
  90. onClick={() => setShowExternalApiPanel(true)}
  91. >
  92. <ApiConnectionMod className='w-4 h-4 text-components-button-secondary-text' />
  93. <div className='flex px-0.5 justify-center items-center gap-1 text-components-button-secondary-text system-sm-medium'>{t('dataset.externalAPIPanelTitle')}</div>
  94. </Button> */}
  95. </div>
  96. )}
  97. {/* {activeTab === 'api' && data && <ApiServer apiBaseUrl={data.api_base_url || ''} />} */}
  98. </div>
  99. {activeTab === 'dataset' && (
  100. <>
  101. <Database containerRef={containerRef} tags={tagIDs} keywords={searchKeywords} />
  102. {/* <DatasetFooter /> */}
  103. {showTagManagementModal && (
  104. <TagManagementModal type='knowledge' show={showTagManagementModal} />
  105. )}
  106. </>
  107. )}
  108. {/* {activeTab === 'api' && data && <Doc apiBaseUrl={data.api_base_url || ''} />} */}
  109. {showExternalApiPanel && <ExternalAPIPanel onClose={() => setShowExternalApiPanel(false)} />}
  110. </div>
  111. )
  112. }
  113. export default Container