import { useEffect, useMemo, useState } from "react"; import { Link } from "react-router-dom"; import { api, type JellyfinHistory, type JellyfinInfo } from "../api/client"; import EmptyState from "../components/EmptyState"; import RangeSelect from "../components/RangeSelect"; import TimeAgo from "../components/TimeAgo"; import TimeChart from "../components/TimeChart"; import { BlockSkeleton } from "../components/Skeleton"; import { ErrorNotice } from "../components/Notice"; import { useChartRange } from "../utils/chartRanges"; import { readableError } from "../utils/errors"; function Badge({ j }: { j: JellyfinInfo }) { if (!j.enabled) return Off; if (!j.state) return Not read yet; if (!j.fresh) return Can’t be read; if (j.problems.some((p) => p.severity === "critical")) return Needs a look; if (j.problems.length > 0) return Needs a look; return Up; } const count = (n: number | null, one: string, many: string) => (n === null ? null : `${n.toLocaleString()} ${n === 1 ? one : many}`); function History({ id, name }: { id: number; name: string }) { const [days, setDays] = useChartRange(7); const shown = days > 30 ? 30 : days; const [history, setHistory] = useState(null); const [error, setError] = useState(null); const [to, setTo] = useState(() => Date.now()); useEffect(() => { let alive = true; api.jellyfin .history(id, shown) .then((res) => { if (!alive) return; setHistory(res); setTo(Date.now()); setError(null); }) .catch((err) => alive && setError(readableError(err))); return () => { alive = false; }; }, [id, shown]); const from = to - shown * 86_400_000; const streams = useMemo( () => [ { name: "Streaming", color: "#4299e1", points: (history?.points ?? []).map((p) => ({ t: p.t, v: p.streams })) }, { name: "Transcoded", color: "#f76707", dash: "4 3", points: (history?.points ?? []).map((p) => ({ t: p.t, v: p.transcodes })) }, ], [history], ); return (

Who was watching

{!history && !error && } {history && `${Math.round(v)}`} />}
The most that were streaming at once in each stretch — an average would hide the evening somebody watched.
); } function Card({ j }: { j: JellyfinInfo }) { const s = j.state; const failed = (s?.tasks ?? []).filter((t) => t.lastStatus === "Failed"); const libraries = s ? [count(s.counts.movies, "film", "films"), count(s.counts.series, "series", "series"), count(s.counts.episodes, "episode", "episodes"), count(s.counts.songs, "song", "songs")].filter(Boolean) : []; return (

{j.name}

{j.runsOn && ( <> runs on {j.runsOn.name} ·{" "} )} {j.status ? <>read : "not read yet"}
{j.status && !j.status.ok &&
The last attempt to read it failed: {j.status.error ?? "no answer"}.
} {j.problems.length > 0 && (
    {j.problems.map((p) => (
  • {p.message}
  • ))}
)} {s && ( <>
{[s.serverName && s.serverName !== j.name ? s.serverName : null, s.version ? `version ${s.version}` : null, s.os, s.updateAvailable ? "an update is available" : null].filter(Boolean).join(" · ")} {libraries.length > 0 &&
{libraries.join(" · ")}
}

Streaming now

{s.streams.length === 0 ? (
Nobody.
) : (
{s.streams.map((x, i) => ( ))}
Who Watching On How
{x.user} {x.title} {x.paused && (paused)} {[x.client, x.device].filter(Boolean).join(" · ") || "—"} {x.transcoding ? Transcoded : Direct}
)} {failed.length > 0 && ( <>

Scheduled tasks that failed

    {failed.map((t) => (
  • {t.name} {t.lastEndedAt && ( <> {" "} — )} {t.error &&
    {t.error}
    }
  • ))}
)} {s.failedLogins24h !== null && s.failedLogins24h > 0 &&
{s.failedLogins24h} failed sign-in{s.failedLogins24h === 1 ? "" : "s"} in the last day.
} )} {j.enabled && s && }
); } /** Jellyfin: whether it is up, who is streaming from it right now and whether that is being transcoded, and the things that need a look. */ export default function Jellyfin() { const [data, setData] = useState(null); const [error, setError] = useState(null); useEffect(() => { const load = () => api.jellyfin .list() .then((r) => { setData(r.jellyfins); setError(null); }) .catch((err) => setError(readableError(err))); void load(); const t = setInterval(load, 30_000); return () => clearInterval(t); }, []); return ( <>

Jellyfin

Whether Jellyfin is up and who is watching. It is read every couple of minutes with an API key (see Integrations). A Jellyfin that wants a restart, a scheduled task that failed or a burst of{" "} failed sign-ins is an alert; and an update run on the machine it lives on asks first when somebody is streaming.

{!data && !error && } {data && data.length === 0 && (
)} {data?.map((j) => )} ); }