2022-10-20 10:39:22 -07:00
|
|
|
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
2023-01-23 14:09:45 -08:00
|
|
|
import axios from 'axios';
|
2022-08-03 09:53:17 -07:00
|
|
|
import React, { useState, useEffect, useRef } from 'react';
|
2022-08-03 09:42:54 -07:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2022-08-01 15:35:04 -07:00
|
|
|
|
2022-11-25 10:28:43 -08:00
|
|
|
import { Avatar, Card, HStack, Icon, IconButton, Stack, Text } from 'soapbox/components/ui';
|
2022-08-02 18:14:28 -07:00
|
|
|
import StatusCard from 'soapbox/features/status/components/card';
|
2022-11-26 08:38:16 -08:00
|
|
|
import { useInstance } from 'soapbox/hooks';
|
2022-10-05 12:15:16 -07:00
|
|
|
import { AdKeys } from 'soapbox/queries/ads';
|
2022-08-01 15:35:04 -07:00
|
|
|
|
2022-10-20 14:29:14 -07:00
|
|
|
import type { Ad as AdEntity } from 'soapbox/types/soapbox';
|
2022-08-01 15:35:04 -07:00
|
|
|
|
|
|
|
interface IAd {
|
2022-10-20 14:29:14 -07:00
|
|
|
ad: AdEntity,
|
2022-08-01 15:35:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Displays an ad in sponsored post format. */
|
2022-10-20 14:29:14 -07:00
|
|
|
const Ad: React.FC<IAd> = ({ ad }) => {
|
2022-08-26 11:58:02 -07:00
|
|
|
const queryClient = useQueryClient();
|
2022-11-26 08:38:16 -08:00
|
|
|
const instance = useInstance();
|
2022-08-01 15:35:04 -07:00
|
|
|
|
2022-08-26 11:58:02 -07:00
|
|
|
const timer = useRef<NodeJS.Timeout | undefined>(undefined);
|
2022-08-03 09:53:17 -07:00
|
|
|
const infobox = useRef<HTMLDivElement>(null);
|
2022-08-03 09:42:54 -07:00
|
|
|
const [showInfo, setShowInfo] = useState(false);
|
|
|
|
|
2022-10-20 10:39:22 -07:00
|
|
|
// Fetch the impression URL (if any) upon displaying the ad.
|
|
|
|
// Don't fetch it more than once.
|
2023-01-23 14:09:45 -08:00
|
|
|
useQuery(['ads', 'impression', ad.impression], async () => {
|
2022-10-20 14:29:14 -07:00
|
|
|
if (ad.impression) {
|
2023-01-23 14:09:45 -08:00
|
|
|
return await axios.get(ad.impression);
|
2022-10-20 10:39:22 -07:00
|
|
|
}
|
|
|
|
}, { cacheTime: Infinity, staleTime: Infinity });
|
|
|
|
|
2022-08-26 11:58:02 -07:00
|
|
|
/** Invalidate query cache for ads. */
|
|
|
|
const bustCache = (): void => {
|
2022-10-05 12:15:16 -07:00
|
|
|
queryClient.invalidateQueries(AdKeys.ads);
|
2022-08-26 11:58:02 -07:00
|
|
|
};
|
|
|
|
|
2022-08-03 09:53:17 -07:00
|
|
|
/** Toggle the info box on click. */
|
|
|
|
const handleInfoButtonClick: React.MouseEventHandler = () => {
|
|
|
|
setShowInfo(!showInfo);
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Hide the info box when clicked outside. */
|
|
|
|
const handleClickOutside = (event: MouseEvent) => {
|
|
|
|
if (event.target && infobox.current && !infobox.current.contains(event.target as any)) {
|
|
|
|
setShowInfo(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Hide the info box when clicked outside.
|
|
|
|
// https://stackoverflow.com/a/42234988
|
|
|
|
useEffect(() => {
|
|
|
|
document.addEventListener('mousedown', handleClickOutside);
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener('mousedown', handleClickOutside);
|
|
|
|
};
|
|
|
|
}, [infobox]);
|
|
|
|
|
2022-08-26 11:58:02 -07:00
|
|
|
// Wait until the ad expires, then invalidate cache.
|
|
|
|
useEffect(() => {
|
2022-10-20 14:29:14 -07:00
|
|
|
if (ad.expires_at) {
|
|
|
|
const delta = new Date(ad.expires_at).getTime() - (new Date()).getTime();
|
2022-08-26 11:58:02 -07:00
|
|
|
timer.current = setTimeout(bustCache, delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (timer.current) {
|
|
|
|
clearTimeout(timer.current);
|
|
|
|
}
|
|
|
|
};
|
2022-10-20 14:29:14 -07:00
|
|
|
}, [ad.expires_at]);
|
2022-08-26 11:58:02 -07:00
|
|
|
|
2022-08-03 09:42:54 -07:00
|
|
|
return (
|
|
|
|
<div className='relative'>
|
2022-08-22 09:11:01 -07:00
|
|
|
<Card className='py-6 sm:p-5' variant='rounded'>
|
2022-08-03 09:42:54 -07:00
|
|
|
<Stack space={4}>
|
|
|
|
<HStack alignItems='center' space={3}>
|
|
|
|
<Avatar src={instance.thumbnail} size={42} />
|
2022-08-01 15:35:04 -07:00
|
|
|
|
2022-08-03 09:42:54 -07:00
|
|
|
<Stack grow>
|
|
|
|
<HStack space={1}>
|
|
|
|
<Text size='sm' weight='semibold' truncate>
|
|
|
|
{instance.title}
|
2022-08-01 15:35:04 -07:00
|
|
|
</Text>
|
2022-08-03 09:42:54 -07:00
|
|
|
|
|
|
|
<Icon
|
2023-02-01 14:13:42 -08:00
|
|
|
className='h-5 w-5 stroke-accent-500'
|
2022-08-03 09:42:54 -07:00
|
|
|
src={require('@tabler/icons/timeline.svg')}
|
|
|
|
/>
|
2022-08-01 15:35:04 -07:00
|
|
|
</HStack>
|
2022-08-03 09:42:54 -07:00
|
|
|
|
|
|
|
<Stack>
|
|
|
|
<HStack alignItems='center' space={1}>
|
|
|
|
<Text theme='muted' size='sm' truncate>
|
|
|
|
<FormattedMessage id='sponsored.subtitle' defaultMessage='Sponsored post' />
|
|
|
|
</Text>
|
|
|
|
</HStack>
|
|
|
|
</Stack>
|
2022-08-01 15:35:04 -07:00
|
|
|
</Stack>
|
2022-08-02 18:52:27 -07:00
|
|
|
|
2022-08-03 09:42:54 -07:00
|
|
|
<Stack justifyContent='center'>
|
2022-08-02 18:52:27 -07:00
|
|
|
<IconButton
|
|
|
|
iconClassName='stroke-gray-600 w-6 h-6'
|
|
|
|
src={require('@tabler/icons/info-circle.svg')}
|
2022-08-03 09:42:54 -07:00
|
|
|
onClick={handleInfoButtonClick}
|
2022-08-02 18:52:27 -07:00
|
|
|
/>
|
2022-08-03 09:42:54 -07:00
|
|
|
</Stack>
|
|
|
|
</HStack>
|
2022-08-01 15:35:04 -07:00
|
|
|
|
2022-10-24 09:25:35 -07:00
|
|
|
<StatusCard card={ad.card} onOpenMedia={() => { }} horizontal />
|
2022-08-03 09:42:54 -07:00
|
|
|
</Stack>
|
|
|
|
</Card>
|
|
|
|
|
|
|
|
{showInfo && (
|
2022-08-03 09:53:17 -07:00
|
|
|
<div ref={infobox} className='absolute top-5 right-5 max-w-[234px]'>
|
2022-08-03 09:42:54 -07:00
|
|
|
<Card variant='rounded'>
|
|
|
|
<Stack space={2}>
|
|
|
|
<Text size='sm' weight='bold'>
|
|
|
|
<FormattedMessage id='sponsored.info.title' defaultMessage='Why am I seeing this ad?' />
|
|
|
|
</Text>
|
|
|
|
|
|
|
|
<Text size='sm' theme='muted'>
|
2022-10-20 14:29:14 -07:00
|
|
|
{ad.reason ? (
|
|
|
|
ad.reason
|
|
|
|
) : (
|
|
|
|
<FormattedMessage
|
|
|
|
id='sponsored.info.message'
|
|
|
|
defaultMessage='{siteTitle} displays ads to help fund our service.'
|
|
|
|
values={{ siteTitle: instance.title }}
|
|
|
|
/>
|
|
|
|
)}
|
2022-08-03 09:42:54 -07:00
|
|
|
</Text>
|
|
|
|
</Stack>
|
|
|
|
</Card>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2022-08-01 15:35:04 -07:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Ad;
|