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.

1.4 KB
Raw Blame
import type { DragEvent } from 'react'
import { useRef, useState } from 'react'

export function useImageDrop(onFile: (file: File) => void, disabled = false) {
  const [isDragActive, setIsDragActive] = useState(false)
  const depthRef = useRef(0)
  const isFileDrag = (event: DragEvent) => event.dataTransfer.types.includes('Files')
  return {
    isDragActive: !disabled && isDragActive,
    onDragEnter(event: DragEvent<HTMLDivElement>) {
      if (!isFileDrag(event)) return
      event.preventDefault()
      event.stopPropagation()
      depthRef.current += 1
      setIsDragActive(!disabled)
    },
    onDragOver(event: DragEvent<HTMLDivElement>) {
      if (!isFileDrag(event)) return
      event.preventDefault()
      event.stopPropagation()
      event.dataTransfer.dropEffect = disabled ? 'none' : 'copy'
    },
    onDragLeave(event: DragEvent<HTMLDivElement>) {
      if (!isFileDrag(event)) return
      event.preventDefault()
      event.stopPropagation()
      depthRef.current = Math.max(0, depthRef.current - 1)
      if (!depthRef.current) setIsDragActive(false)
    },
    onDrop(event: DragEvent<HTMLDivElement>) {
      if (!isFileDrag(event)) return
      event.preventDefault()
      event.stopPropagation()
      depthRef.current = 0
      setIsDragActive(false)
      const file = event.dataTransfer.files[0]
      if (file && !disabled) onFile(file)
    },
  }
}