supermemory/apps/web/app/lib/hooks/use-upload-file.ts
2025-01-20 17:50:45 -07:00

30 lines
732 B
TypeScript

import { useFetcher } from "@remix-run/react";
import { useFetcherWithPromise } from "./use-fetcher-with-promise";
export function useUploadFile() {
const fetcher = useFetcherWithPromise<{ url: string, error?: string }>();
const uploadFile = async (file: File) => {
const formData = new FormData();
formData.append("file", file);
formData.append("name", file.name);
const response = await fetcher.submit(formData, {
method: "post",
action: "/action/upload",
encType: "multipart/form-data",
})
return {
url: response?.url,
error: response?.error,
};
};
const isUploading = fetcher.state === "submitting" || fetcher.state === "loading";
return {
uploadFile,
isUploading,
...fetcher,
};
}