Created
October 25, 2023 19:50
-
-
Save pontusab/44eddb72bcceaafbad725b134f530fa6 to your computer and use it in GitHub Desktop.
useUpload.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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