/** * External dependencies */ import clsx from 'clsx'; /** * WordPress dependencies */ import { __, sprintf, _x } from '@wordpress/i18n'; import { createInterpolateElement, useState } from '@wordpress/element'; import { Dialog, Stack, Tabs, Text } from '@wordpress/ui'; import { DataViews, DataViewsPicker } from '@wordpress/dataviews'; import type { ActionButton, Field, View } from '@wordpress/dataviews'; /** * Internal dependencies */ import { isAdjusted, type ImportItem } from '../../utils'; type SkippedItem = { path: string; code: string; message: string; }; type ImportConfirmDialogProps = { items: ImportItem[]; unchanged: ImportItem[]; skipped: SkippedItem[]; onApply: ( items: ImportItem[] ) => void; onClose: () => void; }; function renderValue( value: unknown ) { return { JSON.stringify( value ) ?? '' }; } const itemFields: Field< ImportItem >[] = [ { id: 'path', label: __( 'Setting', 'custom-html-block-extension' ), enableSorting: false, enableHiding: false, }, { id: 'currentValue', label: _x( 'Current', 'setting value', 'custom-html-block-extension' ), render: ( { item } ) => renderValue( item.currentValue ), enableSorting: false, enableHiding: false, }, { id: 'value', label: _x( 'New', 'setting value', 'custom-html-block-extension' ), render: ( { item } ) => isAdjusted( item.originalValue, item.value ) ? createInterpolateElement( sprintf( /* translators: 1: New value, 2: Imported value out of range. */ __( '%1$s (%2$s is out of range)', 'custom-html-block-extension' ), '', '' ), { value: renderValue( item.value ), original: renderValue( item.originalValue ) } ) : renderValue( item.value ), enableSorting: false, enableHiding: false, }, ]; const unchangedFields: Field< ImportItem >[] = [ { id: 'path', label: __( 'Setting', 'custom-html-block-extension' ), enableSorting: false, enableHiding: false, }, { id: 'value', label: _x( 'Value', 'setting value', 'custom-html-block-extension' ), render: ( { item } ) => renderValue( item.value ), enableSorting: false, enableHiding: false, }, ]; const skippedFields: Field< SkippedItem >[] = [ { id: 'path', label: __( 'Setting', 'custom-html-block-extension' ), enableSorting: false, enableHiding: false, }, { id: 'message', label: __( 'Reason', 'custom-html-block-extension' ), enableSorting: false, enableHiding: false, }, ]; export default function ImportConfirmDialog( { items, unchanged, skipped, onApply, onClose, }: ImportConfirmDialogProps ) { const [ selection, setSelection ] = useState( () => items.map( ( item ) => `${ item.group }.${ item.path }` ) ); const [ itemView, setItemView ] = useState< View >( { type: 'pickerTable', layout: { enableMoving: false }, titleField: 'path', fields: [ 'currentValue', 'value' ], page: 1, perPage: Math.max( items.length, 1 ), } ); const [ unchangedView, setUnchangedView ] = useState< View >( { type: 'table', layout: { enableMoving: false }, titleField: 'path', fields: [ 'value' ], page: 1, perPage: Math.max( unchanged.length, 1 ), } ); const [ skippedView, setSkippedView ] = useState< View >( { type: 'table', layout: { enableMoving: false }, titleField: 'path', fields: [ 'message' ], page: 1, perPage: Math.max( skipped.length, 1 ), } ); const actions: ActionButton< ImportItem >[] = [ { id: 'apply', label: __( 'Apply', 'custom-html-block-extension' ), isPrimary: true, supportsBulk: true, callback: ( selectedItems ) => onApply( selectedItems ), }, ]; return ( { if ( ! open ) { onClose(); } } } > { __( 'Import editor config', 'custom-html-block-extension' ) } 0 ? 'apply' : 'skipped' } > { sprintf( /* translators: %d: Number of settings. */ __( 'Settings to apply (%d)', 'custom-html-block-extension' ), items.length ) } { sprintf( /* translators: %d: Number of settings. */ __( 'Unchanged settings (%d)', 'custom-html-block-extension' ), unchanged.length ) } { sprintf( /* translators: %d: Number of settings. */ __( 'Skipped settings (%d)', 'custom-html-block-extension' ), skipped.length ) } { items.length > 0 && ( }> { __( 'Select the settings to import. They are applied as unsaved changes, so check the preview and save them.', 'custom-html-block-extension' ) } ) } `${ item.group }.${ item.path }` } paginationInfo={ { totalItems: items.length, totalPages: 1 } } defaultLayouts={ { pickerTable: true } } selection={ selection } onChangeSelection={ setSelection } actions={ actions } itemListLabel={ __( 'Settings to apply', 'custom-html-block-extension' ) } empty={ __( 'No settings to apply.', 'custom-html-block-extension' ) } > { unchanged.length > 0 && ( }> { __( 'The following settings are the same as the current ones.', 'custom-html-block-extension' ) } ) } `${ item.group }.${ item.path }` } paginationInfo={ { totalItems: unchanged.length, totalPages: 1 } } defaultLayouts={ { table: true } } empty={ __( 'No settings are unchanged.', 'custom-html-block-extension' ) } > { skipped.length > 0 && ( }> { __( 'The following settings cannot be imported.', 'custom-html-block-extension' ) } ) } item.path } paginationInfo={ { totalItems: skipped.length, totalPages: 1 } } defaultLayouts={ { table: true } } empty={ __( 'No settings were skipped.', 'custom-html-block-extension' ) } > ); }