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.

5.4 KB
Raw Blame
'use client'

import type { KnowledgeFsUploadPhase } from '../upload/knowledge-fs-upload'
import type { DocumentUploadIssue } from '../upload/policy'
import { cn } from '@langgenius/dify-ui/cn'
import { useId, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { createRequestId } from '../request-id'
import { DocumentUploadFileList } from '../upload/file-list'
import { DocumentUploadPlanNotice } from '../upload/plan-notice'
import {
  DOCUMENT_UPLOAD_ACCEPT,
  documentUploadIssue,
  uniqueDocumentUploadFiles,
} from '../upload/policy'

export type QueuedUpload = {
  file: File
  id: string
  issue?: DocumentUploadIssue
  stagedUploadId?: string
  stagingFailed?: boolean
}

function createQueuedUpload(file: File, fileSizeLimitMb: number): QueuedUpload {
  return {
    file,
    id: createRequestId(),
    issue: documentUploadIssue(file, fileSizeLimitMb),
  }
}

function mergeFiles(current: QueuedUpload[], files: File[], fileSizeLimitMb: number) {
  return [
    ...current,
    ...uniqueDocumentUploadFiles(
      current.map(({ file }) => file),
      files,
    ).map((file) => createQueuedUpload(file, fileSizeLimitMb)),
  ]
}

function revealDropzone(element: HTMLLabelElement | null) {
  element?.scrollIntoView({ block: 'nearest', inline: 'nearest' })
}

export function CreateUploadQueue({
  disabled,
  fileSizeLimitMb,
  supportBatchUpload,
  uploadPhases = new Map(),
  uploading,
  uploads,
  onChange,
}: {
  disabled: boolean
  fileSizeLimitMb: number
  supportBatchUpload: boolean
  uploadPhases?: ReadonlyMap<File, KnowledgeFsUploadPhase>
  uploading: boolean
  uploads: QueuedUpload[]
  onChange: (uploads: QueuedUpload[]) => void
}) {
  const { t } = useTranslation(['knowledgeSpace', 'knowledgeCreate'])
  const inputId = useId()
  const [dragging, setDragging] = useState(false)
  const validUploadCount = uploads.filter(({ issue }) => !issue).length

  const addFiles = (files: File[]) => {
    if (!disabled && files.length) onChange(mergeFiles(uploads, files, fileSizeLimitMb))
  }

  return (
    <div className="mx-[14.5px] mt-[-3.5px] mb-[14.5px] space-y-3">
      <input
        id={inputId}
        className="peer sr-only"
        multiple={supportBatchUpload}
        type="file"
        accept={DOCUMENT_UPLOAD_ACCEPT}
        aria-label={t(($) => $.uploadFiles)}
        disabled={disabled}
        onChange={(event) => {
          addFiles([...(event.currentTarget.files ?? [])])
          event.currentTarget.value = ''
        }}
      />
      <label
        ref={revealDropzone}
        htmlFor={inputId}
        className={cn(
          'flex min-h-16 scroll-mb-4 flex-col items-center justify-center gap-1 rounded-[10px] border border-dashed border-divider-regular bg-background-section px-4 py-2 text-center outline-hidden transition-colors motion-reduce:transition-none',
          'peer-focus-visible:ring-2 peer-focus-visible:ring-state-accent-solid',
          disabled
            ? 'cursor-not-allowed opacity-60'
            : 'cursor-pointer hover:border-components-option-card-option-selected-border hover:bg-state-base-hover has-focus-visible:ring-2 has-focus-visible:ring-state-accent-solid',
          dragging && 'border-components-option-card-option-selected-border bg-state-base-hover',
        )}
        onDragEnter={(event) => {
          event.preventDefault()
          if (!disabled) setDragging(true)
        }}
        onDragLeave={(event) => {
          event.preventDefault()
          if (!event.currentTarget.contains(event.relatedTarget as Node | null)) setDragging(false)
        }}
        onDragOver={(event) => event.preventDefault()}
        onDrop={(event) => {
          event.preventDefault()
          setDragging(false)
          addFiles([...event.dataTransfer.files])
        }}
      >
        <span className="flex items-center gap-1.5 system-xs-medium text-text-primary">
          <span
            aria-hidden
            className={cn(
              'size-5 text-text-tertiary',
              uploading
                ? 'i-ri-loader-2-line animate-spin motion-reduce:animate-none'
                : 'i-ri-upload-cloud-2-line',
            )}
          />
          {uploading
            ? t(($) => $.uploadingFiles, { ns: 'knowledgeCreate' })
            : t(($) => $.uploadDropZoneTitle, { ns: 'knowledgeCreate' })}
        </span>
        <span className="system-2xs-medium text-text-tertiary">
          {t(($) => $.documentUploadFormats, { size: fileSizeLimitMb })}
        </span>
      </label>

      <DocumentUploadPlanNotice fileSizeLimitMb={fileSizeLimitMb} />

      {!!uploads.length && (
        <section aria-label={t(($) => $.uploadFiles)}>
          <h3 className="system-sm-semibold text-text-primary">
            {t(($) => $.selectedFiles, {
              ns: 'knowledgeCreate',
              total: uploads.length,
              valid: validUploadCount,
            })}
          </h3>
          <DocumentUploadFileList
            ariaLabel={t(($) => $.uploadFiles)}
            className="mt-2"
            disabled={disabled}
            fileSizeLimitMb={fileSizeLimitMb}
            idleStatus={t(($) => $.uploadCharactersUnavailable, { ns: 'knowledgeCreate' })}
            items={uploads}
            uploadProgress={uploadPhases}
            variant="compact"
            onRemove={(upload) =>
              onChange(uploads.filter((candidate) => candidate.id !== upload.id))
            }
          />
        </section>
      )}
    </div>
  )
}