-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
167 additions
and
18 deletions.
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
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
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,84 @@ | ||
import { useCallback, useRef, useState } from "react"; | ||
import { useRecoilState } from "recoil"; | ||
import { handleUploadFileToIPFS } from "../utils/uploadFileToIpfs"; | ||
|
||
interface TokenLogoUploadProps { | ||
onChange: (imageUrl: string) => void | ||
} | ||
|
||
function isValidUrl(url: string | undefined) { | ||
// Regular expression for matching a URL | ||
const urlRegex = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/i; | ||
|
||
if (!url) return false | ||
// Test if the URL matches the regex | ||
return urlRegex.test(url); | ||
} | ||
|
||
const TokenLogoUpload: React.FC<TokenLogoUploadProps> = ({onChange}) => { | ||
const fileInputRef = useRef<HTMLInputElement>(null); | ||
const [imageUrl, setImageUrl] = useState("https://assets.deform.cc/default/logo15.png") | ||
|
||
const handlePhotoUpload = useCallback(async (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const file = event.target.files && event.target.files[0]; | ||
if (file) { | ||
console.log("file", file) | ||
|
||
try { | ||
const ipfsImageHash = await handleUploadFileToIPFS(file, new Date().toTimeString()) | ||
const url = `https://ipfs.io/ipfs/${ipfsImageHash}` | ||
console.log(url) | ||
setImageUrl(url) | ||
onChange(url) | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
} | ||
}, []); | ||
|
||
const handleDragOver = (event: React.DragEvent<HTMLDivElement>) => { | ||
event.preventDefault(); | ||
}; | ||
|
||
// const handleDrop = (event: React.DragEvent<HTMLDivElement>) => { | ||
// event.preventDefault(); | ||
// const file = event.dataTransfer.files && event.dataTransfer.files[0]; | ||
// if (file) { | ||
// setformImage(file); | ||
// } | ||
// }; | ||
|
||
const handleProfileClick = () => { | ||
if (fileInputRef.current) { | ||
fileInputRef.current.click(); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="w-full flex items-center" > | ||
<div | ||
className="h-20 w-20 bg-white rounded-full relative" | ||
// onDrop={handleDrop} | ||
onDragOver={handleDragOver} | ||
> | ||
<div className={`h-20 w-20 rounded-full hover:opacity-80 overflow-hidden cursor-pointer`}> | ||
<img | ||
src={imageUrl} | ||
alt="Profile" | ||
className="h-20 w-20 rounded-full" | ||
onClick={handleProfileClick} | ||
/> | ||
</div> | ||
<input | ||
type="file" | ||
accept="image/*" | ||
className="hidden" | ||
ref={fileInputRef} | ||
onChange={handlePhotoUpload} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default TokenLogoUpload; |
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
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,36 @@ | ||
import axios from "axios"; | ||
import FormData from "form-data"; | ||
import { pinataApiKey, pinataApiSecret } from "./constants"; | ||
|
||
export const handleUploadFileToIPFS = async (file: File, postID: string) : Promise<string | null> => { | ||
const formData = new FormData(); | ||
formData.append("file", file); | ||
const metadata = JSON.stringify({ | ||
name: postID, | ||
}); | ||
formData.append("pinataMetadata", metadata); | ||
|
||
const options = JSON.stringify({ | ||
cidVersion: 0, | ||
}); | ||
formData.append("pinataOptions", options); | ||
|
||
try { | ||
const res = await axios.post( | ||
"https://api.pinata.cloud/pinning/pinFileToIPFS", | ||
formData, | ||
{ | ||
maxBodyLength: -1, | ||
headers: { | ||
"Content-Type": `multipart/form-data`, | ||
pinata_api_key: pinataApiKey, | ||
pinata_secret_api_key: pinataApiSecret, | ||
}, | ||
} | ||
); | ||
return res.data.IpfsHash; | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
return null | ||
}; |
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 |
---|---|---|
|
@@ -1035,11 +1035,6 @@ | |
"@babel/plugin-transform-react-jsx-source" "^7.22.5" | ||
react-refresh "^0.14.0" | ||
|
||
Buffer@^0.0.0: | ||
version "0.0.0" | ||
resolved "https://registry.yarnpkg.com/Buffer/-/Buffer-0.0.0.tgz#82cf8e986a2109ff6d1d6f1c436e47d07127aea4" | ||
integrity sha512-+zdncl8lI5TCkARStn9F1BwcuJYofYmD0oEHe5FNfCvGfeDJwf6+dSikCdQN6BMXXmHMhNNUagBN367WST1AIQ== | ||
|
||
abort-controller@^3.0.0: | ||
version "3.0.0" | ||
resolved "https://registry.yarnpkg.com/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392" | ||
|
@@ -1259,7 +1254,7 @@ classnames@^2.3.2: | |
resolved "https://registry.yarnpkg.com/classnames/-/classnames-2.3.2.tgz#351d813bf0137fcc6a76a16b88208d2560a0d924" | ||
integrity sha512-CSbhY4cFEJRe6/GQzIk5qXZ4Jeg5pcsP7b5peFSDpffpe1cqjASH/n9UTjBwOp6XpMSTwQ8Za2K5V02ueA7Tmw== | ||
|
||
clsx@^1.2.1: | ||
clsx@^1.1.1, clsx@^1.2.1: | ||
version "1.2.1" | ||
resolved "https://registry.yarnpkg.com/clsx/-/clsx-1.2.1.tgz#0ddc4a20a549b59c93a4116bb26f5294ca17dc12" | ||
integrity sha512-EcR6r5a8bj6pu3ycsa/E/cKVGuTgZJZdsyUYHOksG/UHIiKfjxzRxYJpyVBwYaQeOvghal9fcc4PidlgzugAQg== | ||
|
@@ -2372,6 +2367,13 @@ [email protected]: | |
dependencies: | ||
"@remix-run/router" "1.7.1" | ||
|
||
react-toastify@^9.1.3: | ||
version "9.1.3" | ||
resolved "https://registry.yarnpkg.com/react-toastify/-/react-toastify-9.1.3.tgz#1e798d260d606f50e0fab5ee31daaae1d628c5ff" | ||
integrity sha512-fPfb8ghtn/XMxw3LkxQBk3IyagNpF/LIKjOBflbexr2AWxAH1MJgvnESwEwBn9liLFXgTKWgBSdZpw9m4OTHTg== | ||
dependencies: | ||
clsx "^1.1.1" | ||
|
||
react-transition-group@^4.4.5: | ||
version "4.4.5" | ||
resolved "https://registry.yarnpkg.com/react-transition-group/-/react-transition-group-4.4.5.tgz#e53d4e3f3344da8521489fbef8f2581d42becdd1" | ||
|