Skip to content

Commit 4b987c8

Browse files
authored
Prevent blurhash crashes (#13866)
1 parent f061663 commit 4b987c8

File tree

4 files changed

+73
-7
lines changed

4 files changed

+73
-7
lines changed

packages/story-editor/src/components/canvas/renderResourcePlaceholder.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
import styled, { css } from 'styled-components';
2121
import { Blurhash } from 'react-blurhash';
2222

23+
/**
24+
* Internal dependencies
25+
*/
26+
import { HideOnError } from '../hideOnError';
27+
2328
const placeholderStyles = css`
2429
position: absolute !important;
2530
top: 0;
@@ -40,7 +45,14 @@ const BaseColorContainer = styled.div`
4045
function renderResourcePlaceholder({ blurHash, baseColor }) {
4146
if (blurHash) {
4247
return (
43-
<BlurhashContainer hash={blurHash} punch={1} height="100%" width="100%" />
48+
<HideOnError>
49+
<BlurhashContainer
50+
hash={blurHash}
51+
punch={1}
52+
height="100%"
53+
width="100%"
54+
/>
55+
</HideOnError>
4456
);
4557
}
4658

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* External dependencies
19+
*/
20+
import type { PropsWithChildren } from 'react';
21+
import { Component } from '@googleforcreators/react';
22+
23+
interface HideOnErrorState {
24+
error: Error | null;
25+
}
26+
27+
export class HideOnError extends Component<PropsWithChildren<unknown>> {
28+
state: HideOnErrorState;
29+
30+
constructor(props: PropsWithChildren<unknown>) {
31+
super(props);
32+
33+
this.state = {
34+
error: null,
35+
};
36+
}
37+
38+
static getDerivedStateFromError(error: Error) {
39+
return { error };
40+
}
41+
42+
render() {
43+
const { error } = this.state;
44+
if (!error) {
45+
return this.props.children;
46+
}
47+
48+
return null;
49+
}
50+
}

packages/story-editor/src/components/library/panes/media/common/mediaElement.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import DropDownMenu from '../local/dropDownMenu';
3838
import { ContentType, useLocalMedia } from '../../../../../app/media';
3939
import Tooltip from '../../../../tooltip';
4040
import { noop } from '../../../../../utils/noop';
41+
import { HideOnError } from '../../../../hideOnError';
4142
import Attribution from './attribution';
4243
import InnerElement from './innerElement';
4344
import InsertionMenu from './insertionMenu';
@@ -211,12 +212,14 @@ function Element({
211212
/>
212213
{attribution}
213214
{isPlaceholder && blurHash && (
214-
<BlurhashContainer
215-
hash={blurHash}
216-
width={width}
217-
height={height}
218-
punch={1}
219-
/>
215+
<HideOnError>
216+
<BlurhashContainer
217+
hash={blurHash}
218+
width={width}
219+
height={height}
220+
punch={1}
221+
/>
222+
</HideOnError>
220223
)}
221224
{(!src ||
222225
isCurrentResourceProcessing(resourceId) ||

packages/story-editor/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"src/app/pageDataUrls",
4848
"src/app/story",
4949
"src/app/taxonomy",
50+
"src/components/hideOnError.tsx",
5051
"src/components/library/panes/text/textPresets.ts",
5152
"src/components/canvas/*.ts",
5253
"src/components/canvas/utils/*.ts",

0 commit comments

Comments
 (0)