'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 } } const knowledgeSpacePageTitle = ( pathname: string, t: ReturnType>['t'], ) => { if (pathname.includes('/sources/new')) return t(($) => $['newKnowledge.addSource']) if (pathname.includes('/sources')) return t(($) => $['newKnowledge.sources']) if (pathname.includes('/documents')) return t(($) => $['newKnowledge.documents']) return t(($) => $.knowledge) } const isDocumentDetailPath = (pathname: string) => /^\/datasets\/new\/[^/]+\/documents\/[^/]+\/?$/.test(pathname) function KnowledgeSpacePageTitle({ title }: { title: string }) { useDocumentTitle(title) return null } 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 }, }) const pageTitle = knowledgeSpacePageTitle(pathname, t) const documentTitle = `${pageTitle} · ${knowledgeSpaceQuery.data?.name ?? t(($) => $.knowledge)}` const documentTitleOwnedByChild = isDocumentDetailPath(pathname) && !knowledgeSpaceQuery.isPending && !knowledgeSpaceQuery.error && !!knowledgeSpaceQuery.data const pageTitleElement = !documentTitleOwnedByChild ? ( ) : null if (knowledgeSpaceQuery.isPending) return ( <> {pageTitleElement}
) if (knowledgeSpaceQuery.error || !knowledgeSpaceQuery.data) { const status = responseStatus(knowledgeSpaceQuery.error) const notFound = status === 403 || status === 404 return ( <> {pageTitleElement}

{t(($) => notFound ? $['newKnowledge.notFoundTitle'] : $['newKnowledge.detailErrorTitle'], )}

{t(($) => notFound ? $['newKnowledge.notFoundDescription'] : $['newKnowledge.detailErrorDescription'], )}

{!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 (
{pageTitleElement}
{children}
) }