Captions for uploaded videos (migration 066)

Video blocks get a Captions field and the hero a Video captions field,
each a WebVTT upload drawn as <track kind="captions" default> on a
video file. The media library takes .vtt (sniffed by its WEBVTT
header, kept as is, always in /uploads/captions/) with its own tab.
Newsletter parts leave the field out. nginx needs text/vtt in
mime.types: docs/media-uploads.md, step 5.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-10-04 21:55:05 -05:00
parent aaab1a945c
commit 33be618331
18 changed files with 123 additions and 43 deletions

View file

@ -37,7 +37,7 @@
cached list would lie.
═══════════════════════════════════════════════════════════════ */
import { FileAudio, X } from "@phosphor-icons/react";
import { ClosedCaptioning, FileAudio, X } from "@phosphor-icons/react";
import { useCallback, useEffect, useId, useRef, useState, type ReactNode } from "react";
import { useLocation } from "react-router-dom";
@ -59,7 +59,7 @@ export type MediaItem = {
processed: boolean;
uploaded_by: string | null;
created_at: string;
/** logos, images or thumbnails; null for clips and older files in the root. */
/** logos, images, thumbnails or captions; null for clips and older files in the root. */
folder: string | null;
};
@ -69,6 +69,7 @@ export const IMAGE_FOLDERS = [
["logos", "Logos"],
["images", "Images"],
["thumbnails", "Previews"],
["captions", "Captions"],
] as const;
export type ImageFolder = (typeof IMAGE_FOLDERS)[number][0];
@ -198,6 +199,8 @@ export function UploadButton({
const [keepOriginal, setKeepOriginal] = useState(false);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
// Captions are kept as they are, so there's no original to keep.
const captions = folder === "captions";
async function send(files: FileList | null) {
if (!files || files.length === 0) return;
@ -235,21 +238,23 @@ export function UploadButton({
<input
ref={inputRef}
type="file"
accept="image/jpeg,image/png,image/gif,image/webp,image/svg+xml"
accept={captions ? ".vtt,text/vtt" : "image/jpeg,image/png,image/gif,image/webp,image/svg+xml"}
multiple={multiple}
className="hidden"
onChange={(e) => send(e.target.files)}
/>
<label htmlFor={keepId} className="flex items-center gap-1.5 text-xs text-muted">
<input
id={keepId}
type="checkbox"
checked={keepOriginal}
onChange={(e) => setKeepOriginal(e.target.checked)}
className="h-3.5 w-3.5 border-muted/40 text-link"
/>
Keep original
</label>
{!captions && (
<label htmlFor={keepId} className="flex items-center gap-1.5 text-xs text-muted">
<input
id={keepId}
type="checkbox"
checked={keepOriginal}
onChange={(e) => setKeepOriginal(e.target.checked)}
className="h-3.5 w-3.5 border-muted/40 text-link"
/>
Keep original
</label>
)}
</div>
{error && <p className="mt-1 text-xs text-danger">{error}</p>}
</div>
@ -293,6 +298,10 @@ export function MediaGrid({
<div className="flex h-full w-full items-center justify-center bg-tint" aria-hidden="true">
<FileAudio weight="bold" className="h-10 w-10 text-link" />
</div>
) : item.content_type === "text/vtt" ? (
<div className="flex h-full w-full items-center justify-center bg-tint" aria-hidden="true">
<ClosedCaptioning weight="bold" className="h-10 w-10 text-link" />
</div>
) : (
<Thumb url={item.url} />
)}
@ -429,7 +438,7 @@ export function MediaPicker({
const [query, setQuery] = useState("");
const [tab, setTab] = useState<string>(folder);
const fromRequests = tab === REQUESTS_TAB;
const { items, error, setItems } = useMediaLibrary(query, "image", fromRequests ? undefined : tab);
const { items, error, setItems } = useMediaLibrary(query, tab === "captions" ? undefined : "image", fromRequests ? undefined : tab);
const dialogRef = useRef<HTMLDialogElement>(null);
// showModal gives focus trapping, Escape and the backdrop for free.
@ -556,11 +565,13 @@ export function ImageInput({
className="flex h-20 w-20 shrink-0 items-center justify-center overflow-hidden border border-muted/20"
style={CHECKER}
>
{src && !broken ? (
{folder === "captions" && src ? (
<ClosedCaptioning weight="bold" className="h-8 w-8 text-link" aria-label="Captions file" />
) : src && !broken ? (
<img src={src} alt="" className="h-full w-full object-contain" onError={() => setBroken(true)} />
) : (
<span className="px-1 text-center text-[10px] text-muted/70">
{src ? "Not found" : "No image"}
{src ? "Not found" : folder === "captions" ? "No file" : "No image"}
</span>
)}
</div>