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 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 | 1x 153x 153x 153x 153x 153x 151x 151x 151x 3677x 50x 150x 1x 149x 150x 153x 153x 153x 7803x 153x 153x 153x 153x 151x 153x 3x 3x 3x 3x 153x 7803x 132598x | import { useAnalytics } from '@automattic/jetpack-shared-extension-utils';
import { Composite } from '@wordpress/components';
import { useCallback, useState, useRef, useEffect } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { Spinner, Stack } from '@wordpress/ui';
import clsx from 'clsx';
import MediaBrowserSelectButton from './media-browser-select-button';
import MediaItem from './media-item';
import usePageSource from './use-page-source';
import './style.scss';
const MAX_SELECTED = 50;
/**
* MediaBrowser component
*
* @param {object} props - The component props
* @param {object[]} props.media - The list of media
* @param {string} props.mediaSource - The source of media
* @param {boolean} props.isCopying - Whether the media browser is copying the media
* @param {boolean} props.isLoading - Whether the media browser is loading
* @param {boolean} props.imageOnly - Whether to skip non-media items
* @param {number} props.pageHandle - The current page
* @param {string} props.className - The class name
* @param {boolean} props.multiple - Whether to allow multiple selection
* @param {Function} props.setPath - To set the path for the folder item
* @param {Function} props.nextPage - To get the next path
* @param {Function} props.onCopy - To handle the copy
* @param {Function} props.selectButtonText - To get the select button text
* @param {boolean} props.shouldProxyImg - Whether to use the proxy for the media URL
* @return {import('react').ReactElement} - JSX element
*/
function MediaBrowser( {
media,
mediaSource,
isCopying,
isLoading,
imageOnly,
pageHandle,
className,
multiple,
setPath,
nextPage,
onCopy,
selectButtonText,
shouldProxyImg,
} ) {
const [ selected, setSelected ] = useState( [] );
const gridEl = useRef( null );
const { tracks } = useAnalytics();
const pageSource = usePageSource();
const select = useCallback(
newlySelected => {
let newSelected = [ newlySelected ];
Iif ( newlySelected.type === 'folder' ) {
setPath( newlySelected.ID );
} else if ( multiple ) {
if ( selected.find( item => newlySelected.ID === item.ID ) ) {
newSelected = selected.filter( item => item.ID !== newlySelected.ID );
} else if ( selected.length >= MAX_SELECTED ) {
return;
} else {
newSelected = selected.concat( newlySelected );
}
} else Eif ( selected.length === 1 && newlySelected.ID === selected[ 0 ].ID ) {
newSelected = [];
}
setSelected( newSelected );
},
[ selected, multiple, setPath ]
);
const onCopyAndInsert = useCallback( () => {
tracks.recordEvent( 'jetpack_external_media_modal_submit', {
page: pageSource,
media_source: mediaSource,
media_count: selected.length,
multiple: !! multiple,
} );
onCopy( selected );
}, [ tracks, pageSource, mediaSource, selected, multiple, onCopy ] );
const isAtLimit = multiple && selected.length >= MAX_SELECTED;
const limitMessage = sprintf(
/* translators: %d: maximum number of media items that can be selected at once. */
__( 'You can select up to %d items at a time.', 'jetpack-external-media' ),
MAX_SELECTED
);
const hasMediaItems = media.filter( item => item.type !== 'folder' ).length > 0;
const getSelectButtonLabel = () => {
const defaultLabel = isCopying
? __( 'Inserting…', 'jetpack-external-media' )
: __( 'Select', 'jetpack-external-media', /* dummy arg to avoid bad minification */ 0 );
return selectButtonText ? selectButtonText( selected.length, isCopying ) : defaultLabel;
};
// Using _event to avoid eslint errors. Can change to event if it's in use again.
const handleMediaItemClick = ( _event, { item } ) => {
select( item );
};
// Infinite scroll
useEffect( () => {
const target = gridEl.current?.lastElementChild;
let observer;
Iif ( pageHandle && ! isLoading && target ) {
observer = new window.IntersectionObserver( entries => {
if ( entries[ 0 ].isIntersecting ) {
nextPage();
}
} );
observer.observe( target );
}
return () => {
observer?.unobserve( target );
};
}, [ pageHandle, isLoading, gridEl ] ); // eslint-disable-line react-hooks/exhaustive-deps
return (
<Stack
direction="column"
align="start"
className={ clsx( {
'jetpack-external-media-browser': true,
[ className ]: true,
} ) }
>
<Composite
role="listbox"
ref={ gridEl }
className={ clsx( {
'jetpack-external-media-browser__media': true,
'jetpack-external-media-browser__media__loading': isLoading,
} ) }
aria-label={ __( 'Media list', 'jetpack-external-media' ) }
render={ <ul /> }
>
{ media.map( item => (
<MediaItem
item={ item }
imageOnly={ imageOnly }
key={ item.ID }
onClick={ handleMediaItemClick }
isSelected={ selected.find( toFind => toFind.ID === item.ID ) }
isCopying={ isCopying }
shouldProxyImg={ shouldProxyImg }
/>
) ) }
</Composite>
{ media.length === 0 && ! isLoading && (
<div className="jetpack-external-media-browser__empty">
<p>
{ __( 'Sorry, but nothing matched your search criteria.', 'jetpack-external-media' ) }
</p>
</div>
) }
{ isLoading && (
<Stack align="center" justify="center" className="jetpack-external-media-browser__loading">
<Spinner />
</Stack>
) }
{ hasMediaItems && (
<MediaBrowserSelectButton
label={ getSelectButtonLabel() }
isLoading={ isCopying }
limitMessage={ isAtLimit ? limitMessage : null }
disabled={ selected.length === 0 || isCopying }
onClick={ onCopyAndInsert }
/>
) }
</Stack>
);
}
export default MediaBrowser;
|