|
| 1 | +import { store as blockEditorStore } from '@wordpress/block-editor'; |
| 2 | +import { useSelect, useDispatch } from '@wordpress/data'; |
| 3 | +import { useEffect, useRef } from '@wordpress/element'; |
| 4 | +import { |
| 5 | + FIRST_NAME_ID, |
| 6 | + LAST_NAME_ID, |
| 7 | + DEFAULT_FIRST_NAME_LABEL, |
| 8 | + DEFAULT_LAST_NAME_LABEL, |
| 9 | + DEFAULT_NAME_LABEL, |
| 10 | +} from '../variations'; |
| 11 | + |
| 12 | +const isKnownId = id => id === FIRST_NAME_ID || id === LAST_NAME_ID; |
| 13 | + |
| 14 | +const getDefaultLabelForId = id => { |
| 15 | + if ( id === FIRST_NAME_ID ) return DEFAULT_FIRST_NAME_LABEL; |
| 16 | + if ( id === LAST_NAME_ID ) return DEFAULT_LAST_NAME_LABEL; |
| 17 | + return DEFAULT_NAME_LABEL; |
| 18 | +}; |
| 19 | + |
| 20 | +/** |
| 21 | + * Sync the nested label text with the Name field's variation id when users transform |
| 22 | + * between known variations (first-name/last-name). |
| 23 | + * |
| 24 | + * @param {object} params - Parameters. |
| 25 | + * @param {string} params.clientId - Block clientId for the Name field |
| 26 | + * @param {string} params.id - Current variation id (e.g., 'first-name' | 'last-name') |
| 27 | + */ |
| 28 | +export default function useNameLabelSync( { clientId, id } ) { |
| 29 | + const prevIdRef = useRef( id ); |
| 30 | + const { updateBlockAttributes } = useDispatch( blockEditorStore ); |
| 31 | + |
| 32 | + const labelClientId = useSelect( |
| 33 | + select => { |
| 34 | + const block = select( blockEditorStore ).getBlock( clientId ); |
| 35 | + const labelBlock = block?.innerBlocks?.find( b => b.name === 'jetpack/label' ); |
| 36 | + return labelBlock?.clientId; |
| 37 | + }, |
| 38 | + [ clientId ] |
| 39 | + ); |
| 40 | + |
| 41 | + const currentLabel = useSelect( |
| 42 | + select => { |
| 43 | + return labelClientId |
| 44 | + ? select( blockEditorStore ).getBlockAttributes( labelClientId )?.label |
| 45 | + : undefined; |
| 46 | + }, |
| 47 | + [ labelClientId ] |
| 48 | + ); |
| 49 | + |
| 50 | + useEffect( () => { |
| 51 | + const newId = id; |
| 52 | + const prevId = prevIdRef.current; |
| 53 | + |
| 54 | + if ( ! labelClientId ) { |
| 55 | + prevIdRef.current = newId; |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + // Handle transforms between known variations. |
| 60 | + if ( isKnownId( newId ) && newId !== prevId ) { |
| 61 | + const nextDefault = getDefaultLabelForId( newId ); |
| 62 | + // Ensure the parent block id matches the variation id. |
| 63 | + if ( newId ) { |
| 64 | + updateBlockAttributes( clientId, { id: newId } ); |
| 65 | + } |
| 66 | + // Always set the label to the default for the selected variation. |
| 67 | + updateBlockAttributes( labelClientId, { label: nextDefault } ); |
| 68 | + } |
| 69 | + |
| 70 | + // Handle transforms from a known variation back to the base Name (no id). |
| 71 | + const becameBase = isKnownId( prevId ) && ( newId === undefined || newId === '' ); |
| 72 | + if ( becameBase ) { |
| 73 | + // Clear the parent block id when returning to base. |
| 74 | + updateBlockAttributes( clientId, { id: '' } ); |
| 75 | + // Always set the label back to the base default. |
| 76 | + updateBlockAttributes( labelClientId, { label: DEFAULT_NAME_LABEL } ); |
| 77 | + } |
| 78 | + |
| 79 | + prevIdRef.current = newId; |
| 80 | + }, [ id, clientId, labelClientId, currentLabel, updateBlockAttributes ] ); |
| 81 | +} |
0 commit comments