Skip to content

Commit

Permalink
fix: add support of ipfs urls in useStarkProfile (#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
fracek authored Nov 29, 2024
2 parents 78e864e + 4459282 commit 5229e0a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: useStarkProfile support ipfs image urls",
"packageName": "@starknet-react/core",
"email": "[email protected]",
"dependentChangeType": "patch"
}
16 changes: 11 additions & 5 deletions packages/core/src/hooks/use-stark-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,15 @@ const parseBase64Image = (metadata: string): string => {
return JSON.parse(atob(metadata.split(",")[1].slice(0, -1))).image;
};

const fetchImageUrl = async (url: string): Promise<string> => {
const parseImageUrl = (url: string): string => {
return url.startsWith("ipfs://")
? url.replace("ipfs://", "https://ipfs.io/ipfs/")
: url;
};

const fetchImageUrl = async (url: string): Promise<string | undefined> => {
try {
const response = await fetch(url);
const response = await fetch(parseImageUrl(url));

if (!response.ok) {
throw new Error("Network response was not ok");
Expand All @@ -340,13 +346,13 @@ const fetchImageUrl = async (url: string): Promise<string> => {

// Check if the "image" key exists and is not null
if (data.image) {
return data.image;
return parseImageUrl(data.image);
}

return "Image is not set";
return undefined;
} catch (error) {
console.error("There was a problem fetching the image URL:", error);
return "Error fetching data";
return undefined;
}
};

Expand Down

0 comments on commit 5229e0a

Please sign in to comment.