Skip to content

Commit f64db94

Browse files
chore: add leader slots set
1 parent a8cd6f6 commit f64db94

5 files changed

Lines changed: 52 additions & 16 deletions

File tree

src/atoms.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,16 @@ export const leaderSlotsAtom = atom((get) => {
364364
return getLeaderSlots(epoch, pubkey);
365365
});
366366

367+
/**
368+
* Set of your leader slots, in ascending order
369+
*/
370+
export const ascendingLeaderSlotsSetAtom = atom((get) => {
371+
const leaderSlots = get(leaderSlotsAtom);
372+
if (!leaderSlots) return;
373+
374+
return new Set(leaderSlots);
375+
});
376+
367377
/** In order array of your leader slots (only first slot in group of 4) */
368378
export const nextEpochLeaderSlotsAtom = atom((get) => {
369379
const epoch = get(nextEpochAtom);

src/features/Navigation/SlotsList.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useAtomValue } from "jotai";
22
import {
3+
ascendingLeaderSlotsSetAtom,
34
currentSlotAtom,
45
epochAtom,
56
isCurrentlyLeaderAtom,
@@ -74,12 +75,18 @@ const MAllSlotsList = memo(function AllSlotsList({
7475
}: SlotsListProps) {
7576
const epoch = useAtomValue(epochAtom);
7677
const currentSlot = useAtomValue(currentSlotAtom);
77-
const leaderSlots = useAtomValue(leaderSlotsAtom);
78+
const ascendingLeaderSlotsSet = useAtomValue(ascendingLeaderSlotsSetAtom);
7879
const nextLeaderSlot = useAtomValue(nextLeaderSlotAtom);
7980

8081
const slotsListProps = useMemo(
81-
() => getAllSlotsListProps(epoch, currentSlot, leaderSlots, nextLeaderSlot),
82-
[epoch, currentSlot, leaderSlots, nextLeaderSlot],
82+
() =>
83+
getAllSlotsListProps(
84+
epoch,
85+
currentSlot,
86+
ascendingLeaderSlotsSet,
87+
nextLeaderSlot,
88+
),
89+
[epoch, currentSlot, ascendingLeaderSlotsSet, nextLeaderSlot],
8390
);
8491

8592
if (!slotsListProps) return null;

src/features/Navigation/__tests__/utils.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,25 @@ describe("navigation utils", () => {
4141
});
4242

4343
it("handles undefined epoch", () => {
44-
const result = getAllSlotsListProps(undefined, undefined, undefined, undefined);
44+
const result = getAllSlotsListProps(
45+
undefined,
46+
undefined,
47+
undefined,
48+
undefined,
49+
);
4550
expect(result).toBeUndefined();
4651
});
4752
});
4853

4954
describe("getMySlotsListProps", () => {
5055
it("gets correct values and gives the index for the closest smaller my slot leader", () => {
51-
const result = getMySlotsListProps([4, 12, 20, 40, 44, 48], undefined, undefined, false, undefined);
56+
const result = getMySlotsListProps(
57+
[4, 12, 20, 40, 44, 48],
58+
undefined,
59+
undefined,
60+
false,
61+
undefined,
62+
);
5263

5364
expect(result).not.toBeUndefined();
5465
const { itemsCount, getSlotAtIndex, getIndexForSlot } = result!;

src/features/Navigation/allSlotsUtils.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { getHeightSum, type Counts } from "./utils";
1717
export function getAllSlotsOffsetHelpers(
1818
epoch: Epoch,
1919
currentSlot: number,
20-
leaderSlots: number[],
20+
ascendingLeaderSlotsSet: Set<number>,
2121
nextLeaderSlot: number | undefined,
2222
getSlotAtIndex: (index: number) => number | undefined,
2323
): OffsetHelpers {
@@ -30,7 +30,7 @@ export function getAllSlotsOffsetHelpers(
3030
lastLeaderSlot,
3131
currentLeaderSlot,
3232
nextLeaderSlot,
33-
leaderSlots,
33+
ascendingLeaderSlotsSet,
3434
);
3535
const totalHeight = getHeightSum(counts);
3636

@@ -44,7 +44,7 @@ export function getAllSlotsOffsetHelpers(
4444
lastLeaderSlot,
4545
currentLeaderSlot,
4646
nextLeaderSlot,
47-
leaderSlots,
47+
ascendingLeaderSlotsSet,
4848
);
4949
return getHeightSum(counts);
5050
};
@@ -58,7 +58,7 @@ export function getAllSlotsOffsetHelpers(
5858

5959
const getSlotHeight = (anySlot: number) => {
6060
const slot = getSlotGroupLeader(anySlot);
61-
const isYours = leaderSlots.includes(slot);
61+
const isYours = ascendingLeaderSlotsSet.has(slot);
6262
if (slot < currentLeaderSlot)
6363
return isYours ? YOUR_PAST_HEIGHT : OTHER_PAST_HEIGHT;
6464
if (slot === currentLeaderSlot)
@@ -92,7 +92,7 @@ export function getAllSlotsOffsetHelpers(
9292
export function getAllSlotsListProps(
9393
epoch: Epoch | undefined,
9494
currentSlot: number | undefined,
95-
leaderSlots: number[] | undefined,
95+
ascendingLeaderSlotsSet: Set<number> | undefined,
9696
nextLeaderSlot: number | undefined,
9797
): SlotsIndexProps | undefined {
9898
if (!epoch) return;
@@ -111,11 +111,11 @@ export function getAllSlotsListProps(
111111
};
112112

113113
const offsetHelpers =
114-
currentSlot != null && leaderSlots != null
114+
currentSlot != null && ascendingLeaderSlotsSet != null
115115
? getAllSlotsOffsetHelpers(
116116
epoch,
117117
currentSlot,
118-
leaderSlots,
118+
ascendingLeaderSlotsSet,
119119
nextLeaderSlot,
120120
getSlotAtIndex,
121121
)
@@ -134,13 +134,13 @@ function getTypeCounts(
134134
maxLeaderSlot: number,
135135
currentLeaderSlot: number,
136136
yourNextLeaderSlot: number | undefined,
137-
leaderSlots: number[],
137+
ascendingLeaderSlotsSet: Set<number>,
138138
): Counts {
139139
let yourPastCount = 0;
140140
let yourCurrentCount = 0;
141141
let yourNextFutureCount = 0;
142142
let yourNonNextFutureCount = 0;
143-
for (const slot of leaderSlots) {
143+
for (const slot of ascendingLeaderSlotsSet) {
144144
if (slot < minLeaderSlot) continue;
145145
if (slot > maxLeaderSlot) break;
146146

src/features/Navigation/mySlotsUtils.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export function getMySlotsOffsetHelpers(
2626
mySlots.length - 1,
2727
isCurrentlyLeader,
2828
nextLeaderSlotIndex,
29+
mySlots.length,
2930
);
3031
const totalHeight = getHeightSum(counts);
3132

@@ -41,6 +42,7 @@ export function getMySlotsOffsetHelpers(
4142
leaderEndIdx,
4243
isCurrentlyLeader,
4344
nextLeaderSlotIndex,
45+
mySlots.length,
4446
);
4547

4648
return getHeightSum(countsAbove);
@@ -141,6 +143,7 @@ function getTypeCountsForLeaderIndices(
141143
endLeadersIdx: number,
142144
isCurrentlyLeader: boolean,
143145
nextLeaderSlotIndex: number | undefined,
146+
totalLeadersCount: number,
144147
): Counts {
145148
const totalCount = endLeadersIdx - startLeadersIdx + 1;
146149
const futureCount =
@@ -156,6 +159,7 @@ function getTypeCountsForLeaderIndices(
156159
endLeadersIdx,
157160
nextLeaderSlotIndex,
158161
isCurrentlyLeader,
162+
totalLeadersCount,
159163
)
160164
? 1
161165
: 0;
@@ -180,10 +184,14 @@ function isCurrentLeaderAndInRange(
180184
endIdx: number,
181185
nextLeaderSlotIndex: number | undefined,
182186
isCurrentlyLeader: boolean,
187+
totalLeadersCount: number,
183188
) {
184-
if (!isCurrentlyLeader || nextLeaderSlotIndex == null) return false;
189+
if (!isCurrentlyLeader) return false;
185190

186-
const currentIndex = nextLeaderSlotIndex - 1;
191+
const currentIndex =
192+
nextLeaderSlotIndex != null
193+
? nextLeaderSlotIndex - 1
194+
: totalLeadersCount - 1;
187195
if (currentIndex < 0) return false;
188196

189197
return currentIndex >= startIdx && currentIndex <= endIdx;

0 commit comments

Comments
 (0)