import React from 'react'; import {Link, Route} from "react-router-dom"; import Paginator, { changePageHistory, DEFAULT_LIMIT, defaultSearchOrder, defaultVideoOrder, humanFileSize, Progresses, scrollToTop, searchOrders, secondsToString, VideoCards, videoOrders } from "./Common" import VideoPage from "./VideoPlayer"; import {download, getChannel, getStatistics, getVideo, refresh, searchVideos} from "../api"; import {Button, Dropdown, Form, Grid, Header, Icon, Input, Loader, Segment, Statistic, Tab} 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 (
Manage Videos


{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 } } } class VideosPreview extends React.Component { constructor(props) { super(props); this.state = { videos: null, }; } async componentDidMount() { await this.fetchVideos(); } render() { let header =

{this.title}

; let body = if (this.state.videos && this.state.videos.length === 0) { body =

No videos available.

} else if (this.state.videos && this.state.videos.length > 0) { body = } return <> {header} {body} } } export class FavoriteVideosPreview extends VideosPreview { constructor(props) { super(props); this.title = 'Favorite Videos' } async fetchVideos() { let [videos] = await searchVideos( 0, 3, null, null, true, '-favorite'); this.setState({videos}); } } export class ViewedVideosPreview extends VideosPreview { constructor(props) { super(props); this.title = 'Recently Viewed Videos' } async fetchVideos() { let [videos] = await searchVideos( 0, 3, null, null, null, '-viewed'); this.setState({videos}); } } 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; this.state = { channel: null, videos: null, queryStr: searchStr, searchStr: '', limit: DEFAULT_LIMIT, activePage: activePage, totalPages: null, prev: null, next: null, videoOrders: searchStr === '' ? videoOrders : searchOrders, searchOrder: searchOrder, title: '', }; } async componentDidMount() { if (this.props.match.params.channel_link) { await this.fetchChannel(); } else { await this.fetchVideos(); } this.setTitle(); } 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(); await this.fetchVideos(); } } setTitle() { let title = ''; if (this.props.filter === 'favorites') { title = 'Favorite Videos'; } else if (this.state.channel) { title = this.state.channel.name; } else { // Find the matching title 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) { title = item.title; } } } this.setState({title: title}); } 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); } async fetchVideos() { let offset = this.state.limit * this.state.activePage - this.state.limit; let channel_link = this.state.channel ? this.state.channel.link : null; let favorites = this.props.filter !== undefined ? this.props.filter === 'favorites' : null; let [videos, total] = await searchVideos( offset, this.state.limit, channel_link, this.state.queryStr, favorites, this.state.searchOrder); 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; changePageHistory(history, location, activePage, queryStr, searchOrder); } 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); } render() { let { activePage, channel, queryStr, searchOrder, searchStr, title, totalPages, videoOrders, videos, } = 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 (

{title} { channel && <>     }

{queryStr && clearSearchButton}
{body} {pagination}
) } } class ManageTabs extends React.Component { render() { const panes = [ {menuItem: 'Manage', render: () => }, {menuItem: 'Statistics', render: () => }, ] return ( ) } } export class VideosRoute extends React.Component { render() { 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 } } }