forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {useCallback, useRef} from 'react'
2import {type AppBskyFeedDefs} from '@atproto/api'
3
4import {logger} from '#/logger'
5import {type MetricEvents} from '#/logger/metrics'
6
7/**
8 * Hook that returns a callback to track post:view events.
9 * Handles deduplication so the same post URI is only tracked once per mount.
10 *
11 * @param logContext - The context where the post is being viewed
12 * @returns A callback that accepts a post and logs the view event
13 */
14export function usePostViewTracking(
15 logContext: MetricEvents['post:view']['logContext'],
16) {
17 const seenUrisRef = useRef(new Set<string>())
18
19 const trackPostView = useCallback(
20 (post: AppBskyFeedDefs.PostView) => {
21 if (seenUrisRef.current.has(post.uri)) return
22 seenUrisRef.current.add(post.uri)
23
24 logger.metric(
25 'post:view',
26 {
27 uri: post.uri,
28 authorDid: post.author.did,
29 logContext,
30 },
31 {statsig: false},
32 )
33 },
34 [logContext],
35 )
36
37 return trackPostView
38}