-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathSidebarLayers.js
More file actions
408 lines (386 loc) · 12.8 KB
/
Copy pathSidebarLayers.js
File metadata and controls
408 lines (386 loc) · 12.8 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
* External dependencies
*/
import { useSelector, useDispatch } from 'react-redux';
/**
* Internal dependencies
*/
import { addLayer, setCurrentLayer, setAddLayerModalTime } from '../redux/slice/videoSlice';
import { v4 as uuidv4 } from 'uuid';
import GFIcon from '../assets/layers/GFIcon.svg';
import WPFormsIcon from '../assets/layers/WPForms-Mascot.svg';
import EverestFormsIcon from '../assets/layers/EverestFormsIcon.svg';
import CF7Icon from '../assets/layers/CF7Icon.svg';
import JetpackIcon from '../assets/layers/JetpackIcon.svg';
import SureformsIcon from '../assets/layers/SureFormsIcons.svg';
import ForminatorIcon from '../assets/layers/Forminator.png';
import FluentFormsIcon from '../assets/layers/FluentFormsIcon.png';
import NinjaFormsIcon from '../assets/layers/NinjaFormsIcon.png';
import MetformIcon from '../assets/layers/MetFormIcon.png';
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { Button, Icon, Tooltip } from '@wordpress/components';
import { plus, preformatted, customLink, arrowRight, video, customPostType, thumbsUp, error } from '@wordpress/icons';
import { useState, useEffect, useCallback } from '@wordpress/element';
import Layer from './layers/Layer';
import LayerSelector from './LayerSelector.jsx';
/**
* Layer types with their labels, icons, and integration-specific state.
*/
export const layerTypes = [
{
title: __( 'CTA', 'godam' ),
icon: customLink,
type: 'cta',
layerText: __( 'CTA', 'godam' ),
},
{
title: __( 'Hotspot', 'godam' ),
icon: customPostType,
type: 'hotspot',
layerText: __( 'Hotspot', 'godam' ),
},
{
title: __( 'Forms', 'godam' ),
icon: preformatted,
type: 'form',
formType: {
gravity: {
layerText: __( 'Gravity Forms', 'godam' ),
icon: GFIcon,
isActive: window?.videoData?.gfActive ?? false,
tooltipMessage: __( 'Gravity Forms plugin is not active', 'godam' ),
},
wpforms: {
layerText: __( 'WPForms', 'godam' ),
icon: WPFormsIcon,
isActive: window?.videoData?.wpformsActive ?? false,
tooltipMessage: __( 'WPForms plugin is not active', 'godam' ),
},
cf7: {
layerText: __( 'Contact Form 7', 'godam' ),
isActive: window?.videoData?.cf7Active ?? false,
icon: CF7Icon,
tooltipMessage: __( 'Contact Form 7 plugin is not active', 'godam' ),
},
jetpack: {
layerText: __( 'Jetpack Forms', 'godam' ),
icon: JetpackIcon,
isActive: window?.videoData?.jetpackActive ?? false,
tooltipMessage: __( 'Jetpack plugin is not active', 'godam' ),
},
sureforms: {
layerText: __( 'SureForms', 'godam' ),
icon: SureformsIcon,
isActive: window?.videoData?.sureformsActive ?? false,
tooltipMessage: __( 'SureForms plugin is not active', 'godam' ),
},
forminator: {
layerText: __( 'Forminator Forms', 'godam' ),
icon: ForminatorIcon,
isActive: window?.videoData?.forminatorActive ?? false,
tooltipMessage: __( 'Forminator Forms plugin is not active', 'godam' ),
},
fluentforms: {
layerText: __( 'Fluent Forms', 'godam' ),
icon: FluentFormsIcon,
isActive: window?.videoData?.fluentformsActive ?? false,
tooltipMessage: __( 'Fluent Forms plugin is not active', 'godam' ),
},
everestforms: {
layerText: __( 'Everest Forms', 'godam' ),
icon: EverestFormsIcon,
isActive: window?.videoData?.everestFormsActive ?? false,
tooltipMessage: __( 'Everest Forms plugin is not active', 'godam' ),
},
ninjaforms: {
layerText: __( 'Ninja Forms', 'godam' ),
icon: NinjaFormsIcon,
isActive: window?.videoData?.ninjaFormsActive ?? false,
tooltipMessage: __( 'Ninja Forms plugin is not active', 'godam' ),
},
metform: {
layerText: __( 'MetForm', 'godam' ),
icon: MetformIcon,
isActive: window?.videoData?.metformActive ?? false,
tooltipMessage: __( 'MetForm plugin is not active', 'godam' ),
},
},
},
{
title: __( 'Ad', 'godam' ),
icon: video,
type: 'ad',
layerText: __( 'Ad', 'godam' ),
tooltipMessage: __( 'This ad will be overridden by Ad server\'s ads', 'godam' ),
},
{
title: __( 'Poll', 'godam' ),
icon: thumbsUp,
type: 'poll',
layerText: __( 'Poll', 'godam' ),
isActive: Boolean( window?.easydamMediaLibrary?.isPollPluginActive ) ?? false,
tooltipMessage: __( 'Poll plugin is not active', 'godam' ),
},
// Add-on layers (e.g., WooCommerce) are merged from PHP via godamVideoEditorConfig.
...( window.godamVideoEditorConfig?.layerOptions || [] ),
];
/**
* Sidebar component to display and select different types of layers to be added to the video.
*
* @param {Object} param0 - Props passed to SidebarLayers component.
* @param {number} param0.currentTime - The current playback time of the video (in seconds or milliseconds).
* @param {Function} param0.onSelectLayer - Callback function invoked when a layer is selected.
* @param {Function} param0.onPauseVideo - Function to pause the video playback.
* @param {number} param0.duration - The total duration of the video (in seconds or milliseconds).
*
* @return {JSX.Element} The rendered SidebarLayers component.
*/
const SidebarLayers = ( { currentTime, onSelectLayer, onPauseVideo, duration } ) => {
const [ isOpen, setOpen ] = useState( false );
const loading = useSelector( ( state ) => state.videoReducer.loading );
const addLayerModalTime = useSelector( ( state ) => state.videoReducer.addLayerModalTime );
const dispatch = useDispatch();
const openModal = useCallback( () => {
setOpen( true );
if ( onPauseVideo ) {
onPauseVideo();
}
}, [ onPauseVideo ] );
const closeModal = () => {
setOpen( false );
// Clear the addLayerModalTime when closing the modal
dispatch( setAddLayerModalTime( null ) );
};
// Listen for addLayerModalTime changes to open the modal from the slider
useEffect( () => {
if ( addLayerModalTime !== null ) {
openModal();
}
}, [ addLayerModalTime, openModal ] );
const layers = useSelector( ( state ) => state.videoReducer.layers );
const currentLayer = useSelector( ( state ) => state.videoReducer.currentLayer );
const videoConfig = useSelector( ( state ) => state.videoReducer.videoConfig );
const adServer = videoConfig?.adServer ?? 'self-hosted';
// Sort the array (ascending order), excluding layers with unknown types.
const sortedLayers = [ ...layers ]
.filter( ( layer ) => layerTypes.some( ( lt ) => lt.type === layer.type ) )
.sort( ( a, b ) => a.displayTime - b.displayTime );
const addNewLayer = ( type, formType ) => {
switch ( type ) {
case 'form':
dispatch( addLayer( {
id: uuidv4(),
displayTime: currentTime,
type,
form_type: formType || 'gravity',
submitted: false,
allow_skip: true,
custom_css: '',
theme: '',
} ) );
break;
case 'cta':
dispatch( addLayer( {
id: uuidv4(),
displayTime: currentTime,
type,
cta_type: 'image',
cardLayout: 'card-layout--imagecover-text',
text: '',
html: '',
link: '',
allow_skip: true,
} ) );
break;
case 'hotspot':
dispatch(
addLayer( {
id: uuidv4(),
displayTime: currentTime,
type,
duration: 5,
pauseOnHover: false,
hotspots: [],
isNew: true,
} ),
);
break;
case 'ad':
dispatch( addLayer( {
id: uuidv4(),
displayTime: currentTime,
type,
adTagUrl: '',
ad_url: '',
skippable: false,
skip_offset: 5,
} ) );
break;
case 'poll':
dispatch( addLayer( {
id: uuidv4(),
displayTime: currentTime,
type,
poll_id: '',
allow_skip: true,
custom_css: '',
} ) );
break;
default: {
// Check for add-on layer creators (registered via window.godamLayerCreators).
const addonCreator = window.godamLayerCreators?.[ type ];
if ( addonCreator ) {
const layerData = addonCreator( { layers, currentTime, type } );
if ( layerData ) {
dispatch( addLayer( { ...layerData, id: uuidv4() } ) );
}
}
break;
}
}
};
return (
<>
{
! currentLayer ? (
<div id="sidebar-layers" className="pt-4 h-max">
{
sortedLayers?.map( ( layer ) => {
let addWarning = false;
const layerData = layerTypes.find( ( l ) => l.type === layer.type );
const formType = 'form' === layerData?.type ? layerData?.formType[ layer.form_type ?? 'gravity' ] : false;
const icon = formType ? formType?.icon : ( layerData?.icon || layerData?.iconUrl );
const layerText = formType ? formType?.layerText : ( layerData?.layerText || layerData?.title );
/**
* Get Tooltip message.
*/
const tooltipMessage = ( () => {
if ( formType && ! formType.isActive ) {
return formType.tooltipMessage;
}
if ( 'ad-server' === adServer && 'ad' === layerData?.type ) {
return layerData?.tooltipMessage;
}
if ( layerData?.isActive === false ) {
return layerData?.tooltipMessage ?? '';
}
return '';
} )();
if ( '' !== tooltipMessage ) {
addWarning = true;
}
// Disable the button when the required plugin/feature is inactive
// (e.g. form plugin not installed, or WooCommerce layer API key missing/expired).
const isLayerDisabled = ( formType && ! formType.isActive ) || layerData?.isActive === false;
return (
<Tooltip
key={ layer.id }
text={ tooltipMessage }
placement="right"
>
<div className="border rounded-lg mb-2">
<Button
className={ `w-full flex justify-between items-center px-2 py-3 border-1 rounded-lg h-auto hover:bg-gray-50 cursor-pointer border-[#e5e7eb] ${ addWarning ? 'bg-orange-50 hover:bg-orange-50' : '' }` }
onClick={ () => {
dispatch( setCurrentLayer( layer ) );
onSelectLayer( layer.displayTime );
} }
disabled={ isLayerDisabled }
>
<div className="flex items-center gap-2">
{
formType || ( typeof icon === 'string' && icon ) ? (
<img src={ icon } alt={ layer.type } className="w-6 h-6" />
) : (
<Icon icon={ icon } />
)
}
<p className="m-0 text-base">{ layerText } layer at <b>{ layer.displayTime }s</b></p>
{ layer.badge && (
<span className="px-2 py-0.5 text-xs bg-gray-100 text-gray-700 rounded-full">
{ layer.badge }
</span>
) }
</div>
<div>
<Icon icon={ arrowRight } />
</div>
</Button>
</div>
</Tooltip>
);
} )
}
{
! loading && layers.length === 0 && (
<>
<h3 className="text-2xl m-0 text-center">{ __( 'No layers added', 'godam' ) }</h3>
</>
)
}
{
loading && (
<div className="loading-skeleton">
<div className="skeleton-container skeleton-container-short">
<div className="skeleton-header"></div>
</div>
<div className="skeleton-container skeleton-container-short">
<div className="skeleton-header"></div>
</div>
<div className="skeleton-container skeleton-container-short">
<div className="skeleton-header"></div>
</div>
</div>
)
}
{
! loading && (
<div className="mt-10 flex justify-center flex-col items-center">
<Button
className="godam-button w-fit"
variant="primary"
id="add-layer-btn"
icon={ plus }
iconPosition="left"
onClick={ openModal }
disabled={ ! currentTime || layers.find( ( l ) => ( l.displayTime ) === ( currentTime ) ) }
>
{
// translators: %s is the current time in seconds.
sprintf( __( 'Add layer at %ss', 'godam' ), currentTime )
}
</Button>
{ layers.find( ( l ) => l.displayTime === currentTime ) && (
<p className="text-slate-500 text-center">
{ __( 'There is already a layer at this timestamp. Please choose a different timestamp.', 'godam' ) }
</p>
) }
{ ! currentTime && (
<div className="flex items-center gap-2">
<Icon icon={ error } className="w-4 h-4" style={ { fill: '#EAB308' } } />
<p className="text-center text-[#AB3A6C]">{ __( 'Play video to add layer.', 'godam' ) }</p>
</div>
) }
</div>
)
}
{ isOpen && (
<LayerSelector
closeModal={ closeModal }
addNewLayer={ addNewLayer }
/>
) }
</div>
) : (
<div id="sidebar-layers">
<Layer layer={ currentLayer } goBack={ () => dispatch( setCurrentLayer( null ) ) } duration={ duration } />
</div>
)
}
</>
);
};
export default SidebarLayers;