// Streams list: search, filter, sort, bulk actions. Create is a full page. import { el, pill, setText, reconcile, toast, confirmDialog, debounce, fmtAge, clear, append, } from "../util.js"; import { state, subscribe, refresh, canEdit } from "../store.js"; import { api } from "../api.js"; import { publishLinks, tagList } from "../components.js"; const view = { q: "", health: "all", tag: "all", sort: "name", dir: 1, selected: new Set(), }; export function streams(root) { const tbody = el("tbody"); const countLabel = el("span.muted"); const bulkBar = el("div.row"); const search = el("input", { type: "search", placeholder: "Search name, title, tag…", value: view.q, oninput: debounce((e) => { view.q = e.target.value.trim().toLowerCase(); paint(); }), }); const healthSel = el("select", { onchange: (e) => { view.health = e.target.value; paint(); }, }, ...["all", "ok", "starting", "down", "stopped"].map((v) => el("option", { value: v, text: v === "all" ? "Any health" : v, selected: view.health === v }))); const tagSel = el("select", { onchange: (e) => { view.tag = e.target.value; paint(); }, }); const head = el("thead", el("tr", el("th", { style: { width: "28px" } }, el("input", { type: "checkbox", title: "Select all shown", onchange: (e) => { const shown = visible(); if (e.target.checked) shown.forEach((s) => view.selected.add(s.id)); else shown.forEach((s) => view.selected.delete(s.id)); paint(); }, })), sortTh("name", "Stream"), sortTh("health", "Health"), sortTh("viewers", "Viewers"), el("th", { text: "Publish" }), el("th", { text: "Actions" }), )); root.append(el("div.stack", el("div.card", el("div.card-head", el("h2", { text: "Streams" }), countLabel, el("span.spacer"), search, healthSel, tagSel, canEdit() ? el("button.sm", { text: "+ Add stream", onclick: () => { location.hash = "#/streams/new"; }, }) : null, ), bulkBar, el("div.table-wrap", el("table", head, tbody)), ), )); function sortTh(key, label) { const th = el("th.sortable", { onclick: () => { if (view.sort === key) view.dir *= -1; else { view.sort = key; view.dir = 1; } paint(); }, }, el("span", { text: label }), el("span.arrow")); th.dataset.sortKey = key; return th; } function visible() { let list = (state.streams || []).slice(); if (view.q) { list = list.filter((s) => `${s.name} ${s.title} ${s.tags} ${s.app} ${s.channel}`.toLowerCase().includes(view.q)); } if (view.health !== "all") list = list.filter((s) => (s.health || "stopped") === view.health); if (view.tag !== "all") { list = list.filter((s) => String(s.tags || "").split(",").includes(view.tag)); } const dir = view.dir; list.sort((a, b) => { switch (view.sort) { case "health": return dir * String(a.health || "").localeCompare(String(b.health || "")); case "viewers": return dir * ((a.viewers || 0) - (b.viewers || 0)); default: return dir * String(a.name).localeCompare(String(b.name)); } }); return list; } function paint() { const list = visible(); const total = (state.streams || []).length; setText(countLabel, list.length === total ? `${total}` : `${list.length} of ${total}`); const tags = new Set(); for (const s of state.streams || []) { for (const t of String(s.tags || "").split(",").filter(Boolean)) tags.add(t); } const wantTags = ["all", ...Array.from(tags).sort()]; if (tagSel.dataset.opts !== wantTags.join("|")) { tagSel.dataset.opts = wantTags.join("|"); clear(tagSel); append(tagSel, wantTags.map((t) => el("option", { value: t, text: t === "all" ? "Any tag" : t, selected: view.tag === t }))); } for (const th of head.querySelectorAll("th.sortable")) { const arrow = th.querySelector(".arrow"); setText(arrow, th.dataset.sortKey === view.sort ? (view.dir > 0 ? "▲" : "▼") : ""); } const ids = new Set((state.streams || []).map((s) => s.id)); for (const id of Array.from(view.selected)) if (!ids.has(id)) view.selected.delete(id); paintBulk(); if (!list.length) { reconcile(tbody, [{ id: "none" }], (x) => x.id, () => el("tr", el("td.empty", { colspan: "6" }, total ? el("span", { text: "No stream matches these filters." }) : el("div", el("p", { text: "No streams yet." }), canEdit() ? el("p", el("a", { href: "#/streams/new", text: "Add your first stream" })) : null, ), )), () => {}); return; } reconcile(tbody, list, (s) => s.id, createRow, updateRow); } function paintBulk() { const n = view.selected.size; clear(bulkBar); if (!n || !canEdit()) return; append(bulkBar, [ el("span.muted", { text: `${n} selected` }), el("button.ghost.sm", { text: "Start", onclick: () => bulk("start") }), el("button.ghost.sm", { text: "Stop", onclick: () => bulk("stop") }), el("button.ghost.sm", { text: "Restart", onclick: () => bulk("restart") }), el("button.ghost.sm", { text: "Clear", onclick: () => { view.selected.clear(); paint(); } }), ]); bulkBar.style.marginBottom = "12px"; } async function bulk(action) { const ids = Array.from(view.selected); if (!ids.length) return; if (!(await confirmDialog( `${action[0].toUpperCase()}${action.slice(1)} ${ids.length} stream(s)?`, action === "start" ? "Selected streams will start (or wake) now." : "Viewers currently watching will be disconnected.", action[0].toUpperCase() + action.slice(1), action !== "start", ))) return; try { const res = await api.bulk(ids, action); const failed = (res.results || []).filter((r) => !r.ok); if (failed.length) { toast(`${res.ok_count} ok, ${failed.length} failed: ${failed[0].error}`, true); } else { toast(`${action} applied to ${res.ok_count} stream(s)`); } view.selected.clear(); await refresh("bulk"); } catch (err) { toast(err.message, true); } } function createRow(s) { const tr = el("tr", el("td", el("input", { type: "checkbox", class: "sel", onchange: (e) => { const id = Number(tr.dataset.key); if (e.target.checked) view.selected.add(id); else view.selected.delete(id); paintBulk(); }, })), el("td.name-cell"), el("td.health-cell"), el("td.viewers-cell.mono"), el("td.links-cell"), el("td.actions"), ); updateRow(tr, s); return tr; } function flagPills(s) { const flags = []; if (s.enabled === false) flags.push(pill("out of service", "off")); if (s.on_demand) flags.push(pill("on-demand", "tag")); if (s.public === false) flags.push(pill("private", "tag")); if (!s.mpd) flags.push(pill("no manifest", "down")); if (s.needs_capture) flags.push(pill("needs keys", "starting")); for (const n of Array.from(tagList(s.tags).childNodes)) flags.push(n); const shown = flags.slice(0, 2); const rest = flags.length - shown.length; if (rest > 0) { const titles = flags.slice(2).map((n) => n.textContent).join(", "); shown.push(el("span.flag-more", { text: `+${rest}`, title: titles })); } return shown; } function updateRow(tr, s) { tr.querySelector(".sel").checked = view.selected.has(s.id); const nameCell = tr.querySelector(".name-cell"); nameCell.replaceChildren( el("div", el("a", { href: `#/stream/${s.id}`, text: s.title || s.name })), el("div.sub.mono", { text: s.name }), el("div.row", { style: { gap: "4px", marginTop: "3px" } }, ...flagPills(s)), ); tr.querySelector(".health-cell").replaceChildren( el("div", pill(s.health || "stopped")), s.last_error ? el("div.sub", { text: s.last_error, title: s.last_error }) : null, s.playlist_age_s ? el("div.sub", { text: fmtAge(s.playlist_age_s) }) : null, ); setText(tr.querySelector(".viewers-cell"), s.viewers ? String(s.viewers) : "—"); tr.querySelector(".links-cell").replaceChildren( publishLinks(s, { limit: 1, compact: true }), ); const actions = tr.querySelector(".actions"); if (!canEdit()) { actions.replaceChildren(el("a", { href: `#/stream/${s.id}`, text: "Open" })); return; } actions.replaceChildren(el("div.row", el("button.ghost.sm", { text: "Start", onclick: () => act(s, "start") }), el("button.ghost.sm", { text: "Stop", onclick: () => act(s, "stop") }), el("a.sm", { href: `#/stream/${s.id}`, text: "Open" }), )); } async function act(s, what) { if (what === "stop" && s.health === "ok") { const ok = await confirmDialog(`Stop ${s.title || s.name}?`, "Anyone watching will be disconnected.", "Stop"); if (!ok) return; } try { await api.action(s.id, what); toast(`${s.name}: ${what}`); await refresh("action"); } catch (err) { toast(err.message, true); } } const off = subscribe(paint); paint(); refresh("streams"); return off; }