Back

70.37% Statements 19/27
64.28% Branches 9/14
76.47% Functions 13/17
65.21% Lines 15/23

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 153 154 155 156                                3x 18x   10x                     105x 8x         3x                                                                     35x                                       1x 1x 1x       105x                                     4x   6x                                     2x               1x                    
import { __ } from '@wordpress/i18n';
import type { LibraryItem, LibraryItemPrivacy } from '../../types/library';
import type { Action } from '@wordpress/dataviews';
 
type Api = {
	promoteLocal: ( id: string ) => void;
	retryUpload: ( id: string ) => void;
	deleteItems: ( ids: string[] ) => void;
	setPrivacy: ( ids: string[], privacy: LibraryItemPrivacy ) => void;
	openVideoDetails: ( id: string ) => void;
	manageCaptions: ( item: LibraryItem ) => void;
};
 
// Allowlist on 'idle' (matching TitleText and ThumbnailField) rather than a
// blocklist of known in-flight statuses, so any future status is excluded
// from row actions by default instead of silently slipping through.
const isVideoPressIdle = ( item: LibraryItem ) =>
	item.type === 'videopress' && item.upload.status === 'idle';
 
const isLocalIdle = ( item: LibraryItem ) => item.type === 'local' && item.upload.status === 'idle';
 
/**
 * Eligibility for a privacy action: the item must be an idle VideoPress video
 * that does not already have the target privacy. Local items, in-flight videos,
 * and videos already at the requested setting are filtered out, so a mixed bulk
 * selection only touches the rows that actually need to change.
 *
 * @param target - The privacy value the action would apply.
 * @return An `isEligible` predicate for the DataViews action.
 */
const isPrivacyChangeEligible = ( target: LibraryItemPrivacy ) => ( item: LibraryItem ) =>
	isVideoPressIdle( item ) && item.privacy !== target;
 
// The privacy actions differ only by target value, label, and id suffix, so
// build them from one descriptor to keep them in lockstep. The id suffix is
// kept separate because `site-default` ships under the shorter `set-privacy-site`.
const PRIVACY_ACTIONS: { idSuffix: string; label: string; privacy: LibraryItemPrivacy }[] = [
	{ idSuffix: 'public', label: __( 'Make public', 'jetpack-videopress-pkg' ), privacy: 'public' },
	{
		idSuffix: 'private',
		label: __( 'Make private', 'jetpack-videopress-pkg' ),
		privacy: 'private',
	},
	{
		idSuffix: 'site',
		label: __( 'Reset to site default', 'jetpack-videopress-pkg' ),
		privacy: 'site-default',
	},
];
 
/**
 * Build the DataViews actions array for the Library tab. Eligibility predicates
 * gate per-row availability based on `item.type` and `item.upload.status`. The
 * Delete, privacy, and Upload to VideoPress actions set `supportsBulk: true`
 * and use `isEligible` to filter out items that can't accept the change
 * (in-flight videos, videos already at the target privacy, non-local items for
 * the promote). DataViews silently skips ineligible items, so a mixed selection
 * only applies to the rows that qualify. Rows with a delete already in flight
 * are ineligible for every action so a slow delete can't be double-fired or
 * raced by an edit.
 *
 * Invariant: every idle row — local or VideoPress — must satisfy at least one
 * `supportsBulk` action's `isEligible`. DataViews disables an item's selection
 * checkbox when no bulk action applies to it, so a row type with zero
 * bulk-capable actions silently becomes unselectable in both layouts (the bug
 * behind JETPACK-2032, where local rows only had single-item actions).
 *
 * @param api - Hook mutators forwarded into the action callbacks.
 * @return The actions array for `<DataViews>`.
 */
export function buildLibraryActions( api: Api ): Action< LibraryItem >[] {
	return [
		{
			id: 'edit-details',
			label: __( 'Edit details', 'jetpack-videopress-pkg' ),
			isPrimary: true,
			supportsBulk: false,
			isEligible: isVideoPressIdle,
			callback: items => {
				const [ item ] = items;
				if ( item ) {
					api.openVideoDetails( item.id );
				}
			},
		},
		{
			id: 'manage-captions',
			label: __( 'Manage subtitles', 'jetpack-videopress-pkg' ),
			supportsBulk: false,
			isEligible: isVideoPressIdle,
			callback: items => {
				const [ item ] = items;
				Eif ( item ) {
					api.manageCaptions( item );
				}
			},
		},
		...PRIVACY_ACTIONS.map( ( { idSuffix, label, privacy } ) => ( {
			id: `set-privacy-${ idSuffix }`,
			label,
			supportsBulk: true,
			isEligible: isPrivacyChangeEligible( privacy ),
			callback: ( items: LibraryItem[] ) => {
				api.setPrivacy(
					items.map( i => i.id ),
					privacy
				);
			},
		} ) ),
		{
			id: 'delete',
			label: __( 'Delete', 'jetpack-videopress-pkg' ),
			supportsBulk: true,
			// Local rows are deletable too: the pipeline is a plain core
			// DELETE /wp/v2/media/{id}?force=true either way, with nothing
			// VideoPress-specific about it.
			isEligible: item => isVideoPressIdle( item ) || isLocalIdle( item ),
			callback: items => {
				api.deleteItems( items.map( i => i.id ) );
			},
		},
		{
			id: 'upload-to-vp',
			label: __( 'Upload to VideoPress', 'jetpack-videopress-pkg' ),
			isPrimary: true,
			supportsBulk: true,
			// On WordPress.com Simple the promote mutation routes through
			// wpcom/v2/videopress/promote (in-process — the file is already on
			// WordPress.com storage); elsewhere it walks /videopress/v1/upload/{id}.
			// Allowlist on 'idle' (matching the design note at the top of this
			// file) so a row whose promote is already in flight — overlaid as
			// 'promoting' — can't double-fire it.
			isEligible: isLocalIdle,
			// Fan out per item: promoteLocal owns per-id in-flight state (a
			// re-entrant id is a no-op) and each promote settles independently
			// with its own notice and row overlay.
			callback: items => {
				items.forEach( item => api.promoteLocal( item.id ) );
			},
		},
		{
			id: 'retry-upload',
			label: __( 'Retry', 'jetpack-videopress-pkg' ),
			isPrimary: true,
			supportsBulk: false,
			isEligible: item => item.upload.status === 'failed',
			callback: items => {
				const [ item ] = items;
				if ( item ) {
					api.retryUpload( item.id );
				}
			},
		},
	];
}