Skip to content

Commit

Permalink
Render EmbedFallback when third-party consent is not given
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriyyakym committed Jan 21, 2025
1 parent 06b594b commit 73c182a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
13 changes: 13 additions & 0 deletions components/ContentRenderer/components/Embed.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
/* eslint-disable @next/next/no-img-element */

'use client';

import { MEDIA, useAnalytics } from '@prezly/analytics-nextjs';
import { Elements } from '@prezly/content-renderer-react-js';
import type { EmbedNode } from '@prezly/story-content-format';
import { useCallback, useRef } from 'react';

import { useCookieConsent } from '@/modules/CookieConsent/CookieConsentContext';
import { ConsentCategory } from '@/modules/CookieConsent/types';

import { EmbedFallback } from './EmbedFallback';

interface Props {
node: EmbedNode;
}

export function Embed({ node }: Props) {
const { track } = useAnalytics();
const isEventTracked = useRef(false);
const { consent } = useCookieConsent();
const hasThirdPartyConsent = consent?.categories.includes(ConsentCategory.THIRD_PARTY_COOKIES);

const handlePlay = useCallback(() => {
if (!isEventTracked.current) {
Expand All @@ -20,5 +29,9 @@ export function Embed({ node }: Props) {
}
}, [track]);

if (!hasThirdPartyConsent) {
return <EmbedFallback node={node} />;
}

return <Elements.Embed node={node} onPlay={handlePlay} />;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.imageFallback {
display: block;
margin: $spacing-4 0;
}
27 changes: 27 additions & 0 deletions components/ContentRenderer/components/EmbedFallback.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable @next/next/no-img-element */

'use client';

import type { EmbedNode } from '@prezly/story-content-format';
import NextLink from 'next/link';

import styles from './EmbedFallback.module.scss';

interface Props {
node: EmbedNode;
}

export function EmbedFallback({ node }: Props) {
if (node.oembed.screenshot_url) {
return (
<NextLink className={styles.imageFallback} href={node.url} target="__blank">
<img
src={node.oembed.screenshot_url}
alt={node.oembed.title || node.oembed.description || ''}
/>
</NextLink>
);
}

return null;
}

0 comments on commit 73c182a

Please sign in to comment.