Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/addons/addons/gamepad/userscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export default async function ({addon, console, msg}) {
}

const renderer = vm.runtime.renderer;
if (!renderer) return;
const stageWidth = () => vm.runtime.stageWidth;
const stageHeight = () => vm.runtime.stageHeight;
const canvas = renderer.canvas;
Expand Down
26 changes: 20 additions & 6 deletions src/community/components/RouteErrorBoundary.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import PropTypes from 'prop-types';
import React from 'react';
import {Link} from 'react-router-dom';
import {House, RotateCcw, TriangleAlert} from 'lucide-react';
import {House, RotateCcw, TriangleAlert, WifiOff} from 'lucide-react';
import log from '../../lib/utils/log.js';
import {reportSiteError} from '../../lib/error-reporter.js';
import {isChunkLoadError} from '../../lib/lazy-with-retry.js';
import {useCommunityIntl as useCommunityText} from '../i18n.jsx';
import Button from './ui/Button.jsx';
import EmptyState from './ui/EmptyState.jsx';
import styles from '../pages/InfoPage.module.css';

const RouteErrorFallback = ({onRetry}) => {
const RouteErrorFallback = ({offline, onRetry}) => {
const {text: communityText} = useCommunityText();
return (
<main className={`${styles.page} ${styles.notFound}`}>
<EmptyState
icon={TriangleAlert}
title={communityText('Something went wrong on this page.')}
icon={offline ? WifiOff : TriangleAlert}
title={offline ?
communityText('This page could not load.') :
communityText('Something went wrong on this page.')}
action={(
<React.Fragment>
<Button variant="primary" onClick={onRetry}>
Expand All @@ -29,13 +32,16 @@ const RouteErrorFallback = ({onRetry}) => {
</React.Fragment>
)}
>
{communityText('The problem has been reported. Try again, or head back to the home page.')}
{offline ?
communityText('Part of the site did not download. Check your connection and try again.') :
communityText('The problem has been reported. Try again, or head back to the home page.')}
</EmptyState>
</main>
);
};

RouteErrorFallback.propTypes = {
offline: PropTypes.bool,
onRetry: PropTypes.func.isRequired
};

Expand All @@ -61,6 +67,7 @@ class RouteErrorBoundary extends React.Component {
const stack = (error && error.stack) || String(error);
const componentStack = (errorInfo && errorInfo.componentStack) || '';
log.error(`Community page crashed at ${this.props.resetKey}: ${stack}\nComponent stack: ${componentStack}`);
if (isChunkLoadError(error)) return;
reportSiteError({
message: (error && error.message) || String(error),
stack: (error && error.stack) || '',
Expand All @@ -74,7 +81,14 @@ class RouteErrorBoundary extends React.Component {
}

render () {
if (this.state.error) return <RouteErrorFallback onRetry={this.handleRetry} />;
if (this.state.error) {
return (
<RouteErrorFallback
offline={isChunkLoadError(this.state.error)}
onRetry={this.handleRetry}
/>
);
}
return this.props.children;
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/community/translations/_sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@
],
"Try again": [
"src/community/components/LanguagePicker.jsx:20",
"src/community/components/RouteErrorBoundary.jsx:23",
"src/community/components/RouteErrorBoundary.jsx:26",
"src/community/components/ui/StatusMessage.jsx:26",
"src/community/pages/Notifications.jsx:418",
"src/community/pages/Random.jsx:56",
Expand Down Expand Up @@ -2309,14 +2309,20 @@
"This only reads your public Rotur info.": [
"src/community/components/RoturConsentModal.jsx:113"
],
"This page could not load.": [
"src/community/components/RouteErrorBoundary.jsx:20"
],
"Something went wrong on this page.": [
"src/community/components/RouteErrorBoundary.jsx:18"
"src/community/components/RouteErrorBoundary.jsx:21"
],
"Go to home": [
"src/community/components/RouteErrorBoundary.jsx:27"
"src/community/components/RouteErrorBoundary.jsx:30"
],
"Part of the site did not download. Check your connection and try again.": [
"src/community/components/RouteErrorBoundary.jsx:36"
],
"The problem has been reported. Try again, or head back to the home page.": [
"src/community/components/RouteErrorBoundary.jsx:32"
"src/community/components/RouteErrorBoundary.jsx:37"
],
"Loading page…": [
"src/community/components/RouteLoading.jsx:53"
Expand Down
4 changes: 3 additions & 1 deletion src/community/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2818,5 +2818,7 @@
"Buy this project to comment.": "Buy this project to comment.",
"Buy this project to react.": "Buy this project to react.",
"Remove from library, {value1} saves": "Remove from library, {value1} saves",
"Save to library, {value1} saves": "Save to library, {value1} saves"
"Save to library, {value1} saves": "Save to library, {value1} saves",
"This page could not load.": "This page could not load.",
"Part of the site did not download. Check your connection and try again.": "Part of the site did not download. Check your connection and try again."
}
2 changes: 1 addition & 1 deletion src/components/project-load-error/project-load-error.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
align-items: center;
justify-content: center;
width: 100%;
min-height: 100%;
min-height: 100vh;
background: $ui-primary;
color: $text-primary;
}
Expand Down
86 changes: 56 additions & 30 deletions src/components/project-load-error/project-load-error.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import styles from './project-load-error.css';
const newProjectHref = () => (process.env.ROUTING_STYLE === 'wildcard' ?
`${process.env.ROOT || '/'}editor` : 'editor.html');

const ProjectLoadError = ({error, onNewProject, onOpenFile, onRetry}) => {
export const unknownExtensionId = error => {
const match = /^Unknown extension: (.+)$/.exec(String((error && error.message) || error || ''));
return match ? match[1] : null;
};

const ProjectLoadError = ({error, isEmbedded, onNewProject, onOpenFile, onRetry}) => {
const missingExtension = unknownExtensionId(error);
const handleNewProject = useCallback(event => {
if (!onNewProject) return;
event.preventDefault();
Expand All @@ -26,37 +32,56 @@ const ProjectLoadError = ({error, onNewProject, onOpenFile, onRetry}) => {
id="mw.projectLoadError.title"
/>
</h1>
<p>
<FormattedMessage
defaultMessage="Check your connection and try again."
description="Advice when a project download fails"
id="mw.projectLoadError.description"
/>
</p>
<p className={styles.details}>{error.message || String(error)}</p>
<div className={styles.actions}>
<button
type="button"
onClick={onRetry}
>
{missingExtension ? (
<p>
<FormattedMessage
defaultMessage="Try again"
description="Retry downloading the requested project"
id="mw.projectLoadError.retry"
// eslint-disable-next-line max-len
defaultMessage="This project uses the extension {extension}, which MistWarp does not have. It was probably made in another Scratch mod."
description="Shown when a project needs an extension MistWarp does not include"
id="mw.projectLoadError.unknownExtension"
values={{extension: <code>{missingExtension}</code>}}
/>
</button>
</p>
) : (
<React.Fragment>
<p>
<FormattedMessage
defaultMessage="Check your connection and try again."
description="Advice when a project download fails"
id="mw.projectLoadError.description"
/>
</p>
<p className={styles.details}>{error.message || String(error)}</p>
</React.Fragment>
)}
<div className={styles.actions}>
{missingExtension || !onRetry ? null : (
<button
type="button"
onClick={onRetry}
>
<FormattedMessage
defaultMessage="Try again"
description="Retry downloading the requested project"
id="mw.projectLoadError.retry"
/>
</button>
)}
{/* A link, so it works even if the editor never finished starting. */}
<a
className={styles.secondary}
href={newProjectHref()}
onClick={handleNewProject}
>
<FormattedMessage
defaultMessage="Start a new project"
description="Link on the project load error screen that opens the editor with a new project"
id="mw.projectLoadError.newProject"
/>
</a>
{isEmbedded ? null : (
<a
className={styles.secondary}
href={newProjectHref()}
onClick={handleNewProject}
>
<FormattedMessage
defaultMessage="Start a new project"
// eslint-disable-next-line max-len
description="Link on the project load error screen that opens the editor with a new project"
id="mw.projectLoadError.newProject"
/>
</a>
)}
{onOpenFile ? (
<button
type="button"
Expand All @@ -79,9 +104,10 @@ const ProjectLoadError = ({error, onNewProject, onOpenFile, onRetry}) => {

ProjectLoadError.propTypes = {
error: PropTypes.oneOfType([PropTypes.object, PropTypes.string]).isRequired,
isEmbedded: PropTypes.bool,
onNewProject: PropTypes.func,
onOpenFile: PropTypes.func,
onRetry: PropTypes.func.isRequired
onRetry: PropTypes.func
};

export default ProjectLoadError;
4 changes: 3 additions & 1 deletion src/containers/dom-element-renderer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class DOMElementRenderer extends React.Component {
this.container.appendChild(this.props.domElement);
}
componentWillUnmount () {
this.container.removeChild(this.props.domElement);
if (this.container && this.props.domElement.parentNode === this.container) {
this.container.removeChild(this.props.domElement);
}
}
setContainer (c) {
this.container = c;
Expand Down
11 changes: 10 additions & 1 deletion src/containers/gui.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import vmManagerHOC from '../lib/components/vm-manager-hoc.jsx';
import cloudManagerHOC from '../lib/components/cloud-manager-hoc.jsx';

import GUIComponent from '../components/gui/gui.jsx';
import ProjectLoadError from '../components/project-load-error/project-load-error.jsx';
import ProjectLoadError, {unknownExtensionId} from '../components/project-load-error/project-load-error.jsx';
import {setIsScratchDesktop} from '../lib/utils/isScratchDesktop.js';
import TWFullScreenResizerHOC from '../lib/components/tw-fullscreen-resizer-hoc.jsx';
import TWThemeManagerHOC from './tw-theme-manager-hoc.jsx';
Expand Down Expand Up @@ -188,12 +188,21 @@ class GUI extends React.Component {
}
render () {
if (this.props.isError) {
if (unknownExtensionId(this.props.error)) {
return (
<ProjectLoadError
error={this.props.error}
isEmbedded={this.props.isEmbedded}
/>
);
}
throw this.props.error;
}
if (this.props.projectFetchError) {
return (
<ProjectLoadError
error={this.props.projectFetchError}
isEmbedded={this.props.isEmbedded}
onRetry={this.props.onRetryProjectFetch}
/>
);
Expand Down
15 changes: 13 additions & 2 deletions src/lib/api/restore-points.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,24 @@ const removeExtraneousRestorePoints = () => openDB().then(db => new Promise((res
* @param {VirtualMachine} vm scratch-vm instance
* @returns {Promise<{type: string; data: ArrayBuffer;}>} Thumbnail data
*/
const BLANK_THUMBNAIL = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=';

const generateThumbnail = vm => new Promise(resolve => {
const renderer = vm.renderer;
if (!renderer) {
resolve({
type: 'image/png',
data: base64ToArrayBuffer(BLANK_THUMBNAIL)
});
return;
}

// Piggyback off of the next draw if we can, otherwise just force it to render
const drawTimeout = setTimeout(() => {
vm.renderer.draw();
renderer.draw();
}, 100);

vm.renderer.requestSnapshot(dataURL => {
renderer.requestSnapshot(dataURL => {
clearTimeout(drawTimeout);

const index = dataURL.indexOf(',');
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/sortable-hoc.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const SortableHOC = function (WrappedComponent) {
// the dragging object. Obviously only exists if there is a drag (i.e. currentOffset).
// Return null if outside the container, zero if there are no boxes.
let mouseOverIndex = null;
if (this.props.dragInfo.currentOffset) {
if (this.props.dragInfo.currentOffset && this.containerBox && this.boxes) {
const {x, y} = this.props.dragInfo.currentOffset;
const {top, left, bottom, right} = this.containerBox;
if (x >= left && x <= right && y >= top && y <= bottom) {
Expand Down
42 changes: 21 additions & 21 deletions src/lib/lazy-with-retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export const isChunkLoadError = error => Boolean(error && (
/Loading (?:CSS )?chunk [\w-]+ failed/i.test(error.message || '') ||
/Failed to fetch dynamically imported module/i.test(error.message || '') ||
/error loading dynamically imported module/i.test(error.message || '') ||
/Importing a module script failed|Unable to preload CSS/i.test(error.message || '')
/Importing a module script failed|Unable to preload CSS/i.test(error.message || '') ||
/Cross-origin script load denied/i.test(error.message || '')
));

// Retry transport failures once. Execution errors must reach the error boundary.
Expand All @@ -23,22 +24,6 @@ export const importWithRetry = async load => {

export default load => lazy(() => importWithRetry(load));

// React.lazy remembers a failed import forever, so an error boundary's "Try again" would rethrow it.
// This variant starts a fresh import the next time it renders after a failure, and never reloads the page.
export const retryableLazy = load => {
let Lazy = null;
const createLazy = () => lazy(() => importWithRetry(load).catch(error => {
Lazy = createLazy();
throw error;
}));
Lazy = createLazy();
const RetryableLazy = props => React.createElement(Lazy, props);
RetryableLazy.preload = () => importWithRetry(load).catch(() => {
// Rendering retries the import and reports the error.
});
return RetryableLazy;
};

// Community page navigation can recover from assets removed by a deployment.
// Do not use this for editor imports, where a reload could discard project edits.
const RELOAD_COOLDOWN = 60 * 1000;
Expand All @@ -59,7 +44,22 @@ export const reloadStalePage = (error, browser = window) => {
}
};

export const lazyWithReload = load => lazy(() => importWithRetry(load).catch(error => {
if (reloadStalePage(error)) return new Promise(() => {});
throw error;
}));
// React.lazy remembers a failed import forever, so an error boundary's "Try again" would rethrow it.
// This variant starts a fresh import the next time it renders after a failure. It only reloads the
// page when reloadStale is set.
export const retryableLazy = (load, {reloadStale = false} = {}) => {
let Lazy = null;
const createLazy = () => lazy(() => importWithRetry(load).catch(error => {
if (reloadStale && reloadStalePage(error)) return new Promise(() => {});
Lazy = createLazy();
throw error;
}));
Lazy = createLazy();
const RetryableLazy = props => React.createElement(Lazy, props);
RetryableLazy.preload = () => importWithRetry(load).catch(() => {
// Rendering retries the import and reports the error.
});
return RetryableLazy;
};

export const lazyWithReload = load => retryableLazy(load, {reloadStale: true});
1 change: 1 addition & 0 deletions src/lib/tw-translations/default-messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@
"gui.playButton.play": "Play",
"gui.playButton.stop": "Stop",
"mw.projectLoadError.title": "Could not load project",
"mw.projectLoadError.unknownExtension": "This project uses the extension {extension}, which MistWarp does not have. It was probably made in another Scratch mod.",
"mw.projectLoadError.description": "Check your connection and try again.",
"mw.projectLoadError.retry": "Try again",
"mw.projectLoadError.newProject": "Start a new project",
Expand Down
4 changes: 2 additions & 2 deletions src/playground/embed.ejs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!DOCTYPE html>
<html>
<html translate="no">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="google" value="notranslate">
<meta name="google" content="notranslate">
<!-- OpenGraph Meta Tags for Discord/Social Embeds -->
<meta property="og:title" content="<%= page.title %>" />
<meta property="og:description" content="An embedded <%= page.APP_NAME %> project" />
Expand Down
Loading
Loading