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: %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 (