'use client' // Libraries import { useEffect, useMemo, useRef, useState } from 'react' import { useRouter } from 'next/navigation' import { useTranslation } from 'react-i18next' import { useBoolean, useDebounceFn } from 'ahooks' import { useQuery } from '@tanstack/react-query' // Components import ExternalAPIPanel from '../../components/datasets/external-api/external-api-panel' import Datasets from './Datasets' import DatasetFooter from './DatasetFooter' import ApiServer from './ApiServer' import Doc from './Doc' import TabSliderNew from '@/app/components/base/tab-slider-new' import TagManagementModal from '@/app/components/base/tag-management' import TagFilter from '@/app/components/base/tag-management/filter' import Input from '@/app/components/base/input' // Services import { fetchDatasetApiBaseUrl } from '@/service/datasets' // Hooks import { useTabSearchParams } from '@/hooks/use-tab-searchparams' import { useStore as useTagStore } from '@/app/components/base/tag-management/store' import { useAppContext } from '@/context/app-context' import { useExternalApiPanel } from '@/context/external-api-panel-context' import { SimpleSelect } from '@/app/components/base/select' import { fetchDepts, fetchTypes } from '@/service/common' import Statistic from '@/app/(commonLayout)/datasets/Statistic' import { TreeSelect as AntdTreeSelect } from 'antd' const Container = () => { const { t } = useTranslation() const router = useRouter() const { currentWorkspace, isCurrentWorkspaceOwner } = useAppContext() const showTagManagementModal = useTagStore(s => s.showTagManagementModal) const { showExternalApiPanel, setShowExternalApiPanel } = useExternalApiPanel() const [includeAll, { toggle: toggleIncludeAll }] = useBoolean(false) const options = useMemo(() => { return [ { value: 'dataset', text: t('dataset.datasets') }, ...(currentWorkspace.role === 'dataset_operator' ? [] : [{ value: 'api', text: t('dataset.datasetsApi') }]), { value: 'statistic', text: '知识总览' }, ] }, [currentWorkspace.role, t]) const [activeTab, setActiveTab] = useTabSearchParams({ defaultTab: 'dataset', }) const containerRef = useRef(null) const { data } = useQuery( { queryKey: ['datasetApiBaseInfo'], queryFn: () => fetchDatasetApiBaseUrl('/datasets/api-base-info'), enabled: activeTab !== 'dataset', }, ) const [keywords, setKeywords] = useState('') const [searchKeywords, setSearchKeywords] = useState('') const { run: handleSearch } = useDebounceFn(() => { setSearchKeywords(keywords) }, { wait: 500 }) const handleKeywordsChange = (value: string) => { setKeywords(value) handleSearch() } const [creator, setCreator] = useState('') const [searchCreator, setSearchCreator] = useState('') const { run: handleSearchCreator } = useDebounceFn(() => { setSearchCreator(creator) }, { wait: 500 }) const handleCreatorChange = (value: string) => { setCreator(value) handleSearchCreator() } const [tagFilterValue, setTagFilterValue] = useState([]) const [tagIDs, setTagIDs] = useState([]) const { run: handleTagsUpdate } = useDebounceFn(() => { setTagIDs(tagFilterValue) }, { wait: 500 }) const handleTagsChange = (value: string[]) => { setTagFilterValue(value) handleTagsUpdate() } // useEffect(() => { // if (currentWorkspace.role === 'normal') // return router.replace('/apps') // }, [currentWorkspace, router]) const [type, setType] = useState() const [searchType, setSearchType] = useState('') const { run: handleTypeUpdate } = useDebounceFn(() => { setSearchType(type) }, { wait: 500 }) const [optionsType, setOptionsType] = useState([]) useEffect(() => { fetchTypes({ url: '/tags/page', params: { page: 1, limit: 99999, tag_type: 'knowledge_category', }, }).then((res: any) => { setOptionsType(res.data.map((v: any) => ({ name: v.name, value: v.id })) || []) }) }, []) const [dept, setDept] = useState() const [optionsDept, setOptionsDept] = useState([]) useEffect(() => { fetchDepts({ url: '/depts', }).then((res: any) => { setOptionsDept(res.data || []) }) }, []) const [authType, setAuthType] = useState('') const optionsAuthType = [ { name: '创建', value: '1' }, { name: '编辑', value: '2' }, { name: '授权编辑', value: '3' }, { name: '授权可见', value: '4' }, ] return (
setActiveTab(newActiveTab)} options={options} /> {activeTab === 'dataset' && (
handleCreatorChange(e.target.value)} onClear={() => handleCreatorChange('')} placeholder="请输入创建人" /> { setAuthType(i.value) }} items={optionsAuthType} allowSearch={false} placeholder="请选择权限类型" /> setDept(v || '')} treeData={optionsDept} fieldNames={{ label: 'dept_name', value: 'dept_id' }} /> { setType(i.value) handleTypeUpdate() }} items={optionsType} allowSearch={false} placeholder="请选择类型" /> {/* {isCurrentWorkspaceOwner && } */} handleKeywordsChange(e.target.value)} onClear={() => handleKeywordsChange('')} /> {/*
*/} {/* */}
)} {activeTab === 'api' && data && }
{activeTab === 'dataset' && ( <> {showTagManagementModal && ( )} )} {activeTab === 'api' && data && } {activeTab === 'statistic' && data && } {showExternalApiPanel && setShowExternalApiPanel(false)} />}
) } export default Container export const GetDatasetAuth = (row: any) => { const { currentWorkspace, userProfile } = useAppContext() let isCreate = false let isEdit = false let isOperation = false if (row) { isCreate = currentWorkspace.role === 'owner' || row.created_by === userProfile.id isEdit = isCreate || currentWorkspace.role === 'admin' || (row.edit_auth === 2 && currentWorkspace.role === 'leader' && row.dept_id === userProfile.dept_id) isOperation = isEdit || row.has_edit_permission || (row.edit_auth === 2 && row.dept_id === userProfile.dept_id) } return { isCreate, isEdit, isOperation, } }