2022-10-20 17:39:22 +00:00
|
|
|
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
2022-08-03 16:53:17 +00:00
|
|
|
import React, { useState, useEffect, useRef } from 'react';
|
2022-08-03 16:42:54 +00:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2022-11-30 23:43:30 +00:00
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
import HoverRefWrapper from 'soapbox/components/hover-ref-wrapper';
|
2022-08-01 22:35:04 +00:00
|
|
|
|
2022-11-25 18:28:43 +00:00
|
|
|
import { Avatar, Card, HStack, Icon, IconButton, Stack, Text } from 'soapbox/components/ui';
|
2022-08-03 01:14:28 +00:00
|
|
|
import StatusCard from 'soapbox/features/status/components/card';
|
2022-11-26 16:38:16 +00:00
|
|
|
import { useInstance } from 'soapbox/hooks';
|
2022-08-01 22:35:04 +00:00
|
|
|
|
2022-10-20 21:29:14 +00:00
|
|
|
import type { Ad as AdEntity } from 'soapbox/types/soapbox';
|
2022-08-01 22:35:04 +00:00
|
|
|
|
|
|
|
interface IAd {
|
2022-10-20 21:29:14 +00:00
|
|
|
ad: AdEntity,
|
2022-08-01 22:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Displays an ad in sponsored post format. */
|
2022-10-20 21:29:14 +00:00
|
|
|
const Ad: React.FC<IAd> = ({ ad }) => {
|
2022-08-26 18:58:02 +00:00
|
|
|
const queryClient = useQueryClient();
|
2022-11-26 16:38:16 +00:00
|
|
|
const instance = useInstance();
|
2022-08-01 22:35:04 +00:00
|
|
|
|
2022-08-26 18:58:02 +00:00
|
|
|
const timer = useRef<NodeJS.Timeout | undefined>(undefined);
|
2022-08-03 16:53:17 +00:00
|
|
|
const infobox = useRef<HTMLDivElement>(null);
|
2022-08-03 16:42:54 +00:00
|
|
|
const [showInfo, setShowInfo] = useState(false);
|
|
|
|
|
2022-10-20 17:39:22 +00:00
|
|
|
// Fetch the impression URL (if any) upon displaying the ad.
|
|
|
|
// Don't fetch it more than once.
|
2022-10-20 21:29:14 +00:00
|
|
|
useQuery(['ads', 'impression', ad.impression], () => {
|
|
|
|
if (ad.impression) {
|
|
|
|
return fetch(ad.impression);
|
2022-10-20 17:39:22 +00:00
|
|
|
}
|
|
|
|
}, { cacheTime: Infinity, staleTime: Infinity });
|
|
|
|
|
2022-08-26 18:58:02 +00:00
|
|
|
/** Invalidate query cache for ads. */
|
|
|
|
const bustCache = (): void => {
|
|
|
|
queryClient.invalidateQueries(['ads']);
|
|
|
|
};
|
|
|
|
|
2022-08-03 16:53:17 +00: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 18:58:02 +00:00
|
|
|
// Wait until the ad expires, then invalidate cache.
|
|
|
|
useEffect(() => {
|
2022-10-20 21:29:14 +00:00
|
|
|
if (ad.expires_at) {
|
|
|
|
const delta = new Date(ad.expires_at).getTime() - (new Date()).getTime();
|
2022-08-26 18:58:02 +00:00
|
|
|
timer.current = setTimeout(bustCache, delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (timer.current) {
|
|
|
|
clearTimeout(timer.current);
|
|
|
|
}
|
|
|
|
};
|
2022-10-20 21:29:14 +00:00
|
|
|
}, [ad.expires_at]);
|
2022-08-26 18:58:02 +00:00
|
|
|
|
2022-08-03 16:42:54 +00:00
|
|
|
return (
|
|
|
|
<div className='relative'>
|
2022-08-22 16:11:01 +00:00
|
|
|
<Card className='py-6 sm:p-5' variant='rounded'>
|
2022-08-03 16:42:54 +00:00
|
|
|
<Stack space={4}>
|
|
|
|
<HStack alignItems='center' space={3}>
|
2022-11-30 23:43:30 +00:00
|
|
|
{ad.account ? (
|
|
|
|
<HoverRefWrapper accountId={ad.account.id} inline>
|
|
|
|
<Link to={`/@${ad.account.acct}`}>
|
|
|
|
<Avatar src={ad.account.avatar} size={42} />
|
|
|
|
</Link>
|
|
|
|
</HoverRefWrapper>
|
|
|
|
) : (
|
|
|
|
<Avatar src={instance.thumbnail} size={42} />
|
|
|
|
)}
|
2022-08-01 22:35:04 +00:00
|
|
|
|
2022-08-03 16:42:54 +00:00
|
|
|
<Stack grow>
|
|
|
|
<HStack space={1}>
|
|
|
|
<Text size='sm' weight='semibold' truncate>
|
2022-11-30 23:43:30 +00:00
|
|
|
{ad.account ? (
|
|
|
|
<HoverRefWrapper accountId={ad.account.id} inline>
|
|
|
|
<Link
|
|
|
|
to={`/@${ad.account.acct}`}
|
|
|
|
dangerouslySetInnerHTML={{ __html: ad.account.display_name_html }}
|
|
|
|
/>
|
|
|
|
</HoverRefWrapper>
|
|
|
|
) : (
|
|
|
|
instance.title
|
|
|
|
)}
|
2022-08-01 22:35:04 +00:00
|
|
|
</Text>
|
2022-08-03 16:42:54 +00:00
|
|
|
|
|
|
|
<Icon
|
|
|
|
className='w-5 h-5 stroke-accent-500'
|
|
|
|
src={require('@tabler/icons/timeline.svg')}
|
|
|
|
/>
|
2022-08-01 22:35:04 +00:00
|
|
|
</HStack>
|
2022-08-03 16:42:54 +00: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 22:35:04 +00:00
|
|
|
</Stack>
|
2022-08-03 01:52:27 +00:00
|
|
|
|
2022-08-03 16:42:54 +00:00
|
|
|
<Stack justifyContent='center'>
|
2022-08-03 01:52:27 +00:00
|
|
|
<IconButton
|
|
|
|
iconClassName='stroke-gray-600 w-6 h-6'
|
|
|
|
src={require('@tabler/icons/info-circle.svg')}
|
2022-08-03 16:42:54 +00:00
|
|
|
onClick={handleInfoButtonClick}
|
2022-08-03 01:52:27 +00:00
|
|
|
/>
|
2022-08-03 16:42:54 +00:00
|
|
|
</Stack>
|
|
|
|
</HStack>
|
2022-08-01 22:35:04 +00:00
|
|
|
|
2022-10-20 21:29:14 +00:00
|
|
|
<StatusCard card={ad.card} onOpenMedia={() => {}} horizontal />
|
2022-08-03 16:42:54 +00:00
|
|
|
</Stack>
|
|
|
|
</Card>
|
|
|
|
|
|
|
|
{showInfo && (
|
2022-08-03 16:53:17 +00:00
|
|
|
<div ref={infobox} className='absolute top-5 right-5 max-w-[234px]'>
|
2022-08-03 16:42:54 +00: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 21:29:14 +00: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 16:42:54 +00:00
|
|
|
</Text>
|
|
|
|
</Stack>
|
|
|
|
</Card>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2022-08-01 22:35:04 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Ad;
|