'use client' import type { LogicalDocument, LogicalDocumentRevision, } from '@dify/contracts/knowledge-fs/types.gen' import { Button } from '@langgenius/dify-ui/button' import { useInfiniteQuery } from '@tanstack/react-query' import { useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' import Loading from '@/app/components/base/loading' import { DocumentChunkDetail } from './document-chunk-detail' import { DocumentChunkTreePanel } from './document-chunk-tree' import { buildDocumentChunkTree } from './document-detail-model' import { documentChunksQueryOptions } from './document-detail-queries' export function DocumentRevisionContent({ document, documentId, effectiveRevision, knowledgeSpaceId, locale, revision, revisionHistoryError, revisionHistoryPending, retryRevisionHistory, }: { document: LogicalDocument documentId: string effectiveRevision?: number knowledgeSpaceId: string locale: string revision?: Exclude revisionHistoryError: boolean revisionHistoryPending: boolean retryRevisionHistory: () => void }) { const { t } = useTranslation('dataset') const { t: tCommon } = useTranslation('common') if (effectiveRevision === undefined && revisionHistoryPending) return (
) if (effectiveRevision === undefined && revisionHistoryError) return (

{t(($) => $['newKnowledge.documentLoadErrorTitle'])}

{t(($) => $['newKnowledge.documentLoadErrorDescription'])}

) if (effectiveRevision === undefined) return (

{t(($) => $['newKnowledge.documentRevisionMissingTitle'])}

{t(($) => $['newKnowledge.documentRevisionMissingDescription'])}

) return ( ) } function LoadedDocumentRevisionContent({ document, documentId, effectiveRevision, knowledgeSpaceId, locale, revision, }: { document: LogicalDocument documentId: string effectiveRevision: number knowledgeSpaceId: string locale: string revision?: Exclude }) { const [selectedChunkId, setSelectedChunkId] = useState() const chunksQueryOptions = useMemo( () => documentChunksQueryOptions({ documentId, effectiveRevision, knowledgeSpaceId }), [documentId, effectiveRevision, knowledgeSpaceId], ) const chunksQuery = useInfiniteQuery(chunksQueryOptions) const chunks = useMemo( () => [...(chunksQuery.data?.pages.flatMap((page) => page.items) ?? [])].sort( (left, right) => left.ordinal - right.ordinal || left.id.localeCompare(right.id), ), [chunksQuery.data], ) const tree = useMemo(() => buildDocumentChunkTree(chunks), [chunks]) const selectedChunk = (selectedChunkId ? tree.byId.get(selectedChunkId)?.chunk : undefined) ?? tree.roots[0]?.chunk return (
void chunksQuery.refetch()} onSelectChunk={setSelectedChunkId} selectedChunkId={selectedChunk?.id} tree={tree} />
) }