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,
useContext,
useEffect,
useRef,
useState,
} from "react";
import {
@ -179,16 +180,31 @@ export function Navigator() {
}
export function Browser() {
const { url } = useBrowserState();
const { url, setUrl } = useBrowserState();
const status = useLumeStatus();
const auth = useAuth();
const iframeRef = useRef<HTMLIFrameElement>(null);
useEffect(() => {
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 (
<iframe
onLoad={handleIframeLoad}
src={url ? `/browse/${url}` : "about:blank"}
className="w-full h-full"
></iframe>