Container.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 Button from '@/app/components/base/button'
  18. import Input from '@/app/components/base/input'
  19. import { ApiConnectionMod } from '@/app/components/base/icons/src/vender/solid/development'
  20. import CheckboxWithLabel from '@/app/components/datasets/create/website/base/checkbox-with-label'
  21. // Services
  22. import { fetchDatasetApiBaseUrl } from '@/service/datasets'
  23. // Hooks
  24. import { useTabSearchParams } from '@/hooks/use-tab-searchparams'
  25. import { useStore as useTagStore } from '@/app/components/base/tag-management/store'
  26. import { useAppContext } from '@/context/app-context'
  27. import { useExternalApiPanel } from '@/context/external-api-panel-context'
  28. import { SimpleSelect } from '@/app/components/base/select'
  29. import { fetchTypes } from '@/service/common'
  30. import Statistic from '@/app/(commonLayout)/datasets/Statistic'
  31. const Container = () => {
  32. const { t } = useTranslation()
  33. const router = useRouter()
  34. const { currentWorkspace, isCurrentWorkspaceOwner } = useAppContext()
  35. const showTagManagementModal = useTagStore(s => s.showTagManagementModal)
  36. const { showExternalApiPanel, setShowExternalApiPanel } = useExternalApiPanel()
  37. const [includeAll, { toggle: toggleIncludeAll }] = useBoolean(false)
  38. const options = useMemo(() => {
  39. return [
  40. { value: 'dataset', text: t('dataset.datasets') },
  41. ...(currentWorkspace.role === 'dataset_operator' ? [] : [{ value: 'api', text: t('dataset.datasetsApi') }]),
  42. { value: 'statistic', text: '知识总览' },
  43. ]
  44. }, [currentWorkspace.role, t])
  45. const [activeTab, setActiveTab] = useTabSearchParams({
  46. defaultTab: 'dataset',
  47. })
  48. const containerRef = useRef<HTMLDivElement>(null)
  49. const { data } = useQuery(
  50. {
  51. queryKey: ['datasetApiBaseInfo'],
  52. queryFn: () => fetchDatasetApiBaseUrl('/datasets/api-base-info'),
  53. enabled: activeTab !== 'dataset',
  54. },
  55. )
  56. const [keywords, setKeywords] = useState('')
  57. const [searchKeywords, setSearchKeywords] = useState('')
  58. const { run: handleSearch } = useDebounceFn(() => {
  59. setSearchKeywords(keywords)
  60. }, { wait: 500 })
  61. const handleKeywordsChange = (value: string) => {
  62. setKeywords(value)
  63. handleSearch()
  64. }
  65. const [tagFilterValue, setTagFilterValue] = useState<string[]>([])
  66. const [tagIDs, setTagIDs] = useState<string[]>([])
  67. const { run: handleTagsUpdate } = useDebounceFn(() => {
  68. setTagIDs(tagFilterValue)
  69. }, { wait: 500 })
  70. const handleTagsChange = (value: string[]) => {
  71. setTagFilterValue(value)
  72. handleTagsUpdate()
  73. }
  74. useEffect(() => {
  75. if (currentWorkspace.role === 'normal')
  76. return router.replace('/apps')
  77. }, [currentWorkspace, router])
  78. const [type, setType] = useState<any>()
  79. const [searchType, setSearchType] = useState('')
  80. const { run: handleTypeUpdate } = useDebounceFn(() => {
  81. setSearchType(type)
  82. }, { wait: 500 })
  83. const [optionsType, setOptionsType] = useState<any>([])
  84. useEffect(() => {
  85. fetchTypes({
  86. url: '/workspaces/123123',
  87. params: {},
  88. }).then((res: any) => {
  89. setOptionsType(res.data.map((v: any) => ({ name: v.name, value: v.id })) || [])
  90. })
  91. }, [])
  92. return (
  93. <div ref={containerRef} className='scroll-container relative flex grow flex-col overflow-y-auto bg-background-body'>
  94. <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]'>
  95. <TabSliderNew
  96. value={activeTab}
  97. onChange={newActiveTab => setActiveTab(newActiveTab)}
  98. options={options}
  99. />
  100. {activeTab === 'dataset' && (
  101. <div className='flex items-center justify-center gap-2'>
  102. <SimpleSelect
  103. className="h-[32px] w-[200px]"
  104. defaultValue={type}
  105. onSelect={(i) => {
  106. setType(i.value)
  107. handleTypeUpdate()
  108. }}
  109. items={optionsType}
  110. allowSearch={false}
  111. />
  112. {isCurrentWorkspaceOwner && <CheckboxWithLabel
  113. isChecked={includeAll}
  114. onChange={toggleIncludeAll}
  115. label={t('dataset.allKnowledge')}
  116. labelClassName='system-md-regular text-text-secondary'
  117. className='mr-2'
  118. tooltip={t('dataset.allKnowledgeDescription') as string}
  119. />}
  120. <TagFilter type='knowledge' value={tagFilterValue} onChange={handleTagsChange} />
  121. <Input
  122. showLeftIcon
  123. showClearIcon
  124. wrapperClassName='w-[200px]'
  125. value={keywords}
  126. onChange={e => handleKeywordsChange(e.target.value)}
  127. onClear={() => handleKeywordsChange('')}
  128. />
  129. <div className="h-4 w-[1px] bg-divider-regular" />
  130. <Button
  131. className='shadows-shadow-xs gap-0.5'
  132. onClick={() => setShowExternalApiPanel(true)}
  133. >
  134. <ApiConnectionMod className='h-4 w-4 text-components-button-secondary-text' />
  135. <div className='system-sm-medium flex items-center justify-center gap-1 px-0.5 text-components-button-secondary-text'>{t('dataset.externalAPIPanelTitle')}</div>
  136. </Button>
  137. </div>
  138. )}
  139. {activeTab === 'api' && data && <ApiServer apiBaseUrl={data.api_base_url || ''} />}
  140. </div>
  141. {activeTab === 'dataset' && (
  142. <>
  143. <Datasets containerRef={containerRef} tags={tagIDs} keywords={searchKeywords} includeAll={includeAll} type={searchType} optionsType={optionsType} />
  144. <DatasetFooter />
  145. {showTagManagementModal && (
  146. <TagManagementModal type='knowledge' show={showTagManagementModal} />
  147. )}
  148. </>
  149. )}
  150. {activeTab === 'api' && data && <Doc apiBaseUrl={data.api_base_url || ''} />}
  151. {activeTab === 'statistic' && data && <Statistic/>}
  152. {showExternalApiPanel && <ExternalAPIPanel onClose={() => setShowExternalApiPanel(false)} />}
  153. </div>
  154. )
  155. }
  156. export default Container