| 1 |
import { |
| 2 |
currentHeaderState, |
| 3 |
HEADER_STATES, |
| 4 |
headerBlockEl, |
| 5 |
nextClasses, |
| 6 |
} from '@agent/workflows/theme/tools/update-header-style'; |
| 7 |
import { useCallback, useEffect, useRef, useState } from '@wordpress/element'; |
| 8 |
import { __ } from '@wordpress/i18n'; |
| 9 |
|
| 10 |
const OWNED = [...new Set(Object.values(HEADER_STATES).flat())]; |
| 11 |
|
| 12 |
const applyClasses = (el, classes) => { |
| 13 |
for (const cls of OWNED) el.classList.toggle(cls, classes.includes(cls)); |
| 14 |
}; |
| 15 |
|
| 16 |
export const HeaderStyleConfirm = ({ inputs, onConfirm, onCancel }) => { |
| 17 |
const previewed = useRef(null); |
| 18 |
const confirmed = useRef(false); |
| 19 |
const [noChange, setNoChange] = useState(false); |
| 20 |
|
| 21 |
// Without this the confirm asks about a change nothing on the page shows. |
| 22 |
useEffect(() => { |
| 23 |
if (previewed.current) return; |
| 24 |
const el = headerBlockEl(); |
| 25 |
if (!el) return; |
| 26 |
const classes = [...el.classList]; |
| 27 |
previewed.current = { el, classes }; |
| 28 |
if (inputs?.state === currentHeaderState()) return setNoChange(true); |
| 29 |
applyClasses(el, nextClasses(classes, inputs?.state)); |
| 30 |
}, [inputs]); |
| 31 |
|
| 32 |
useEffect( |
| 33 |
() => () => { |
| 34 |
const { el, classes } = previewed.current ?? {}; |
| 35 |
if (!el || confirmed.current) return; |
| 36 |
applyClasses(el, classes); |
| 37 |
}, |
| 38 |
[], |
| 39 |
); |
| 40 |
|
| 41 |
const handleConfirm = useCallback(() => { |
| 42 |
confirmed.current = true; |
| 43 |
onConfirm({ data: inputs }); |
| 44 |
}, [inputs, onConfirm]); |
| 45 |
const handleCancel = useCallback(() => onCancel(), [onCancel]); |
| 46 |
|
| 47 |
return ( |
| 48 |
<div className="mb-4 ms-12 me-2 flex flex-col rounded-lg border border-gray-300 bg-gray-50"> |
| 49 |
<div className="rounded-lg border-b border-gray-300 bg-white"> |
| 50 |
<div className="p-3"> |
| 51 |
<p className="m-0 p-0 text-sm text-gray-900"> |
| 52 |
{noChange |
| 53 |
? __( |
| 54 |
'Your header already looks like that, so there is nothing to change.', |
| 55 |
'extendify-local', |
| 56 |
) |
| 57 |
: __( |
| 58 |
'The agent has changed the header in the browser. Please review and confirm.', |
| 59 |
'extendify-local', |
| 60 |
)} |
| 61 |
</p> |
| 62 |
</div> |
| 63 |
</div> |
| 64 |
<div className="flex flex-wrap justify-start gap-2 p-3"> |
| 65 |
<button |
| 66 |
type="button" |
| 67 |
className="flex-1 rounded-sm border border-gray-500 bg-white p-2 text-sm text-gray-900" |
| 68 |
onClick={handleCancel} |
| 69 |
> |
| 70 |
{noChange |
| 71 |
? __('Close', 'extendify-local') |
| 72 |
: __('Cancel', 'extendify-local')} |
| 73 |
</button> |
| 74 |
{!noChange && ( |
| 75 |
<button |
| 76 |
type="button" |
| 77 |
className="flex-1 rounded-sm border border-design-main bg-design-main p-2 text-sm text-white" |
| 78 |
onClick={handleConfirm} |
| 79 |
> |
| 80 |
{__('Save', 'extendify-local')} |
| 81 |
</button> |
| 82 |
)} |
| 83 |
</div> |
| 84 |
</div> |
| 85 |
); |
| 86 |
}; |
| 87 |
|