'use client'
import type { ReactNode } from 'react'
import { Button } from '@langgenius/dify-ui/button'
import { cn } from '@langgenius/dify-ui/cn'
import { toast } from '@langgenius/dify-ui/toast'
import { useQuery } from '@tanstack/react-query'
import { useTranslation } from 'react-i18next'
import Loading from '@/app/components/base/loading'
import useDocumentTitle from '@/hooks/use-document-title'
import Link from '@/next/link'
import { usePathname } from '@/next/navigation'
import { consoleQuery } from '@/service/client'
import { newKnowledgeDetailPath, newKnowledgeDocumentsPath, newKnowledgeListPath } from './routes'
function responseStatus(error: unknown) {
if (error instanceof Response) return error.status
if (error && typeof error === 'object' && 'status' in error) return error.status
if (error && typeof error === 'object' && 'data' in error) {
const data = error.data
if (data && typeof data === 'object' && 'status' in data) return data.status
}
}
export function KnowledgeSpaceShell({
children,
knowledgeSpaceId,
}: {
children: ReactNode
knowledgeSpaceId: string
}) {
const { t } = useTranslation('dataset')
const { t: tCommon } = useTranslation('common')
const pathname = usePathname()
const knowledgeSpaceQuery = useQuery({
...consoleQuery.knowledgeFs.getKnowledgeSpacesById.queryOptions({
input: { params: { id: knowledgeSpaceId } },
}),
retry: (failureCount, error) => {
const status = responseStatus(error)
if (status === 403 || status === 404) return false
return failureCount < 3
},
})
useDocumentTitle(knowledgeSpaceQuery.data?.name ?? t(($) => $.knowledge))
if (knowledgeSpaceQuery.isPending)
return (
)
if (knowledgeSpaceQuery.error || !knowledgeSpaceQuery.data) {
const status = responseStatus(knowledgeSpaceQuery.error)
const notFound = status === 403 || status === 404
return (
{t(($) =>
notFound ? $['newKnowledge.notFoundTitle'] : $['newKnowledge.detailErrorTitle'],
)}
{t(($) =>
notFound
? $['newKnowledge.notFoundDescription']
: $['newKnowledge.detailErrorDescription'],
)}
}>
{t(($) => $['newKnowledge.backToList'])}
{!notFound && (
)}
)
}
const sourcesPath = newKnowledgeDetailPath(knowledgeSpaceId)
const documentsPath = newKnowledgeDocumentsPath(knowledgeSpaceId)
const sourcesActive = pathname === sourcesPath || pathname.startsWith(`${sourcesPath}/`)
const documentsActive = pathname === documentsPath || pathname.startsWith(`${documentsPath}/`)
const showDeferredPage = () => toast.info(t(($) => $['cornerLabel.unavailable']))
const navItemClassName =
'flex h-8 shrink-0 items-center gap-2 rounded-lg px-3 system-sm-medium outline-hidden hover:bg-state-base-hover focus-visible:ring-2 focus-visible:ring-state-accent-solid'
return (
)
}