import React from 'react'; import {NavLink, Route} from "react-router-dom"; import Paginator, { changePageHistory, DEFAULT_LIMIT, defaultSearchOrder, defaultVideoOrder, humanFileSize, Progresses, scrollToTop, searchOrders, secondsToString, TabLinks, VideoCards, videoOrders, WROLModeMessage } from "./Common" import VideoPage from "./VideoPlayer"; import {download, getChannel, getStatistics, getVideo, refresh, searchVideos} from "../api"; import { Button, Dropdown, Form, Grid, Header, Icon, Input, Loader, Modal, Radio, Segment, Statistic } from "semantic-ui-react"; import * as QueryString from 'query-string'; import Container from "semantic-ui-react/dist/commonjs/elements/Container"; import {Channels, EditChannel, NewChannel} from "./Channels"; import {VideoPlaceholder} from "./Placeholder"; class ManageVideos extends React.Component { constructor(props) { super(props); this.state = { streamUrl: null, mostRecentDownload: '', } } download = async (e) => { e.preventDefault(); let response = await download(); if (response.stream_url) { this.setState({streamUrl: response.stream_url}); } } refresh = async (e) => { e.preventDefault(); let response = await refresh(); if (response.stream_url) { this.setState({streamUrl: response.stream_url}); } } render() { return (


{this.state.streamUrl && }
) } } export class VideoWrapper extends React.Component { constructor(props) { super(props); this.state = { video: null, prev: null, next: null, channel: null, no_channel: null, } } async componentDidMount() { await this.fetchVideo(); } async fetchVideo() { // Get and display the Video specified in the Router match let [video, prev, next] = await getVideo(this.props.match.params.video_id); let channel_link = this.props.match.params.channel_link; let channel = channel_link ? await getChannel(channel_link) : null; let no_channel = false; if (!channel) { no_channel = true; } this.setState({video, prev, next, channel, no_channel}, scrollToTop); } async componentDidUpdate(prevProps, prevState) { if (prevProps.match.params.video_id !== this.props.match.params.video_id) { // Clear the current video so that it will change, even if the video is playing. this.setState({video: null, prev: null, next: null, channel: null}); await this.fetchVideo(); } } render() { if (this.state.video && (this.state.no_channel || this.state.channel)) { return } else { return } } } export function VideosPreview({videos}) { let body = if (videos && videos.length === 0) { body =

No videos available.

} else if (videos && videos.length > 0) { body = } return <> {body} } function FilterModal(props) { /* Expecting props.filters = [ {label: 'Favorites', name: 'favorite', value: false}, {label: 'Censored', name: 'censored', value: false}, ]; */ let inputs = props.filters.map((i) => props.toggleFilter(i.name) : null} disabled={i.disabled} /> ) return ( {props.header}
{inputs}
{props.applyButton && }
) } class Videos extends React.Component { constructor(props) { super(props); const query = QueryString.parse(this.props.location.search); let activePage = query.page ? parseInt(query.page) : 1; // First page is 1 by default, of course. let searchStr = query.q || ''; let searchOrder = query.o || defaultVideoOrder; let filtersEnabled = query.f && query.f.length > 0 ? query.f.split(',') : []; let filters = [ { label: 'Favorites', name: 'favorite', value: filtersEnabled.indexOf('favorite') >= 0 || props.filter === 'favorite', disabled: props.filter === 'favorite', }, { label: 'Censored', name: 'censored', value: filtersEnabled.indexOf('censored') >= 0 }, ]; this.state = { activePage, channel: null, filters, filtersEnabled, filtersOpen: false, header: '', limit: DEFAULT_LIMIT, next: null, prev: null, queryStr: searchStr, searchOrder, searchStr: '', totalPages: null, videoOrders: searchStr === '' ? videoOrders : searchOrders, videos: null, }; } async componentDidMount() { if (this.props.match.params.channel_link) { await this.fetchChannel(); } else { await this.fetchVideos(); } this.setHeader(); } async componentDidUpdate(prevProps, prevState, snapshot) { let params = this.props.match.params; let channelChanged = params.channel_link !== prevProps.match.params.channel_link; let pageChanged = ( prevState.activePage !== this.state.activePage || prevState.searchOrder !== this.state.searchOrder || prevState.queryStr !== this.state.queryStr ); if (channelChanged) { await this.fetchChannel(); } else if (pageChanged) { this.applyStateToHistory(); // TODO this causes a double search request. This will be fixed with a custom hook. await this.fetchVideos(); } } setHeader() { let header = ''; if (this.props.header) { header = this.props.header; } else if (this.state.channel) { header = this.state.channel.name; } else { // Find the matching header from the search orders. for (let i = 0; i < this.state.videoOrders.length; i++) { let item = this.state.videoOrders[i]; if (item.value === this.state.searchOrder) { header = item.title; } } } this.setState({header: header}); } async fetchChannel() { // Get and display the channel specified in the Router match let channel_link = this.props.match.params.channel_link; let channel = null; if (channel_link) { channel = await getChannel(channel_link); } this.setState({ channel, title: channel ? channel.title : '', offset: 0, total: null, videos: null, video: null, }, this.fetchVideos); } enabledFilters = () => { let filters = this.state.filters; let filtersArr = []; for (let i = 0; i < filters.length; i++) { let filter = filters[i]; if (filter['value'] === true) { filtersArr = filtersArr.concat([filter['name']]); } } return filtersArr; } async fetchVideos() { this.setState({videos: null}); let offset = this.state.limit * this.state.activePage - this.state.limit; let channel_link = this.state.channel ? this.state.channel.link : null; let {queryStr, searchOrder, limit} = this.state; // Pass any enabled filters to the search. let filtersArr = this.enabledFilters(); let [videos, total] = await searchVideos(offset, limit, channel_link, queryStr, searchOrder, filtersArr); let totalPages = Math.round(total / this.state.limit) || 1; this.setState({videos, totalPages}); } changePage = async (activePage) => { this.setState({activePage}); } clearSearch = async () => { this.setState({searchStr: '', queryStr: '', searchOrder: defaultVideoOrder, activePage: 1}); } applyStateToHistory = () => { let {history, location} = this.props; let {activePage, queryStr, searchOrder} = this.state; let filters = this.enabledFilters(); changePageHistory(history, location, activePage, queryStr, searchOrder, filters); } handleSearch = async (e) => { e && e.preventDefault(); this.setState({activePage: 1, searchOrder: defaultSearchOrder, queryStr: this.state.searchStr}, this.applyStateToHistory); } handleInputChange = (event, {name, value}) => { this.setState({[name]: value}); } changeSearchOrder = (event, {value}) => { this.setState({searchOrder: value, activePage: 1}, this.applyStateToHistory); } toggleFilter = (name) => { let filters = this.state.filters; let filtersEnabled = []; for (let i = 0; i < filters.length; i++) { let filter = filters[i]; if (filter.name === name) { filter.value = !filter.value; } if (filter.value) { filtersEnabled = filtersEnabled.concat([filter.name]); } } this.setState({filters, filtersEnabled}); } applyFilters = async () => { this.applyStateToHistory(); await this.fetchVideos(); } closeFilters = () => { this.setState({filtersOpen: false}, this.applyFilters); } openFilters = () => { this.setState({filtersOpen: true}); } render() { let { activePage, channel, queryStr, searchOrder, searchStr, header, totalPages, videoOrders, videos, filtersEnabled, } = this.state; let body = ; if (videos && videos.length === 0) { // API didn't send back any videos, tell the user what to do. if (this.props.filter === 'favorites') { body =

You haven't tagged any videos as favorite.

; } else { // default empty body. body =

No videos retrieved. Have you downloaded videos yet?

; } } else if (videos) { body = ; } let pagination = null; if (totalPages) { pagination = (
); } let clearSearchButton = ( ); return (

{header} { channel && <>     }

{queryStr && clearSearchButton}
{body} {pagination}
) } } function ManageTab(props) { return ( ) } function Link(props) { let classes = 'item'; if (props.link.header) { classes = `${classes} header`; } return ( {props.link.text} ) } export function VideosRoute(props) { const links = [ {text: 'Videos', to: '/videos', exact: true}, {text: 'Favorites', to: '/videos/favorites'}, {text: 'Channels', to: '/videos/channel',}, {text: 'Manage', to: '/videos/manage'}, ]; return ( <> } /> }/> } /> ) } class Statistics extends React.Component { constructor(props) { super(props); this.state = { videos: null, historical: null, channels: null, }; this.videoNames = [ {key: 'videos', label: 'Downloaded Videos'}, {key: 'favorites', label: 'Favorite Videos'}, {key: 'sum_size', label: 'Total Size'}, {key: 'max_size', label: 'Largest Video'}, {key: 'week', label: 'Downloads Past Week'}, {key: 'month', label: 'Downloads Past Month'}, {key: 'year', label: 'Downloads Past Year'}, {key: 'sum_duration', label: 'Total Duration'}, ]; this.historicalNames = [ {key: 'average_count', label: 'Average Monthly Downloads'}, {key: 'average_size', label: 'Average Monthly Usage'}, ]; this.channelNames = [ {key: 'channels', label: 'Channels'}, ]; } async componentDidMount() { await this.fetchStatistics(); } async fetchStatistics() { let stats = await getStatistics(); stats.videos.sum_duration = secondsToString(stats.videos.sum_duration); stats.videos.sum_size = humanFileSize(stats.videos.sum_size, true); stats.videos.max_size = humanFileSize(stats.videos.max_size, true); stats.historical.average_size = humanFileSize(stats.historical.average_size, true); this.setState({...stats}); } buildSegment(title, names, stats) { return
{title}
{names.map( ({key, label}) => {stats[key]} {label} )}
} render() { if (this.state.videos) { return ( <> {this.buildSegment('Videos', this.videoNames, this.state.videos)} {this.buildSegment('Historical Video', this.historicalNames, this.state.historical)} {this.buildSegment('Channels', this.channelNames, this.state.channels)} ) } else { return } } }