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.

4.3 KB
Raw Blame
import type { DocumentStatusResponse } from '@dify/contracts/api/console/datasets/types.gen'
import type { ComponentProps, FC } from 'react'
import { cn } from '@langgenius/dify-ui/cn'
import { Infotip, InfotipContent, InfotipTrigger } from '@langgenius/dify-ui/infotip'
import { RiCheckboxCircleFill } from '@remixicon/react'
import { useId } from 'react'
import { useTranslation } from 'react-i18next'
import NotionIcon from '@/app/components/base/notion-icon'
import PriorityLabel from '@/app/components/billing/priority-label'
import { DataSourceType } from '@/models/datasets'
import DocumentFileIcon from '../../common/document-file-icon'
import { getFileType, getSourcePercent, isSourceEmbedding } from './utils'

type IndexingProgressItemProps = {
  detail: DocumentStatusResponse
  name?: string
  sourceType?: DataSourceType
  notionIcon?: ComponentProps<typeof NotionIcon>['src']
}

// Status icon component for completed/error states
const StatusIcon: FC<{ status: string; error?: string }> = ({ status, error }) => {
  const { t } = useTranslation(['common'])

  if (status === 'completed')
    return <RiCheckboxCircleFill aria-hidden="true" className="size-4 shrink-0 text-text-success" />

  if (status === 'error') {
    const errorLabel = error || t(($) => $.error, { ns: 'common' })

    return (
      <Infotip>
        <InfotipTrigger
          aria-label={t(($) => $.error, { ns: 'common' })}
          iconVariant="warning"
          iconSize="large"
          className="text-text-destructive"
        />
        <InfotipContent
          aria-label={t(($) => $.error, { ns: 'common' })}
          className="whitespace-pre-wrap"
          sideOffset={4}
        >
          {errorLabel}
        </InfotipContent>
      </Infotip>
    )
  }

  return null
}

// Source type icon component
const SourceTypeIcon: FC<{
  sourceType?: DataSourceType
  name?: string
  notionIcon?: ComponentProps<typeof NotionIcon>['src']
}> = ({ sourceType, name, notionIcon }) => {
  if (sourceType === DataSourceType.FILE) {
    return (
      <DocumentFileIcon size="sm" className="shrink-0" name={name} extension={getFileType(name)} />
    )
  }

  if (sourceType === DataSourceType.NOTION) {
    return <NotionIcon className="shrink-0" type="page" src={notionIcon} />
  }

  return null
}

const IndexingProgressItem: FC<IndexingProgressItemProps> = ({
  detail,
  name,
  sourceType,
  notionIcon,
}) => {
  const { t } = useTranslation(['common', 'datasetDocuments'])
  const nameId = useId()
  const isEmbedding = isSourceEmbedding(detail)
  const percent = getSourcePercent(detail)
  const isError = detail.indexing_status === 'error'
  const statusText =
    detail.indexing_status === 'completed'
      ? t(($) => $['embedding.completed'], { ns: 'datasetDocuments' })
      : isError
        ? `${t(($) => $.error, { ns: 'common' })}: ${detail.error || ''}`
        : detail.indexing_status === 'paused'
          ? t(($) => $['embedding.paused'], { ns: 'datasetDocuments' })
          : ''

  return (
    <div
      className={cn(
        'relative h-6.5 overflow-hidden rounded-md bg-components-progress-bar-bg',
        isError && 'bg-state-destructive-hover-alt',
      )}
    >
      {isEmbedding && (
        <div
          className="absolute top-0 left-0 h-full min-w-0.5 border-r-2 border-r-components-progress-bar-progress-highlight bg-components-progress-bar-progress"
          role="progressbar"
          aria-labelledby={nameId}
          aria-valuemin={0}
          aria-valuemax={100}
          aria-valuenow={percent}
          style={{ width: `${percent}%` }}
        />
      )}
      <span role="status" aria-atomic="true" className="sr-only">
        {statusText && `${name}: ${statusText}`}
      </span>
      <div className="z-1 flex h-full items-center gap-1 pr-2 pl-1.5">
        <SourceTypeIcon sourceType={sourceType} name={name} notionIcon={notionIcon} />
        <div className="flex w-0 grow items-center gap-1" title={name}>
          <div id={nameId} className="truncate system-xs-medium text-text-secondary">
            {name}
          </div>
          <PriorityLabel className="ml-0" />
        </div>
        {isEmbedding && <div className="shrink-0 text-xs text-text-secondary">{`${percent}%`}</div>}
        <StatusIcon status={detail.indexing_status} error={detail.error ?? undefined} />
      </div>
    </div>
  )
}

export default IndexingProgressItem