import {Image} from '@sanity/types' import * as React from 'react' import {Box, Card, Text} from '@sanity/ui' import { DiffCard, DiffComponent, DiffTooltip, ObjectDiff, ChangeList, getAnnotationAtPath, } from '../../../diff' import {FromTo} from '../../../diff/components' import {ImagePreview, NoImagePreview} from './ImagePreview' const IMAGE_META_FIELDS = ['crop', 'hotspot'] const BASE_IMAGE_FIELDS = ['asset', ...IMAGE_META_FIELDS] const CARD_STYLES = { flex: 1, } export const ImageFieldDiff: DiffComponent> = ({diff, schemaType}) => { const {fromValue, toValue, fields, isChanged} = diff const fromRef = fromValue?.asset?._ref const toRef = toValue?.asset?._ref const assetAnnotation = getAnnotationAtPath(diff, ['asset', '_ref']) // Get all the changed fields within this image field const changedFields = Object.keys(fields).filter( (name) => fields[name].isChanged && name !== '_type' ) const nestedFields = schemaType.fields .filter( (field) => !BASE_IMAGE_FIELDS.includes(field.name) && changedFields.includes(field.name) ) .map((field) => field.name) let assetAction: 'changed' | 'added' | 'removed' = 'changed' if (!fromRef && toRef) { assetAction = 'added' } else if (!toRef && fromRef) { assetAction = 'removed' } const didAssetChange = changedFields.includes('asset') const didCropChange = changedFields.includes('crop') const didHotspotChange = changedFields.includes('hotspot') const didMetaChange = didCropChange || didHotspotChange const showImageDiff = didAssetChange || didMetaChange const showMetaChange = didMetaChange && !didAssetChange const from = fromValue && fromRef ? ( ) : ( ) const to = toValue && toRef ? ( ) : ( ) if (!from && !to) { return ( Image not set ) } if (!isChanged) { return toRef ? ( ) : null } const imageDiff = return ( <> {showImageDiff && (didAssetChange ? ( {imageDiff} ) : ( imageDiff ))} {nestedFields.length > 0 && ( )} ) }