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 && }
); } function CountBlocks({ service, countData }) { const { t } = useTranslation(); // allows filtering // eslint-disable-next-line no-param-reassign if (service.widget?.type === 'jellyfin') service.widget.type = 'emby' if (!countData) { return ( ) } return ( ) } 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 ; } const enableBlocks = service.widget?.enableBlocks const enableNowPlaying = service.widget?.enableNowPlaying ?? true if (!sessionsData || !countData) { return ( <> {enableBlocks && } {enableNowPlaying &&
-
-
} ); } if (enableNowPlaying) { 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) { return ( <> {enableBlocks && }
{t("emby.no_active")}
-
); } if (playing.length === 1) { const session = playing[0]; return ( <> {enableBlocks && }
handlePlayCommand(currentSession, command)} session={session} />
); } if (playing.length > 0) return ( <> {enableBlocks && }
{playing.map((session) => ( handlePlayCommand(currentSession, command)} session={session} /> ))}
); } if (enableBlocks) { return ( ) } }