/**
* External dependencies
*/
import { Text, Button, ThemeProvider } from '@automattic/jetpack-components';
import { Popover, Dropdown, Modal } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { image, trash, globe as siteDefaultPrivacyIcon } from '@wordpress/icons';
import classNames from 'classnames';
import { useState, useEffect } from 'react';
/**
* Internal dependencies
*/
import privatePrivacyIcon from '../../../components/icons/crossed-eye-icon';
import publicPrivacyIcon from '../../../components/icons/uncrossed-eye-icon';
import {
VIDEO_PRIVACY_LEVELS,
VIDEO_PRIVACY_LEVEL_PRIVATE,
VIDEO_PRIVACY_LEVEL_PUBLIC,
VIDEO_PRIVACY_LEVEL_SITE_DEFAULT,
} from '../../../state/constants';
import { useActionItem } from '../../hooks/use-action-item';
import { usePermission } from '../../hooks/use-permission';
import usePlaybackToken from '../../hooks/use-playback-token';
import usePosterEdit from '../../hooks/use-poster-edit';
import useVideo from '../../hooks/use-video';
import { VideoThumbnailDropdownButtons } from '../video-thumbnail';
import VideoThumbnailSelectorModal from '../video-thumbnail-selector-modal';
import styles from './style.module.scss';
/**
* Types
*/
import {
ActionItemProps,
PopoverWithAnchorProps,
ThumbnailActionsDropdownProps,
VideoQuickActionsProps,
PrivacyActionsDropdownProps,
ConnectVideoQuickActionsProps,
} from './types';
const PopoverWithAnchor = ( {
showPopover = false,
isAnchorFocused = false,
anchor,
children = null,
}: PopoverWithAnchorProps ) => {
if ( ! anchor || ! showPopover ) {
return null;
}
useEffect( () => {
if ( showPopover && ! isAnchorFocused ) {
( anchor?.querySelector( '.components-popover' ) as HTMLElement | null )?.focus();
}
}, [ showPopover ] );
const popoverProps = {
anchor,
offset: 15,
};
return (
{ children }
);
};
const ActionItem = ( { icon, children, className, ...props }: ActionItemProps ) => {
const {
setAnchor,
setIsFocused,
setIsHovering,
anchor,
isFocused,
showPopover,
} = useActionItem();
return (
);
};
const ThumbnailActionsDropdown = ( {
description,
onUpdate,
isUpdatingPoster,
disabled,
}: ThumbnailActionsDropdownProps ) => {
const {
setAnchor,
setIsFocused,
setIsHovering,
setShowPopover,
anchor,
isFocused,
showPopover,
} = useActionItem();
return (
(
) }
renderContent={ ( { onClose } ) => (
onUpdate( 'default' ) }
onSelectFromVideo={ () => onUpdate( 'select-from-video' ) }
onUploadImage={ () => onUpdate( 'upload-image' ) }
/>
) }
/>
);
};
const PrivacyActionsDropdown = ( {
description,
privacySetting,
isUpdatingPrivacy,
onUpdate,
disabled,
}: PrivacyActionsDropdownProps ) => {
const {
setAnchor,
setIsFocused,
setIsHovering,
setShowPopover,
anchor,
isFocused,
showPopover,
} = useActionItem();
let currentPrivacyIcon = siteDefaultPrivacyIcon;
if ( VIDEO_PRIVACY_LEVELS[ privacySetting ] === VIDEO_PRIVACY_LEVEL_PRIVATE ) {
currentPrivacyIcon = privatePrivacyIcon;
} else if ( VIDEO_PRIVACY_LEVELS[ privacySetting ] === VIDEO_PRIVACY_LEVEL_PUBLIC ) {
currentPrivacyIcon = publicPrivacyIcon;
}
return (
(
) }
renderContent={ ( { onClose } ) => (
) }
/>
);
};
const VideoQuickActions = ( {
className,
privacySetting,
isUpdatingPrivacy,
isUpdatingPoster,
onUpdateVideoThumbnail,
onUpdateVideoPrivacy,
onDeleteVideo,
}: VideoQuickActionsProps ) => {
const { canPerformAction } = usePermission();
return (
{ __( 'Delete video', 'jetpack-videopress-pkg' ) }
);
};
export const ConnectVideoQuickActions = ( props: ConnectVideoQuickActionsProps ) => {
const { videoId } = props;
if ( ! Number.isFinite( videoId ) ) {
return null;
}
const { data, updateVideoPrivacy, deleteVideo, isUpdatingPrivacy, isUpdatingPoster } = useVideo(
videoId
);
const { isFetchingPlaybackToken } = usePlaybackToken( data );
const [ showDeleteModal, setShowDeleteModal ] = useState( false );
const {
frameSelectorIsOpen,
handleCloseSelectFrame,
handleOpenSelectFrame,
handleVideoFrameSelected,
selectedTime,
handleConfirmFrame,
updatePosterImageFromFrame,
selectAndUpdatePosterImageFromLibrary,
} = usePosterEdit( { video: data } );
const onUpdateVideoThumbnail: VideoQuickActionsProps[ 'onUpdateVideoThumbnail' ] = async action => {
switch ( action ) {
case 'select-from-video':
return handleOpenSelectFrame();
case 'upload-image':
return selectAndUpdatePosterImageFromLibrary();
}
};
useEffect( () => {
if ( selectedTime == null ) {
return;
}
updatePosterImageFromFrame();
}, [ selectedTime ] );
if ( showDeleteModal ) {
return (
setShowDeleteModal( false ) }
className={ styles[ 'delete-video-modal' ] }
>
{ __( 'This action cannot be undone.', 'jetpack-videopress-pkg' ) }
);
}
if ( frameSelectorIsOpen ) {
return (
<>
>
);
}
const { privacySetting } = data;
return (
setShowDeleteModal( true ) }
privacySetting={ privacySetting }
isUpdatingPrivacy={ isUpdatingPrivacy || isFetchingPlaybackToken }
isUpdatingPoster={ isUpdatingPoster }
/>
);
};
export default VideoQuickActions;