1 | /** |
---|
2 | * External dependencies |
---|
3 | */ |
---|
4 | import { Button, Text, useBreakpointMatch } from '@automattic/jetpack-components'; |
---|
5 | import { createInterpolateElement } from '@wordpress/element'; |
---|
6 | import { __, sprintf } from '@wordpress/i18n'; |
---|
7 | import { grid, formatListBullets } from '@wordpress/icons'; |
---|
8 | import classnames from 'classnames'; |
---|
9 | import React, { useState } from 'react'; |
---|
10 | import { useHistory } from 'react-router-dom'; |
---|
11 | /** |
---|
12 | * Internal dependencies |
---|
13 | */ |
---|
14 | import useQueryStringPages from '../../hooks/use-query-string-pages'; |
---|
15 | import useVideos from '../../hooks/use-videos'; |
---|
16 | import { SearchInput } from '../input'; |
---|
17 | import { ConnectLocalPagination, ConnectPagination } from '../pagination'; |
---|
18 | import { FilterButton, ConnectFilterSection } from '../video-filter'; |
---|
19 | import VideoGrid from '../video-grid'; |
---|
20 | import VideoList, { LocalVideoList } from '../video-list'; |
---|
21 | import styles from './styles.module.scss'; |
---|
22 | /** |
---|
23 | * Types |
---|
24 | */ |
---|
25 | import { LocalLibraryProps, VideoLibraryProps } from './types'; |
---|
26 | |
---|
27 | const LIBRARY_TYPE_LOCALSORAGE_KEY = 'videopress-library-type'; |
---|
28 | |
---|
29 | const LibraryType = { |
---|
30 | List: 'list', |
---|
31 | Grid: 'grid', |
---|
32 | } as const; |
---|
33 | |
---|
34 | type LibraryType = typeof LibraryType[ keyof typeof LibraryType ]; |
---|
35 | |
---|
36 | const 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 | |
---|
115 | export 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 | |
---|
188 | export 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 | }; |
---|