mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-05-15 05:08:16 +08:00
55 lines
1.3 KiB
TypeScript
55 lines
1.3 KiB
TypeScript
import type { ChangeEvent, FC } from 'react'
|
|
import { useState } from 'react'
|
|
import { useLocalFileUploader } from './hooks'
|
|
import type { ImageFile } from '@/types/app'
|
|
import { ALLOW_FILE_EXTENSIONS } from '@/types/app'
|
|
|
|
type UploaderProps = {
|
|
children: (hovering: boolean) => JSX.Element
|
|
onUpload: (imageFile: ImageFile) => void
|
|
limit?: number
|
|
disabled?: boolean
|
|
}
|
|
|
|
const Uploader: FC<UploaderProps> = ({
|
|
children,
|
|
onUpload,
|
|
limit,
|
|
disabled,
|
|
}) => {
|
|
const [hovering, setHovering] = useState(false)
|
|
const { handleLocalFileUpload } = useLocalFileUploader({ limit, onUpload, disabled })
|
|
|
|
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
|
|
const file = e.target.files?.[0]
|
|
|
|
if (!file)
|
|
return
|
|
|
|
handleLocalFileUpload(file)
|
|
}
|
|
|
|
return (
|
|
<div
|
|
className='relative'
|
|
onMouseEnter={() => setHovering(true)}
|
|
onMouseLeave={() => setHovering(false)}
|
|
>
|
|
{children(hovering)}
|
|
<input
|
|
className={`
|
|
absolute block inset-0 opacity-0 text-[0] w-full
|
|
${disabled ? 'cursor-not-allowed' : 'cursor-pointer'}
|
|
`}
|
|
onClick={e => (e.target as HTMLInputElement).value = ''}
|
|
type='file'
|
|
accept={ALLOW_FILE_EXTENSIONS.map(ext => `.${ext}`).join(',')}
|
|
onChange={handleChange}
|
|
disabled={disabled}
|
|
/>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default Uploader
|