"use client"; // THE X RUN CONSOLE (/review, lane x-console). Pick X posts channels, order // them, start ONE `x-run` job: fetch-posts for each in sequence, X's queue gap // between two, at most 200 posts each, the cookie jar checked before each // channel. When the jar passes social.x.maxCookieAgeMinutes the run stops and // the rest are deferred; after the operator refreshes the jar (/settings), // Resume runs them. Nothing here refreshes cookies or asks X anything — the // console reads disk (the run's state file, the jar's mtime) through the // `xRun` view. import Link from "next/link"; import { useEffect, useMemo, useState, useTransition } from "react"; import type { XRunCandidate, XRunChannelStatus, XRunConsole as XRunConsolePayload, XRunView, } from "yt-dlp-transcript-common/controller/xRun"; import { useNow, usePolledPayload } from "../../lib/usePolledPayload"; import { resumeXRunAction, startXRunAction } from "../xRunActions"; // "4:05", "1:02:03". function clock(ms: number): string { const s = Math.max(0, Math.floor(ms / 1000)); const h = Math.floor(s / 3600); const m = Math.floor((s % 3600) / 60); const sec = String(s % 60).padStart(2, "0"); return h > 0 ? `${h}:${String(m).padStart(2, "0")}:${sec}` : `${m}:${sec}`; } // "45 min", "3 h", "16 days". function age(ms: number): string { const min = Math.max(0, Math.floor(ms / 60_000)); if (min < 120) return `${min} min`; const h = Math.floor(min / 60); return h < 48 ? `${h} h` : `${Math.floor(h / 24)} days`; } // X's queue gap, as the run waits it between two channels. function gapText(seconds: number, maxSeconds = seconds): string { if (maxSeconds <= 0) return "no gap between channels"; if (maxSeconds < 120) return seconds === maxSeconds ? `${seconds} s between channels` : `${seconds}-${maxSeconds} s between channels`; const lo = Math.round(seconds / 60); const hi = Math.round(maxSeconds / 60); return lo === hi ? `${lo} min between channels` : `${lo}-${hi} min between channels, drawn per channel`; } function when(iso: string | null): string { if (!iso) return "never"; return `${iso.slice(0, 10)} ${iso.slice(11, 16)}`; } const STATUS_TONE: Record = { queued: "bg-muted text-muted-foreground", running: "bg-info-soft text-info", done: "bg-success-soft text-success", deferred: "bg-warning-soft text-warning", failed: "bg-destructive-soft text-destructive", }; const TIER_TONE: Record = { focus: "bg-brand-soft text-brand-ink", normal: "bg-muted text-muted-foreground", low: "bg-muted text-faint", paused: "bg-warning-soft text-warning", }; // THE JAR, COUNTED DOWN. Ticks every second from the jar's mtime and the // limit; the poll refreshes both (a refresh on /cookies rewrites the jar). function JarCountdown({ jar, now }: { jar: XRunConsolePayload["jar"]; now: number | null }) { const href = "/cookies"; if (jar.alert) { return (

The X cookie alert is up ({jar.alert.line}): no X job starts until it is cleared on the dashboard.

); } if (!jar.ageChecked) { return (

{jar.source === "browser" ? "X login: the browser's cookies — their age cannot be read, so the limit is not checked." : "The cookie age limit is off (social.x.maxCookieAgeMinutes = 0)."}

); } if (!jar.cookiesUpdatedAt || !jar.staleAt) { return (

No X cookie jar: Connect X account on /cookies.

); } const at = now ?? Date.parse(jar.cookiesUpdatedAt); const left = Date.parse(jar.staleAt) - at; const old = at - Date.parse(jar.cookiesUpdatedAt); if (left <= 0) { return (

Cookie jar {age(old)} old, past the {jar.maxAgeMinutes} min limit —{" "} Refresh cookies on /cookies, then Resume.

); } return (

Cookie jar {age(old)} old ·{" "} {clock(left)} {" "} left of {jar.maxAgeMinutes} min

); } function RunPanel({ run, now, onResume, pending, }: { run: XRunView; now: number | null; onResume: () => void; pending: boolean; }) { const waitLeft = run.waitingUntil && now ? Date.parse(run.waitingUntil) - now : null; return (
Run {run.id.slice(-6).toLowerCase()} {run.status} {run.jobId && ( job log )} ≤ {run.limit} posts each

{run.summary}

{waitLeft !== null && waitLeft > 0 && (

Next channel in {clock(waitLeft)} (X's queue gap).

)} {run.stopped && (

{run.stopped}

)}
    {run.channels.map((c) => (
  1. {c.slug} {c.status === "done" ? `done · ${c.posts ?? 0} posts` : c.status} {c.error && ( {c.error} )}
  2. ))}
{run.resumable > 0 && (
after a cookie refresh; starts with the deferred channels
)}
); } function Picker({ candidates, order, setOrder, picked, setPicked, }: { candidates: XRunCandidate[]; order: string[]; setOrder: (o: string[]) => void; picked: Set; setPicked: (p: Set) => void; }) { const bySlug = useMemo(() => new Map(candidates.map((c) => [c.slug, c])), [candidates]); const move = (i: number, d: -1 | 1) => { const j = i + d; if (j < 0 || j >= order.length) return; const next = [...order]; [next[i], next[j]] = [next[j], next[i]]; setOrder(next); }; const toggle = (slug: string) => { const next = new Set(picked); if (next.has(slug)) next.delete(slug); else next.add(slug); setPicked(next); }; return (
    {order.map((slug, i) => { const c = bySlug.get(slug); if (!c) return null; return (
  1. toggle(slug)} /> {i + 1} {slug} {c.tier} {c.search && search} {c.postCount} · {when(c.lastFetchedAt)}
  2. ); })}
); } export function XRunConsole({ initial }: { initial: XRunConsolePayload }) { const [pending, startTransition] = useTransition(); const [fast, setFast] = useState(initial.run?.live ?? false); const { data, refetch } = usePolledPayload("/api/view/xRun", true, fast ? 2000 : 10_000, initial, { immediate: false, }); const view = data ?? initial; const live = view.run?.live ?? false; // Polled every 2 s while a run is live, every 10 s otherwise. useEffect(() => setFast(live), [live]); const now = useNow(); // The default order is the candidates' (focus, tier, rank, slug); paused // channels start unpicked. const [order, setOrder] = useState(() => initial.candidates.map((c) => c.slug)); const [picked, setPicked] = useState>( () => new Set(initial.candidates.filter((c) => c.tier !== "paused").map((c) => c.slug)), ); // A channel added since the page loaded joins the end of the order. const known = new Set(order); const extra = view.candidates.filter((c) => !known.has(c.slug)).map((c) => c.slug); const fullOrder = extra.length ? [...order, ...extra] : order; const chosen = fullOrder.filter((s) => picked.has(s) && view.candidates.some((c) => c.slug === s)); const [note, setNote] = useState(null); const [error, setError] = useState(null); const after = (res: Awaited>, ok: string) => { if (res.ok) { setNote(ok); setError(null); setFast(true); } else { setNote(null); setError(res.error); } void refetch(); }; const start = () => startTransition(async () => { after(await startXRunAction(chosen), `X run queued: ${chosen.length} channel(s).`); }); const resume = () => startTransition(async () => { if (!view.run) return; after(await resumeXRunAction(view.run.id), "Resume queued: the deferred channels run first."); }); return (

X run

{view.candidates.length === 0 ? (

No X posts channels.

) : (
≤ {view.maxPosts} posts each · {gapText(view.gapSeconds, view.gapMaxSeconds ?? view.gapSeconds)}
{note && (

{note}

)} {error && (

{error}

)}
{view.run ? ( ) : (

No X run yet.

)}
)}
); }