Debounce metric
This commit is contained in:
@@ -1046,12 +1046,12 @@ export type Events = {
|
|||||||
|
|
||||||
// Gallery carousel events
|
// Gallery carousel events
|
||||||
'post:gallery:swipe': {
|
'post:gallery:swipe': {
|
||||||
fromIndex: number
|
fromImage: number
|
||||||
toIndex: number
|
toImage: number
|
||||||
totalImages: number
|
totalImages: number
|
||||||
}
|
}
|
||||||
'post:gallery:openLightbox': {
|
'post:gallery:openLightbox': {
|
||||||
imageIndex: number
|
fromImage: number
|
||||||
totalImages: number
|
totalImages: number
|
||||||
}
|
}
|
||||||
'post:gallery:impression': {
|
'post:gallery:impression': {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import {Image} from 'expo-image'
|
|||||||
import {type AppBskyEmbedImages} from '@atproto/api'
|
import {type AppBskyEmbedImages} from '@atproto/api'
|
||||||
import {utils} from '@bsky.app/alf'
|
import {utils} from '@bsky.app/alf'
|
||||||
import {Trans, useLingui} from '@lingui/react/macro'
|
import {Trans, useLingui} from '@lingui/react/macro'
|
||||||
|
import debounce from 'lodash.debounce'
|
||||||
|
|
||||||
import {type Dimensions} from '#/lib/media/types'
|
import {type Dimensions} from '#/lib/media/types'
|
||||||
import {mergeRefs} from '#/lib/merge-refs'
|
import {mergeRefs} from '#/lib/merge-refs'
|
||||||
@@ -93,6 +94,7 @@ export function Gallery({
|
|||||||
onPressIn,
|
onPressIn,
|
||||||
viewContext,
|
viewContext,
|
||||||
}: GalleryProps) {
|
}: GalleryProps) {
|
||||||
|
// images = images.concat(images)
|
||||||
const ax = useAnalytics()
|
const ax = useAnalytics()
|
||||||
const {screenReaderEnabled} = useA11y()
|
const {screenReaderEnabled} = useA11y()
|
||||||
const largeAltBadge = useLargeAltBadgeEnabled()
|
const largeAltBadge = useLargeAltBadgeEnabled()
|
||||||
@@ -147,15 +149,23 @@ export function Gallery({
|
|||||||
const thumbDimsRef = useRef<Map<number, Dimensions>>(new Map())
|
const thumbDimsRef = useRef<Map<number, Dimensions>>(new Map())
|
||||||
const currentIndexRef = useRef(0)
|
const currentIndexRef = useRef(0)
|
||||||
|
|
||||||
|
const emitSwipeMetric = useMemo(
|
||||||
|
() =>
|
||||||
|
debounce((fromIndex: number, toIndex: number) => {
|
||||||
|
ax.metric('post:gallery:swipe', {
|
||||||
|
fromImage: fromIndex + 1, // convert to 1-based index for easier analysis
|
||||||
|
toImage: toIndex + 1, // convert to 1-based index for easier analysis
|
||||||
|
totalImages: images.length,
|
||||||
|
})
|
||||||
|
}, 200),
|
||||||
|
[ax, images.length],
|
||||||
|
)
|
||||||
|
|
||||||
const setCurrentIndex = (index: number) => {
|
const setCurrentIndex = (index: number) => {
|
||||||
const prev = currentIndexRef.current
|
const prev = currentIndexRef.current
|
||||||
if (prev !== index) {
|
if (prev !== index) {
|
||||||
currentIndexRef.current = index
|
currentIndexRef.current = index
|
||||||
ax.metric('post:gallery:swipe', {
|
emitSwipeMetric(prev, index)
|
||||||
fromIndex: prev,
|
|
||||||
toIndex: index,
|
|
||||||
totalImages: images.length,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,7 +203,7 @@ export function Gallery({
|
|||||||
<View style={[a.relative, a.gap_sm]}>
|
<View style={[a.relative, a.gap_sm]}>
|
||||||
{images.map((image, index) => (
|
{images.map((image, index) => (
|
||||||
<AutoSizedImage
|
<AutoSizedImage
|
||||||
key={image.thumb}
|
key={image.thumb + index}
|
||||||
crop={
|
crop={
|
||||||
viewContext === PostEmbedViewContext.ThreadHighlighted
|
viewContext === PostEmbedViewContext.ThreadHighlighted
|
||||||
? 'none'
|
? 'none'
|
||||||
@@ -238,7 +248,7 @@ export function Gallery({
|
|||||||
alwaysBounceVertical={false}
|
alwaysBounceVertical={false}
|
||||||
scrollEventThrottle={16}
|
scrollEventThrottle={16}
|
||||||
data={images}
|
data={images}
|
||||||
keyExtractor={item => item.thumb}
|
keyExtractor={(item, index) => item.thumb + index}
|
||||||
renderItem={({item, index}) => {
|
renderItem={({item, index}) => {
|
||||||
return (
|
return (
|
||||||
<GalleryImage
|
<GalleryImage
|
||||||
@@ -267,7 +277,7 @@ export function Gallery({
|
|||||||
onPress
|
onPress
|
||||||
? () => {
|
? () => {
|
||||||
ax.metric('post:gallery:openLightbox', {
|
ax.metric('post:gallery:openLightbox', {
|
||||||
imageIndex: index,
|
fromImage: index + 1, // convert to 1-based index for easier analysis
|
||||||
totalImages: images.length,
|
totalImages: images.length,
|
||||||
})
|
})
|
||||||
const refs: AnimatedRef<any>[] = []
|
const refs: AnimatedRef<any>[] = []
|
||||||
|
|||||||
Reference in New Issue
Block a user