({
post,
shouldTrack = true,
}: UseTrackPostViewProps)
| 10 | } |
| 11 | |
| 12 | export const useTrackPostView = ({ |
| 13 | post, |
| 14 | shouldTrack = true, |
| 15 | }: UseTrackPostViewProps): void => { |
| 16 | const { user } = useAuthContext(); |
| 17 | const onSendViewPost = useViewPost(); |
| 18 | |
| 19 | useEffect(() => { |
| 20 | if (!shouldTrack || !post?.id || !user?.id) { |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | // Nothing awaits this, so a rejected `mutateAsync` would surface as an |
| 25 | // unhandled rejection. A lost view is not worth reporting. |
| 26 | onSendViewPost(post.id).catch(() => undefined); |
| 27 | emitPatchyEvent({ type: 'post_open', tags: post.tags, post }); |
| 28 | // Tags ride along with the id; a new array for the same post is no reason |
| 29 | // to count the view again. |
| 30 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 31 | }, [onSendViewPost, post?.id, shouldTrack, user?.id]); |
| 32 | }; |
no test coverage detected