-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathimport-controls.js
172 lines (149 loc) · 3.98 KB
/
import-controls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { useMemo, useState } from '@wordpress/element';
import { useDispatch, useSelect, useRegistry } from '@wordpress/data';
import {
Button,
FlexBlock,
FlexItem,
SelectControl,
__experimentalHStack as HStack,
__experimentalSpacer as Spacer,
} from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
import { store as noticesStore } from '@wordpress/notices';
/**
* Internal dependencies
*/
import { useCreateTemplatePartFromBlocks } from './utils/hooks';
import { transformWidgetToBlock } from './utils/transformers';
const SIDEBARS_QUERY = {
per_page: -1,
_fields: 'id,name,description,status,widgets',
};
export function TemplatePartImportControls( { area, setAttributes } ) {
const [ selectedSidebar, setSelectedSidebar ] = useState( '' );
const [ isBusy, setIsBusy ] = useState( false );
const registry = useRegistry();
const { sidebars, hasResolved } = useSelect( ( select ) => {
const { getSidebars, hasFinishedResolution } = select( coreStore );
return {
sidebars: getSidebars( SIDEBARS_QUERY ),
hasResolved: hasFinishedResolution( 'getSidebars', [
SIDEBARS_QUERY,
] ),
};
}, [] );
const { createErrorNotice } = useDispatch( noticesStore );
const createFromBlocks = useCreateTemplatePartFromBlocks(
area,
setAttributes
);
const options = useMemo( () => {
const sidebarOptions = ( sidebars ?? [] )
.filter(
( widgetArea ) =>
widgetArea.id !== 'wp_inactive_widgets' &&
widgetArea.widgets.length > 0
)
.map( ( widgetArea ) => {
return {
value: widgetArea.id,
label: widgetArea.name,
};
} );
if ( ! sidebarOptions.length ) {
return [];
}
return [
{ value: '', label: __( 'Select widget area' ) },
...sidebarOptions,
];
}, [ sidebars ] );
// Render an empty node while data is loading to avoid SlotFill re-positioning bug.
// See: https://github.com/WordPress/gutenberg/issues/15641.
if ( ! hasResolved ) {
return <Spacer marginBottom="0" />;
}
if ( hasResolved && ! options.length ) {
return null;
}
async function createFromWidgets( event ) {
event.preventDefault();
if ( isBusy || ! selectedSidebar ) {
return;
}
setIsBusy( true );
const sidebar = options.find(
( { value } ) => value === selectedSidebar
);
const { getWidgets } = registry.resolveSelect( coreStore );
// The widgets API always returns a successful response.
const widgets = await getWidgets( {
sidebar: sidebar.value,
_embed: 'about',
} );
const skippedWidgets = new Set();
const blocks = widgets.flatMap( ( widget ) => {
const block = transformWidgetToBlock( widget );
// Skip the block if we have no matching transformations.
if ( ! block ) {
skippedWidgets.add( widget.id_base );
return [];
}
return block;
} );
await createFromBlocks(
blocks,
/* translators: %s: name of the widget area */
sprintf( __( 'Widget area: %s' ), sidebar.label )
);
if ( skippedWidgets.size ) {
createErrorNotice(
sprintf(
/* translators: %s: the list of widgets */
__( 'Unable to import the following widgets: %s.' ),
Array.from( skippedWidgets ).join( ', ' )
),
{
type: 'snackbar',
}
);
}
setIsBusy( false );
}
return (
<Spacer marginBottom="4">
<HStack as="form" onSubmit={ createFromWidgets }>
<FlexBlock>
<SelectControl
label={ __( 'Import widget area' ) }
value={ selectedSidebar }
options={ options }
onChange={ ( value ) => setSelectedSidebar( value ) }
disabled={ ! options.length }
__next36pxDefaultSize
__nextHasNoMarginBottom
/>
</FlexBlock>
<FlexItem
style={ {
marginBottom: '8px',
marginTop: 'auto',
} }
>
<Button
variant="primary"
type="submit"
isBusy={ isBusy }
aria-disabled={ isBusy || ! selectedSidebar }
>
{ __( 'Import' ) }
</Button>
</FlexItem>
</HStack>
</Spacer>
);
}