Video stills: Frame at on video blocks and the front page's video, and Tools > Video thumbnails (migration 074)

Frame at (a still from a YouTube or Facebook video at a typed time)
now also fills a video block's still and the front page's new Video
still, which the hero shows before its featured video plays. Tools >
Video thumbnails takes one from any link, to download or copy into a
field. All go through the existing POST /api/admin/video-frame.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aidan Dona 2026-10-08 12:10:44 -05:00
parent 31d9f78120
commit 14a22ca44d
12 changed files with 240 additions and 13 deletions

View file

@ -0,0 +1,171 @@
/* ═══════════════════════════════════════════════════════════════
ADMIN: VIDEO THUMBNAILS (Tools → Video thumbnails, editors and up)
Paste a YouTube or Facebook link and a time, and the server takes
that one frame (grabFrame in server/src/clips.js, the same as an
image field's Frame at) and saves it to the media library's
Previews folder (thumbnails/). A link's own t= fills in the time.
Frames taken here are listed until the page is left, each with a
full-size link, a download and its path to copy into a field.
They stay in the library (Tools → Media) after that.
═══════════════════════════════════════════════════════════════ */
import { useId, useState, type FormEvent } from "react";
import { Link } from "react-router-dom";
import { ApiError, post } from "../../lib/api.ts";
import { clock, parseClock } from "../../lib/clock.ts";
import { startOf, type MediaItem } from "../../components/admin/MediaLibrary.tsx";
const MUTED = "var(--color-muted)";
const button =
"btn border border-brand px-4 py-2 text-sm text-link transition-colors hover:bg-tint disabled:cursor-default disabled:opacity-50";
const primary =
"btn bg-action px-5 py-2.5 text-white transition-colors hover:bg-action-hover disabled:bg-muted/25";
const input =
"border border-muted/25 bg-surface px-3 py-2 text-sm text-body outline-none focus:border-brand focus:ring-2 focus:ring-brand/25";
type Taken = { media: MediaItem; url: string; seconds: number };
function Frame({ frame }: { frame: Taken }) {
const [copied, setCopied] = useState(false);
async function copy() {
try {
await navigator.clipboard.writeText(frame.media.url);
setCopied(true);
setTimeout(() => setCopied(false), 1500);
} catch {
setCopied(false);
}
}
return (
<li className="bg-raised shadow-md">
<a href={frame.media.url} target="_blank" rel="noopener noreferrer">
<img src={frame.media.url} alt="" className="aspect-video w-full bg-tint object-cover" />
</a>
<div className="space-y-3 p-4">
<div>
<p className="font-medium text-body">At {clock(frame.seconds)}</p>
<p className="truncate text-xs" style={{ color: MUTED }} title={frame.url}>
{frame.url}
</p>
{frame.media.width && frame.media.height && (
<p className="text-xs" style={{ color: MUTED }}>
{frame.media.width} × {frame.media.height}
</p>
)}
</div>
<div className="flex flex-wrap gap-2">
<a href={frame.media.url} download className={button}>
Download
</a>
<button type="button" className={button} onClick={copy}>
{copied ? "Copied" : "Copy path"}
</button>
</div>
</div>
</li>
);
}
export default function AdminVideoFrames() {
const [url, setUrl] = useState("");
const [time, setTime] = useState("0:00");
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const [frames, setFrames] = useState<Taken[]>([]);
const urlId = useId();
const timeId = useId();
const seconds = parseClock(time);
// A pasted link that carries its own time (t=83) sets the box.
function changeUrl(next: string) {
setUrl(next);
const start = startOf(next.trim());
if (start > 0) setTime(clock(start));
}
async function take(e: FormEvent) {
e.preventDefault();
if (seconds == null) return;
setBusy(true);
setError(null);
try {
const link = url.trim();
const { media } = await post("/admin/video-frame", { url: link, seconds });
setFrames((list) => [{ media, url: link, seconds }, ...list]);
} catch (err) {
setError(err instanceof ApiError ? err.message : "Couldn't take that frame.");
} finally {
setBusy(false);
}
}
return (
<div>
<div className="flex flex-wrap items-baseline gap-3">
<h1 className="text-2xl font-bold text-link">Video thumbnails</h1>
<p className="text-sm" style={{ color: MUTED }}>
Take a still from one of our YouTube or Facebook videos at any moment.
</p>
</div>
<section className="mt-6 bg-raised p-5 shadow-md">
<form onSubmit={take} className="flex flex-wrap items-end gap-3">
<div className="min-w-0 flex-1">
<label htmlFor={urlId} className="block text-sm font-medium text-body">
Video link
</label>
<input
id={urlId}
type="url"
value={url}
onChange={(e) => changeUrl(e.target.value)}
placeholder="eg. https://www.youtube.com/watch?v=…"
className={`mt-1 w-full ${input}`}
/>
</div>
<div className="w-28">
<label htmlFor={timeId} className="block text-sm font-medium text-body">
Time
</label>
<input
id={timeId}
type="text"
value={time}
onChange={(e) => setTime(e.target.value)}
placeholder="eg. 1:23"
className={`mt-1 w-full ${input}`}
/>
</div>
<button type="submit" className={primary} disabled={busy || !url.trim() || seconds == null}>
{busy ? "Taking…" : "Take frame"}
</button>
</form>
<p className="mt-2 text-xs" style={{ color: MUTED }}>
YouTube and Facebook links only, and only videos NGU owns or has permission to use. A link copied with
its time (eg. Share → Start at) fills in the time. Frames are saved to{" "}
<Link to="/admin/media" className="text-link underline">
Media
</Link>{" "}
→ Previews.
</p>
{seconds == null && <p className="mt-2 text-sm text-danger">Write the time as seconds or m:ss, eg. 83 or 1:23.</p>}
{error && <p className="mt-2 text-sm text-danger">{error}</p>}
</section>
{frames.length > 0 && (
<ul className="mt-6 grid gap-6 sm:grid-cols-2 lg:grid-cols-3">
{frames.map((frame) => (
<Frame key={frame.media.id} frame={frame} />
))}
</ul>
)}
</div>
);
}

View file

@ -190,6 +190,12 @@ export const TOOLS_NAV = {
blurb: "Cut clips from our YouTube and Facebook videos: trimmed, reshaped for Reels, or audio only.",
minRole: "admin",
},
{
to: "/admin/thumbnails",
label: "Video thumbnails",
blurb: "A still from one of our YouTube or Facebook videos at any moment, saved to the media library.",
minRole: "editor",
},
{
to: "/admin/newsletter",
label: "Newsletter",

View file

@ -113,7 +113,13 @@ export default function HeroStage({ hero }: { hero: Hero | null }) {
{feature === 'video' && hero?.video && (
<div className="lg:col-span-7">
<VideoFrame source={video} url={hero.video.url} title={hero.video.title} captions={mediaUrl(hero.video.captions)} />
<VideoFrame
source={video}
url={hero.video.url}
title={hero.video.title}
captions={mediaUrl(hero.video.captions)}
still={mediaUrl(hero.video.still)}
/>
</div>
)}
@ -493,18 +499,22 @@ function LiveFrame({ src, url, title }: { src: string | null; url: string; title
"metadata" fetches enough for the first frame and the length,
not the whole file. An embed is a VideoPlayer: a still until
pressed, so the video's host sets nothing before then. A link
that is neither still gets a button out to it. */
that is neither still gets a button out to it. `still` is the
editor's chosen picture (074): the embed's still, or the file's
poster; without one, YouTube's thumbnail or the first frame. */
function VideoFrame({
source,
url,
title,
captions,
still,
}: {
source: VideoSource | null
url: string
title?: string | null
/** A WebVTT file's URL; only a file plays it, an embed has its own. */
captions?: string | null
still?: string | null
}) {
return (
<div
@ -520,11 +530,12 @@ function VideoFrame({
controls
playsInline
preload="metadata"
poster={still ?? undefined}
>
{captions && <track kind="captions" src={captions} srcLang="en" label="Captions" default />}
</video>
) : source?.kind === 'embed' ? (
<VideoPlayer url={url} title={title || 'Featured video'} className="h-full" />
<VideoPlayer url={url} still={still} title={title || 'Featured video'} className="h-full" />
) : (
<div className="flex h-full w-full items-center justify-center">
<HomeLink