Skip to content

Instantly share code, notes, and snippets.

@pontusab
Created October 25, 2023 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pontusab/44eddb72bcceaafbad725b134f530fa6 to your computer and use it in GitHub Desktop.
Save pontusab/44eddb72bcceaafbad725b134f530fa6 to your computer and use it in GitHub Desktop.
useUpload.ts
import { getSupabaseBrowserClient } from "@midday/supabase/browser-client";
import { getUserDetails } from "@midday/supabase/queries";
import { upload } from "@midday/supabase/storage";
import { useState } from "react";
export function useUpload() {
const supabase = getSupabaseBrowserClient();
const [isLoading, setLoading] = useState(false);
const uploadFile = async ({ bucketName, file, path }) => {
setLoading(true);
const { data: userData } = await getUserDetails(supabase);
const basePath = `${bucketName}/${userData?.team_id}/${path}`;
const filePath = `${userData?.team_id}/${path}/${file.name}`;
const url = await upload(supabase, {
path: basePath,
file,
});
setLoading(false);
return {
url,
path: filePath,
};
};
return {
uploadFile,
isLoading,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment