diff --git a/app/src/api.js b/app/src/api.js index fc80cfe1..6eae64d9 100644 --- a/app/src/api.js +++ b/app/src/api.js @@ -264,6 +264,9 @@ export async function transcodeVideo(fileGroupId, {video_codec = null, audio_cod export async function getJobs() { const response = await apiGet(`${API_URI}/jobs`); + if (!response.ok) { + throw new Error(await getErrorMessage(response, 'Unable to get the jobs')); + } const data = await response.json(); return data['jobs']; } diff --git a/app/src/components/ThemeSamplePage.js b/app/src/components/ThemeSamplePage.js index 824f9e7e..4b2f9982 100644 --- a/app/src/components/ThemeSamplePage.js +++ b/app/src/components/ThemeSamplePage.js @@ -385,10 +385,7 @@ export function ThemeSamplePage() {
A dropdown of actions
- {/* The video page's Edit button: one labelled button opens a menu of the - rarer actions, so the row stays short. The trailing chevron is what - says "menu" rather than "does the thing"; a disabled item stays listed - so the user learns it exists. */} + {/* Mirrors the video page's Edit control. */} diff --git a/app/src/components/VideoEditMenu.js b/app/src/components/VideoEditMenu.js index b84ed5fa..23812cfd 100644 --- a/app/src/components/VideoEditMenu.js +++ b/app/src/components/VideoEditMenu.js @@ -1,7 +1,9 @@ import React, {useEffect, useRef, useState} from 'react'; import {cancelJob, fetchVideoDownloadDefaults, getJob, transcodeVideo, updateVideo} from '../api'; import {useWROLMode} from '../hooks/customHooks'; -import {Button, Confirm, Group, Icon, Menu, Modal, Progress, Select, Stack, Status, Text, Textarea, TextInput, toast} from './ui'; +import { + Button, Confirm, Group, Icon, Menu, Modal, Progress, Select, Stack, Status, Text, Textarea, TextInput, toast, +} from './ui'; import { TRANSCODE_COPY, transcodeAudioCodecOptions, @@ -9,12 +11,13 @@ import { transcodeVideoCodecOptions, } from './Vars'; -// How often the modal asks the API about the running Job. const JOB_POLL_MS = 2000; -const FINISHED_STATUSES = ['complete', 'failed', 'cancelled']; -// Embedded thumbnails are "video" streams to ffprobe, but not codecs a user chooses. +export const FINISHED_STATUSES = ['complete', 'failed', 'cancelled']; +// ffprobe calls an embedded thumbnail a video stream. const THUMBNAIL_CODECS = ['mjpeg', 'png', 'bmp', 'gif']; +export const isJobFinished = (job) => !!job && FINISHED_STATUSES.includes(job.status); + /** The codec names of a Video's real streams, from the `codec_names`/`codec_types` the API sends. */ export function currentCodecs(video) { const names = video?.codec_names || []; @@ -38,6 +41,12 @@ export function preferredTarget(preferences, options) { return TRANSCODE_COPY; } +/** The container of a video file, when it is one ffmpeg can write for us. */ +export function currentContainer(video) { + const suffix = String(video?.video_path || '').split('.').pop().toLowerCase(); + return transcodeContainerOptions.some(i => i.value === suffix) ? suffix : null; +} + const statusKind = (status) => { if (status === 'complete') return 'complete'; if (status === 'running') return 'active'; @@ -45,51 +54,68 @@ const statusKind = (status) => { return 'pending'; } -/** Progress, status and log tail of one Job, polled until it finishes. */ -export function JobProgress({jobId, onFinished}) { +/** + * Poll a Job until it finishes. Lives in whichever component must outlive the UI showing the Job: + * the page must learn the Job finished even when the user closed the modal that started it. + * + * A failed request is retried on the next tick; only unmount, a new `jobId`, or a finished status + * stops the polling. `onFinished` is called once per Job. + */ +export function useJob(jobId, onFinished) { const [job, setJob] = useState(null); - const [cancelling, setCancelling] = useState(false); - const timer = useRef(null); - const finishedRef = useRef(false); + const onFinishedRef = useRef(onFinished); + onFinishedRef.current = onFinished; useEffect(() => { - finishedRef.current = false; - let cancelled = false; + setJob(null); + if (!jobId) { + return; + } + let stopped = false; + let timer = null; const poll = async () => { - let latest; + let latest = null; try { latest = await getJob(jobId); } catch (e) { console.error(e); - return; } - if (cancelled) return; - setJob(latest); - if (FINISHED_STATUSES.includes(latest.status)) { - if (!finishedRef.current) { - finishedRef.current = true; - if (onFinished) onFinished(latest); + if (stopped) return; + if (latest) { + setJob(latest); + if (isJobFinished(latest)) { + if (onFinishedRef.current) onFinishedRef.current(latest); + return; } - return; } - timer.current = window.setTimeout(poll, JOB_POLL_MS); + timer = window.setTimeout(poll, JOB_POLL_MS); }; poll(); return () => { - cancelled = true; - clearTimeout(timer.current); + stopped = true; + clearTimeout(timer); }; - // Only a new Job restarts polling; `onFinished` may be a fresh closure on every render. - // eslint-disable-next-line react-hooks/exhaustive-deps }, [jobId]); + const cancel = async () => { + const latest = await cancelJob(jobId); + setJob(latest); + return latest; + }; + + return {job, cancel}; +} + +/** Progress, status and log tail of one Job. */ +export function JobProgress({job, onCancel}) { + const [cancelling, setCancelling] = useState(false); + const handleCancel = async () => { setCancelling(true); try { - const latest = await cancelJob(jobId); - setJob(latest); + await onCancel(); } catch (e) { console.error(e); } finally { @@ -101,14 +127,14 @@ export function JobProgress({jobId, onFinished}) { return ; } - const finished = FINISHED_STATUSES.includes(job.status); + const finished = isJobFinished(job); const percent = job.progress ?? 0; const logTail = (job.log || []).slice(-8); return {job.status} - {!finished &&