// The site form's cited scope (site.json `citedChannels`, release 23), pure so // the parse is tested without a server (saveSiteAction calls it). // // The form posts `citedChannelsJson`: the slugs whose "Cited only" box is // ticked, a box shown only on a channel row outside the pool. A channel the // pool holds is never cited-only (the pool wins — ticking "Include" clears the // box, and the schema drops such a slug anyway). The Site is rebuilt from the // form on save, so a missing field means no cited scope; a payload that is not // a JSON array is refused (null), never read as "none". import { parseSiteCitedChannels, type SiteChannelMembership, type SiteCitedChannel, } from "yt-dlp-transcript-common/lib/site"; export const CITED_CHANNELS_FIELD = "citedChannelsJson"; export function citedChannelsFromForm( formData: FormData, pool: readonly Pick[], ): SiteCitedChannel[] | null { const raw = formData.get(CITED_CHANNELS_FIELD); if (raw === null) return []; let input: unknown; try { input = JSON.parse(String(raw)); } catch { return null; } if (!Array.isArray(input)) return null; // The form sends bare slugs; a `{slug}` entry reads the same. const entries = input.map((v) => (typeof v === "string" ? { slug: v } : v)); const inPool = new Set(pool.map((c) => c.slug)); return parseSiteCitedChannels(entries).filter((c) => !inPool.has(c.slug)); }