Container.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. import useSWR from 'swr'
  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 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. const Container = () => {
  28. const { t } = useTranslation()
  29. const router = useRouter()
  30. const { currentWorkspace } = useAppContext()
  31. const showTagManagementModal = useTagStore(s => s.showTagManagementModal)
  32. const { showExternalApiPanel, setShowExternalApiPanel } = useExternalApiPanel()
  33. const options = useMemo(() => {
  34. return [
  35. { value: 'dataset', text: t('dataset.datasets') },
  36. ...(currentWorkspace.role === 'dataset_operator' ? [] : [{ value: 'api', text: t('dataset.datasetsApi') }]),
  37. ]
  38. }, [currentWorkspace.role, t])
  39. const [activeTab, setActiveTab] = useTabSearchParams({
  40. defaultTab: 'dataset',
  41. })
  42. const containerRef = useRef<HTMLDivElement>(null)
  43. const { data } = useSWR(activeTab === 'dataset' ? null : '/datasets/api-base-info', fetchDatasetApiBaseUrl)
  44. const [keywords, setKeywords] = useState('')
  45. const [searchKeywords, setSearchKeywords] = useState('')
  46. const { run: handleSearch } = useDebounceFn(() => {
  47. setSearchKeywords(keywords)
  48. }, { wait: 500 })
  49. const handleKeywordsChange = (value: string) => {
  50. setKeywords(value)
  51. handleSearch()
  52. }
  53. const [tagFilterValue, setTagFilterValue] = useState<string[]>([])
  54. const [tagIDs, setTagIDs] = useState<string[]>([])
  55. const { run: handleTagsUpdate } = useDebounceFn(() => {
  56. setTagIDs(tagFilterValue)
  57. }, { wait: 500 })
  58. const handleTagsChange = (value: string[]) => {
  59. setTagFilterValue(value)
  60. handleTagsUpdate()
  61. }
  62. useEffect(() => {
  63. if (currentWorkspace.role === 'normal')
  64. return router.replace('/apps')
  65. }, [currentWorkspace, router])
  66. return (
  67. <div ref={containerRef} className='grow relative flex flex-col bg-gray-100 overflow-y-auto'>
  68. <div className='sticky top-0 flex justify-between pt-4 px-12 pb-2 leading-[56px] bg-gray-100 z-10 flex-wrap gap-y-2'>
  69. <TabSliderNew
  70. value={activeTab}
  71. onChange={newActiveTab => setActiveTab(newActiveTab)}
  72. options={options}
  73. />
  74. {activeTab === 'dataset' && (
  75. <div className='flex items-center gap-2'>
  76. <TagFilter type='knowledge' value={tagFilterValue} onChange={handleTagsChange} />
  77. <SearchInput className='w-[200px]' value={keywords} onChange={handleKeywordsChange} />
  78. <div className="w-[1px] h-4 bg-divider-regular" />
  79. <Button
  80. className='gap-0.5 shadows-shadow-xs'
  81. onClick={() => setShowExternalApiPanel(true)}
  82. >
  83. <ApiConnectionMod className='w-4 h-4 text-components-button-secondary-text' />
  84. <div className='flex px-0.5 justify-center items-center gap-1 text-components-button-secondary-text system-sm-medium'>{t('dataset.externalAPIPanelTitle')}</div>
  85. </Button>
  86. </div>
  87. )}
  88. {activeTab === 'api' && data && <ApiServer apiBaseUrl={data.api_base_url || ''} />}
  89. </div>
  90. {activeTab === 'dataset' && (
  91. <>
  92. <Datasets containerRef={containerRef} tags={tagIDs} keywords={searchKeywords} />
  93. <DatasetFooter />
  94. {showTagManagementModal && (
  95. <TagManagementModal type='knowledge' show={showTagManagementModal} />
  96. )}
  97. </>
  98. )}
  99. {activeTab === 'api' && data && <Doc apiBaseUrl={data.api_base_url || ''} />}
  100. {showExternalApiPanel && <ExternalAPIPanel onClose={() => setShowExternalApiPanel(false)} />}
  101. </div>
  102. )
  103. }
  104. export default Container