Plugin Directory

source: jetpack/trunk/jetpack_vendor/automattic/jetpack-videopress/src/client/admin/components/admin-page/libraries.tsx @ 2832426

Last change on this file since 2832426 was 2832426, checked in by wpkaren, 20 months ago

Updating trunk to version 11.7-a.3

File size: 5.7 KB
Line 
1/**
2 * External dependencies
3 */
4import { Button, Text, useBreakpointMatch } from '@automattic/jetpack-components';
5import { createInterpolateElement } from '@wordpress/element';
6import { __, sprintf } from '@wordpress/i18n';
7import { grid, formatListBullets } from '@wordpress/icons';
8import classnames from 'classnames';
9import React, { useState } from 'react';
10import { useHistory } from 'react-router-dom';
11/**
12 * Internal dependencies
13 */
14import useQueryStringPages from '../../hooks/use-query-string-pages';
15import useVideos from '../../hooks/use-videos';
16import { SearchInput } from '../input';
17import { ConnectLocalPagination, ConnectPagination } from '../pagination';
18import { FilterButton, ConnectFilterSection } from '../video-filter';
19import VideoGrid from '../video-grid';
20import VideoList, { LocalVideoList } from '../video-list';
21import styles from './styles.module.scss';
22/**
23 * Types
24 */
25import { LocalLibraryProps, VideoLibraryProps } from './types';
26
27const LIBRARY_TYPE_LOCALSORAGE_KEY = 'videopress-library-type';
28
29const LibraryType = {
30        List: 'list',
31        Grid: 'grid',
32} as const;
33
34type LibraryType = typeof LibraryType[ keyof typeof LibraryType ];
35
36const VideoLibraryWrapper = ( {
37        children,
38        totalVideos = 0,
39        libraryType = LibraryType.List,
40        onChangeType,
41        hideFilter = false,
42        title,
43        disabled,
44}: {
45        children: React.ReactNode;
46        libraryType?: LibraryType;
47        totalVideos?: number;
48        onChangeType?: () => void;
49        hideFilter?: boolean;
50        title?: string;
51        disabled?: boolean;
52} ) => {
53        const { setSearch, search, isFetching } = useVideos();
54        const { setPageOnURL } = useQueryStringPages();
55
56        const onSearchHandler = searchQuery => {
57                // clear the pagination, setting it back to page 1
58                setPageOnURL( 1 );
59                setSearch( searchQuery );
60        };
61
62        const [ searchQuery, setSearchQuery ] = useState( search );
63        const [ isLg ] = useBreakpointMatch( 'lg' );
64
65        const [ isFilterActive, setIsFilterActive ] = useState( false );
66
67        const singularTotalVideosLabel = __( '1 Video', 'jetpack-videopress-pkg' );
68        const pluralTotalVideosLabel = sprintf(
69                /* translators: placeholder is the number of videos */
70                __( '%s Videos', 'jetpack-videopress-pkg' ),
71                totalVideos
72        );
73        const totalVideosLabel = totalVideos === 1 ? singularTotalVideosLabel : pluralTotalVideosLabel;
74
75        return (
76                <div className={ styles[ 'library-wrapper' ] }>
77                        <Text variant="headline-small" mb={ 1 }>
78                                { title }
79                        </Text>
80                        { ! isLg && <Text className={ styles[ 'total-sm' ] }>{ totalVideosLabel }</Text> }
81                        <div className={ styles[ 'total-filter-wrapper' ] }>
82                                { isLg && <Text>{ totalVideosLabel }</Text> }
83                                { hideFilter ? null : (
84                                        <div className={ styles[ 'filter-wrapper' ] }>
85                                                <SearchInput
86                                                        className={ classnames( styles[ 'search-input' ], { [ styles.small ]: ! isLg } ) }
87                                                        onSearch={ onSearchHandler }
88                                                        value={ searchQuery }
89                                                        loading={ isFetching }
90                                                        onChange={ setSearchQuery }
91                                                        disabled={ disabled }
92                                                />
93
94                                                <FilterButton
95                                                        onClick={ () => setIsFilterActive( v => ! v ) }
96                                                        isActive={ isFilterActive }
97                                                        disabled={ disabled }
98                                                />
99
100                                                <Button
101                                                        variant="tertiary"
102                                                        size="small"
103                                                        icon={ libraryType === LibraryType.List ? grid : formatListBullets }
104                                                        onClick={ onChangeType }
105                                                />
106                                        </div>
107                                ) }
108                        </div>
109                        { isFilterActive && <ConnectFilterSection className={ styles[ 'filter-section' ] } /> }
110                        { children }
111                </div>
112        );
113};
114
115export const VideoPressLibrary = ( { videos, totalVideos, loading }: VideoLibraryProps ) => {
116        const history = useHistory();
117        const { search } = useVideos();
118
119        const libraryTypeFromLocalStorage = localStorage.getItem(
120                LIBRARY_TYPE_LOCALSORAGE_KEY
121        ) as LibraryType;
122
123        const [ libraryType, setLibraryType ] = useState< LibraryType >(
124                libraryTypeFromLocalStorage ?? LibraryType.Grid
125        );
126
127        const uploading = videos?.some?.( video => video.uploading );
128
129        const toggleType = () => {
130                setLibraryType( current => {
131                        const next = current === LibraryType.Grid ? LibraryType.List : LibraryType.Grid;
132                        localStorage.setItem( LIBRARY_TYPE_LOCALSORAGE_KEY, next );
133                        return next;
134                } );
135        };
136
137        const handleClickEditDetails = video => {
138                history.push( `/video/${ video?.id }/edit` );
139        };
140
141        const library =
142                libraryType === LibraryType.Grid ? (
143                        <VideoGrid
144                                videos={ videos }
145                                onVideoDetailsClick={ handleClickEditDetails }
146                                loading={ loading }
147                                count={ uploading ? videos.length : 6 }
148                        />
149                ) : (
150                        <VideoList
151                                videos={ videos }
152                                onVideoDetailsClick={ handleClickEditDetails }
153                                hidePlays
154                                loading={ loading }
155                        />
156                );
157
158        return (
159                <VideoLibraryWrapper
160                        totalVideos={ totalVideos }
161                        onChangeType={ toggleType }
162                        libraryType={ libraryType }
163                        title={ __( 'Your VideoPress library', 'jetpack-videopress-pkg' ) }
164                >
165                        { videos.length > 0 || loading ? (
166                                library
167                        ) : (
168                                <Text>
169                                        { search.trim()
170                                                ? createInterpolateElement(
171                                                                sprintf(
172                                                                        /* translators: placeholder is the search term */
173                                                                        __( 'No videos match your search for <em>%s</em>.', 'jetpack-videopress-pkg' ),
174                                                                        search
175                                                                ),
176                                                                {
177                                                                        em: <em className={ styles[ 'query-no-results' ] } />,
178                                                                }
179                                                  )
180                                                : __( 'No videos match your filtering criteria.', 'jetpack-videopress-pkg' ) }
181                                </Text>
182                        ) }
183                        <ConnectPagination className={ styles.pagination } />
184                </VideoLibraryWrapper>
185        );
186};
187
188export const LocalLibrary = ( {
189        videos,
190        totalVideos,
191        loading,
192        uploading,
193        onUploadClick,
194}: LocalLibraryProps ) => {
195        return (
196                <VideoLibraryWrapper
197                        totalVideos={ totalVideos }
198                        hideFilter
199                        title={ __( 'Local videos', 'jetpack-videopress-pkg' ) }
200                >
201                        <LocalVideoList
202                                videos={ videos }
203                                loading={ loading }
204                                onActionClick={ onUploadClick }
205                                uploading={ uploading }
206                        />
207                        <ConnectLocalPagination className={ styles.pagination } />
208                </VideoLibraryWrapper>
209        );
210};
Note: See TracBrowser for help on using the repository browser.