/* eslint-disable camelcase */
import useSWR from "swr";
import { useTranslation } from "react-i18next";
import { BsFillPlayFill, BsPauseFill, BsCpu, BsFillCpuFill } from "react-icons/bs";
import { MdOutlineSmartDisplay } from "react-icons/md";
import Widget from "../widget";
import { formatApiUrl } from "utils/api-helpers";
function millisecondsToTime(milliseconds) {
const seconds = Math.floor((milliseconds / 1000) % 60);
const minutes = Math.floor((milliseconds / (1000 * 60)) % 60);
const hours = Math.floor((milliseconds / (1000 * 60 * 60)) % 24);
return { hours, minutes, seconds };
}
function millisecondsToString(milliseconds) {
const { hours, minutes, seconds } = millisecondsToTime(milliseconds);
const parts = [];
if (hours > 0) {
parts.push(hours);
}
parts.push(minutes);
parts.push(seconds);
return parts.map((part) => part.toString().padStart(2, "0")).join(":");
}
function SingleSessionEntry({ session }) {
const { full_title, duration, view_offset, progress_percent, state, video_decision, audio_decision } = session;
return (
<>
{video_decision === "copy" && audio_decision === "copy" && }
{video_decision !== "copy" && audio_decision !== "copy" && }
{video_decision === "copy" && audio_decision !== "copy" && }
{state === "paused" && (
)}
{state !== "paused" && (
)}
{millisecondsToString(view_offset)}
/
{millisecondsToString(duration)}
>
);
}
function SessionEntry({ session }) {
const { full_title, view_offset, progress_percent, state, video_decision, audio_decision } = session;
return (
{state === "paused" && (
)}
{state !== "paused" && (
)}
{video_decision === "copy" && audio_decision === "copy" && }
{video_decision !== "copy" && audio_decision !== "copy" && }
{video_decision === "copy" && audio_decision !== "copy" && }
{millisecondsToString(view_offset)}
);
}
export default function Tautulli({ service }) {
const { t } = useTranslation();
const config = service.widget;
const { data: activityData, error: activityError } = useSWR(formatApiUrl(config, "get_activity"), {
refreshInterval: 5000,
});
if (activityError) {
return ;
}
if (!activityData) {
return (
);
}
const playing = activityData.response.data.sessions.sort((a, b) => {
if (a.view_offset > b.view_offset) {
return 1;
}
if (a.view_offset < b.view_offset) {
return -1;
}
return 0;
});
if (playing.length === 0) {
return (
{t("tautulli.no_active")}
-
);
}
if (playing.length === 1) {
const session = playing[0];
return (
);
}
return (
{playing.map((session) => (
))}
);
}