import React, {useState} from "react"; import { AccordionContent, AccordionTitle, Grid, StatisticLabel, StatisticValue, TableCell, TableRow, } from "semantic-ui-react"; import {createChannel, deleteChannel, downloadChannel, refreshChannel, updateChannel, validateRegex} from "../api"; import { APIButton, BackButton, DirectoryInput, ErrorMessage, frequencyOptions, humanFileSize, RequiredAsterisk, SearchInput, secondsToFrequency, secondsToFullDuration, Toggle, useTitle, WROLModeMessage } from "./Common"; import {Link, useNavigate, useParams} from "react-router-dom"; import Message from "semantic-ui-react/dist/commonjs/collections/Message"; import Icon from "semantic-ui-react/dist/commonjs/elements/Icon"; import {useChannel, useChannels, useOneQuery} from "../hooks/customHooks"; import _ from "lodash"; import {Accordion, Button, Form, FormField, FormGroup, FormInput, Header, Loader, Segment, Statistic} from "./Theme"; import Dropdown from "semantic-ui-react/dist/commonjs/modules/Dropdown"; import {Media, ThemeContext} from "../contexts/contexts"; import {SortableTable} from "./SortableTable"; import {toast} from "react-semantic-toasts-2"; function ChannelStatistics({statistics}) { if (!statistics) { return <> } return
Statistics
{statistics.video_count} Videos {humanFileSize(statistics.size, true)} Total Size {humanFileSize(statistics.largest_video, true)} Largest Video {secondsToFullDuration(statistics.length)} Total Duration
} function ChannelPage({create, header}) { const [disabled, setDisabled] = useState(false); const [validRegex, setValidRegex] = useState(true); const [activeIndex, setActiveIndex] = useState(-1); const [errors, setErrors] = useState({}); const [error, setError] = useState(false); const [success, setSuccess] = useState(false); const [messageHeader, setMessageHeader] = useState(); const [messageContent, setMessageContent] = useState(); const navigate = useNavigate(); const {channelId} = useParams(); const {channel, changeValue, fetchChannel} = useChannel(channelId); let title; if (channel && channel.name) { title = channel.name; } useTitle(title); if (!channel) { return ; } const checkRegex = async (e, {value}) => { changeValue('match_regex', value); const valid = await validateRegex(value); setValidRegex(valid); } const handleCheckbox = (e, {name, checked}) => { if (e) { e.preventDefault(); } changeValue(name, checked); } const handleInputChange = (e, {name, value}) => { if (e) { e.preventDefault(); } changeValue(name, value); } const setErrorMessage = (header, message) => { setError(true); setSuccess(false); setMessageHeader(header); setMessageContent(message); } const setSuccessMessage = (header, message) => { setError(false); setSuccess(true); setMessageHeader(header); setMessageContent(message); } const handleSubmit = async (e) => { if (e) { e.preventDefault(); } const body = { name: channel.name, directory: channel.directory, mkdir: channel.mkdir, url: channel.url, download_frequency: channel.download_frequency, match_regex: channel.match_regex, }; setDisabled(true); let response = null; try { if (create !== undefined) { response = await createChannel(body); } else { response = await updateChannel(channelId, body); } } catch (e) { console.error(e); toast({ type: 'error', title: 'Unexpected server response', description: 'Could not save channel', time: 5000, }); return; } finally { setDisabled(false); } if (response && 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) { setSuccessMessage( 'Channel created', Your channel was created. View it here ); } else { await fetchChannel(); toast({ type: 'success', title: 'Channel updated', description: 'The channel was updated', }) } } else if (response) { // Some error occurred. let error = await response.json(); let cause = error.cause; if (error.code === 'UNKNOWN_DIRECTORY' || (cause && cause.code === 'UNKNOWN_DIRECTORY')) { setErrorMessage( 'Invalid directory', 'This directory does not exist.', 'directory', ); } else if (cause && cause.code === 'CHANNEL_DIRECTORY_CONFLICT') { setErrorMessage( 'Invalid directory', 'This directory is already used by another channel', 'directory', ); } else if (cause && cause.code === 'CHANNEL_NAME_CONFLICT') { setErrorMessage( 'Invalid name', 'This channel name is already taken', 'name', ); } else { setErrorMessage('Invalid channel', 'Unable to save channel. See logs.'); } } else { console.error('Did not get a response for channel!'); } } const handleAdvancedClick = (e, {index}) => { setActiveIndex(activeIndex === index ? -1 : index); } const handleDownloadChannel = async (e) => { if (e) { e.preventDefault(); } try { const response = await downloadChannel(channelId); if (response.status === 204) { toast({ type: 'success', title: 'Download Created', description: 'Channel download has been started.', time: 5000, }); } } catch (e) { console.error(e); } } const handleRefreshChannel = async (e) => { if (e) { e.preventDefault(); } const response = await refreshChannel(channelId); if (response.status !== 204) { toast({ type: 'error', title: 'Failed to refresh', description: "Failed to refresh this channel's directory", time: 5000, }) } } const handleDelete = async () => { try { let response = await deleteChannel(channelId); if (response.status === 204) { navigate('/videos/channel'); } } catch (e) { setErrorMessage('Failed to delete', 'Failed to delete this channel, check logs.'); } } return <> {!create && }
{header}
changeValue('name', value)} /> changeValue('directory', value)} placeholder='videos/channel directory' /> { create !== undefined && {/* Empty field*/} { handleCheckbox(null, {name: 'mkdir', checked: i}) }} /> } Advanced Settings
The following settings are encouraged by default, modify them at your own risk.
{!create && <> Delete Download Refresh } Save
{channel.statistics && }
} export function ChannelEditPage(props) { return ; } export function ChannelNewPage(props) { useTitle('New Channel'); return } function ChannelRow({channel}) { const videosTo = `/videos/channel/${channel.id}/video`; const {inverted} = React.useContext(ThemeContext); const editTo = `/videos/channel/${channel.id}/edit`; const buttonClass = `ui button secondary ${inverted}`; return {channel.name} {channel.video_count} {channel.url && channel.download_frequency ? secondsToFrequency(channel.download_frequency) : null} {channel.size ? humanFileSize(channel.size) : null} Edit ; } function MobileChannelRow({channel}) { const editTo = `/videos/channel/${channel.id}/edit`; const videosTo = `/videos/channel/${channel.id}/video`; return

{channel.name}

Videos: {channel.video_count}

Edit

; } export function ChannelsPage() { useTitle('Channels'); const {channels} = useChannels(); const [searchStr, setSearchStr] = useOneQuery('name'); // Hides Channels with few videos. const [hideSmall, setHideSmall] = React.useState(true); const enoughChannelsToHideSmall = channels && channels.length > 10; const header =
setSearchStr('')} onChange={setSearchStr} onSubmit={null} />
; const headers = [ {key: 'name', text: 'Name', sortBy: 'name', width: 8}, {key: 'video_count', text: 'Videos', sortBy: 'video_count', width: 2}, {key: 'download_frequency', text: 'Download Frequency', sortBy: 'download_frequency', width: 2}, {key: 'size', text: 'Size', sortBy: 'size', width: 2}, {key: 'manage', text: 'Manage', width: 2}, ]; const mobileHeaders = [ {key: 'name', text: 'Name', sortBy: 'name'}, {key: 'video_count', text: 'Videos', sortBy: 'video_count'}, {key: 'manage', text: 'Manage'}, ]; if (channels && channels.length === 0) { return <> {header} No channels exist yet! Create one. } else if (channels === undefined) { return <> {header} Could not fetch Channels } let filteredChannels = channels; if (searchStr && Array.isArray(channels)) { const re = new RegExp(_.escapeRegExp(searchStr), 'i'); filteredChannels = channels.filter(i => re.test(i['name'])); } else if (channels && hideSmall && enoughChannelsToHideSmall) { filteredChannels = channels.filter(i => i['video_count'] > 5 // Channels need to have at least 5 videos. || i['name'].toLowerCase() === 'wrolpi' // Always show the WROLPi channel so users can find tutorials. ); } return <> {header} } /> } /> }