import React, { useEffect, useSyncExternalStore, type MouseEvent, type PointerEvent } from 'react';
import { createPortal } from 'react-dom';
import {
Box,
Button,
Flex,
IconButton,
Typography,
} from '@strapi/design-system';
import { Cross, Sparkle } from '@strapi/icons';
import { useIntl } from 'react-intl';
import { AssetOptimizationLabel } from './AssetOptimizationLabel';
import { OptimizationChoicePicker } from './OptimizationChoicePicker';
import { OptimizationCustomForm } from './OptimizationCustomForm';
import { getTranslationKey } from '../pluginId';
import {
closeAssetEditor,
createCustomForAsset,
createCustomFromGlobal,
getAssetPreference,
getDraftPreference,
getEditingAssetId,
getUploadAssetCards,
getUploadDialogElement,
isPreferenceCustomized,
openAssetEditor,
saveAssetEditor,
setDraftChoice,
setDraftCustom,
subscribeUploadAssets,
} from '../utils/uploadAssetStore';
const stopEventPropagation = (event: MouseEvent | PointerEvent) => {
event.stopPropagation();
};
export const UploadEnhancerBridge = () => {
const { formatMessage } = useIntl();
const cards = useSyncExternalStore(subscribeUploadAssets, getUploadAssetCards);
const editingAssetId = useSyncExternalStore(subscribeUploadAssets, getEditingAssetId);
const draftPreference = useSyncExternalStore(subscribeUploadAssets, getDraftPreference);
const dialogElement = useSyncExternalStore(subscribeUploadAssets, getUploadDialogElement);
const editingCard = cards.find((card) => card.assetId === editingAssetId);
useEffect(() => {
if (!dialogElement || !editingAssetId) {
return;
}
const previousOverflow = dialogElement.style.overflow;
dialogElement.style.overflow = 'visible';
return () => {
dialogElement.style.overflow = previousOverflow;
};
}, [dialogElement, editingAssetId]);
const editorPanel =
editingAssetId && dialogElement ? (
{formatMessage({ id: getTranslationKey('upload.modal.title') })}
{editingCard?.assetName && (
{editingCard.assetName}
)}
{draftPreference.choice === 'custom' && (
)}
) : null;
return (
<>
{cards.map((card) => (
{createPortal(
{
event.preventDefault();
event.stopPropagation();
openAssetEditor(card.assetId);
}}
variant={isPreferenceCustomized(card.assetId) ? 'default' : 'ghost'}
>
,
card.actionsContainer
)}
{card.footerHost
? createPortal(
,
card.footerHost
)
: null}
))}
{editorPanel ? createPortal(editorPanel, dialogElement) : null}
>
);
};