"use client"; import { useEffect, useId, useState } from "react"; import { STORED_URL_HELP, STORED_URL_LABEL } from "yt-dlp-transcript-common/lib/storedMediaUrl"; import { cn } from "@/lib/utils"; // --------------------------------------------------------------------------- // "Cut from the stored media URL" (release 32 R6), umtool's half: a checkbox // beside a fetch button, shown only when the span's record stores a media file // address the editor can cut from (GET /api/cited/stored — Odysee's CDN file). // Off by default; ticked, the fetch asks the editor with STORED_URL_UI_MODE // ("fallback": the page first, the stored file when the platform refuses). // --------------------------------------------------------------------------- export type StoredUrlQuery = { project: string; clip: string } | { channel: string; video: string }; const answers = new Map>(); function keyOf(q: StoredUrlQuery): string { return "project" in q ? `p|${q.project}|${q.clip}` : `c|${q.channel}|${q.video}`; } function lookup(q: StoredUrlQuery): Promise { const key = keyOf(q); let p = answers.get(key); if (!p) { p = fetch(`/api/cited/stored?${new URLSearchParams(q)}`, { cache: "no-store" }) .then((r) => (r.ok ? r.json() : { stored: false })) .then((j: { stored?: boolean }) => j.stored === true) .catch(() => false); answers.set(key, p); } return p; } /** Whether the option is offered for `q` (null while asking, or with no query). */ export function useStoredUrlOffered(q: StoredUrlQuery | null): boolean { const [offered, setOffered] = useState(false); const key = q ? keyOf(q) : ""; useEffect(() => { if (!q) return; let live = true; void lookup(q).then((v) => live && setOffered(v)); return () => { live = false; }; // `q` is named by its key. // eslint-disable-next-line react-hooks/exhaustive-deps }, [key]); return offered; } /** The checkbox itself, for a caller that already knows it is offered. */ export function StoredUrlCheckbox({ checked, onChange, disabled, compact, className, }: { checked: boolean; onChange: (v: boolean) => void; disabled?: boolean; /** One line; the help on hover only. */ compact?: boolean; className?: string; }) { const helpId = useId(); return ( ); } /** Asks whether it is offered, and renders nothing when it is not. */ export default function StoredUrlOption({ query, ...rest }: { query: StoredUrlQuery | null } & Parameters[0]) { const offered = useStoredUrlOffered(query); if (!offered) return null; return ; }