import React, { useState, useRef } from 'react'; import { useNavigate } from 'react-router-dom'; import styles from './UploadPhotoBlock.module.css'; import { getTranslation } from '../../constants/translations'; interface UploadPhotoBlockProps { onPhotoSelect?: (file: File) => void; previewUrl?: string; } const UploadPhotoBlock: React.FC = ({ onPhotoSelect, previewUrl }) => { const navigate = useNavigate(); const fileInputRef = useRef(null); const [isDragging, setIsDragging] = useState(false); const handleFileSelect = (file: File) => { if (file && file.type.startsWith('image/')) { // Очищаем предыдущие данные изображения при загрузке нового localStorage.removeItem('stickerPreviewUrl'); localStorage.removeItem('stickerImageData'); onPhotoSelect?.(file); navigate('/crop-photo', { state: { file } }); } }; const handleClick = () => { fileInputRef.current?.click(); }; const handleDrop = (e: React.DragEvent) => { e.preventDefault(); setIsDragging(false); const file = e.dataTransfer.files[0]; if (file) { handleFileSelect(file); } }; const handleDragOver = (e: React.DragEvent) => { e.preventDefault(); setIsDragging(true); }; const handleDragLeave = () => { setIsDragging(false); }; const handleFileChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { handleFileSelect(file); } }; return (
{previewUrl ? (
{getTranslation('preview_alt')}
{getTranslation('change_photo')}
) : ( <>
📷
{getTranslation('upload_photo_block_title')} {getTranslation('upload_photo_block_subtitle')}
)}
); }; export default UploadPhotoBlock;