import React from 'react'; import { Field, Grid, TextInput, Typography } from '@strapi/design-system'; import { useIntl } from 'react-intl'; import { getTranslationKey, type OptimizationSettings } from '../pluginId'; interface OptimizationResizeFieldsProps { value: Pick; sourceWidth?: number; sourceHeight?: number; onChange: (patch: Partial) => void; disabled?: boolean; namePrefix?: string; } export const OptimizationResizeFields = ({ value, sourceWidth, sourceHeight, onChange, disabled = false, namePrefix = '', }: OptimizationResizeFieldsProps) => { const { formatMessage } = useIntl(); const aspectRatio = sourceWidth && sourceHeight && sourceHeight > 0 ? sourceWidth / sourceHeight : null; const handleWidthChange = (rawValue: string) => { const nextWidth = Math.max(1, Number(rawValue) || 1); if (!aspectRatio) { onChange({ maxWidth: nextWidth }); return; } onChange({ maxWidth: nextWidth, maxHeight: Math.max(1, Math.round(nextWidth / aspectRatio)), }); }; const handleHeightChange = (rawValue: string) => { const nextHeight = Math.max(1, Number(rawValue) || 1); if (!aspectRatio) { onChange({ maxHeight: nextHeight }); return; } onChange({ maxHeight: nextHeight, maxWidth: Math.max(1, Math.round(nextHeight * aspectRatio)), }); }; return ( <> {formatMessage({ id: getTranslationKey('settings.resize.title'), defaultMessage: 'Output dimensions', })} {formatMessage({ id: getTranslationKey('settings.resize.width'), defaultMessage: 'Width (px)', })} ) => handleWidthChange(e.target.value)} disabled={disabled} /> {formatMessage({ id: getTranslationKey('settings.resize.height'), defaultMessage: 'Height (px)', })} ) => handleHeightChange(e.target.value)} disabled={disabled} /> {formatMessage({ id: getTranslationKey('settings.resize.hint'), defaultMessage: 'Defaults to the original image size. Change either value to resize; the other updates to keep aspect ratio.', })} ); };