"use client"; import { useEffect, useRef, useState } from "react"; import { VIDEO_STATES, VIDEO_STATE_LABELS, type VideoState } from "yt-dlp-transcript-common/lib/availability"; import { badgeVariants } from "@/components/ui/badge"; import { controlClass } from "@/components/ui/field"; import { cn } from "@/lib/utils"; import type { Scope, ScopeSummary, SourceOption } from "@/lib/research/types"; import ConnectArchive from "./ConnectArchive"; // The Source select's last option: not a source, the Connect form (demo epic D1). const CONNECT = "connect:"; // --------------------------------------------------------------------------- // The scope chips and the "holds …" line under them (canvas `.scope`): which // corpus, which channels, which dates, states and types, posts or not -- and, // BEFORE anything runs, what that scope holds. A chip that opens is a native //
, so it works from the keyboard with no script of its own; one // click outside closes whichever is open. // --------------------------------------------------------------------------- const chip = (on: boolean) => cn(badgeVariants({ variant: on ? "on" : "neutral", size: "md" }), "cursor-pointer select-none list-none [&::-webkit-details-marker]:hidden"); const nf = new Intl.NumberFormat("en-US"); const year = (d: string | null) => (d ? d.slice(0, 4) : ""); const pct = (n: number, of: number) => (of > 0 ? `${Math.round((n / of) * 100)}%` : "0%"); function Pop({ label, on, children, wide = false }: { label: React.ReactNode; on: boolean; children: React.ReactNode; wide?: boolean }) { return (
{label} ▾
{children}
); } export default function ScopeBar({ sources, scope, summary, summaryError, loading, disabled, onChange, }: { sources: SourceOption[]; scope: Scope; summary: ScopeSummary | null; summaryError: string | null; loading: boolean; disabled: boolean; onChange: (next: Scope) => void; }) { const ref = useRef(null); const [connecting, setConnecting] = useState(false); // One click outside closes an open chip. useEffect(() => { const close = (e: MouseEvent) => { for (const d of ref.current?.querySelectorAll("details[open]") ?? []) { if (!d.contains(e.target as Node)) d.open = false; } }; document.addEventListener("click", close); return () => document.removeEventListener("click", close); }, []); const channels = summary?.channels ?? []; const chosen = new Set(scope.channels ?? []); const states = new Set(scope.states ?? []); const toggle = (set: Set, v: string) => { const next = new Set(set); if (next.has(v)) next.delete(v); else next.add(v); return [...next]; }; const source = sources.find((s) => s.handle === scope.source) ?? sources[0]; const tracks = summary?.tracks; const trackTotal = tracks ? tracks.transcript + tracks.captions + tracks.unknown : 0; return (
0} wide label={chosen.size === 0 ? `All ${channels.length} channels` : chosen.size === 1 ? channels.find((c) => chosen.has(c.key))?.name ?? "1 channel" : `${chosen.size} channels`} >
Channels {channels.map((c) => ( ))} {chosen.size > 0 && ( )}
Uploaded
0} label={states.size === 0 ? "Any state" : [...states].map((s) => VIDEO_STATE_LABELS[s as VideoState] ?? s).join(", ")}>
On the platform {VIDEO_STATES.map((s) => ( ))}
Type {([undefined, "video", "livestream"] as const).map((m) => ( ))}
{summaryError ? ( {summaryError} ) : !summary ? ( {loading ? "reading the scope…" : "—"} ) : ( <> holds{" "} {summary.selected} {summary.selected === 1 ? "channel" : "channels"} · {nf.format(summary.videos)} videos {summary.first && summary.last ? ` · ${year(summary.first)}–${year(summary.last)}` : ""} {tracks && trackTotal > 0 ? ( {" "} · transcripts {pct(tracks.transcript, trackTotal)} · captions {pct(tracks.captions, trackTotal)} {tracks.unknown > 0 ? ` · unknown ${pct(tracks.unknown, trackTotal)}` : ""} {tracks.counted < tracks.of ? ` (${tracks.counted} of ${tracks.of} channels counted)` : ""} ) : ( {" "} · transcripts vs captions: {summary.tracksStale ?? "counted by the first full search"} )} {summary.aliases > 0 ? " · aliases on" : ""} {summary.warnings.length > 0 && · {summary.warnings.join("; ")}} {loading && · updating…} )}
{connecting && ( setConnecting(false)} onConnected={(handle) => { setConnecting(false); onChange({ source: handle }); }} /> )}
); }