Back

0% Statements 0/16
0% Branches 0/12
0% Functions 0/5
0% Lines 0/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                                                                               
/**
 * External dependencies
 */
import { PanelBody, SelectControl, ToggleControl } from '@wordpress/components';
import { __, _x } from '@wordpress/i18n';
/**
 * Internal dependencies
 */
import {
	VIDEO_PRIVACY_LEVELS,
	VIDEO_PRIVACY_LEVEL_PRIVATE,
	VIDEO_PRIVACY_LEVEL_PUBLIC,
	VIDEO_PRIVACY_LEVEL_SITE_DEFAULT,
	VIDEO_RATING_G,
	VIDEO_RATING_PG_13,
	VIDEO_RATING_R_17,
} from '../../../../../state/constants';
/**
 * Types
 */
import type { PrivacyAndRatingPanelProps } from '../../types';
import type { ReactElement } from 'react';
 
/**
 * React component that renders the settings within the privacy and ratings panel.
 *
 * @param {PrivacyAndRatingPanelProps} props - Component props.
 * @return {ReactElement}               Settings to change video's privacy and ratings.
 */
export default function PrivacyAndRatingSettings( {
	attributes,
	setAttributes,
	privateEnabledForSite,
	videoBelongToSite,
}: PrivacyAndRatingPanelProps ): ReactElement {
	const { privacySetting, rating, allowDownload, displayEmbed } = attributes;
	const shareMenuDisabledForSite = Boolean( window?.videoPressEditorState?.shareMenuDisabled );
 
	const privacyLabels = {
		private: _x( 'Site Default (Private)', 'VideoPress privacy setting', 'jetpack-videopress-pkg' ),
		public: _x( 'Site Default (Public)', 'VideoPress privacy setting', 'jetpack-videopress-pkg' ),
	};
 
	const privacyOptionSiteDefault = {
		value: String( VIDEO_PRIVACY_LEVELS.indexOf( VIDEO_PRIVACY_LEVEL_SITE_DEFAULT ) ),
		label: privateEnabledForSite ? privacyLabels.private : privacyLabels.public,
	};
	const privacyOptionPublic = {
		value: String( VIDEO_PRIVACY_LEVELS.indexOf( VIDEO_PRIVACY_LEVEL_PUBLIC ) ),
		label: _x( 'Public', 'VideoPress privacy setting', 'jetpack-videopress-pkg' ),
	};
	const privacyOptionPrivate = {
		value: String( VIDEO_PRIVACY_LEVELS.indexOf( VIDEO_PRIVACY_LEVEL_PRIVATE ) ),
		label: _x( 'Private', 'VideoPress privacy setting', 'jetpack-videopress-pkg' ),
	};
 
	return (
		<PanelBody title={ __( 'Privacy and rating', 'jetpack-videopress-pkg' ) } initialOpen={ false }>
			<SelectControl
				label={ _x( 'Rating', 'The age rating for this video.', 'jetpack-videopress-pkg' ) }
				value={ ( rating ?? '' ) as 'G' | 'PG-13' | 'R-17' }
				options={ [
					{
						label: _x( 'G', 'Video rating for "General Audiences".', 'jetpack-videopress-pkg' ),
						value: VIDEO_RATING_G,
					},
					{
						label: _x(
							'PG-13',
							'Video rating for "Parental Guidance", unsuitable for children under 13.',
							'jetpack-videopress-pkg'
						),
						value: VIDEO_RATING_PG_13,
					},
					{
						label: _x(
							'R',
							'Video rating for "Restricted", not recommended for children under 17.',
							'jetpack-videopress-pkg'
						),
						value: VIDEO_RATING_R_17,
					},
				] }
				onChange={ value => {
					setAttributes( { rating: value } );
				} }
				disabled={ ! videoBelongToSite }
				__nextHasNoMarginBottom={ true }
				__next40pxDefaultSize={ true }
			/>
 
			<SelectControl
				label={ __( 'Privacy', 'jetpack-videopress-pkg' ) }
				onChange={ value => {
					const attrsToUpdate: {
						privacySetting?: number;
						isPrivate?: boolean;
					} = {};
 
					// Anticipate the isPrivate attribute.
					if ( value !== privacyOptionSiteDefault.value ) {
						attrsToUpdate.isPrivate = value === privacyOptionPrivate.value;
					} else {
						attrsToUpdate.isPrivate = privateEnabledForSite;
					}
 
					attrsToUpdate.privacySetting = Number( value );
					setAttributes( attrsToUpdate );
				} }
				value={ String( privacySetting ) }
				options={ [ privacyOptionSiteDefault, privacyOptionPublic, privacyOptionPrivate ] }
				disabled={ ! videoBelongToSite }
				__nextHasNoMarginBottom={ true }
				__next40pxDefaultSize={ true }
			/>
 
			<ToggleControl
				label={ __( 'Allow download', 'jetpack-videopress-pkg' ) }
				checked={ allowDownload }
				onChange={ value => {
					setAttributes( { allowDownload: value } );
				} }
				disabled={ ! videoBelongToSite }
				__nextHasNoMarginBottom={ true }
			/>
 
			<ToggleControl
				label={ __( 'Show video sharing menu', 'jetpack-videopress-pkg' ) }
				checked={ displayEmbed && ! shareMenuDisabledForSite }
				onChange={ value => {
					setAttributes( { displayEmbed: value } );
				} }
				help={
					shareMenuDisabledForSite
						? __(
								'Sharing is turned off for all videos in VideoPress Settings.',
								'jetpack-videopress-pkg'
							)
						: __(
								'Gives viewers the option to share the video link and HTML embed code',
								'jetpack-videopress-pkg',
								// @ts-expect-error Dummy arg to avoid bad minification; ignored at runtime.
								/* dummy arg to avoid bad minification */ 0
							)
				}
				disabled={ ! videoBelongToSite || shareMenuDisabledForSite }
				__nextHasNoMarginBottom={ true }
			/>
		</PanelBody>
	);
}