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