Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions client/app/components/Fragment/CandidateDescription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,32 @@ const CandidateDescription = ({ IpfsHash }: { IpfsHash: String }) => {
name: "",
description: "",
});
const [error, setError] = useState<string | null>(null);

const getIpfsFile = async () => {
const res = await fetchFileFromIPFS(IpfsHash);
setipfsFile(res);
try {
const res = await fetchFileFromIPFS(IpfsHash);
if (res && typeof res === 'object') {
setipfsFile(res);
}
} catch (err) {
// Handle error gracefully - set error state but don't crash the component
setError("Failed to load description");
console.error("Error fetching candidate description:", err);
}
};

useEffect(() => {
ipfsFile.name === "" && getIpfsFile();
if (ipfsFile.name === "" && !error) {
getIpfsFile();
}
}, []);
return <p>{ipfsFile?.description}</p>;

if (error) {
return <p className="text-gray-400 italic">{error}</p>;
}

return <p>{ipfsFile?.description || ""}</p>;
};

export default CandidateDescription;
28 changes: 23 additions & 5 deletions client/app/helpers/fetchFileFromIPFS.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
const GATEWAY = "orange-confused-boar-516.mypinata.cloud";
const JWT = process.env.NEXT_PUBLIC_PINATA_JWT;

/**
* Fetches a file from IPFS using the provided CID (Content Identifier)
* @param CID - The IPFS Content Identifier (hash) of the file to fetch
* @returns Promise that resolves to the parsed JSON content of the file
* @throws Error if the fetch fails, response is not ok, or JSON parsing fails
*/
export async function fetchFileFromIPFS(CID: String) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Use primitive string instead of String wrapper type.

TypeScript best practice is to use the lowercase primitive type string rather than the String object wrapper type. The String type refers to the JavaScript object wrapper, which is rarely what you want.

Proposed fix
-export async function fetchFileFromIPFS(CID: String) {
+export async function fetchFileFromIPFS(CID: string) {
🤖 Prompt for AI Agents
In `@client/app/helpers/fetchFileFromIPFS.ts` at line 9, The function
fetchFileFromIPFS currently types its CID parameter as the String object
wrapper; change the parameter type to the primitive string (i.e., export async
function fetchFileFromIPFS(CID: string)) in the fetchFileFromIPFS declaration
and update any callers/type annotations that reference this signature so they
use the primitive string type as well.

const url = `https://${GATEWAY}/ipfs/${CID}`;
try {
const request = await fetch(url);
const response = await request.json();
return response;
const response = await fetch(url);

// Check if the HTTP response was successful (status 200-299)
if (!response.ok) {
throw new Error(
`Failed to fetch file from IPFS: ${response.status} ${response.statusText}`
);
}

// Parse JSON response
const data = await response.json();
return data;
} catch (error) {
console.log(error);
// Re-throw the error with additional context if it's not already an Error
if (error instanceof Error) {
throw new Error(`Error fetching IPFS file (CID: ${CID}): ${error.message}`);
}
throw new Error(`Unknown error fetching IPFS file (CID: ${CID})`);
}
}