Skip to content

🎥 Make videos programmatically with React

2.3 KB
Raw Blame
import React, {useCallback, useContext, useMemo} from 'react';
import {CURRENT_COLOR} from '../../helpers/colors';
import {ActionTooltip} from '../ActionTooltip';
import type {RenderInlineAction} from '../InlineAction';
import {InlineAction} from '../InlineAction';
import {showNotification} from '../Notifications/NotificationCenter';
import {cancelRenderJob} from './actions';
import type {CaptionJob} from './caption-job-types';
import type {AnyRenderJob} from './context';
import {isClientRenderJob, RenderQueueContext} from './context';
import type {VideoMattingJob} from './video-matting-job-types';

export const RenderQueueCancelButton: React.FC<{
	readonly job: AnyRenderJob | CaptionJob | VideoMattingJob;
}> = ({job}) => {
	const {cancelClientJob} = useContext(RenderQueueContext);

	const onClick: React.MouseEventHandler = useCallback(
		(e) => {
			e.stopPropagation();

			if (
				job.type === 'caption' ||
				job.type === 'video-matting' ||
				isClientRenderJob(job)
			) {
				cancelClientJob(job.id);
				return;
			}

			cancelRenderJob(job).catch((err) => {
				showNotification(`Could not cancel job: ${err.message}`, 2000);
			});
		},
		[job, cancelClientJob],
	);

	const icon: React.CSSProperties = useMemo(() => {
		return {
			height: 14,
			color: CURRENT_COLOR,
		};
	}, []);

	const renderAction: RenderInlineAction = useCallback(
		(color) => {
			return (
				<svg
					style={icon}
					xmlns="http://www.w3.org/2000/svg"
					viewBox="0 0 512 512"
				>
					<path
						fill={color}
						d="M367.2 412.5L99.5 144.8C77.1 176.1 64 214.5 64 256c0 106 86 192 192 192c41.5 0 79.9-13.1 111.2-35.5zm45.3-45.3C434.9 335.9 448 297.5 448 256c0-106-86-192-192-192c-41.5 0-79.9 13.1-111.2 35.5L412.5 367.2zM512 256c0 141.4-114.6 256-256 256S0 397.4 0 256S114.6 0 256 0S512 114.6 512 256z"
					/>
				</svg>
			);
		},
		[icon],
	);

	return (
		<ActionTooltip label="Cancel" shortcut={null} delay={800} dismissOnClick>
			<InlineAction
				aria-label={
					job.type === 'caption'
						? 'Cancel transcription'
						: job.type === 'video-matting'
							? 'outName' in job
								? 'Cancel background removal'
								: 'Cancel video matting'
							: 'Cancel render'
				}
				renderAction={renderAction}
				onClick={onClick}
				variant={null}
			/>
		</ActionTooltip>
	);
};