Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

3.7 KB
Raw Blame
'use client'
import type { NotionPage } from '@/models/common'
import { RiCloseLine } from '@remixicon/react'
import { useMutation } from '@tanstack/react-query'
import * as React from 'react'
import { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Markdown } from '@/app/components/base/markdown'
import { selectOnlineDocumentPreview } from '@/app/components/rag-pipeline/utils/query-select'
import { toast } from '@/app/notifications'
import { useDatasetDetailContextWithSelector } from '@/context/dataset-detail'
import { DatasourceType } from '@/models/pipeline'
import { consoleQuery } from '@/service/console'
import { formatNumberAbbreviated } from '@/utils/format'
import { useDataSourceStore } from '../data-source/store'
import Loading from './loading'

type OnlineDocumentPreviewProps = {
  currentPage: NotionPage
  datasourceNodeId: string
  hidePreview: () => void
}

const OnlineDocumentPreview = ({
  currentPage,
  datasourceNodeId,
  hidePreview,
}: OnlineDocumentPreviewProps) => {
  const { t } = useTranslation(['datasetPipeline'])
  const [content, setContent] = useState('')
  const pipelineId = useDatasetDetailContextWithSelector((state) => state.dataset?.pipeline_id)
  const { mutate: getOnlineDocumentContent, isPending } = useMutation(
    consoleQuery.rag.pipelines.byPipelineId.workflows.published.datasource.nodes.byNodeId.preview.post.mutationOptions(
      { context: { silent: true } },
    ),
  )
  const dataSourceStore = useDataSourceStore()

  useEffect(() => {
    const { currentCredentialId } = dataSourceStore.getState()
    getOnlineDocumentContent(
      {
        params: { pipeline_id: pipelineId || '', node_id: datasourceNodeId },
        body: {
          datasource_type: DatasourceType.onlineDocument,
          credential_id: currentCredentialId,
          inputs: {
            workspace_id: currentPage.workspace_id,
            page_id: currentPage.page_id,
            type: currentPage.type,
          },
        },
      },
      {
        onSuccess(data) {
          setContent(selectOnlineDocumentPreview(data).content)
        },
        onError(error) {
          toast.error(error.message)
        },
      },
    )
  }, [currentPage.page_id])

  return (
    <div className="flex size-full flex-col rounded-t-xl border-t border-l border-components-panel-border bg-background-default-lighter shadow-md shadow-shadow-shadow-5">
      <div className="flex gap-x-2 border-b border-divider-subtle pt-4 pr-4 pb-3 pl-6">
        <div className="flex grow flex-col gap-y-1">
          <div className="system-2xs-semibold-uppercase text-text-accent">
            {t(($) => $['addDocuments.stepOne.preview'], { ns: 'datasetPipeline' })}
          </div>
          <div className="title-md-semi-bold">{currentPage?.page_name}</div>
          <div className="flex items-center gap-x-1 system-xs-medium text-text-tertiary">
            <span aria-hidden className="i-custom-public-common-notion size-3.5" />
            <span>{currentPage.type}</span>
            <span>·</span>
            <span>{`${formatNumberAbbreviated(content.length)} ${t(($) => $['addDocuments.characters'], { ns: 'datasetPipeline' })}`}</span>
          </div>
        </div>
        <button
          type="button"
          className="flex size-8 shrink-0 items-center justify-center"
          onClick={hidePreview}
        >
          <RiCloseLine className="size-4.5" />
        </button>
      </div>
      {isPending && (
        <div className="grow">
          <Loading />
        </div>
      )}
      {!isPending && content && (
        <div className="grow overflow-hidden px-6 py-5 body-md-regular text-text-secondary">
          <Markdown content={content} />
        </div>
      )}
    </div>
  )
}

export default OnlineDocumentPreview