fix: keep Browser iframe in sync with url bar on page changes

This commit is contained in:
Derrick Hammer 2023-10-16 20:37:59 -04:00
parent cf826dce82
commit b4ae99b74c
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 17 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import {
useCallback, useCallback,
useContext, useContext,
useEffect, useEffect,
useRef,
useState, useState,
} from "react"; } from "react";
import { import {
@ -179,16 +180,31 @@ export function Navigator() {
} }
export function Browser() { export function Browser() {
const { url } = useBrowserState(); const { url, setUrl } = useBrowserState();
const status = useLumeStatus(); const status = useLumeStatus();
const auth = useAuth(); const auth = useAuth();
const iframeRef = useRef<HTMLIFrameElement>(null);
useEffect(() => { useEffect(() => {
boot(status, auth); boot(status, auth);
}, []); }, []);
const handleIframeLoad = () => {
try {
const newUrl = iframeRef?.current?.contentWindow?.location.href as string;
setUrl(newUrl);
} catch (e) {
// This will catch errors related to cross-origin requests, in which case we can't access the iframe's contentWindow.location
console.warn(
"Couldn't access iframe URL due to cross-origin restrictions:",
e,
);
}
};
return ( return (
<iframe <iframe
onLoad={handleIframeLoad}
src={url ? `/browse/${url}` : "about:blank"} src={url ? `/browse/${url}` : "about:blank"}
className="w-full h-full" className="w-full h-full"
></iframe> ></iframe>