Skip to content

Commit 4dc8994

Browse files
chore: store mappings from my slot to index
1 parent 2d6fc87 commit 4dc8994

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/features/Navigation/SlotsList.tsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import ResetLive from "./ResetLive";
2222
import type { DebouncedState } from "use-debounce";
2323
import { useDebouncedCallback } from "use-debounce";
2424
import { useCurrentRoute } from "../../hooks/useCurrentRoute";
25+
import { getSlotGroupLeader } from "../../utils";
2526

2627
const computeItemKey = (slot: number) => slot;
2728

@@ -274,13 +275,23 @@ function AllSlotsList({ width, height }: SlotsListProps) {
274275
}
275276

276277
function MySlotsList({ width, height }: SlotsListProps) {
277-
const leaderSlots = useAtomValue(leaderSlotsAtom);
278+
const mySlots = useAtomValue(leaderSlotsAtom);
278279

279280
const slotGroupsDescending = useMemo(
280-
() => leaderSlots?.toReversed() ?? [],
281-
[leaderSlots],
281+
() => mySlots?.toReversed() ?? [],
282+
[mySlots],
282283
);
283284

285+
const slotToIndexMapping = useMemo(() => {
286+
return slotGroupsDescending.reduce<{ [slot: number]: number }>(
287+
(acc, slot, index) => {
288+
acc[slot] = index;
289+
return acc;
290+
},
291+
{},
292+
);
293+
}, [slotGroupsDescending]);
294+
284295
const getSlotAtIndex = useCallback(
285296
(index: number) => slotGroupsDescending[index],
286297
[slotGroupsDescending],
@@ -289,12 +300,15 @@ function MySlotsList({ width, height }: SlotsListProps) {
289300
// Get the slot index, or if unavailable, the closest past index
290301
const getClosestIndexForSlot = useCallback(
291302
(slot: number) => {
292-
return slotGroupsDescending.findIndex((s) => s <= slot);
303+
return (
304+
slotToIndexMapping[getSlotGroupLeader(slot)] ??
305+
slotGroupsDescending.findIndex((s) => s <= slot)
306+
);
293307
},
294-
[slotGroupsDescending],
308+
[slotGroupsDescending, slotToIndexMapping],
295309
);
296310

297-
if (!leaderSlots) return null;
311+
if (!mySlots) return null;
298312

299313
return (
300314
<InnerSlotsList

0 commit comments

Comments
 (0)