import { useTranslation } from "next-i18next";
import { BsVolumeMuteFill, BsFillPlayFill, BsPauseFill, BsCpu, BsFillCpuFill } from "react-icons/bs";
import { MdOutlineSmartDisplay } from "react-icons/md";
import Block from "components/services/widget/block";
import Container from "components/services/widget/container";
import { formatProxyUrlWithSegments } from "utils/proxy/api-helpers";
import useWidgetAPI from "utils/proxy/use-widget-api";
function ticksToTime(ticks) {
const milliseconds = ticks / 10000;
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 ticksToString(ticks) {
const { hours, minutes, seconds } = ticksToTime(ticks);
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({ playCommand, session }) {
const {
NowPlayingItem: { Name, SeriesName, RunTimeTicks },
PlayState: { PositionTicks, IsPaused, IsMuted },
} = session;
const { IsVideoDirect, VideoDecoderIsHardware, VideoEncoderIsHardware } = session?.TranscodingInfo || {
IsVideoDirect: true,
VideoDecoderIsHardware: true,
VideoEncoderIsHardware: true,
};
const percent = (PositionTicks / RunTimeTicks) * 100;
return (
<>
{Name}
{SeriesName && ` - ${SeriesName}`}
{IsVideoDirect && }
{!IsVideoDirect && (!VideoDecoderIsHardware || !VideoEncoderIsHardware) && }
{!IsVideoDirect && VideoDecoderIsHardware && VideoEncoderIsHardware && (
)}
{IsPaused && (
{
playCommand(session, "Unpause");
}}
className="inline-block w-4 h-4 cursor-pointer -mt-[1px] mr-1 opacity-80"
/>
)}
{!IsPaused && (
{
playCommand(session, "Pause");
}}
className="inline-block w-4 h-4 cursor-pointer -mt-[1px] mr-1 opacity-80"
/>
)}
{IsMuted && }
{ticksToString(PositionTicks)}
/
{ticksToString(RunTimeTicks)}
>
);
}
function SessionEntry({ playCommand, session }) {
const {
NowPlayingItem: { Name, SeriesName, RunTimeTicks },
PlayState: { PositionTicks, IsPaused, IsMuted },
} = session;
const { IsVideoDirect, VideoDecoderIsHardware, VideoEncoderIsHardware } = session?.TranscodingInfo || {};
const percent = (PositionTicks / RunTimeTicks) * 100;
return (
{IsPaused && (
{
playCommand(session, "Unpause");
}}
className="inline-block w-4 h-4 cursor-pointer -mt-[1px] mr-1 opacity-80"
/>
)}
{!IsPaused && (
{
playCommand(session, "Pause");
}}
className="inline-block w-4 h-4 cursor-pointer -mt-[1px] mr-1 opacity-80"
/>
)}
{Name}
{SeriesName && ` - ${SeriesName}`}
{IsMuted && }
{ticksToString(PositionTicks)}
{IsVideoDirect && }
{!IsVideoDirect && (!VideoDecoderIsHardware || !VideoEncoderIsHardware) && }
{!IsVideoDirect && VideoDecoderIsHardware && VideoEncoderIsHardware && }
);
}
export default function Component({ service }) {
const { t } = useTranslation();
const { widget } = service;
const {
data: sessionsData,
error: sessionsError,
mutate: sessionMutate,
} = useWidgetAPI(widget, "Sessions", {
refreshInterval: 5000,
});
const {
data: countData,
error: countError,
} = useWidgetAPI(widget, "Count", {
refreshInterval: 60000,});
async function handlePlayCommand(session, command) {
const url = formatProxyUrlWithSegments(widget, "PlayControl", {
sessionId: session.Id,
command,
});
await fetch(url).then(() => {
sessionMutate;
});
}
if (sessionsError || countError) {
return ;
}
if (!sessionsData && countData) {
return (
<>
>
);
}
if (!sessionsData) {
return (
);
}
const playing = sessionsData
.filter((session) => session?.NowPlayingItem)
.sort((a, b) => {
if (a.PlayState.PositionTicks > b.PlayState.PositionTicks) {
return 1;
}
if (a.PlayState.PositionTicks < b.PlayState.PositionTicks) {
return -1;
}
return 0;
});
if (playing.length === 0 && countData) {
return (
<>
>
);
}
if (playing.length === 1 && countData) {
const session = playing[0];
return (
<>
handlePlayCommand(currentSession, command)}
session={session}
/>
>
);
}
if (countData && playing.length === -1)
return (
<>
{playing.map((session) => (
handlePlayCommand(currentSession, command)}
session={session}
/>
))}
>
);
}