import { useState } from 'react'; import { __ } from '@wordpress/i18n'; import { toast } from 'sonner'; import { Switch } from '../ui/switch'; import { saveDisabledThumbnails, ThumbnailsSizes } from '../../api'; interface RegenerateThumbnailsProps { sizes: ThumbnailsSizes; disabled: string[]; onDisabledChange: (disabled: string[]) => void; loading?: boolean; } const SKELETON_LABEL_WIDTHS = ['w-28', 'w-36', 'w-32', 'w-44', 'w-32', 'w-36', 'w-36', 'w-32']; function ThumbnailsLoadingSkeleton() { return (
{SKELETON_LABEL_WIDTHS.map((lw, i) => (
))}
); } export default function RegenerateThumbnails({ sizes, disabled, onDisabledChange, loading = false, }: RegenerateThumbnailsProps) { const [saving, setSaving] = useState(false); const toggleSize = (name: string) => { onDisabledChange( disabled.includes(name) ? disabled.filter((s) => s !== name) : [...disabled, name], ); }; const isEnabled = (name: string) => !disabled.includes(name); const handleSave = async () => { setSaving(true); try { await saveDisabledThumbnails(disabled); toast.success(__('Settings saved successfully.', 'image-sizes')); } catch { toast.error(__('Failed to save settings.', 'image-sizes')); } finally { setSaving(false); } }; const handleReset = () => { onDisabledChange(Object.keys(sizes).filter((name) => name !== 'full')); toast.success(__('Settings reset successfully.', 'image-sizes')); }; if (loading) return ; return (
{Object.keys(sizes).length === 0 ? (

{__('No thumbnail sizes found.', 'image-sizes')}

) : ( Object.entries(sizes) .sort(([a], [b]) => (a === 'full' ? -1 : b === 'full' ? 1 : 0)) .map(([name, size]) => (

{size.name} {name === 'full' ? '' : `(${size.width}x${size.height})`}

{name !== 'full' ? (

{isEnabled(name) ? __('Enabled - This size will be generated for new uploads.', 'image-sizes') : __('Disabled - This size will not be generated for new uploads.', 'image-sizes')}

) : (

{__('This is the original image size and cannot be disabled.', 'image-sizes')}

)}
toggleSize(name)} disabled={name === 'full'} />
)) )}
); }