Back

100% Statements 5/5
100% Branches 8/8
100% Functions 2/2
100% Lines 5/5

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                                      7x 5x 5x     2x                             2x    
import type { LibraryItem } from '../types/library';
 
/**
 * The shareable URL for a video, or null when there isn't an honest one.
 *
 * A VideoPress-hosted video gets its player URL — private videos live on
 * `video.wordpress.com`, public ones on `videopress.com`, the same split
 * the preview player's embed origin uses. A local attachment (no GUID — the
 * usual case on a site with no WordPress.com connection) has no player URL, so
 * it falls back to the attachment's own `source_url`, which does resolve and
 * does play.
 *
 * Returns null rather than a guess when there is neither, so the caller can
 * omit the button entirely instead of shipping one that copies nothing.
 *
 * @param item - The library item.
 * @return The URL to copy, or null.
 */
export function resolveShareLink( item: LibraryItem ): string | null {
	if ( item.guid ) {
		const host = item.isPrivate ? 'video.wordpress.com' : 'videopress.com';
		return `https://${ host }/v/${ item.guid }`;
	}
 
	return item.sourceUrl ? item.sourceUrl : null;
}
 
/**
 * The embed snippet for a video, or null when there isn't one.
 *
 * `LibraryItem.shortcode` is built from the GUID (see `buildShortcode`), so it
 * is empty for every non-VideoPress attachment. An empty string must not reach
 * a "Copy embed" button — a button that copies nothing is worse than an absent
 * one.
 *
 * @param item - The library item.
 * @return The `[videopress …]` shortcode, or null.
 */
export function resolveEmbedSnippet( item: LibraryItem ): string | null {
	return item.shortcode ? item.shortcode : null;
}