Skip to content

Commit

Permalink
fix: reset resolved kpi tokens array when switching network in campai…
Browse files Browse the repository at this point in the history
…gns page (#441)

* fix(react): reset result array when fetching a new batch in use resolved kpi tokens hook

* chore: add changeset

---------

Co-authored-by: luzzifoss <fedeluzzi00@gmail.com>
  • Loading branch information
luzzif and luzzifoss authored Oct 16, 2023
1 parent 2763792 commit c68bd00
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/afraid-hairs-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@carrot-kpi/react": patch
---

Reset result array before filling it again in useResolvedKPITokens hook (it
avoids resolved KPI tokens accumulation)
9 changes: 4 additions & 5 deletions packages/react/src/hooks/useResolvedKPITokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function useResolvedKPITokens(params?: ResolvedKPITokensParams): {
blacklisted: params?.blacklisted,
});

const resolvedKPITokens: ResolvedKPIToken[] = [];
await Promise.allSettled(
Object.values(kpiTokens).map(async (kpiToken) => {
try {
Expand All @@ -43,11 +44,7 @@ export function useResolvedKPITokens(params?: ResolvedKPITokensParams): {
})
)[kpiToken.address];
if (!resolved) return;
if (!cancelled)
setResolvedKPITokens((previousState) => [
...previousState,
resolved,
]);
resolvedKPITokens.push(resolved);
} catch (error) {
console.error(
`error resolving kpi token at address ${kpiToken.address}`,
Expand All @@ -56,6 +53,8 @@ export function useResolvedKPITokens(params?: ResolvedKPITokensParams): {
}
}),
);

if (!cancelled) setResolvedKPITokens(resolvedKPITokens);
} catch (error) {
console.error("error fetching resolved kpi tokens", error);
} finally {
Expand Down

0 comments on commit c68bd00

Please sign in to comment.