"use client"; import React, { useState, useRef } from "react"; import { Card, Spinner } from "@nextui-org/react"; import { useTranslation } from "@/shared/lib/useTranslation"; import toast from "react-hot-toast"; import { UploadIcon } from "lucide-react"; import { calculateFileSha256 } from "@/shared/utils/crypto"; interface UploadImageProps { onSave: (file: File, sha256?: string) => Promise; imageUrl?: string; setImageUrl: (imageUrl: string) => void; className?: string; description?: string; } const UploadImage: React.FC = ({ onSave, imageUrl, setImageUrl, className = "h-32 w-32", description, }) => { const { t } = useTranslation(); const [isUploading, setIsUploading] = useState(false); const fileInputRef = useRef(null); const handleImageUpload = async ( event: React.ChangeEvent ) => { const file = event.target.files?.[0]; if (!file) return; // Validate file type if (!file.type.startsWith("image/")) { toast.error(t("Please select a valid image file")); return; } // Validate file size (max 5MB) if (file.size > 5 * 1024 * 1024) { toast.error(t("Image size should be less than 5MB")); return; } setIsUploading(true); try { const sha256 = await calculateFileSha256(file); const imageUrl = await onSave(file, sha256); setImageUrl(imageUrl); } catch (error) { console.error("Upload failed:", error); toast.error(t("Failed to upload image")); } finally { setIsUploading(false); } }; const handleImageClick = () => { if (!isUploading) { fileInputRef.current?.click(); } }; return (
{imageUrl && ( Uploaded )}
{isUploading ? : } {description}
); }; export default UploadImage;