Skip to content

Commit 927cede

Browse files
committed
fix: few code smells
1 parent 01234cb commit 927cede

File tree

1 file changed

+22
-25
lines changed
  • web/src/pages/Courts/CourtDetails/StakingHistoryByCourt/DisplayStakes

1 file changed

+22
-25
lines changed

web/src/pages/Courts/CourtDetails/StakingHistoryByCourt/DisplayStakes/index.tsx

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,36 +43,27 @@ const getAllChildCourtIds = (court: CourtTreeQuery["court"], courtId: string): n
4343

4444
const ids: number[] = [];
4545

46-
const traverse = (node: CourtTreeQuery["court"]) => {
47-
if (!node) return;
46+
const collectAllDescendants = (node: CourtTreeQuery["court"]) => {
47+
ids.push(parseInt(node.id));
48+
if (node.children) {
49+
node.children.forEach((child) => collectAllDescendants(child));
50+
}
51+
};
52+
53+
const findAndCollect = (node: CourtTreeQuery["court"]): boolean => {
4854
if (node.id === courtId) {
49-
ids.push(parseInt(node.id));
50-
if (node.children) {
51-
node.children.forEach((child) => {
52-
ids.push(parseInt(child.id));
53-
if (child.children) {
54-
child.children.forEach((gc) => {
55-
ids.push(parseInt(gc.id));
56-
if (gc.children) {
57-
gc.children.forEach((ggc) => {
58-
ids.push(parseInt(ggc.id));
59-
if (ggc.children) {
60-
ggc.children.forEach((gggc) => ids.push(parseInt(gggc.id)));
61-
}
62-
});
63-
}
64-
});
65-
}
66-
});
67-
}
68-
return;
55+
collectAllDescendants(node);
56+
return true;
6957
}
7058
if (node.children) {
71-
node.children.forEach((child) => traverse(child));
59+
for (const child of node.children) {
60+
if (findAndCollect(child)) return true;
61+
}
7262
}
63+
return false;
7364
};
7465

75-
traverse(court);
66+
findAndCollect(court);
7667
return ids;
7768
};
7869

@@ -134,7 +125,13 @@ const DisplayStakes: React.FC = () => {
134125

135126
const sortedChunk = chunk.sort((a, b) => parseInt(b.timestamp) - parseInt(a.timestamp));
136127

137-
if (sortedChunk.length) setAcc((prev) => [...prev, ...sortedChunk]);
128+
if (sortedChunk.length) {
129+
setAcc((prev) => {
130+
const seen = new Set(prev.map((item) => item.id));
131+
const next = sortedChunk.filter((item) => !seen.has(item.id));
132+
return next.length ? [...prev, ...next] : prev;
133+
});
134+
}
138135
}, [data, courtIds, courtTree]);
139136

140137
const sentinelRef = useRef<HTMLDivElement | null>(null);

0 commit comments

Comments
 (0)