-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from hackdays-io/feature/front-hooks-ipfs
Feature/front hooks ipfs
- Loading branch information
Showing
2 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { useState } from "react"; | ||
import { PinataSDK } from "pinata-web3"; | ||
import { Readable } from "stream"; | ||
|
||
export const useUploadMetadataToIpfs = () => { | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
const uploadMetadataToIpfs = async ({ | ||
name, | ||
description, | ||
responsibilities, | ||
authorities, | ||
eligibility, | ||
toggle, | ||
}: { | ||
name: string; | ||
description: string; | ||
responsibilities: string; | ||
authorities: string; | ||
eligibility: boolean; | ||
toggle: boolean; | ||
}) => { | ||
setIsLoading(true); | ||
|
||
try { | ||
const pinata = new PinataSDK({ pinataJwt: process.env.VITE_PINATA_JWT }); | ||
|
||
const upload = await pinata.upload.json({ | ||
type: "1.0", | ||
data: { | ||
name, | ||
description, | ||
responsibilities, | ||
authorities, | ||
eligibility, | ||
toggle, | ||
}, | ||
}); | ||
|
||
console.log("CID:", upload.IpfsHash); | ||
console.log("URI:", `ipfs://${upload.IpfsHash}`); | ||
} catch (error) { | ||
console.error(error); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
return { uploadMetadataToIpfs, isLoading }; | ||
}; | ||
|
||
export const useUploadImageToIpfs = () => { | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [imageFile, setImageFile] = useState<File | null>(null); | ||
|
||
const uploadImageToIpfs = async () => { | ||
if (!imageFile) return; | ||
|
||
setIsLoading(true); | ||
|
||
try { | ||
const pinata = new PinataSDK({ pinataJwt: process.env.VITE_PINATA_JWT }); | ||
|
||
const buffer = await imageFile.arrayBuffer(); | ||
const stream = Readable.from(Buffer.from(buffer)); | ||
|
||
const upload = await pinata.upload.stream(stream, { | ||
metadata: { name: `TobanFrontend_${new Date().getTime()}` }, | ||
}); | ||
|
||
console.log("CID:", upload.IpfsHash); | ||
console.log("URI:", `ipfs://${upload.IpfsHash}`); | ||
} catch (error) { | ||
console.error(error); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
return { uploadImageToIpfs, setImageFile, isLoading }; | ||
}; |