Skip to content

Commit 914cc2d

Browse files
committed
fix: latest data fetching
1 parent 0a5cf40 commit 914cc2d

File tree

2 files changed

+10682
-16
lines changed

2 files changed

+10682
-16
lines changed

src/scripts/retropgf6-live-data/data/retropgf6-live-data/retropgf6-live-data.json

Lines changed: 10649 additions & 1 deletion
Large diffs are not rendered by default.

src/scripts/retropgf6-live-data/retropgf6-live-data.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ query {
4545
}
4646
}) {
4747
decodedDataJson
48+
refUID
49+
time
4850
}
4951
}
5052
`;
@@ -53,7 +55,21 @@ query {
5355
query: query,
5456
});
5557

56-
const attestations = result.data.attestations.map((attestation: any) => {
58+
// Create a copy of the array and sort it by "time" in descending order
59+
const sortedAttestations = [...result.data.attestations].sort((a: any, b: any) => b.time - a.time);
60+
61+
// Filter to keep only the latest attestation for each "refUID"
62+
const uniqueAttestations = new Map();
63+
for (const attestation of sortedAttestations) {
64+
if (!uniqueAttestations.has(attestation.refUID)) {
65+
uniqueAttestations.set(attestation.refUID, attestation);
66+
}
67+
}
68+
69+
const filteredAttestations = Array.from(uniqueAttestations.values());
70+
71+
// Parse the decodedDataJson for each attestation
72+
const attestations = filteredAttestations.map((attestation: any) => {
5773
return JSON.parse(attestation.decodedDataJson);
5874
});
5975

@@ -87,22 +103,24 @@ async function fetchMetadataURL(refUid: string, client: ApolloClient<NormalizedC
87103
decodedDataJson
88104
}
89105
}`;
90-
try {
91-
const result = await client.query({
92-
query: query,
93-
});
106+
try {
107+
const result = await client.query({
108+
query: query,
109+
});
94110

95-
const attestation = result.data.attestations[0]; // Assuming you want the first attestation
96-
const parsedData = JSON.parse(attestation.decodedDataJson);
111+
const sortedAttestations = [...result.data.attestations].sort((a: any, b: any) => b.time - a.time);
97112

98-
const projectRefUid = parsedData.find((item: any) => item.name === 'projectRefUID');
99-
const metadataURL = parsedData.find((item: any) => item.name === 'metadataUrl');
113+
const attestation = sortedAttestations[0]; // Assuming you want the first attestation
114+
const parsedData = JSON.parse(attestation.decodedDataJson);
100115

101-
return {
102-
metadataURL: metadataURL.value.value,
103-
projectRefUid: projectRefUid.value.value
104-
};
105-
} catch (error) {
116+
const projectRefUid = parsedData.find((item: any) => item.name === 'projectRefUID');
117+
const metadataURL = parsedData.find((item: any) => item.name === 'metadataUrl');
118+
119+
return {
120+
metadataURL: metadataURL.value.value,
121+
projectRefUid: projectRefUid.value.value
122+
};
123+
} catch (error) {
106124
console.error("Error fetching data:", error);
107125
return null;
108126
}
@@ -127,7 +145,7 @@ export async function fetchAndProcessData(client: ApolloClient<NormalizedCacheOb
127145
};
128146
}));
129147

130-
console.log("data", arrObjectSomething)
148+
console.log("data", arrObjectSomething)
131149

132150
try {
133151
const responses: any[] = [];

0 commit comments

Comments
 (0)