Clean up index setting, add callback to track changes

This commit is contained in:
Eric Bailey
2026-04-14 15:45:25 -05:00
parent 85b38ff7ad
commit 8cc6aec271
3 changed files with 23 additions and 9 deletions
+15 -2
View File
@@ -134,11 +134,24 @@ export function Gallery({
const itemRefsRef = useRef<Map<number, View>>(new Map()) const itemRefsRef = useRef<Map<number, View>>(new Map())
const currentIndexRef = useRef(0) const currentIndexRef = useRef(0)
const setCurrentIndex = (index: number) => {
const prev = currentIndexRef.current
if (prev !== index) {
currentIndexRef.current = index
ax.metric('post:gallery:swipe', {
fromIndex: prev,
toIndex: index,
totalImages: images.length,
})
}
}
const scrollTo = (offset: number) => { const scrollTo = (offset: number) => {
flatListRef.current?.scrollToOffset({offset, animated: false}) flatListRef.current?.scrollToOffset({offset, animated: false})
} }
const onSettle = (index: number) => { const onSettle = (index: number) => {
setCurrentIndex(index)
if (!IS_WEB) return if (!IS_WEB) return
const el = itemRefsRef.current.get(index) as unknown as HTMLElement | null const el = itemRefsRef.current.get(index) as unknown as HTMLElement | null
el?.focus({preventScroll: true}) el?.focus({preventScroll: true})
@@ -211,12 +224,12 @@ export function Gallery({
for (let i = 0; i < images.length; i++) { for (let i = 0; i < images.length; i++) {
const w = (itemWidthsRef.current.get(i) ?? 0) + ITEM_GAP const w = (itemWidthsRef.current.get(i) ?? 0) + ITEM_GAP
if (offsetX < accumulated + w / 2) { if (offsetX < accumulated + w / 2) {
currentIndexRef.current = i setCurrentIndex(i)
break break
} }
accumulated += w accumulated += w
if (i === images.length - 1) { if (i === images.length - 1) {
currentIndexRef.current = i setCurrentIndex(i)
} }
} }
}} }}
@@ -47,6 +47,7 @@ export function useKeyboardHandlers({
if (targetIndex != null) { if (targetIndex != null) {
e.preventDefault() e.preventDefault()
console.log('targetIndex', targetIndex)
if (stopTween) { if (stopTween) {
stopTween() stopTween()
stopTween = null stopTween = null
@@ -62,7 +63,6 @@ export function useKeyboardHandlers({
}, },
() => { () => {
stopTween = null stopTween = null
currentIndexRef.current = idx
onSettle(idx) onSettle(idx)
}, },
) )
@@ -69,6 +69,7 @@ export function usePointerHandlers({
let velo = 0 let velo = 0
let t = 0 let t = 0
let stopTween: (() => void) | null = null let stopTween: (() => void) | null = null
let localIndex = currentIndexRef.current
el.style.cursor = 'grab' el.style.cursor = 'grab'
@@ -83,6 +84,7 @@ export function usePointerHandlers({
isMouseDown = true isMouseDown = true
isDragging = false isDragging = false
localIndex = currentIndexRef.current
startX = e.pageX startX = e.pageX
dragScrollLeft = el.scrollLeft dragScrollLeft = el.scrollLeft
delta = 0 delta = 0
@@ -121,17 +123,17 @@ export function usePointerHandlers({
scrollTo(dragScrollLeft - delta) scrollTo(dragScrollLeft - delta)
// Update current index from scroll position // Update local index from scroll position
const offsetX = dragScrollLeft - delta const offsetX = dragScrollLeft - delta
let accumulated = 0 let accumulated = 0
for (let i = 0; i < imageCount; i++) { for (let i = 0; i < imageCount; i++) {
const w = (itemWidthsRef.current.get(i) ?? 0) + ITEM_GAP const w = (itemWidthsRef.current.get(i) ?? 0) + ITEM_GAP
if (offsetX < accumulated + w / 2) { if (offsetX < accumulated + w / 2) {
currentIndexRef.current = i localIndex = i
break break
} }
accumulated += w accumulated += w
if (i === imageCount - 1) currentIndexRef.current = i if (i === imageCount - 1) localIndex = i
} }
} }
@@ -165,7 +167,7 @@ export function usePointerHandlers({
const targetIndex = whichByDistance( const targetIndex = whichByDistance(
itemWidthsRef.current, itemWidthsRef.current,
currentIndexRef.current, localIndex,
totalDistance, totalDistance,
direction, direction,
imageCount, imageCount,
@@ -176,7 +178,7 @@ export function usePointerHandlers({
const to = getOffsetForIndex(itemWidthsRef.current, targetIndex) const to = getOffsetForIndex(itemWidthsRef.current, targetIndex)
if (from === to) { if (from === to) {
currentIndexRef.current = targetIndex onSettle(targetIndex)
return return
} }
@@ -190,7 +192,6 @@ export function usePointerHandlers({
}, },
() => { () => {
stopTween = null stopTween = null
currentIndexRef.current = targetIndex
onSettle(targetIndex) onSettle(targetIndex)
}, },
) )