From 0116924d7953c6bf16b80e5b6ae14ce3c0134151 Mon Sep 17 00:00:00 2001 From: MrX-SNX Date: Thu, 5 Sep 2024 19:27:39 +0100 Subject: [PATCH] fix: api down (#439) * fix: api down * ref: removed unused var --- .../ui/src/queries/useGetHistoricalVotes.ts | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/governance/ui/src/queries/useGetHistoricalVotes.ts b/governance/ui/src/queries/useGetHistoricalVotes.ts index 846a59223..9445234dc 100644 --- a/governance/ui/src/queries/useGetHistoricalVotes.ts +++ b/governance/ui/src/queries/useGetHistoricalVotes.ts @@ -24,23 +24,28 @@ export function useGetHistoricalVotes() { return useQuery({ queryKey: ['historical-votes', network?.id], queryFn: async () => { - const res = await fetch(network?.id !== 2192 ? mainnetURL : testnetURL); - let votesRaw: Record = await res.json(); - if (!('spartan' in votesRaw)) { - votesRaw = { spartan: votesRaw[0], ambassador: [], treasury: [] }; + try { + const res = await fetch(network?.id !== 2192 ? mainnetURL : testnetURL); + let votesRaw: Record = await res.json(); + if (!('spartan' in votesRaw)) { + votesRaw = { spartan: votesRaw[0], ambassador: [], treasury: [] }; + } + return councils.reduce( + (cur, next) => { + cur[next.slug] = votesRaw[next.slug].sort((a, b) => { + if (a.epochId === b.epochId) { + return a.blockTimestamp - b.blockTimestamp; + } + return a.epochId - b.epochId; + }); + return cur; + }, + {} as Record + ); + } catch (err) { + console.error(err); + return { spartan: [], ambassador: [], treasury: [] } as Record; } - return councils.reduce( - (cur, next) => { - cur[next.slug] = votesRaw[next.slug].sort((a, b) => { - if (a.epochId === b.epochId) { - return a.blockTimestamp - b.blockTimestamp; - } - return a.epochId - b.epochId; - }); - return cur; - }, - {} as Record - ); }, staleTime: 900000, });