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

Prevent blurhash crashes #13866

Merged
merged 1 commit into from
Dec 6, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
import styled, { css } from 'styled-components';
import { Blurhash } from 'react-blurhash';

/**
* Internal dependencies
*/
import { HideOnError } from '../hideOnError';

const placeholderStyles = css`
position: absolute !important;
top: 0;
Expand All @@ -40,7 +45,14 @@ const BaseColorContainer = styled.div`
function renderResourcePlaceholder({ blurHash, baseColor }) {
if (blurHash) {
return (
<BlurhashContainer hash={blurHash} punch={1} height="100%" width="100%" />
<HideOnError>
<BlurhashContainer
hash={blurHash}
punch={1}
height="100%"
width="100%"
/>
</HideOnError>
);
}

Expand Down
50 changes: 50 additions & 0 deletions packages/story-editor/src/components/hideOnError.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* External dependencies
*/
import type { PropsWithChildren } from 'react';
import { Component } from '@googleforcreators/react';

interface HideOnErrorState {
error: Error | null;
}

export class HideOnError extends Component<PropsWithChildren<unknown>> {
state: HideOnErrorState;

constructor(props: PropsWithChildren<unknown>) {
super(props);

this.state = {
error: null,
};
}

static getDerivedStateFromError(error: Error) {
return { error };
}

render() {
const { error } = this.state;
if (!error) {
return this.props.children;
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import DropDownMenu from '../local/dropDownMenu';
import { ContentType, useLocalMedia } from '../../../../../app/media';
import Tooltip from '../../../../tooltip';
import { noop } from '../../../../../utils/noop';
import { HideOnError } from '../../../../hideOnError';
import Attribution from './attribution';
import InnerElement from './innerElement';
import InsertionMenu from './insertionMenu';
Expand Down Expand Up @@ -211,12 +212,14 @@ function Element({
/>
{attribution}
{isPlaceholder && blurHash && (
<BlurhashContainer
hash={blurHash}
width={width}
height={height}
punch={1}
/>
<HideOnError>
<BlurhashContainer
hash={blurHash}
width={width}
height={height}
punch={1}
/>
</HideOnError>
)}
{(!src ||
isCurrentResourceProcessing(resourceId) ||
Expand Down
1 change: 1 addition & 0 deletions packages/story-editor/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"src/app/pageDataUrls",
"src/app/story",
"src/app/taxonomy",
"src/components/hideOnError.tsx",
"src/components/library/panes/text/textPresets.ts",
"src/components/canvas/*.ts",
"src/components/canvas/utils/*.ts",
Expand Down
Loading