/** * External dependencies */ import { Button, Text, useBreakpointMatch } from '@automattic/jetpack-components'; import { createInterpolateElement } from '@wordpress/element'; import { __, sprintf } from '@wordpress/i18n'; import { grid, formatListBullets } from '@wordpress/icons'; import classnames from 'classnames'; import React, { useState } from 'react'; import { useHistory } from 'react-router-dom'; /** * Internal dependencies */ import useVideos from '../../hooks/use-videos'; import { SearchInput } from '../input'; import { ConnectLocalPagination, ConnectPagination } from '../pagination'; import { FilterButton, ConnectFilterSection } from '../video-filter'; import VideoGrid from '../video-grid'; import VideoList, { LocalVideoList } from '../video-list'; import styles from './styles.module.scss'; /** * Types */ import { LocalLibraryProps, VideoLibraryProps } from './types'; const LIBRARY_TYPE_LOCALSORAGE_KEY = 'videopress-library-type'; const LibraryType = { List: 'list', Grid: 'grid', } as const; type LibraryType = typeof LibraryType[ keyof typeof LibraryType ]; const VideoLibraryWrapper = ( { children, totalVideos = 0, libraryType = LibraryType.List, onChangeType, hideFilter = false, title, disabled, }: { children: React.ReactNode; libraryType?: LibraryType; totalVideos?: number; onChangeType?: () => void; hideFilter?: boolean; title?: string; disabled?: boolean; } ) => { const { setSearch, search, isFetching } = useVideos(); const [ searchQuery, setSearchQuery ] = useState( search ); const [ isLg ] = useBreakpointMatch( 'lg' ); const [ isFilterActive, setIsFilterActive ] = useState( false ); const singularTotalVideosLabel = __( '1 Video', 'jetpack-videopress-pkg' ); const pluralTotalVideosLabel = sprintf( /* translators: placeholder is the number of videos */ __( '%s Videos', 'jetpack-videopress-pkg' ), totalVideos ); const totalVideosLabel = totalVideos === 1 ? singularTotalVideosLabel : pluralTotalVideosLabel; return (
{ title } { ! isLg && { totalVideosLabel } }
{ isLg && { totalVideosLabel } } { hideFilter ? null : (
setIsFilterActive( v => ! v ) } isActive={ isFilterActive } disabled={ disabled } />
) }
{ isFilterActive && } { children }
); }; export const VideoPressLibrary = ( { videos, totalVideos, loading }: VideoLibraryProps ) => { const history = useHistory(); const { search } = useVideos(); const libraryTypeFromLocalStorage = localStorage.getItem( LIBRARY_TYPE_LOCALSORAGE_KEY ) as LibraryType; const [ libraryType, setLibraryType ] = useState< LibraryType >( libraryTypeFromLocalStorage ?? LibraryType.Grid ); const uploading = videos?.some?.( video => video.uploading ); const toggleType = () => { setLibraryType( current => { const next = current === LibraryType.Grid ? LibraryType.List : LibraryType.Grid; localStorage.setItem( LIBRARY_TYPE_LOCALSORAGE_KEY, next ); return next; } ); }; const handleClickEditDetails = video => { history.push( `/video/${ video?.id }/edit` ); }; const library = libraryType === LibraryType.Grid ? ( ) : ( ); return ( { videos.length > 0 || loading ? ( library ) : ( { search.trim() ? createInterpolateElement( sprintf( /* translators: placeholder is the search term */ __( 'No videos match your search for %s.', 'jetpack-videopress-pkg' ), search ), { em: , } ) : __( 'No videos match your filtering criteria.', 'jetpack-videopress-pkg' ) } ) } ); }; export const LocalLibrary = ( { videos, totalVideos, loading, uploading, onUploadClick, }: LocalLibraryProps ) => { return ( ); };