import React from "react"; import { Accordion, Button, Checkbox, Form, Grid, Header, Input, Loader, Responsive, Segment, Statistic } from "semantic-ui-react"; import { createChannel, deleteChannel, downloadChannel, getChannel, getChannels, getConfig, getDirectories, refreshChannel, updateChannel, validateRegex } from "../api"; import _ from "lodash"; import Container from "semantic-ui-react/dist/commonjs/elements/Container"; import {APIForm, frequencyOptions, humanFileSize, RequiredAsterisk, secondsToDate, secondsToFrequency} from "./Common"; import {Link} from "react-router-dom"; import Message from "semantic-ui-react/dist/commonjs/collections/Message"; import Confirm from "semantic-ui-react/dist/commonjs/addons/Confirm"; import Table from "semantic-ui-react/dist/commonjs/collections/Table"; import {ChannelPlaceholder} from "./Placeholder"; import Dropdown from "semantic-ui-react/dist/commonjs/modules/Dropdown"; import Icon from "semantic-ui-react/dist/commonjs/elements/Icon"; class DirectoryInput extends React.Component { constructor(props) { super(props); this.state = { directories: [], mediaDirectory: null, directory: '', } this.handleChange = this.handleChange.bind(this); } async componentDidMount() { let global_config = await getConfig(); let directories = await getDirectories(this.state.directory); this.setState({ directories, mediaDirectory: global_config.media_directory, directory: this.props.value, }); } async componentDidUpdate(prevProps, prevState, snapshot) { if (prevState.directory !== this.state.directory) { let directories = await getDirectories(this.state.directory); this.setState({directories}); } } async handleChange(e, {name, value}) { e.preventDefault(); this.setState({[name]: value}, () => this.props.setInput(e, {name, value})); } render() { let {directories, mediaDirectory, directory} = this.state; return (
{directories.map((i) => )}
); } } function ChannelStatistics(props) { if (!props.statistics) { return <> } let stats = props.statistics; return ( <>
Statistics
{stats.video_count} Videos {humanFileSize(stats.size, true)} Total Size {humanFileSize(stats.largest_video, true)} Largest Video ) } class ChannelPage extends APIForm { constructor(props) { super(props); this.state = { ...this.state, deleteOpen: false, validRegex: true, original: {}, activeIndex: -1, inputs: { name: '', directory: '', mkdir: false, url: '', match_regex: '', generate_posters: true, calculate_duration: true, download_frequency: null, }, statistics: null, errors: {}, }; this.mkdir = React.createRef(); } async componentDidMount() { if (!this.props.create) { let channel_link = this.props.match.params.channel_link; let channel = await getChannel(channel_link); let statistics = channel.statistics; if (statistics) { this.setState({statistics: statistics || null}); } this.initFormValues(channel); } } checkRegex = async (event, {name, value}) => { event.persist(); await this.handleInputChange(event, {name, value}); let valid = await validateRegex(value); this.setState({validRegex: valid}); } handleConfirm = async () => { this.setState({deleteOpen: false}); let response = await deleteChannel(this.props.match.params.channel_link); if (response.status === 204) { this.props.history.push({ pathname: '/videos/channel' }); } else { this.setError('Failed to delete', 'Failed to delete this channel, check logs.'); } } handleSubmit = async (e) => { e.preventDefault(); let {inputs} = this.state; let response = null; try { this.setLoading(); if (this.props.create) { response = await createChannel(inputs); } else { response = await updateChannel(this.props.match.params.channel_link, inputs); } } finally { this.clearLoading(); } if (response === null) { throw Error('Response was null'); } if (response.status >= 200 && response.status < 300) { let location = response.headers.get('Location'); let channelResponse = await fetch(location); let data = await channelResponse.json(); let channel = data['channel']; if (response.status === 201) { this.setSuccess( 'Channel created', Your channel was created. View it here ); } else { this.initFormValues(channel); this.setSuccess('Channel updated', 'Your channel was updated'); this.checkDirty(); } } else { // Some error occurred. let error = await response.json(); let cause = error.cause; if (error.code === 3 || (cause && cause.code === 3)) { this.setError( 'Invalid directory', 'This directory does not exist.', 'directory', ); } else if (cause && cause.code === 7) { this.setError( 'Invalid directory', 'This directory is already used by another channel', 'directory', ); } else if (cause && cause.code === 5) { this.setError( 'Invalid name', 'This channel name is already taken', 'name', ); } else { this.setError('Invalid channel', 'Unable to save channel. See logs.'); } } } handleAdvancedClick = (e, titleProps) => { const {index} = titleProps const {activeIndex} = this.state const newIndex = activeIndex === index ? -1 : index this.setState({activeIndex: newIndex}) } downloadChannel = async (e) => { e.preventDefault(); return await downloadChannel(this.props.match.params.channel_link); } refreshChannel = async (e) => { e.preventDefault(); return await refreshChannel(this.props.match.params.channel_link); } handleDeleteButton = (e) => { e.preventDefault(); this.setState({deleteOpen: true}); } render() { return (
{this.props.header}
{ this.props.create && this.handleCheckbox(this.mkdir)} /> } Advanced Settings
The following settings are encouraged by default, modify them at your own risk.
{!this.props.create && <> this.setState({deleteOpen: false})} onConfirm={this.handleConfirm} /> }
{}
) } } export function EditChannel(props) { return ( ) } export function NewChannel(props) { return ( ) } class ChannelRow extends React.Component { constructor(props) { super(props); this.editTo = `/videos/channel/${props.channel.link}/edit`; this.videosTo = `/videos/channel/${props.channel.link}/video`; } render() { let channel = this.props.channel; return ( {channel.name} {channel.video_count} {channel.url && channel.info_date ? secondsToDate(channel.info_date) : null} {channel.url && channel.next_download ? secondsToDate(channel.next_download) : null} {channel.url && channel.download_frequency ? secondsToFrequency(channel.download_frequency) : null} Edit ) } } class MobileChannelRow extends ChannelRow { render() { let channel = this.props.channel; return

{channel.name}

Last Update: {channel.url && channel.info_date ? secondsToDate(channel.info_date) : null}

Next Update: {channel.url && channel.next_download ? secondsToDate(channel.next_download) : null}

Videos: {channel.video_count}

Frequency: {channel.url && channel.download_frequency ? secondsToFrequency(channel.download_frequency) : null}

Edit

; } } function NewChannelButton() { return ( ); } export class Channels extends React.Component { initialState = { channels: null, value: '', results: [], }; constructor(props) { super(props); this.state = this.initialState; } async componentDidMount() { let channels = await getChannels(); this.setState({channels, results: channels}); } handleSearchChange = async (e, {value}) => { this.setState({value}); setTimeout(() => { if (this.state.value.length < 1) { return this.setState({value, results: this.state.channels}); } const re = new RegExp(_.escapeRegExp(this.state.value), 'i'); const isMatch = (result) => re.test(result.name); this.setState({ results: _.filter(this.state.channels, isMatch), }); }, 300); }; render() { let header = (
Channels
); let tableHeader = ( Name Videos Last Update Next Update Frequency Manage ); let body; if (this.state.channels === null) { // Placeholders while fetching body = {tableHeader}
} else if (this.state.channels.length === 0) { body = No channels exist yet! Create one. } else { body = ( <> {tableHeader} {this.state.results.map((channel) => )}
{this.state.results.map((channel) => )}
) } return ( <> {header} {body} ) } }