import executeAbility from '@agent/workflows/abilities/tools/execute-ability'; import { useEffect, useState } from '@wordpress/element'; import { __, _n, sprintf } from '@wordpress/i18n'; const ABILITY = 'imagify/optimize-media'; // Imagify translates its own guard messages only where it has language packs. const blockedMessage = (status) => { if (status === 'invalid_api_key') return __( 'Your Imagify API key is missing or invalid. Add a valid key in the Imagify settings.', 'extendify-local', ); if (status === 'insufficient_quota') return __( 'Your Imagify quota is used up. Wait for the next reset or upgrade your plan.', 'extendify-local', ); return null; }; const usePreview = (inputs, enabled) => { const [preview, setPreview] = useState(null); useEffect(() => { if (!enabled) return; let live = true; // Imagify runs nothing without confirm: true, so this only reports the cost. executeAbility({ ability: ABILITY, input: { ...inputs, confirm: false } }) .then((result) => { if (live) setPreview(result ?? {}); }) // Imagify still gates the run, so a failed cost check need not block it. .catch(() => { if (live) setPreview({}); }); return () => { live = false; }; }, [inputs, enabled]); return preview; }; const Line = ({ children }) => (

{children}

); const Cost = ({ preview }) => { const count = preview?.impact?.count ?? 1; const quota = preview?.quota_remaining; return ( <> {sprintf( // translators: %d is how many Imagify credits optimizing this image will use. _n( 'Optimizing this image uses %d Imagify credit.', 'Optimizing this image uses %d Imagify credits.', count, 'extendify-local', ), count, )} {quota == null ? null : (

{sprintf( // translators: %s is the percentage of the user's Imagify quota that is still unused, e.g. "88%". __('%s of your quota remains.', 'extendify-local'), `${Math.round(quota)}%`, )}

)} ); }; const Body = ({ gate, preview, blocked, result }) => { if (result) { const failed = result.error || result[ABILITY]?.status === 'error'; return ( {failed ? __("The image couldn't be optimized.", 'extendify-local') : __('Image optimized.', 'extendify-local')} ); } if (!gate) return {__('Optimizing image...', 'extendify-local')}; if (!preview) return ( {__('Checking your Imagify quota...', 'extendify-local')} ); if (blocked) return {blocked}; return ; }; export const OptimizeMedia = ({ inputs, result, onConfirm, onCancel }) => { const gate = !!onConfirm && !result; const preview = usePreview(inputs, gate); const blocked = blockedMessage(preview?.status); return (
{gate && preview ? (
{blocked ? null : ( )}
) : null}
); };