Skip to content

Commit 035667c

Browse files
Rik Rootsfrederickobrien
authored andcommitted
Tidy code
1 parent 0faf7a4 commit 035667c

File tree

1 file changed

+53
-41
lines changed

1 file changed

+53
-41
lines changed

dotcom-rendering/src/components/InteractiveBlockComponent.importable.tsx

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -326,52 +326,72 @@ export const InteractiveBlockComponent = ({
326326
const placeholderLinkRef = useRef<HTMLAnchorElement>(null);
327327
const [loaded, setLoaded] = useState(false);
328328
const { darkModeAvailable } = useConfig();
329+
330+
// Define some one-time flags
331+
const isDatawrapperGraphic =
332+
url &&
333+
url.substring &&
334+
url.includes('interactive.guim.co.uk/datawrapper');
335+
336+
const isUploaderEmbedPath =
337+
url &&
338+
url.substring &&
339+
url.includes('interactive.guim.co.uk/uploader/embed/');
340+
341+
const scriptUrlIsBoot =
342+
scriptUrl &&
343+
scriptUrl.substring &&
344+
scriptUrl.includes(
345+
'interactive.guim.co.uk/embed/iframe-wrapper/0.1/boot.js',
346+
);
347+
329348
useOnce(() => {
330349
// We've brought the behavior from boot.js into this file to avoid loading 2 extra scripts
331-
if (
332-
scriptUrl ===
333-
'https://interactive.guim.co.uk/embed/iframe-wrapper/0.1/boot.js' &&
334-
url &&
335-
placeholderLinkRef.current
336-
) {
350+
351+
// Define additional one-time flags - these depend on window/document objects
352+
const isRunningInWebEnvironment =
353+
!document.querySelector('.ios') &&
354+
!document.querySelector('.android');
355+
356+
const prefersDarkScheme = window.matchMedia(
357+
'(prefers-color-scheme: dark)',
358+
).matches;
359+
const requiresDarkMode = darkModeAvailable && prefersDarkScheme;
360+
361+
if (url && scriptUrlIsBoot && placeholderLinkRef.current) {
362+
// Prepare for graphic url dynamic updates
363+
const graphicUrl = new URL(url);
364+
const graphicUrlParams = new URLSearchParams(graphicUrl.search);
365+
366+
// Begin creating new iframe element
337367
const iframe = document.createElement('iframe');
338368
iframe.style.width = '100%';
339369
iframe.style.border = 'none';
340370
iframe.height = decideHeight(role).toString();
341371
iframe.title = caption ?? alt ?? 'Interactive Content';
342-
if (url.startsWith('http:')) {
343-
iframe.src = url.replace('http:', 'https:');
344-
} else {
345-
iframe.src = url;
346-
}
347372

348-
// Datawrapper-specific fix to suppress scrollbars appearing
349-
if (url.includes('datawrapper')) iframe.scrolling = 'no';
373+
// Fix for Datawrapper graphic
374+
if (isDatawrapperGraphic) {
375+
iframe.scrolling = 'no';
376+
}
350377

351-
// Bespoke dark mode logic for Datawrapper and ai2html embeds on web
352-
// This should be removed if/when dark mode is implements on the website
353-
const isai2html = url.includes(
354-
'interactive.guim.co.uk/uploader/embed/',
355-
);
356-
if (
357-
!document.querySelector('.ios') &&
358-
!document.querySelector('.android') &&
359-
(url.includes('datawrapper') || isai2html)
360-
) {
361-
const prefersDarkScheme = window.matchMedia(
362-
'(prefers-color-scheme: dark)',
363-
).matches;
364-
const darkMode = darkModeAvailable && prefersDarkScheme;
365-
if (!darkMode) {
366-
iframe.src +=
367-
(isai2html ? '/' : '') +
368-
(iframe.src.includes('?') ? '&' : '?') +
369-
'dark=false';
378+
// Fix darkmode for web environment
379+
if (isRunningInWebEnvironment && !requiresDarkMode) {
380+
if (isDatawrapperGraphic || isUploaderEmbedPath) {
381+
// Add the 'dark=false' search param
382+
graphicUrlParams.set('dark', 'false');
370383
}
371384
}
372385

373-
setupWindowListeners(iframe);
386+
// Always serve graphic over https, not http
387+
graphicUrl.protocol = 'https:';
388+
389+
graphicUrl.search = graphicUrlParams.toString();
390+
391+
// Finalise new iframe element
392+
iframe.src = graphicUrl.href;
374393

394+
setupWindowListeners(iframe);
375395
wrapperRef.current?.prepend(iframe);
376396

377397
setLoaded(true);
@@ -396,18 +416,10 @@ export const InteractiveBlockComponent = ({
396416
}
397417
},
398418
);
399-
400419
setLoaded(true);
401420
}
402421
}, [loaded]);
403422

404-
const isDatawrapperGraphic =
405-
url == undefined
406-
? false
407-
: /^https?:\/\/interactive\.guim\.co\.uk\/datawrapper(-test)?\/embed/.test(
408-
url,
409-
);
410-
411423
return (
412424
<>
413425
<figure

0 commit comments

Comments
 (0)