Video frame stills for featured cards; newsletter Pick from fix

- Image fields take frameFrom/frameWhen: "Frame at" saves a still from
  the row's YouTube or Facebook link to /uploads/thumbnails/
  (POST /api/admin/video-frame, grabFrame in clips.js). Used on video
  featured cards.
- clock/parseClock moved to src/lib/clock.ts, shared with Video clips.
- Newsletter: new drafts no longer store source 'events' (the Pick from
  select's blank choice), which showed as "events no longer exists";
  older drafts load it as blank.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-10-04 21:35:08 -05:00
parent 3b45944094
commit b8012c75e9
9 changed files with 172 additions and 25 deletions

View file

@ -44,6 +44,7 @@ import { useLocation } from "react-router-dom";
import { ApiError, get, post, upload } from "../../lib/api.ts";
import { useAuth } from "../../lib/auth.tsx";
import { clock, parseClock } from "../../lib/clock.ts";
import { canWrite as roleCanWrite } from "../../lib/roles.ts";
import { useSmallImage } from "../../lib/useSmallImage.ts";
@ -524,6 +525,7 @@ export function ImageInput({
value,
resolve,
folder,
videoUrl,
locked,
className,
onChange,
@ -533,6 +535,8 @@ export function ImageInput({
resolve?: (file?: string | null) => string | null;
/** The field's `folder` (logos for logo fields); default images. */
folder?: string;
/** A video link to take a frame from (the field's `frameFrom`). */
videoUrl?: string | null;
locked: boolean;
className: string;
onChange: (value: string) => void;
@ -587,6 +591,7 @@ export function ImageInput({
)}
</div>
)}
{editable && videoUrl && <FrameFromVideo url={videoUrl} className={className} onSaved={onChange} />}
</div>
{picking && (
@ -601,6 +606,55 @@ export function ImageInput({
);
}
/* ── Frame from a video ──────────────────────────────────────── */
/* A link's own start time (YouTube's t=83 or t=83s), so a link
copied at the moment wanted starts the picker there. */
function startOf(url: string): number {
try {
const t = new URL(url).searchParams.get("t") ?? "";
return /^\d+s?$/.test(t) ? parseInt(t, 10) : 0;
} catch {
return 0;
}
}
function FrameFromVideo({ url, className, onSaved }: { url: string; className: string; onSaved: (value: string) => void }) {
const [text, setText] = useState(() => clock(startOf(url)));
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const timeId = useId();
const seconds = parseClock(text);
async function take() {
setBusy(true);
setError(null);
try {
const { media } = await post("/admin/video-frame", { url, seconds });
onSaved(media.url);
} catch (err) {
setError(err instanceof ApiError ? err.message : "Couldn't take that frame.");
} finally {
setBusy(false);
}
}
return (
<div className="flex flex-wrap items-center gap-2">
<label htmlFor={timeId} className="text-sm text-muted">
Frame at
</label>
<div className="w-24">
<input id={timeId} type="text" value={text} onChange={(e) => setText(e.target.value)} placeholder="eg. 1:23" className={className} />
</div>
<button type="button" className={button} disabled={busy || seconds == null} onClick={take}>
{busy ? "Taking…" : "Use this frame"}
</button>
{error && <span className="text-xs text-danger">{error}</span>}
</div>
);
}
/* ── Link preview field ──────────────────────────────────────── */
/* Fills only what's empty: a title somebody already typed is theirs,

View file

@ -71,7 +71,9 @@
`resolve` option is the media.ts helper for the field's public/
directory, so a bare filename previews from the right place. Its
`folder` ("logos" for logo and icon fields) is where uploads go and
the tab the library opens on.
the tab the library opens on. `frameFrom` names the row's video
link field and adds "Frame at" (a still at a typed time, saved to
thumbnails/); `frameWhen` limits it to rows that match.
Two options change the box itself rather than what goes in it:
@ -383,6 +385,7 @@ export function Field({ field, value, row, options, error, onChange, onPatch }:
value={value}
resolve={field.resolve}
folder={field.folder}
videoUrl={field.frameFrom && fieldShown({ when: field.frameWhen }, row) ? getPath(row, field.frameFrom) : null}
locked={locked}
className={locked ? inputLocked : common.className}
onChange={onChange}

View file

@ -191,7 +191,9 @@ export const featuredCardsChild = {
widget: "image",
resolve: mediaUrl,
full: true,
help: "Default: the record's own picture, or for a YouTube video, its thumbnail.",
frameFrom: "url",
frameWhen: { path: "kind", value: "video" },
help: "Default: the record's own picture, or for a YouTube video, its thumbnail. For a video, Frame at takes a still from it instead.",
},
],
};

21
src/lib/clock.ts Normal file
View file

@ -0,0 +1,21 @@
/* Times in a video, as typed and shown: Tools → Video clips and the
image field's frame picker. */
/* 83.5 → "1:23.5"; 3723 → "1:02:03". */
export function clock(seconds: number | null | undefined): string {
if (seconds == null || !Number.isFinite(seconds)) return "-";
const whole = Math.floor(seconds);
const tenths = Math.round((seconds - whole) * 10);
const h = Math.floor(whole / 3600);
const m = Math.floor((whole % 3600) / 60);
const s = whole % 60;
const ss = `${String(s).padStart(2, "0")}${tenths ? `.${tenths % 10}` : ""}`;
return h ? `${h}:${String(m).padStart(2, "0")}:${ss}` : `${m}:${ss}`;
}
/* "1:23.5", "83.5" or "1:02:03" → seconds; null when it isn't a time. */
export function parseClock(text: string): number | null {
const parts = text.trim().split(":");
if (!parts.length || parts.some((p) => !/^\d+(\.\d+)?$/.test(p))) return null;
return parts.reduce((total, part) => total * 60 + Number(part), 0);
}

View file

@ -18,6 +18,7 @@ import { useCallback, useEffect, useRef, useState, type ReactNode } from "react"
import { Link } from "react-router-dom";
import { ApiError, del, get, post } from "../../lib/api.ts";
import { clock, parseClock } from "../../lib/clock.ts";
import { accentText, BRAND } from '../../lib/color.ts'
const MUTED = "var(--color-muted)";
@ -56,25 +57,6 @@ const SHAPES: Array<[string, string, number | null]> = [
["vertical", "Vertical 9:16 (Reels, Shorts, TikTok)", 9 / 16],
];
/* 83.5 → "1:23.5"; 3723 → "1:02:03". */
function clock(seconds: number | null | undefined): string {
if (seconds == null || !Number.isFinite(seconds)) return "-";
const whole = Math.floor(seconds);
const tenths = Math.round((seconds - whole) * 10);
const h = Math.floor(whole / 3600);
const m = Math.floor((whole % 3600) / 60);
const s = whole % 60;
const ss = `${String(s).padStart(2, "0")}${tenths ? `.${tenths % 10}` : ""}`;
return h ? `${h}:${String(m).padStart(2, "0")}:${ss}` : `${m}:${ss}`;
}
/* "1:23.5", "83.5" or "1:02:03" → seconds; null when it isn't a time. */
function parseClock(text: string): number | null {
const parts = text.trim().split(":");
if (!parts.length || parts.some((p) => !/^\d+(\.\d+)?$/.test(p))) return null;
return parts.reduce((total, part) => total * 60 + Number(part), 0);
}
function sizeOf(bytes: number | null): string {
if (!bytes) return "";
return bytes > 1024 * 1024 * 1024

View file

@ -230,7 +230,7 @@ const STARTER: NewsletterPart[] = [
{ type: 'header' },
{ type: 'title', text: '' },
{ type: 'paragraph', text: '' },
{ type: 'picks', source: 'events', picked: [] },
{ type: 'picks', picked: [] },
{ type: 'picks', source: 'posts', picked: [] },
{ type: 'picks', source: 'cards', picked: [] },
{ type: 'footer' },
@ -287,7 +287,8 @@ export default function AdminNewsletter() {
if (row) {
try {
const parsed = JSON.parse(row.layout || '[]')
layout = Array.isArray(parsed) ? parsed : []
// Events is the blank choice of Pick from, so an older draft's 'events' reads as blank.
layout = Array.isArray(parsed) ? parsed.map((part) => (part.source === 'events' ? { ...part, source: '' } : part)) : []
} catch {
// A hand-edited draft: start its parts over.
}