-
Notifications
You must be signed in to change notification settings - Fork 3
fix: move epoch check to selected slot getter #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
asuzuki-jumptrading
merged 3 commits into
amliu/navigation
from
asuzuki/slot-epoch-check
Aug 21, 2025
+155
−148
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,34 +6,48 @@ import { | |
leaderSlotsAtom, | ||
SlotNavFilter, | ||
slotNavFilterAtom, | ||
setSlotScrollListFnAtom, | ||
slotOverrideAtom, | ||
} from "../../atoms"; | ||
import { Box } from "@radix-ui/themes"; | ||
import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react"; | ||
import type { RefObject } from "react"; | ||
import { memo, useCallback, useEffect, useMemo, useRef } from "react"; | ||
import styles from "./slotsList.module.css"; | ||
import { slotsListPinnedSlotOffset, slotsPerLeader } from "../../consts"; | ||
import { clamp, throttle } from "lodash"; | ||
import { throttle } from "lodash"; | ||
import SlotsRenderer, { SlotsPlaceholder } from "./SlotsRenderer"; | ||
import type { ScrollSeekConfiguration, VirtuosoHandle } from "react-virtuoso"; | ||
import { Virtuoso } from "react-virtuoso"; | ||
import { selectedSlotAtom } from "../Overview/SlotPerformance/atoms"; | ||
import { baseSelectedSlotAtom } from "../Overview/SlotPerformance/atoms"; | ||
import ResetLive from "./ResetLive"; | ||
import type { DebouncedState } from "use-debounce"; | ||
import { useDebouncedCallback } from "use-debounce"; | ||
import { useCurrentRoute } from "../../hooks/useCurrentRoute"; | ||
|
||
const computeItemKey = (slot: number) => slot; | ||
|
||
// Add one future slot to prevent current leader transition from flickering | ||
const increaseViewportBy = { top: 24, bottom: 0 }; | ||
|
||
interface SlotsListProps { | ||
width: number; | ||
height: number; | ||
} | ||
|
||
export default function SlotsList({ width, height }: SlotsListProps) { | ||
const currentRoute = useCurrentRoute(); | ||
const navFilter = useAtomValue(slotNavFilterAtom); | ||
const epoch = useAtomValue(epochAtom); | ||
const isSelectionInitialized = | ||
useAtomValue(baseSelectedSlotAtom).isInitialized; | ||
|
||
if (!epoch || (currentRoute === "Slot Details" && !isSelectionInitialized)) { | ||
return null; | ||
} | ||
|
||
return navFilter === SlotNavFilter.MySlots ? ( | ||
<MySlotsList width={width} height={height} /> | ||
<MySlotsList key={epoch.epoch} width={width} height={height} /> | ||
) : ( | ||
<AllSlotsList width={width} height={height} /> | ||
<AllSlotsList key={epoch.epoch} width={width} height={height} /> | ||
); | ||
} | ||
|
||
|
@@ -55,51 +69,16 @@ function InnerSlotsList({ | |
const listRef = useRef<VirtuosoHandle>(null); | ||
const visibleStartIndexRef = useRef<number | null>(null); | ||
|
||
const selectedSlot = useAtomValue(selectedSlotAtom); | ||
const currentLeaderSlot = useAtomValue(currentLeaderSlotAtom); | ||
|
||
const setSlotOverride = useSetAtom(slotOverrideAtom); | ||
const autoScroll = useAtomValue(autoScrollAtom); | ||
const setSlotScrollListFn = useSetAtom(setSlotScrollListFnAtom); | ||
|
||
const slotsCount = slotGroupsDescending.length; | ||
|
||
useEffect(() => { | ||
// set scroll function, called when slot override is updated | ||
setSlotScrollListFn((startOverrideSlot: number | undefined) => { | ||
if (!listRef.current || !startOverrideSlot) return; | ||
|
||
const slotIndex = getIndexForSlot(startOverrideSlot); | ||
|
||
listRef.current.scrollToIndex({ | ||
index: slotIndex - slotsListPinnedSlotOffset, | ||
align: "start", | ||
}); | ||
}); | ||
|
||
return () => { | ||
setSlotScrollListFn(undefined); | ||
}; | ||
}, [getIndexForSlot, setSlotScrollListFn]); | ||
|
||
// Determine initial scroll position | ||
const initialTopMostItemIndex = useMemo(() => { | ||
let slotIndex = -1; | ||
|
||
if (selectedSlot !== undefined) { | ||
slotIndex = getIndexForSlot(selectedSlot); | ||
} else if (currentLeaderSlot !== undefined) { | ||
slotIndex = getIndexForSlot(currentLeaderSlot); | ||
} | ||
|
||
if (slotIndex === -1) return -1; | ||
|
||
return clamp(slotIndex - slotsListPinnedSlotOffset, 0, slotsCount - 1); | ||
}, [selectedSlot, currentLeaderSlot, slotsCount, getIndexForSlot]); | ||
const debouncedScroll = useDebouncedCallback(() => {}, 100); | ||
|
||
const { rangeChanged, scrollSeekConfiguration } = useMemo(() => { | ||
const rangeChangedFn = ({ startIndex }: { startIndex: number }) => | ||
(visibleStartIndexRef.current = startIndex); | ||
const rangeChangedFn = ({ startIndex }: { startIndex: number }) => { | ||
// account for increaseViewportBy | ||
visibleStartIndexRef.current = startIndex + 1; | ||
}; | ||
|
||
const config: ScrollSeekConfiguration = { | ||
enter: (velocity) => Math.abs(velocity) > 1500, | ||
|
@@ -118,13 +97,15 @@ function InnerSlotsList({ | |
() => { | ||
if (visibleStartIndexRef.current === null) return; | ||
|
||
debouncedScroll(); | ||
|
||
const slotIndex = Math.min( | ||
visibleStartIndexRef.current + slotsListPinnedSlotOffset, | ||
slotsCount - 1, | ||
); | ||
|
||
const slot = getSlotAtIndex(slotIndex); | ||
setSlotOverride(slot, true); | ||
setSlotOverride(slot); | ||
}, | ||
50, | ||
{ leading: true, trailing: true }, | ||
|
@@ -141,36 +122,22 @@ function InnerSlotsList({ | |
container.removeEventListener("wheel", handleScroll); | ||
container.removeEventListener("touchmove", handleScroll); | ||
}; | ||
}, [getSlotAtIndex, setSlotOverride, slotsCount, visibleStartIndexRef]); | ||
|
||
// Auto scroll enabled (live scrolling) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to |
||
useEffect(() => { | ||
if ( | ||
!autoScroll || | ||
currentLeaderSlot === undefined || | ||
!listRef.current || | ||
!slotsCount | ||
) | ||
return; | ||
|
||
const slotIndex = getIndexForSlot(currentLeaderSlot); | ||
const visibleStartIndex = slotIndex - slotsListPinnedSlotOffset; | ||
|
||
listRef.current.scrollToIndex({ | ||
index: visibleStartIndex > 0 ? visibleStartIndex : 0, | ||
align: "start", | ||
}); | ||
}, [autoScroll, currentLeaderSlot, getIndexForSlot, listRef, slotsCount]); | ||
|
||
const debouncedHeight = useDebouncedHeight(height); | ||
|
||
const increaseViewportBy = useMemo(() => { | ||
// top must be 0, to prevent rangeChange startIndex offset | ||
return { top: 0, bottom: debouncedHeight }; | ||
}, [debouncedHeight]); | ||
}, [ | ||
getSlotAtIndex, | ||
debouncedScroll, | ||
setSlotOverride, | ||
slotsCount, | ||
visibleStartIndexRef, | ||
]); | ||
|
||
return ( | ||
<Box ref={listContainerRef} width={`${width}px`} height={`${height}px`}> | ||
<RTAutoScroll listRef={listRef} getIndexForSlot={getIndexForSlot} /> | ||
<SlotOverrideScroll | ||
listRef={listRef} | ||
getIndexForSlot={getIndexForSlot} | ||
debouncedScroll={debouncedScroll} | ||
/> | ||
<SlotsPlaceholder width={width} height={height} /> | ||
<ResetLive /> | ||
<Virtuoso | ||
|
@@ -180,7 +147,6 @@ function InnerSlotsList({ | |
height={height} | ||
data={slotGroupsDescending} | ||
totalCount={slotsCount} | ||
initialTopMostItemIndex={initialTopMostItemIndex} | ||
increaseViewportBy={increaseViewportBy} | ||
// height of past slots that the user is most likely to scroll through | ||
defaultItemHeight={42} | ||
|
@@ -200,6 +166,76 @@ const MScrollSeekPlaceHolder = memo(function ScrollSeekPlaceholder() { | |
return null; | ||
}); | ||
|
||
interface RTAutoScrollProps { | ||
listRef: RefObject<VirtuosoHandle>; | ||
getIndexForSlot: (slot: number) => number; | ||
} | ||
function RTAutoScroll({ listRef, getIndexForSlot }: RTAutoScrollProps) { | ||
const currentLeaderSlot = useAtomValue(currentLeaderSlotAtom); | ||
const autoScroll = useAtomValue(autoScrollAtom); | ||
|
||
useEffect(() => { | ||
if (!autoScroll || currentLeaderSlot === undefined || !listRef.current) | ||
return; | ||
|
||
// scroll to new current leader slot | ||
const slotIndex = getIndexForSlot(currentLeaderSlot); | ||
const visibleStartIndex = slotIndex - slotsListPinnedSlotOffset; | ||
|
||
listRef.current.scrollToIndex({ | ||
index: visibleStartIndex > 0 ? visibleStartIndex : 0, | ||
align: "start", | ||
}); | ||
}, [autoScroll, currentLeaderSlot, getIndexForSlot, listRef]); | ||
|
||
return null; | ||
} | ||
|
||
interface SlotOverrideScrollProps { | ||
listRef: RefObject<VirtuosoHandle>; | ||
getIndexForSlot: (slot: number) => number; | ||
debouncedScroll: DebouncedState<() => void>; | ||
} | ||
function SlotOverrideScroll({ | ||
listRef, | ||
getIndexForSlot, | ||
debouncedScroll, | ||
}: SlotOverrideScrollProps) { | ||
const rafIdRef = useRef<number | null>(null); | ||
const slotOverride = useAtomValue(slotOverrideAtom); | ||
|
||
useEffect(() => { | ||
if (!slotOverride || !listRef.current || debouncedScroll.isPending()) | ||
return; | ||
|
||
const targetIndex = Math.max( | ||
0, | ||
getIndexForSlot(slotOverride) - slotsListPinnedSlotOffset, | ||
); | ||
|
||
const prevRafId = rafIdRef.current; | ||
rafIdRef.current = requestAnimationFrame(() => { | ||
if (prevRafId !== null) { | ||
cancelAnimationFrame(prevRafId); | ||
} | ||
|
||
listRef.current?.scrollToIndex({ | ||
index: targetIndex, | ||
align: "start", | ||
}); | ||
}); | ||
|
||
return () => { | ||
if (rafIdRef.current !== null) { | ||
cancelAnimationFrame(rafIdRef.current); | ||
rafIdRef.current = null; | ||
} | ||
}; | ||
}, [getIndexForSlot, slotOverride, listRef, debouncedScroll]); | ||
|
||
return null; | ||
} | ||
|
||
function AllSlotsList({ width, height }: SlotsListProps) { | ||
const epoch = useAtomValue(epochAtom); | ||
|
||
|
@@ -258,6 +294,8 @@ function MySlotsList({ width, height }: SlotsListProps) { | |
[slotGroupsDescending], | ||
); | ||
|
||
if (!leaderSlots) return null; | ||
|
||
return ( | ||
<InnerSlotsList | ||
width={width} | ||
|
@@ -268,17 +306,3 @@ function MySlotsList({ width, height }: SlotsListProps) { | |
/> | ||
); | ||
} | ||
|
||
function useDebouncedHeight(height: number) { | ||
const [debouncedHeight, setDebouncedHeight] = useState(height); | ||
|
||
const updateDebouncedHeight = useDebouncedCallback((h: number) => { | ||
setDebouncedHeight((prev) => (Math.abs(h - prev) >= 100 ? h : prev)); | ||
}, 80); | ||
|
||
useMemo(() => { | ||
updateDebouncedHeight(height); | ||
}, [height, updateDebouncedHeight]); | ||
|
||
return debouncedHeight; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to
SlotOverrideScroll
component. No longer scrolling from inside an atom