Skip to content

Commit cd26b58

Browse files
authored
Merge pull request #2548 from IntersectMBO/fix/2546-fetching-dreps-images-is-throwing-different-errors-in-console
fix(#2546): handle errors on fetching drep images
2 parents 5127326 + 1f31e34 commit cd26b58

File tree

6 files changed

+60
-7
lines changed

6 files changed

+60
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ changes.
1717
### Fixed
1818

1919
- Move matomo initalization out of the react code
20+
- Fix some non-ipfs related errors while fetching the DRep images [Issue 2546](https://github.com/IntersectMBO/govtool/issues/2546)
2021

2122
### Changed
2223

govtool/frontend/src/pages/DRepDirectoryContent.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from "@hooks";
1515
import { DataActionsBar, EmptyStateDrepDirectory } from "@molecules";
1616
import { AutomatedVotingOptions, DRepCard } from "@organisms";
17-
import { correctAdaFormat, isSameDRep } from "@utils";
17+
import { correctAdaFormat, isSameDRep, uniqBy } from "@utils";
1818
import { DRepData, DRepListSort, DRepStatus } from "@models";
1919
import {
2020
AutomatedVotingOptionCurrentDelegation,
@@ -106,8 +106,9 @@ export const DRepDirectoryContent: FC<DRepDirectoryContentProps> = ({
106106

107107
const ada = correctAdaFormat(votingPower);
108108

109-
const listedDRepsWithoutYourself = dRepList?.filter(
110-
(dRep) => !dRep.doNotList && !isSameDRep(dRep, myDRepId),
109+
const listedDRepsWithoutYourself = uniqBy(
110+
dRepList?.filter((dRep) => !dRep.doNotList && !isSameDRep(dRep, myDRepId)),
111+
"view",
111112
);
112113
const dRepListToDisplay =
113114
yourselfDRep && showYourselfDRep
@@ -212,7 +213,7 @@ export const DRepDirectoryContent: FC<DRepDirectoryContentProps> = ({
212213
>
213214
{dRepList?.length === 0 && <EmptyStateDrepDirectory />}
214215
{dRepListToDisplay?.map((dRep) => (
215-
<Box key={dRep.drepId} component="li" sx={{ listStyle: "none" }}>
216+
<Box key={dRep.view} component="li" sx={{ listStyle: "none" }}>
216217
<DRepCard
217218
dRep={dRep}
218219
isConnected={!!isConnected}

govtool/frontend/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ export * from "./removeMarkdown";
3434
export * from "./setProtocolParameterUpdate";
3535
export * from "./testIdFromLabel";
3636
export * from "./wait";
37+
export * from "./uniqBy";

govtool/frontend/src/utils/mapDtoToDrep.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import {
77
import { postValidate } from "@/services";
88
import { fixViewForScriptBasedDRep } from "./dRep";
99

10+
const imageFetchDefaultOptions: RequestInit = {
11+
mode: "no-cors",
12+
};
13+
1014
export const mapDtoToDrep = async (dto: DrepDataDTO): Promise<DRepData> => {
1115
const emptyMetadata = {
1216
paymentAddress: null,
@@ -34,9 +38,13 @@ export const mapDtoToDrep = async (dto: DrepDataDTO): Promise<DRepData> => {
3438
: dto.imageUrl,
3539
isIPFSImage
3640
? {
41+
...imageFetchDefaultOptions,
3742
headers: { project_id: import.meta.env.VITE_IPFS_PROJECT_ID },
3843
}
39-
: {},
44+
: // set request mode no-cors
45+
{
46+
...imageFetchDefaultOptions,
47+
},
4048
)
4149
.then(async (res) => {
4250
const blob = await res.blob();
@@ -46,8 +54,10 @@ export const mapDtoToDrep = async (dto: DrepDataDTO): Promise<DRepData> => {
4654
base64Image = reader.result;
4755
};
4856
})
49-
.catch((e) => {
50-
console.error("Fetching the DRep image failed, reason: ", e);
57+
.catch((error) => {
58+
if (import.meta.env.VITE_IS_DEV) {
59+
console.error("Error fetching image", error);
60+
}
5161
});
5262
}
5363

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { uniqBy } from "../uniqBy";
2+
3+
describe("uniqBy", () => {
4+
it("should return an array of unique elements based on the specified key", () => {
5+
const arr = [
6+
{ id: 1, name: "John" },
7+
{ id: 2, name: "Jane" },
8+
{ id: 3, name: "John" },
9+
{ id: 4, name: "Jane" },
10+
];
11+
12+
const result = uniqBy(arr, "name");
13+
expect(result).toEqual([
14+
{ id: 3, name: "John" },
15+
{ id: 4, name: "Jane" },
16+
]);
17+
});
18+
19+
it("should handle empty input array", () => {
20+
const arr: never[] = [];
21+
22+
const result = uniqBy(arr, "name");
23+
24+
expect(result).toEqual([]);
25+
});
26+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Returns an array of unique elements from the input array based on the specified key.
3+
* @param arr - The input array.
4+
* @param key - The key to determine uniqueness.
5+
* @returns An array of unique elements.
6+
* @template T - The type of elements in the array.
7+
*/
8+
export const uniqBy = <T>(arr: T[], key: keyof T): T[] => {
9+
const map = new Map<T[keyof T], T>();
10+
arr.forEach((item) => {
11+
map.set(item[key], item);
12+
});
13+
return Array.from(map.values());
14+
};

0 commit comments

Comments
 (0)