feat: crossbell renovation - editor page

This commit is contained in:
DIYgod
2022-06-24 23:39:03 +01:00
parent fc5c57f7d8
commit 877cf5be96
9 changed files with 131 additions and 236 deletions
+14 -20
View File
@@ -1,30 +1,24 @@
import { useCallback } from "react"
import { R2_URL } from "~/lib/env"
import { trpc } from "~/lib/trpc"
import { WEB3_STORAGE_API_TOKEN } from "~/lib/env"
import { Web3Storage } from 'web3.storage'
export const useUploadFile = () => {
const { fetchQuery } = trpc.useContext()
const uploadFile = useCallback<UploadFile>(
async (blob, filename) => {
const jwt = await fetchQuery(["user.getSignedJwt"], {})
const form = new FormData()
form.append("file", blob, filename)
const res = await fetch(R2_URL, {
body: form,
method: "post",
headers: {
authorization: `Bearer ${jwt}`,
},
})
if (!res.ok) {
throw new Error(await res.text())
const file = new File([blob], filename);
const web3Storage = new Web3Storage({
token: WEB3_STORAGE_API_TOKEN,
} as any)
const cid = await web3Storage.put([file], {
name: file.name,
maxRetries: 3,
wrapWithDirectory: false,
});
return {
key: `https://gateway.ipfs.io/ipfs/${cid}`
}
const data = await res.json()
return data
},
[fetchQuery]
[]
)
return uploadFile