import { useMutation } from "@tanstack/react-query"; import { CAM_BASE } from "../utils/config"; import { toast } from "sonner"; const camBase = import.meta.env.MODE !== "development" ? CAM_BASE : CAM_BASE; const uploadFile = async (file: File) => { const form = new FormData(); form.append("upload", file, file.name); const response = await fetch(`${camBase}/upload/file-upload/3`, { method: "POST", body: form, }); if (!response.ok) { throw new Error("Cannot reach upload file endpoint"); } return response.text(); }; export const useFileUpload = () => { const mutation = useMutation({ mutationFn: (file: File) => uploadFile(file), mutationKey: ["uploadFile"], onError: (err) => toast.error(err ? err.message : ""), onSuccess: async (msg) => toast.success(msg), }); return { mutation }; };