Skip to content

Commit

Permalink
fix: review follow up
Browse files Browse the repository at this point in the history
  • Loading branch information
loweisz committed Apr 11, 2024
1 parent 6f418e1 commit 97a6a78
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
15 changes: 15 additions & 0 deletions packages/core/src/utils/pathSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,18 @@ const chunkSegments = (

return chunks.filter(excludeEmptyChunks);
};

export const lastPathNamedSegmentEq = (path: string, expectedName: string) => {
// `/key123/fields/featureImage/~locale/fields/file/~locale`
// ['', 'key123', 'fields', 'featureImage', '~locale', 'fields', 'file', '~locale']
const segments = path.split('/');

if (segments.length < 2) {
console.warn(
`[experiences-sdk-react] Attempting to check whether last named segment of the path (${path}) equals to '${expectedName}', but the path doesn't have enough segments.`,
);
return false;
}
const secondLast = segments[segments.length - 2]; // skipping trailing '~locale'
return secondLast === expectedName;
};
5 changes: 2 additions & 3 deletions packages/core/src/utils/transformers/media/transformMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Asset, AssetFile } from 'contentful';
import { getOptimizedBackgroundImageAsset } from './getOptimizedBackgroundImageAsset';
import { getOptimizedImageAsset } from './getOptimizedImageAsset';
import { getBoundValue } from '@/utils/transformers/getBoundValue';
import { isDeepPath } from '@/utils';
import { isDeepPath, lastPathNamedSegmentEq } from '@/utils';

export const transformMedia = (
asset: Asset,
Expand All @@ -23,8 +23,7 @@ export const transformMedia = (

// If it is not a deep path and not pointing to the file of the asset,
// it is just pointing to a normal field and therefore we just resolve the value as normal field
const splitPath = path.split('/');
if (!isDeepPath(path) && splitPath[splitPath.length - 2] !== 'file') {
if (!isDeepPath(path) && !lastPathNamedSegmentEq(path, 'file')) {
return getBoundValue(asset, path);
}

Expand Down
8 changes: 8 additions & 0 deletions packages/test-app/src/components/CustomImageComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function CustomImageComponent(props: any) {
return (
<>
<img src={props.src} style={{ height: '100px', width: '100px' }} />
<span>{props.src}</span>
</>
);
}
15 changes: 15 additions & 0 deletions packages/test-app/src/eb-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineComponents } from '@contentful/experiences-sdk-react';
import ComponentWithChildren from './components/ComponentWithChildren';
import { CustomImageComponent } from './components/CustomImageComponent';

defineComponents([
{
Expand All @@ -22,4 +23,18 @@ defineComponents([
wrapComponent: false,
},
},
{
component: CustomImageComponent,
definition: {
id: 'custom-image',
name: 'Custom Image',
category: 'Custom Components',
variables: {
src: {
displayName: 'SRC',
type: 'Media',
},
},
},
},
]);

0 comments on commit 97a6a78

Please sign in to comment.