Skip to content
Open
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
18 changes: 11 additions & 7 deletions apps/image-focal-point/src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import './index.css';
import { AppView } from './components/AppView';
import { FocalPointView } from './components/FocalPointView';
import { FocalPointDialog } from './components/FocalPointDialog';
import { getField, isCompatibleImageField } from './utils';

const IMAGE_FIELD_ID = 'image';
import { getField, isCompatibleImageField, deriveImageFieldId } from './utils';

export class App extends React.Component {
static propTypes = {
Expand Down Expand Up @@ -57,7 +55,9 @@ export class App extends React.Component {
};

findProperLocale() {
const imageField = this.props.sdk.entry.fields[IMAGE_FIELD_ID];
const currentFieldId = this.props.sdk.field.id;
const imageFieldId = deriveImageFieldId(currentFieldId);
const imageField = this.props.sdk.entry.fields[imageFieldId];

return imageField.locales.includes(this.props.sdk.field.locale)
? this.props.sdk.field.locale
Expand Down Expand Up @@ -87,7 +87,9 @@ export class App extends React.Component {
} = this.props;

try {
const imageField = entry.fields[IMAGE_FIELD_ID];
const currentFieldId = this.props.sdk.field.id;
const imageFieldId = deriveImageFieldId(currentFieldId);
const imageField = entry.fields[imageFieldId];
const asset = await space.getAsset(imageField.getValue(this.findProperLocale()).sys.id);
const file =
asset.fields.file[this.findProperLocale()] ??
Expand Down Expand Up @@ -124,7 +126,9 @@ export class App extends React.Component {

render() {
const { sdk } = this.props;
const imageField = getField(sdk.contentType, IMAGE_FIELD_ID);
const currentFieldId = sdk.field.id;
const imageFieldId = deriveImageFieldId(currentFieldId);
const imageField = getField(sdk.contentType, imageFieldId);
const isImageField = isCompatibleImageField(imageField);

if (isImageField) {
Expand All @@ -139,7 +143,7 @@ export class App extends React.Component {

return (
<Note variant="negative">
Could not find a field of type Asset with the ID &quot;{IMAGE_FIELD_ID}&quot;
Could not find a field of type Asset with the ID &quot;{imageFieldId}&quot;
</Note>
);
}
Expand Down
26 changes: 26 additions & 0 deletions apps/image-focal-point/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,29 @@ export const getField = (contentType, fieldId) =>
contentType.fields.find((field) => field.id === fieldId);

export const isCompatibleImageField = (field) => !!(field && field.linkType === 'Asset');

/**
* Derives the associated image field ID from the focal point field ID
* Examples:
* - focalPoint -> image
* - focalPointLarge -> imageLarge
* - focalPointSomeThing -> imageSomeThing
*
* @param {string} focalPointFieldId - The ID of the focal point field
* @returns {string} The derived image field ID
*/
export const deriveImageFieldId = (focalPointFieldId) => {
// Default to 'image' if the field ID doesn't start with 'focalPoint'
if (!focalPointFieldId.startsWith('focalPoint')) {
return 'image';
}

// If it's exactly 'focalPoint', return 'image'
if (focalPointFieldId === 'focalPoint') {
return 'image';
}

// For cases like 'focalPointLarge', extract 'Large' and return 'imageLarge'
const suffix = focalPointFieldId.substring('focalPoint'.length);
return 'image' + suffix;
};
Loading