Skip to content

Commit

Permalink
Prevent blurhash crashes (#13866)
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy authored Dec 6, 2024
1 parent f061663 commit 4b987c8
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
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

0 comments on commit 4b987c8

Please sign in to comment.