)
}
export default function ImageVaultDAM({
sdk,
config,
initialData,
}: ImageVaultDAMProps) {
const [media, setMedia] = useState(initialData)
const field = sdk.location.CustomField?.field
const frame = sdk.location.CustomField?.frame
const entry = sdk.location.CustomField?.entry
const stack = sdk.location.CustomField?.stack
const updateFrameHeight = useCallback(() => {
if (frame?.updateHeight) {
// We need to recalculate the height of the iframe when an image is added.
// Cannot use flushSync inside useEffect.
setTimeout(() => frame.updateHeight(), 0)
}
}, [frame])
const handleMedia = useCallback(
(asset?: ImageVaultAsset) => {
if (field) {
flushSync(() => {
const data = asset || null
setMedia(data)
field.setData(data)
document.body.style.overflow = "hidden"
})
}
updateFrameHeight()
},
[field, updateFrameHeight]
)
const handleEdit = useCallback(() => {
const fieldData = field?.getData()
if (fieldData) {
cbModal({
// @ts-expect-error: Component is badly typed
component: (compProps) => (
),
modalProps: {
size: "max",
style: {
content: { maxHeight: "90dvh", maxWidth: "90dvw", width: "auto" },
overlay: {},
},
},
})
}
}, [field, handleMedia])
useEffect(() => {
updateFrameHeight()
}, [updateFrameHeight])
if (!field || !frame || !entry || !stack) {
return
Initializing custom field...
}
// The existing data might still be in InsertResponse format if the user has not edited it yet.
// We'll convert it to ImageVaultAsset when the component mounts.
const fieldData = field.getData()
if (isInsertResponse(fieldData)) {
field.setData(initialData)
}
const entryData: EntryDataPublishDetails = {
//TODO: Add support for branches
branch: "main",
contentTypeUid: entry.content_type.uid,
locale: entry.locale as Lang,
stackApiKey: stack._data.api_key,
title:
entry.getField("title").getData().toString() ||
`Untitled (${entry.content_type.uid})`,
uid: entry._data.uid,
}
return (