import React, {useContext, useState} from 'react'; import {deleteVideos} from "../api"; import {Link, useNavigate, useParams} from "react-router-dom"; import _ from "lodash"; import {BackButton, epochToDateString, humanFileSize, humanNumber, PageContainer, useTitle} from "./Common"; import {Confirm} from "semantic-ui-react"; import Grid from "semantic-ui-react/dist/commonjs/collections/Grid"; import Container from "semantic-ui-react/dist/commonjs/elements/Container"; import {VideoPlaceholder} from "./Placeholder"; import {useChannel} from "../hooks/customHooks"; import {ThemeContext} from "../contexts/contexts"; import {Button, darkTheme, Header, Icon, Segment, Tab, TabPane} from "./Theme"; import {VideoCard} from "./Videos"; const MEDIA_PATH = '/media'; function videoFileLink(video, name, directory = false) { const path = video[name]; if (path) { const href = directory ? `${MEDIA_PATH}/${encodeURIComponent(path)}/` : `${MEDIA_PATH}/${encodeURIComponent(path)}`; return
{path}
} else { return 'Unknown' } } function VideoPage({videoFile, prevFile, nextFile, setFavorite, ...props}) { const {theme} = useContext(ThemeContext); const navigate = useNavigate(); const videoRef = React.useRef(); let [deleteOpen, setDeleteOpen] = useState(false); useTitle(videoFile === null || videoFile === undefined ? null : videoFile.video.title); let video; if (videoFile) { video = videoFile.video; } // Seeks to the `seconds` on video player. const setVideoTime = (seconds) => { videoRef.current.currentTime = seconds; } // Get the Video's channel, fallback to the URL's channel id. const {channelId} = useParams(); const {channel} = useChannel(video && video.channel_id ? video.channel_id : channelId); if (videoFile === null) { return ; } if (videoFile === undefined) { return
Could not find video
; } const handleDeleteVideo = async (video_id) => { try { await deleteVideos([video_id]) setDeleteOpen(false); } catch (e) { setDeleteOpen(false); throw e; } navigate(-1); } let videoUrl = `${MEDIA_PATH}/${encodeURIComponent(video.video_path)}`; let downloadUrl = `/download/${encodeURIComponent(video.video_path)}`; let posterUrl = video.poster_path ? `${MEDIA_PATH}/${encodeURIComponent(video.poster_path)}` : null; let captionsUrl = video.caption_path ? `${MEDIA_PATH}/${encodeURIComponent(video.caption_path)}` : null; let vttCaptions = video.caption_path && video.caption_path.endsWith('.vtt'); let description = 'No description available.'; let viewCount = video.view_count; if (video.info_json && video.info_json['description']) { // Only replace empty description if there is one available. description = video.info_json['description']; description = chaptersInDescription(description, setVideoTime) viewCount = viewCount || video.info_json['view_count']; } const favorite = video && video.favorite; let handleFavorite = async (e) => { e.preventDefault(); if (video) { setFavorite(!favorite); } } const favorite_button = (); let descriptionPane = { menuItem: 'Description', render: () =>
                {description}
            
}; let aboutPane = { menuItem: 'About', render: () =>

Size

{video.size ? humanFileSize(video.size) : '???'}

Source URL

{video.url ? {video.url} : 'N/A'}

View Count

{viewCount ? humanNumber(viewCount) : 'N/A'}

Censored

{video.censored ? 'Yes' : 'No'}

} let captionsPane = { menuItem: 'Captions', render: () =>
{video.caption}
}; let filesPane = { menuItem: 'Files', render: () =>

Video File

{videoFileLink(video, 'video_path')}

Info JSON File

{videoFileLink(video, 'info_json_path')}

Caption File

{videoFileLink(video, 'caption_path')}

Poster File

{videoFileLink(video, 'poster_path')}

Directory

{videoFileLink(video, 'directory', true)}
} let tabPanes = [descriptionPane, aboutPane, filesPane, captionsPane]; const tabMenu = theme === darkTheme ? {inverted: true, attached: true} : {attached: true}; return (<>
{video.title || video.video_path}
{video.upload_date &&

{epochToDateString(video.upload_date)}

}

{channel && {channel.name} }

{favorite_button} setDeleteOpen(false)} onConfirm={() => handleDeleteVideo(video.id)} />

{prevFile && <>
Older
}
{nextFile && <>
Newer
}
) } export default VideoPage; const chapterRegex = new RegExp('^(\\(?(?:((\\d?\\d):)?(?:(\\d?\\d):(\\d\\d)))\\)?)\\s+(.*)$', 'i'); function chaptersInDescription(description, setVideoTime) { if (description && description.length > 0) { try { const lines = _.split(description, '\n'); let newLines = []; for (let i = 0; i <= lines.length; i++) { const line = lines[i]; const match = line ? line.match(chapterRegex) : null; let link; if (match) { // This line in the description starts with a chapter timestamp, parse it and create a link. try { const timestamp = match[1]; const title = match[6]; // Assume 0 if no part of timestamp could be found. const [hour, minute, second] = match.slice(3, 6).map(i => parseInt(i) || 0); // Convert timestamp to seconds. const seconds = (((hour * 60) + minute) * 60) + second; link = <> setVideoTime(seconds)}>{timestamp}  {title} ; } catch (e) { // Report an error while creating timestamp link. console.error(e); } // Use the link if we could create it, otherwise fallback to the original `line`. newLines = [...newLines, link || line]; } else if (line !== undefined) { // Line does not start with a timestamp. newLines = [...newLines, line]; } } // Map all lines and links with a break between. return <> {newLines.map((i, idx) =>
{i}
)} ; } catch (e) { // Some error happened while parsing description. Report it but don't fail. console.error(e); } } // Error occurred, or no description, don't fail. return description; }