({
postId,
source: postSource,
isCompact = false,
}: AdAsCommentProps)
| 35 | isCompact?: boolean; |
| 36 | } |
| 37 | export const AdAsComment = ({ |
| 38 | postId, |
| 39 | source: postSource, |
| 40 | isCompact = false, |
| 41 | }: AdAsCommentProps): ReactElement => { |
| 42 | const { logEvent } = useLogContext(); |
| 43 | const { user } = useAuthContext(); |
| 44 | const { isPlus } = usePlusSubscription(); |
| 45 | const isAdFree = shouldSkipSourceAds(postSource); |
| 46 | |
| 47 | const { |
| 48 | data: ad, |
| 49 | isFetching, |
| 50 | isRefetching, |
| 51 | } = useAdQuery({ |
| 52 | placement: AdPlacement.PostComment, |
| 53 | queryKey: generateQueryKey(RequestKey.Ads, user, postId), |
| 54 | enabled: !isAdFree, |
| 55 | staleTime: StaleTime.OneHour, |
| 56 | refreshOnPageView: true, |
| 57 | }); |
| 58 | |
| 59 | const onAdAction = useCallback( |
| 60 | (action: AdActions, extra?: Record<string, unknown>) => { |
| 61 | if (!ad) { |
| 62 | return; |
| 63 | } |
| 64 | logEvent( |
| 65 | adLogEvent(action, ad, { |
| 66 | extra: { |
| 67 | origin: 'post page', |
| 68 | ...extra, |
| 69 | }, |
| 70 | }), |
| 71 | ); |
| 72 | }, |
| 73 | [logEvent, ad], |
| 74 | ); |
| 75 | |
| 76 | const onViewable = useCallback( |
| 77 | (data: ViewabilityData) => |
| 78 | onAdAction(AdActions.Viewable, viewabilityLogExtra(data)), |
| 79 | [onAdAction], |
| 80 | ); |
| 81 | |
| 82 | const promotedText = useScrambler( |
| 83 | !ad ? undefined : `Promoted by ${ad.source}`, |
| 84 | ); |
| 85 | |
| 86 | useEffect(() => { |
| 87 | if (!ad || ad?.impressionStatus === ImpressionStatus.LOGGED) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | onAdAction(AdActions.Impression); |
| 92 | ad.impressionStatus = ImpressionStatus.LOGGED; |
| 93 | }, [ad, onAdAction]); |
| 94 |
nothing calls this directly
no test coverage detected