import { currentHeaderState, HEADER_STATES, headerBlockEl, nextClasses, } from '@agent/workflows/theme/tools/update-header-style'; import { useCallback, useEffect, useRef, useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; const OWNED = [...new Set(Object.values(HEADER_STATES).flat())]; const applyClasses = (el, classes) => { for (const cls of OWNED) el.classList.toggle(cls, classes.includes(cls)); }; export const HeaderStyleConfirm = ({ inputs, onConfirm, onCancel }) => { const previewed = useRef(null); const confirmed = useRef(false); const [noChange, setNoChange] = useState(false); // Without this the confirm asks about a change nothing on the page shows. useEffect(() => { if (previewed.current) return; const el = headerBlockEl(); if (!el) return; const classes = [...el.classList]; previewed.current = { el, classes }; if (inputs?.state === currentHeaderState()) return setNoChange(true); applyClasses(el, nextClasses(classes, inputs?.state)); }, [inputs]); useEffect( () => () => { const { el, classes } = previewed.current ?? {}; if (!el || confirmed.current) return; applyClasses(el, classes); }, [], ); const handleConfirm = useCallback(() => { confirmed.current = true; onConfirm({ data: inputs }); }, [inputs, onConfirm]); const handleCancel = useCallback(() => onCancel(), [onCancel]); return (
{noChange ? __( 'Your header already looks like that, so there is nothing to change.', 'extendify-local', ) : __( 'The agent has changed the header in the browser. Please review and confirm.', 'extendify-local', )}