({
item,
versionIdx
}: {
item: ItemWrapper
versionIdx: number
})
| 18 | import { Button } from './ui/button' |
| 19 | |
| 20 | export default function VersionPreview({ |
| 21 | item, |
| 22 | versionIdx |
| 23 | }: { |
| 24 | item: ItemWrapper |
| 25 | versionIdx: number |
| 26 | }) { |
| 27 | const iframeSrc = /127.0.0.1|localhost/.test(document.location.hostname) |
| 28 | ? 'http://localhost:7878' |
| 29 | : 'https://wandb.github.io' |
| 30 | const iframeRef = useRef<HTMLIFrameElement | null>(null) |
| 31 | const [isFrameReady, setIsFrameReady] = useState(false) |
| 32 | const isVisible = useAtomValue(historySidebarStateAtom) === 'versions' |
| 33 | const uiState = useAtomValue(uiStateAtom) |
| 34 | const isRendering = uiState.rendering |
| 35 | const uiTheme = useAtomValue(uiThemeAtom) |
| 36 | const theme = themes.find(t => t.name === uiTheme) ?? themes[0] |
| 37 | const [html, setHtml] = useState<string | undefined>() |
| 38 | const id = useMemo(() => nanoid(8), []) |
| 39 | const saveHistory = useSaveHistory() |
| 40 | const [currentVersionIdx] = useVersion(item) |
| 41 | const image = useAtomValue(imageDB.item(`image-${item.id}-${versionIdx}`)) |
| 42 | |
| 43 | const darkMode = useAtomValue(darkModeAtom) |
| 44 | const [previewDarkMode, setPreviewDarkMode] = useState<string>(darkMode) |
| 45 | |
| 46 | useEffect(() => { |
| 47 | if (darkMode === 'system') { |
| 48 | if (window.matchMedia('(prefers-color-scheme: dark)').matches) { |
| 49 | setPreviewDarkMode('dark') |
| 50 | } else { |
| 51 | setPreviewDarkMode('light') |
| 52 | } |
| 53 | } |
| 54 | if (['light', 'dark'].includes(previewDarkMode)) { |
| 55 | iframeRef.current?.contentWindow?.postMessage( |
| 56 | { action: 'toggle-dark-mode', mode: previewDarkMode }, |
| 57 | '*' |
| 58 | ) |
| 59 | } |
| 60 | iframeRef.current?.contentWindow?.postMessage( |
| 61 | { action: 'theme', theme }, |
| 62 | '*' |
| 63 | ) |
| 64 | }, [darkMode, previewDarkMode, theme]) |
| 65 | |
| 66 | useEffect(() => { |
| 67 | item |
| 68 | .html(versionIdx) |
| 69 | .then(hjs => setHtml(hjs?.html)) |
| 70 | .catch((error: unknown) => console.error('HTML parse error', error)) |
| 71 | }, [item, versionIdx, setHtml]) |
| 72 | |
| 73 | useEffect(() => { |
| 74 | const listener = (event: MessageEvent<IFrameEvent>) => { |
| 75 | // Only listen to events from our iframe |
| 76 | if (event.origin !== iframeSrc) return |
| 77 | if (event.data.id !== id) return |
nothing calls this directly
no test coverage detected