/** * WordPress dependencies */ import { Button, TextControl } from '@safe-wordpress/components'; import { useDispatch, useSelect } from '@safe-wordpress/data'; import { useEffect, useRef } from '@safe-wordpress/element'; import { _x } from '@safe-wordpress/i18n'; /** * External dependencies */ import { trim } from 'lodash'; import type { RegularQueryArg, QueryArgName, QueryArgValue, } from '@nelio-content/types'; /** * Internal dependencies */ import './style.scss'; import { store as NC_EDIT_POST } from '../../../../store'; export const PermalinkSettings = (): JSX.Element => { const { items, add, replace, set } = useQueryArgs(); const didInitRef = useRef( false ); useEffect( () => { if ( didInitRef.current ) { return; } const cleaned = items.filter( ( [ n ] ) => !! trim( n ) ); if ( cleaned.length !== items.length ) { void set( cleaned ); } didInitRef.current = true; }, [ items, set ] ); return (
{ items.length ? (

{ _x( 'URL Parameters:', 'text', 'nelio-content' ) }

) : (

{ _x( 'Add custom URL parameters when sharing this post on social media:', 'user', 'nelio-content' ) }

) } { items.map( ( item, index ) => ( replace( item, [ n, item[ 1 ] ] ) } onValueChange={ ( v ) => replace( item, [ item[ 0 ], v ] ) } /> ) ) }
); }; // ===== // VIEWS // ===== type QueryArgProps = { readonly name: QueryArgName; readonly value: QueryArgValue; readonly showLabel: boolean; readonly onNameChange: ( name: QueryArgName ) => void; readonly onValueChange: ( value: QueryArgValue ) => void; }; const QueryArg = ( { name, value, showLabel, onNameChange, onValueChange, }: QueryArgProps ) => (
); // ===== // HOOKS // ===== const useQueryArgs = () => { const items = useSelect( ( select ) => select( NC_EDIT_POST ).getQueryArgs(), [] ); const { setQueryArgs } = useDispatch( NC_EDIT_POST ); return { items, add: () => setQueryArgs( [ ...items, [ '', '' ] ] ), replace: ( oldItem: RegularQueryArg, newItem: RegularQueryArg ) => setQueryArgs( items.map( ( item ) => ( item === oldItem ? newItem : item ) ) ), set: setQueryArgs, }; };