Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | 2x 19x 2x 2x 19x 19x 19x 19x 2x 2x 17x 17x 17x 17x 17x 17x 17x 17x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 9x 9x 9x 19x 6x 6x 6x 19x 8x 2x 2x 2x 2x 2x 2x 19x 6x 1x 1x 1x 1x 1x 19x 7x 19x 3x 3x 19x 5x 5x 2x 2x 19x 19x | // src/IframeWidget.tsx
import { useState, useEffect, useMemo, useCallback, useRef } from 'react';
import styles from './IframeWidget.module.css';
import {
formatInputFormattedTemplate,
RefreshingBadge,
useHostVariables,
useWidgetRefreshRate
} from '@ska-octopus-widget-sdk/widget-sdk';
export interface IframeWidgetProps {
instanceId?: string;
url?: unknown;
}
const DEFAULT_IFRAME_URL = 'https://www.openstreetmap.org/export/embed.html';
const hasScheme = (value: string) => /^[a-z][a-z0-9+.-]*:/i.test(value);
const looksLikeHost = (value: string) =>
/^(localhost(?::\d+)?|[a-z0-9-]+(?:\.[a-z0-9-]+)+)(?:\/.*)?$/i.test(value);
function resolveIframeUrl(raw: unknown): string {
const input = String(raw ?? '').trim();
Iif (!input) return DEFAULT_IFRAME_URL;
let candidate = input;
if (!hasScheme(input)) {
// Prevent accidental self-embedding from relative paths/plain tokens.
Eif (
input.startsWith('/') ||
input.startsWith('./') ||
input.startsWith('../') ||
input.startsWith('?') ||
input.startsWith('#') ||
!looksLikeHost(input)
) {
return DEFAULT_IFRAME_URL;
}
candidate = `https://${input}`;
}
let parsed: URL;
try {
parsed = new URL(candidate);
} catch {
return DEFAULT_IFRAME_URL;
}
Iif (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') return DEFAULT_IFRAME_URL;
Eif (typeof window !== 'undefined') {
try {
const current = new URL(window.location.href);
// Avoid recursive embedding of the current application page.
Iif (
parsed.origin === current.origin &&
parsed.pathname === current.pathname &&
parsed.search === current.search
) {
return 'about:blank';
}
} catch {
// Ignore URL parsing issues from host environment.
}
}
return parsed.toString();
}
export default function IframeWidget(config: IframeWidgetProps) {
const { url } = useMemo(() => config, [config]);
const variables = useHostVariables();
const refreshRate = useWidgetRefreshRate(config.instanceId ?? '');
const safeUrl = useMemo(() => {
const formatted = formatInputFormattedTemplate({
template: url,
variables,
fallback: typeof url === 'string' && url.trim() ? url : DEFAULT_IFRAME_URL
});
return resolveIframeUrl(formatted);
}, [url, variables]);
// Double-buffer: two iframes, swap z-index when the background one finishes loading
const [frontIsA, setFrontIsA] = useState(true);
const [srcA, setSrcA] = useState<string>('');
const [srcB, setSrcB] = useState<string>('');
const [aReady, setAReady] = useState(false);
const [bReady, setBReady] = useState(false);
const [refreshing, setRefreshing] = useState(false);
const frontIsARef = useRef(frontIsA);
const prevSafeUrlRef = useRef(safeUrl);
const bust = useCallback((base: string) => {
const hasQuery = base.includes('?');
const sep = hasQuery ? '&' : '?';
return `${base}${sep}_ts=${Date.now()}`;
}, []);
// Initial load into A
useEffect(() => {
setSrcA(bust(safeUrl));
setAReady(false);
setBReady(false);
}, [safeUrl, bust]);
// Periodic refresh: load new content into the background iframe, then swap
useEffect(() => {
if (!refreshRate || refreshRate <= 0) return;
const id = window.setInterval(() => {
setRefreshing(true);
if (frontIsA) {
setSrcB(bust(safeUrl));
setBReady(false);
} else E{
setSrcA(bust(safeUrl));
setAReady(false);
}
}, refreshRate * 1000);
return () => window.clearInterval(id);
}, [refreshRate, safeUrl, bust, frontIsA]);
// Also refresh when the URL changes (smoothly)
useEffect(() => {
if (prevSafeUrlRef.current === safeUrl) return;
prevSafeUrlRef.current = safeUrl;
// Load new URL in background and then swap
setRefreshing(true);
if (frontIsARef.current) {
setSrcB(bust(safeUrl));
setBReady(false);
} else E{
setSrcA(bust(safeUrl));
setAReady(false);
}
}, [safeUrl, bust]);
useEffect(() => {
frontIsARef.current = frontIsA;
}, [frontIsA]);
const onLoadA = () => {
setAReady(true);
Iif (refreshing && !frontIsA) {
setFrontIsA(true);
setRefreshing(false);
}
};
const onLoadB = () => {
setBReady(true);
if (refreshing && frontIsA) {
setFrontIsA(false);
setRefreshing(false);
}
};
const showSpinner = !aReady && !bReady; // only before first content appears
return (
<div className={styles.container} style={{ position: 'relative' }}>
{showSpinner && (
<div className={styles.center}>
<svg className={styles.spinner} viewBox="0 0 50 50" aria-label="Loading">
<circle cx="25" cy="25" r="20" />
</svg>
</div>
)}
{/* Iframe A */}
<iframe
title="iframe-a"
src={srcA || undefined}
className={styles.iframe}
style={{
position: 'absolute',
inset: 0,
zIndex: frontIsA ? 2 : 1,
opacity: frontIsA ? 1 : 0,
transition: 'opacity 120ms linear'
}}
loading="eager"
onLoad={onLoadA}
sandbox="allow-same-origin allow-scripts allow-popups allow-pointer-lock allow-forms"
referrerPolicy="no-referrer-when-downgrade"
allowFullScreen
/>
{/* Iframe B */}
<iframe
title="iframe-b"
src={srcB || undefined}
className={styles.iframe}
style={{
position: 'absolute',
inset: 0,
zIndex: frontIsA ? 1 : 2,
opacity: frontIsA ? 0 : 1,
transition: 'opacity 120ms linear'
}}
loading="eager"
onLoad={onLoadB}
sandbox="allow-same-origin allow-scripts allow-popups allow-pointer-lock allow-forms"
referrerPolicy="no-referrer-when-downgrade"
allowFullScreen
/>
<RefreshingBadge active={refreshing} text="Refreshing…" position="top-right" />
</div>
);
}
|