UNPKG

@replyke/core

Version:

Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.

59 lines 2.17 kB
import { useCallback } from "react"; import useAxiosPrivate from "../../config/useAxiosPrivate"; import useProject from "../projects/useProject"; // Type guard to detect if it's a real browser File function isBrowserFile(file) { return typeof File !== "undefined" && file instanceof File; } function useUploadFile() { const axios = useAxiosPrivate(); const { projectId } = useProject(); const uploadFile = useCallback(async (file, pathParts, options) => { if (!projectId) { throw new Error("No projectId available."); } if (!file || !pathParts || !Array.isArray(pathParts)) { throw new Error("Invalid arguments. File and pathParts are required."); } const formData = new FormData(); // Append file (browser or React Native) if (isBrowserFile(file)) { formData.append("file", file, file.name); } else { formData.append("file", { uri: file.uri, type: file.type || "application/octet-stream", name: file.name, }); } // Append pathParts formData.append("pathParts", JSON.stringify(pathParts)); // Append optional associations if (options?.entityId) { formData.append("entityId", options.entityId); } if (options?.commentId) { formData.append("commentId", options.commentId); } if (options?.spaceId) { formData.append("spaceId", options.spaceId); } if (options?.position !== undefined) { formData.append("position", options.position.toString()); } if (options?.metadata) { formData.append("metadata", JSON.stringify(options.metadata)); } // Make the request const response = await axios.post(`/${projectId}/storage`, formData, { headers: { "Content-Type": "multipart/form-data", }, }); return response.data; }, [projectId, axios]); return uploadFile; } export default useUploadFile; //# sourceMappingURL=useUploadFile.js.map