2022-05-06 17:50:03 +02:00
|
|
|
|
/* eslint-disable react/no-children-prop */
|
2022-05-06 22:45:51 +02:00
|
|
|
|
import { Popover, Box, ScrollArea, Divider, Indicator } from '@mantine/core';
|
2022-05-04 09:30:28 +02:00
|
|
|
|
import { useEffect, useState } from 'react';
|
2022-04-30 21:36:46 +02:00
|
|
|
|
import { Calendar } from '@mantine/dates';
|
2022-05-05 09:22:44 +02:00
|
|
|
|
import { RadarrMediaDisplay, SonarrMediaDisplay } from './MediaDisplay';
|
2022-05-08 20:54:40 +02:00
|
|
|
|
import { useConfig } from '../../../tools/state';
|
|
|
|
|
|
import { CalendarIcon } from '@modulz/radix-icons';
|
|
|
|
|
|
import { MHPModule } from '../modules';
|
|
|
|
|
|
|
|
|
|
|
|
export const CalendarModule: MHPModule = {
|
|
|
|
|
|
title: 'Calendar',
|
|
|
|
|
|
description: 'A calendar module for displaying upcoming releases. It interacts with the Sonarr and Radarr API.',
|
|
|
|
|
|
icon: CalendarIcon,
|
|
|
|
|
|
component: CalendarComponent,
|
|
|
|
|
|
};
|
2022-04-30 21:36:46 +02:00
|
|
|
|
|
|
|
|
|
|
export default function CalendarComponent(props: any) {
|
2022-05-04 09:30:28 +02:00
|
|
|
|
const { config } = useConfig();
|
2022-05-05 09:22:44 +02:00
|
|
|
|
const [sonarrMedias, setSonarrMedias] = useState([] as any);
|
|
|
|
|
|
const [radarrMedias, setRadarrMedias] = useState([] as any);
|
|
|
|
|
|
|
2022-05-04 09:30:28 +02:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
// Filter only sonarr and radarr services
|
|
|
|
|
|
const filtered = config.services.filter(
|
|
|
|
|
|
(service) => service.type === 'Sonarr' || service.type === 'Radarr'
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2022-05-05 09:22:44 +02:00
|
|
|
|
// Get the url and apiKey for all Sonarr and Radarr services
|
|
|
|
|
|
const sonarrService = filtered.filter((service) => service.type === 'Sonarr').at(0);
|
|
|
|
|
|
const radarrService = filtered.filter((service) => service.type === 'Radarr').at(0);
|
|
|
|
|
|
const nextMonth = new Date(new Date().setMonth(new Date().getMonth() + 2)).toISOString();
|
2022-05-06 22:45:51 +02:00
|
|
|
|
if (sonarrService && sonarrService.apiKey) {
|
2022-05-05 09:22:44 +02:00
|
|
|
|
fetch(
|
|
|
|
|
|
`${sonarrService?.url}api/calendar?apikey=${sonarrService?.apiKey}&end=${nextMonth}`
|
|
|
|
|
|
).then((response) => {
|
2022-05-06 22:45:51 +02:00
|
|
|
|
response.ok && response.json().then((data) => setSonarrMedias(data));
|
2022-05-05 09:22:44 +02:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2022-05-06 22:45:51 +02:00
|
|
|
|
if (radarrService && radarrService.apiKey) {
|
2022-05-05 09:22:44 +02:00
|
|
|
|
fetch(
|
2022-05-06 17:50:03 +02:00
|
|
|
|
`${radarrService?.url}api/v3/calendar?apikey=${radarrService?.apiKey}&end=${nextMonth}`
|
2022-05-05 09:22:44 +02:00
|
|
|
|
).then((response) => {
|
2022-05-06 22:45:51 +02:00
|
|
|
|
response.ok && response.json().then((data) => setRadarrMedias(data));
|
2022-05-05 09:22:44 +02:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2022-05-04 09:30:28 +02:00
|
|
|
|
}, [config.services]);
|
2022-04-30 21:36:46 +02:00
|
|
|
|
|
2022-05-05 09:22:44 +02:00
|
|
|
|
if (sonarrMedias === undefined && radarrMedias === undefined) {
|
|
|
|
|
|
return <Calendar />;
|
2022-05-04 09:30:28 +02:00
|
|
|
|
}
|
2022-04-30 21:36:46 +02:00
|
|
|
|
return (
|
|
|
|
|
|
<Calendar
|
2022-05-02 18:10:56 +02:00
|
|
|
|
onChange={(day: any) => {}}
|
2022-05-05 09:22:44 +02:00
|
|
|
|
renderDay={(renderdate) => (
|
|
|
|
|
|
<DayComponent
|
|
|
|
|
|
renderdate={renderdate}
|
|
|
|
|
|
sonarrmedias={sonarrMedias}
|
|
|
|
|
|
radarrmedias={radarrMedias}
|
|
|
|
|
|
/>
|
|
|
|
|
|
)}
|
2022-04-30 21:36:46 +02:00
|
|
|
|
/>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function DayComponent(props: any) {
|
2022-05-05 09:22:44 +02:00
|
|
|
|
const {
|
|
|
|
|
|
renderdate,
|
|
|
|
|
|
sonarrmedias,
|
|
|
|
|
|
radarrmedias,
|
|
|
|
|
|
}: { renderdate: Date; sonarrmedias: []; radarrmedias: [] } = props;
|
2022-04-30 21:36:46 +02:00
|
|
|
|
const [opened, setOpened] = useState(false);
|
|
|
|
|
|
|
|
|
|
|
|
const day = renderdate.getDate();
|
2022-05-04 09:30:28 +02:00
|
|
|
|
// Itterate over the medias and filter the ones that are on the same day
|
2022-05-05 09:22:44 +02:00
|
|
|
|
const sonarrFiltered = sonarrmedias.filter((media: any) => {
|
|
|
|
|
|
const date = new Date(media.airDate);
|
|
|
|
|
|
// Return true if the date is renerdate without counting hours and minutes
|
|
|
|
|
|
return date.getDate() === day && date.getMonth() === renderdate.getMonth();
|
|
|
|
|
|
});
|
|
|
|
|
|
const radarrFiltered = radarrmedias.filter((media: any) => {
|
2022-05-06 17:50:03 +02:00
|
|
|
|
const date = new Date(media.inCinemas);
|
2022-05-05 09:22:44 +02:00
|
|
|
|
// Return true if the date is renerdate without counting hours and minutes
|
|
|
|
|
|
return date.getDate() === day && date.getMonth() === renderdate.getMonth();
|
2022-05-04 09:30:28 +02:00
|
|
|
|
});
|
2022-05-05 09:22:44 +02:00
|
|
|
|
if (sonarrFiltered.length === 0 && radarrFiltered.length === 0) {
|
2022-05-04 09:30:28 +02:00
|
|
|
|
return <div>{day}</div>;
|
2022-04-30 21:36:46 +02:00
|
|
|
|
}
|
2022-05-04 09:30:28 +02:00
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Box
|
|
|
|
|
|
onClick={() => {
|
|
|
|
|
|
setOpened(true);
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
2022-05-06 21:40:21 +02:00
|
|
|
|
{radarrFiltered.length > 0 && <Indicator size={7} color="yellow" children={null} />}
|
|
|
|
|
|
{sonarrFiltered.length > 0 && <Indicator size={7} offset={8} color="blue" children={null} />}
|
|
|
|
|
|
<Popover
|
|
|
|
|
|
position="left"
|
|
|
|
|
|
width={700}
|
|
|
|
|
|
onClose={() => setOpened(false)}
|
|
|
|
|
|
opened={opened}
|
|
|
|
|
|
// TODO: Fix this !! WTF ?
|
|
|
|
|
|
target={` ${day}`}
|
|
|
|
|
|
>
|
|
|
|
|
|
<ScrollArea style={{ height: 400 }}>
|
|
|
|
|
|
{sonarrFiltered.length > 0 && <SonarrMediaDisplay media={sonarrFiltered[0]} />}
|
|
|
|
|
|
{radarrFiltered.length > 0 && sonarrFiltered.length > 0 && (
|
|
|
|
|
|
<Divider variant="dashed" my="xl" />
|
2022-05-06 17:50:03 +02:00
|
|
|
|
)}
|
2022-05-06 21:40:21 +02:00
|
|
|
|
{radarrFiltered.length > 0 && <RadarrMediaDisplay media={radarrFiltered[0]} />}
|
|
|
|
|
|
</ScrollArea>
|
|
|
|
|
|
</Popover>
|
2022-05-04 09:30:28 +02:00
|
|
|
|
</Box>
|
|
|
|
|
|
);
|
2022-04-30 21:36:46 +02:00
|
|
|
|
}
|