pleroma/app/soapbox/features/ads/components/ad.tsx

79 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-08-01 15:55:13 -07:00
import React, { useEffect } from 'react';
2022-08-02 18:52:27 -07:00
import { FormattedMessage, defineMessages, useIntl } from 'react-intl';
2022-08-01 15:35:04 -07:00
2022-08-02 18:52:27 -07:00
import { Stack, HStack, Card, Tooltip, Avatar, Text, Icon } from 'soapbox/components/ui';
import IconButton from 'soapbox/components/ui/icon-button/icon-button';
2022-08-02 18:14:28 -07:00
import StatusCard from 'soapbox/features/status/components/card';
2022-08-01 15:35:04 -07:00
import { useAppSelector } from 'soapbox/hooks';
import type { Card as CardEntity } from 'soapbox/types/entities';
2022-08-02 18:52:27 -07:00
const messages = defineMessages({
tooltip: { id: 'sponsored.tooltip', defaultMessage: '{siteTitle} displays ads to help fund our service.' },
});
2022-08-01 15:35:04 -07:00
interface IAd {
2022-08-01 15:55:13 -07:00
/** Embedded ad data in Card format (almost like OEmbed). */
2022-08-01 15:35:04 -07:00
card: CardEntity,
2022-08-01 15:55:13 -07:00
/** Impression URL to fetch upon display. */
impression?: string,
2022-08-01 15:35:04 -07:00
}
/** Displays an ad in sponsored post format. */
2022-08-01 15:55:13 -07:00
const Ad: React.FC<IAd> = ({ card, impression }) => {
2022-08-02 18:52:27 -07:00
const intl = useIntl();
2022-08-01 15:35:04 -07:00
const instance = useAppSelector(state => state.instance);
2022-08-01 15:55:13 -07:00
// Fetch the impression URL (if any) upon displaying the ad.
// It's common for ad providers to provide this.
useEffect(() => {
if (impression) {
fetch(impression);
}
}, [impression]);
2022-08-01 15:35:04 -07:00
return (
<Card className='p-5' variant='rounded'>
<Stack space={4}>
<HStack alignItems='center' space={3}>
<Avatar src={instance.thumbnail} size={42} />
2022-08-02 18:52:27 -07:00
<Stack grow>
2022-08-01 15:35:04 -07:00
<HStack space={1}>
<Text size='sm' weight='semibold' truncate>
{instance.title}
</Text>
<Icon
className='w-5 h-5 stroke-accent-500'
src={require('@tabler/icons/timeline.svg')}
/>
</HStack>
<Stack>
<HStack alignItems='center' space={1}>
<Text theme='muted' size='sm' truncate>
<FormattedMessage id='sponsored.subtitle' defaultMessage='Sponsored post' />
</Text>
</HStack>
</Stack>
</Stack>
2022-08-02 18:52:27 -07:00
<Stack justifyContent='center'>
<Tooltip text={intl.formatMessage(messages.tooltip, { siteTitle: instance.title })}>
<IconButton
iconClassName='stroke-gray-600 w-6 h-6'
src={require('@tabler/icons/info-circle.svg')}
/>
</Tooltip>
</Stack>
2022-08-01 15:35:04 -07:00
</HStack>
2022-08-02 18:14:28 -07:00
<StatusCard card={card} onOpenMedia={() => {}} horizontal />
2022-08-01 15:35:04 -07:00
</Stack>
</Card>
);
};
export default Ad;