import { useState, useRef } from "react"; import { Button } from "@/components/ui/button"; import { User, X, Loader2 } from "lucide-react"; import axios from "axios"; interface BytescaleUploaderProps { value: string; onChange: (url: string) => void; folderPath: | "/doctors" | "/departments" | "/news" | "/blog" | "/health-packages"; } export function BytescaleUploader({ value, onChange, folderPath, }: BytescaleUploaderProps) { const baseURL = import.meta.env.VITE_API_URL; const [isUploading, setIsUploading] = useState(false); const fileInputRef = useRef(null); const onFileSelected = async (event: React.ChangeEvent) => { const file = event.target.files?.[0]; if (!file) return; if (file.size > 5 * 1024 * 1024) { alert("File is too large (Max 5MB)"); return; } setIsUploading(true); const formData = new FormData(); formData.append("file", file); formData.append("folderPath", folderPath); try { const response = await axios.post(`${baseURL}/upload`, formData, { headers: { "Content-Type": "multipart/form-data", }, }); const { fileUrl } = response.data; onChange(fileUrl); } catch (e: any) { console.error("Upload Error:", e); const errorMessage = e.response?.data?.error || e.message || "Upload failed"; alert(`Upload Error: ${errorMessage}`); } finally { setIsUploading(false); if (fileInputRef.current) fileInputRef.current.value = ""; } }; return (
{value ? ( <> Preview ) : (
{isUploading ? ( ) : ( )}
)}
{value && (

⚠️ Make sure to save the changes by clicking the "Save Changes" button.

)}
); }