"use client" import type { IUploadStepProps } from "./UploadStep.d" import type { JSX } from "react" import { Upload } from "lucide-react" import { FunctionComponent, useCallback, useRef, useState } from "react" import cs from "classnames" import { uploadFileWithProgress } from "@shared/api/uploadFile" import { useWizard } from "@shared/context/WizardContext" import { Button } from "@shared/ui" import styles from "./UploadStep.module.scss" const ACCEPTED_VIDEO_TYPES = "video/*" const ERROR_UPLOAD_FAILED = "Не удалось загрузить файл" export const UploadStep: FunctionComponent = ({ className, }): JSX.Element => { const { projectId, setFileKey } = useWizard() const [isDragging, setIsDragging] = useState(false) const [isUploading, setIsUploading] = useState(false) const [progress, setProgress] = useState(0) const [error, setError] = useState(null) const inputRef = useRef(null) const handleUpload = useCallback( async (file: File) => { setIsUploading(true) setProgress(0) setError(null) try { const result = await uploadFileWithProgress( file, `projects/${projectId}`, setProgress, ) await setFileKey( result.file_path, result.file_id, result.filename ?? null, ) } catch { setError(ERROR_UPLOAD_FAILED) } finally { setIsUploading(false) } }, [projectId, setFileKey], ) const handleFileChange = useCallback( (e: React.ChangeEvent) => { const file = e.target.files?.[0] if (file) handleUpload(file) /* Reset input so re-selecting the same file triggers change */ e.target.value = "" }, [handleUpload], ) const handleDrop = useCallback( (e: React.DragEvent) => { e.preventDefault() setIsDragging(false) const file = e.dataTransfer.files[0] if (file) handleUpload(file) }, [handleUpload], ) const handleDragOver = useCallback((e: React.DragEvent) => { e.preventDefault() setIsDragging(true) }, []) const handleDragLeave = useCallback((e: React.DragEvent) => { e.preventDefault() setIsDragging(false) }, []) return (
inputRef.current?.click()} > {isUploading ? ( <>

Загрузка файла...

{Math.round(progress)}%

) : ( <>

Перетащите видеофайл сюда

или нажмите для выбора файла

)} {error &&

{error}

}
) }