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 {type Metrics, useAnalytics} from '#/analytics'
5
6/**
7 * Hook that returns a callback to track post:view events.
8 * Handles deduplication so the same post URI is only tracked once per mount.
9 *
10 * @param logContext - The context where the post is being viewed
11 * @returns A callback that accepts a post and logs the view event
12 */
13export function usePostViewTracking(
14 logContext: Metrics['post:view']['logContext'],
15) {
16 const ax = useAnalytics()
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 ax.metric('post:view', {
25 uri: post.uri,
26 authorDid: post.author.did,
27 logContext,
28 })
29 },
30 [ax, logContext],
31 )
32
33 return trackPostView
34}