import type { ClientApi } from "../client/index.js"; import { useConvexClient } from "convex-svelte"; import { uploadWithProgress } from "../client/upload.js"; /** * A hook that allows you to upload a file to R2. * * @param api - The client API object from the R2 component, including at least * `generateUploadUrl` and `syncMetadata`. * @returns A function that uploads a file to R2. */ export function useUploadFile( api: Pick, ) { const client = useConvexClient(); return async ( file: File, options?: { onProgress?: (progress: { loaded: number; total: number }) => void; }, ) => { const { url, key } = await client.mutation(api.generateUploadUrl, {}); await uploadWithProgress(url, file, options?.onProgress); await client.mutation(api.syncMetadata, { key }); return key; }; }