Skip to content

Commit

Permalink
fix(#2546): handle errors on fetching drep images
Browse files Browse the repository at this point in the history
  • Loading branch information
MSzalowski committed Dec 17, 2024
1 parent b3f17b4 commit 4ebd529
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ changes.

### Fixed

-
- Fix some non-ipfs related errors while fetching the DRep images [Issue 2546](https://github.com/IntersectMBO/govtool/issues/2546)

### Changed

Expand Down
9 changes: 5 additions & 4 deletions govtool/frontend/src/pages/DRepDirectoryContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "@hooks";
import { DataActionsBar, EmptyStateDrepDirectory } from "@molecules";
import { AutomatedVotingOptions, DRepCard } from "@organisms";
import { correctAdaFormat, isSameDRep } from "@utils";
import { correctAdaFormat, isSameDRep, uniqBy } from "@utils";
import { DRepData, DRepListSort, DRepStatus } from "@models";
import {
AutomatedVotingOptionCurrentDelegation,
Expand Down Expand Up @@ -106,8 +106,9 @@ export const DRepDirectoryContent: FC<DRepDirectoryContentProps> = ({

const ada = correctAdaFormat(votingPower);

const listedDRepsWithoutYourself = dRepList?.filter(
(dRep) => !dRep.doNotList && !isSameDRep(dRep, myDRepId),
const listedDRepsWithoutYourself = uniqBy(
dRepList?.filter((dRep) => !dRep.doNotList && !isSameDRep(dRep, myDRepId)),
"view",
);
const dRepListToDisplay =
yourselfDRep && showYourselfDRep
Expand Down Expand Up @@ -212,7 +213,7 @@ export const DRepDirectoryContent: FC<DRepDirectoryContentProps> = ({
>
{dRepList?.length === 0 && <EmptyStateDrepDirectory />}
{dRepListToDisplay?.map((dRep) => (
<Box key={dRep.drepId} component="li" sx={{ listStyle: "none" }}>
<Box key={dRep.view} component="li" sx={{ listStyle: "none" }}>
<DRepCard
dRep={dRep}
isConnected={!!isConnected}
Expand Down
1 change: 1 addition & 0 deletions govtool/frontend/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export * from "./removeMarkdown";
export * from "./setProtocolParameterUpdate";
export * from "./testIdFromLabel";
export * from "./wait";
export * from "./uniqBy";
16 changes: 13 additions & 3 deletions govtool/frontend/src/utils/mapDtoToDrep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import {
import { postValidate } from "@/services";
import { fixViewForScriptBasedDRep } from "./dRep";

const imageFetchDefaultOptions: RequestInit = {
mode: "no-cors",
};

export const mapDtoToDrep = async (dto: DrepDataDTO): Promise<DRepData> => {
const emptyMetadata = {
paymentAddress: null,
Expand Down Expand Up @@ -34,9 +38,13 @@ export const mapDtoToDrep = async (dto: DrepDataDTO): Promise<DRepData> => {
: dto.imageUrl,
isIPFSImage
? {
...imageFetchDefaultOptions,
headers: { project_id: import.meta.env.VITE_IPFS_PROJECT_ID },
}
: {},
: // set request mode no-cors
{
...imageFetchDefaultOptions,
},
)
.then(async (res) => {
const blob = await res.blob();
Expand All @@ -46,8 +54,10 @@ export const mapDtoToDrep = async (dto: DrepDataDTO): Promise<DRepData> => {
base64Image = reader.result;
};
})
.catch((e) => {
console.error("Fetching the DRep image failed, reason: ", e);
.catch((error) => {
if (import.meta.env.VITE_IS_DEV) {
console.error("Error fetching image", error);
}
});
}

Expand Down
40 changes: 40 additions & 0 deletions govtool/frontend/src/utils/tests/uniqBy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { uniqBy } from "../uniqBy";

describe("uniqBy", () => {
it("should return an array of unique elements based on the specified key", () => {
const arr = [
{ id: 1, name: "John" },
{ id: 2, name: "Jane" },
{ id: 3, name: "John" },
{ id: 4, name: "Jane" },
];

const result = uniqBy(arr, "name");

expect(result).toEqual([
{ id: 1, name: "John" },
{ id: 2, name: "Jane" },
]);
});

it("should handle empty input array", () => {
const arr: never[] = [];

const result = uniqBy(arr, "name");

expect(result).toEqual([]);
});

it("should handle empty key", () => {
const arr = [
{ id: 1, name: "John" },
{ id: 2, name: "Jane" },
];

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const result = uniqBy(arr, "");

expect(result).toEqual(arr);
});
});
14 changes: 14 additions & 0 deletions govtool/frontend/src/utils/uniqBy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Returns an array of unique elements from the input array based on the specified key.
* @param arr - The input array.
* @param key - The key to determine uniqueness.
* @returns An array of unique elements.
* @template T - The type of elements in the array.
*/
export const uniqBy = <T>(arr: T[], key: keyof T): T[] => {
const map = new Map<T[keyof T], T>();
arr.forEach((item) => {
map.set(item[key], item);
});
return Array.from(map.values());
};

0 comments on commit 4ebd529

Please sign in to comment.