From 10e87583e30bb8ac07b248e36b3ca55d5ba58c04 Mon Sep 17 00:00:00 2001 From: Luis Ball Date: Mon, 26 Aug 2024 15:59:13 -0700 Subject: [PATCH] fix: dont stringify values that are already strings --- src/components/Gallery/ImageGallery.tsx | 20 ++++++++++++++------ src/helpers/utils.ts | 6 ++++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/components/Gallery/ImageGallery.tsx b/src/components/Gallery/ImageGallery.tsx index 3c42efd1..64ef06d0 100644 --- a/src/components/Gallery/ImageGallery.tsx +++ b/src/components/Gallery/ImageGallery.tsx @@ -33,9 +33,13 @@ export class Gallery extends Component { /** * Fetches an Object of asset metadata, like width and height attributes. */ - getAssetMetadata = async (assetURL: string) => { - const response = await fetch(`${assetURL.split('?')[0]}?fm=json`); - return await response.json(); + getAssetMetadata = async (assetURL: string): Promise> => { + try { + const response = await fetch(`${assetURL.split('?')[0]}?fm=json`); + return await response.json(); + } catch (error) { + return {}; + } }; handleClick = (selectedAsset: AssetProps) => this.setState({ selectedAsset }); @@ -50,19 +54,23 @@ export class Gallery extends Component { const selectedAsset = { ...this.state.selectedAsset }; if (!selectedAsset.attributes.media_width) { - selectedAsset.attributes.media_width = metadata.PixelWidth; + selectedAsset.attributes.media_width = metadata?.PixelWidth || ''; } if (!selectedAsset.attributes.media_height) { - selectedAsset.attributes.media_height = metadata.PixelHeight; + selectedAsset.attributes.media_height = metadata?.PixelHeight || ''; } - this.props.sdk.close({ + const stringifiedAsset = { ...stringifyJsonFields(selectedAsset, [ 'attributes.custom_fields', 'attributes.tags', 'attributes.colors.dominant_colors', ]), + }; + + this.props.sdk.close({ + ...stringifiedAsset, selectedSource: this.props.selectedSource, }); }; diff --git a/src/helpers/utils.ts b/src/helpers/utils.ts index ecb36f5c..c220d2eb 100644 --- a/src/helpers/utils.ts +++ b/src/helpers/utils.ts @@ -169,8 +169,10 @@ export const stringifyJsonFields = ( for (const field of fields) { const value = _.get(object, field); - const newValue = JSON.stringify(value, replaceNullWithEmptyString); - _.set(modifiedObject, field, newValue); + if (typeof value !== 'string') { + const newValue = JSON.stringify(value, replaceNullWithEmptyString); + _.set(modifiedObject, field, newValue); + } } return modifiedObject;