Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dom not ready crashing the app #614

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions app/src/routes/editor/WebviewArea/BrowserControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { useEffect, useState } from 'react';
import { Links } from '/common/constants';

interface BrowserControlsProps {
webviewRef: React.RefObject<Electron.WebviewTag>;
webviewRef: React.RefObject<Electron.WebviewTag> | null;
webviewSrc: string;
setWebviewSrc: React.Dispatch<React.SetStateAction<string>>;
setWebviewSize: React.Dispatch<React.SetStateAction<{ width: number; height: number }>>;
Expand Down Expand Up @@ -64,7 +64,7 @@ function BrowserControls({
}, [webviewSrc]);

function goForward() {
const webview = webviewRef.current as Electron.WebviewTag | null;
const webview = webviewRef?.current as Electron.WebviewTag | null;
if (!webview) {
return;
}
Expand All @@ -74,15 +74,15 @@ function BrowserControls({
}

function reload() {
const webview = webviewRef.current as Electron.WebviewTag | null;
const webview = webviewRef?.current as Electron.WebviewTag | null;
if (!webview) {
return;
}
webview.reload();
}

function goBack() {
const webview = webviewRef.current as Electron.WebviewTag | null;
const webview = webviewRef?.current as Electron.WebviewTag | null;
if (!webview) {
return;
}
Expand All @@ -106,7 +106,7 @@ function BrowserControls({
}

function handleBlur(e: React.FocusEvent<HTMLInputElement>) {
const webview = webviewRef.current as Electron.WebviewTag | null;
const webview = webviewRef?.current as Electron.WebviewTag | null;
if (!webview) {
return;
}
Expand All @@ -118,7 +118,7 @@ function BrowserControls({
}

function toggleTheme() {
const webview = webviewRef.current as Electron.WebviewTag | null;
const webview = webviewRef?.current as Electron.WebviewTag | null;
if (!webview) {
return;
}
Expand All @@ -127,7 +127,7 @@ function BrowserControls({
}

function resizeToPreset(width: number, height: number, presetName: string) {
const webview = webviewRef.current as Electron.WebviewTag | null;
const webview = webviewRef?.current as Electron.WebviewTag | null;
if (webview) {
setWebviewSize({ width, height });
setSelectedPreset(presetName);
Expand All @@ -149,15 +149,15 @@ function BrowserControls({
variant="outline"
className="bg-background-secondary/60"
onClick={goBack}
disabled={!webviewRef.current?.canGoBack()}
disabled={!webviewRef?.current?.canGoBack()}
>
<ArrowLeftIcon />
</Button>
<Button
variant="outline"
className="bg-background-secondary/60"
onClick={goForward}
disabled={!webviewRef.current?.canGoForward()}
disabled={!webviewRef?.current?.canGoForward()}
>
<ArrowRightIcon />
</Button>
Expand Down
6 changes: 4 additions & 2 deletions app/src/routes/editor/WebviewArea/Frame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ const Frame = observer(
}) => {
const RETRY_TIMEOUT = 3000;
const editorEngine = useEditorEngine();
const webviewRef = useRef<Electron.WebviewTag>(null);
const webviewRef = useRef<Electron.WebviewTag | null>(null);

const [selected, setSelected] = useState<boolean>(false);
const [focused, setFocused] = useState<boolean>(false);
const [hovered, setHovered] = useState<boolean>(false);
const [darkmode, setDarkmode] = useState<boolean>(false);
const [domReady, setDomReady] = useState(false);
const [domFailed, setDomFailed] = useState(false);
const [onlookEnabled, setOnlookEnabled] = useState(false);

Expand Down Expand Up @@ -81,6 +82,7 @@ const Frame = observer(
if (!webview) {
return;
}
setDomReady(true);
webview.setZoomLevel(0);
const body = await editorEngine.dom.getBodyFromWebview(webview);
setDomFailed(body.children.length === 0);
Expand Down Expand Up @@ -119,7 +121,7 @@ const Frame = observer(
return (
<div className="flex flex-col space-y-4">
<BrowserControls
webviewRef={webviewRef}
webviewRef={domReady ? webviewRef : null}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see what you did. Apologies, I missed this. This makes sense and adds a level of security. I'd hate to have an error thrown needlessly.

webviewSrc={webviewSrc}
setWebviewSrc={setWebviewSrc}
setWebviewSize={setWebviewSize}
Expand Down