Files
Homarr/src/widgets/calendar/CalendarTile.tsx

148 lines
3.9 KiB
TypeScript
Raw Normal View History

2023-04-24 21:39:04 +02:00
import { useMantineTheme } from '@mantine/core';
2022-12-11 14:11:25 +01:00
import { Calendar } from '@mantine/dates';
2022-12-18 21:21:23 +01:00
import { IconCalendarTime } from '@tabler/icons';
2022-12-11 14:11:25 +01:00
import { useQuery } from '@tanstack/react-query';
2023-01-31 12:53:32 +09:00
import { i18n } from 'next-i18next';
2022-12-11 14:11:25 +01:00
import { useState } from 'react';
import { useConfigContext } from '../../config/provider';
2022-12-18 21:21:23 +01:00
import { defineWidget } from '../helper';
import { IWidget } from '../widgets';
2022-12-11 14:11:25 +01:00
import { CalendarDay } from './CalendarDay';
2023-04-24 21:39:04 +02:00
import { getBgColorByDateAndTheme } from './bg-calculator';
2022-12-11 14:11:25 +01:00
import { MediasType } from './type';
2022-12-18 21:21:23 +01:00
const definition = defineWidget({
id: 'calendar',
icon: IconCalendarTime,
options: {
2023-02-06 01:09:11 +09:00
useSonarrv4: {
type: 'switch',
defaultValue: false,
},
2022-12-18 21:21:23 +01:00
sundayStart: {
type: 'switch',
defaultValue: false,
},
radarrReleaseType: {
type: 'select',
defaultValue: 'inCinemas',
data: [
{ label: 'In Cinemas', value: 'inCinemas' },
{ label: 'Physical', value: 'physicalRelease' },
{ label: 'Digital', value: 'digitalRelease' },
],
},
2022-12-18 21:21:23 +01:00
},
gridstack: {
2023-01-08 12:41:55 +09:00
minWidth: 2,
minHeight: 2,
maxWidth: 12,
maxHeight: 12,
},
2022-12-18 21:21:23 +01:00
component: CalendarTile,
});
export type ICalendarWidget = IWidget<(typeof definition)['id'], typeof definition>;
2022-12-18 21:21:23 +01:00
interface CalendarTileProps {
widget: ICalendarWidget;
2022-12-11 14:11:25 +01:00
}
function CalendarTile({ widget }: CalendarTileProps) {
2023-04-24 21:39:04 +02:00
const { colorScheme } = useMantineTheme();
2022-12-11 14:11:25 +01:00
const { name: configName } = useConfigContext();
const [month, setMonth] = useState(new Date());
const { data: medias } = useQuery({
queryKey: ['calendar/medias', { month: month.getMonth(), year: month.getFullYear() }],
staleTime: 1000 * 60 * 60 * 5,
2022-12-11 14:11:25 +01:00
queryFn: async () =>
(await (
await fetch(
`/api/modules/calendar?year=${month.getFullYear()}&month=${
month.getMonth() + 1
2023-03-30 23:35:29 +02:00
}&configName=${configName}&widgetId=${widget.id}`
2022-12-11 14:11:25 +01:00
)
).json()) as MediasType,
});
return (
2023-04-17 14:48:04 +09:00
<Calendar
defaultDate={new Date()}
onPreviousMonth={setMonth}
onNextMonth={setMonth}
size="xs"
locale={i18n?.resolvedLanguage ?? 'en'}
firstDayOfWeek={widget.properties.sundayStart ? 0 : 1}
hideWeekdays
style={{ position: 'relative', top: -10 }}
date={month}
maxLevel="month"
hasNextLevel={false}
styles={{
calendarHeader: {
maxWidth: 'inherit',
},
calendar: {
height: '100%',
display: 'flex',
flexDirection: 'column',
width: '100%',
},
monthLevelGroup: {
height: '100%',
},
monthLevel: {
height: '100%',
display: 'flex',
flexDirection: 'column',
width: '100%',
},
month: {
flex: 1,
},
}}
2023-04-24 21:39:04 +02:00
getDayProps={(date) => ({
bg: getBgColorByDateAndTheme(colorScheme, date),
})}
2023-04-17 14:48:04 +09:00
renderDay={(date) => (
<CalendarDay date={date} medias={getReleasedMediasForDate(medias, date, widget)} />
)}
/>
2022-12-11 14:11:25 +01:00
);
2022-12-18 21:21:23 +01:00
}
2022-12-11 14:11:25 +01:00
const getReleasedMediasForDate = (
medias: MediasType | undefined,
date: Date,
widget: ICalendarWidget
): MediasType => {
const { radarrReleaseType } = widget.properties;
2022-12-11 14:11:25 +01:00
const books =
medias?.books.filter((b) => new Date(b.releaseDate).toDateString() === date.toDateString()) ??
[];
const movies =
medias?.movies.filter(
(m) => new Date(m[radarrReleaseType]).toDateString() === date.toDateString()
) ?? [];
2022-12-11 14:11:25 +01:00
const musics =
medias?.musics.filter((m) => new Date(m.releaseDate).toDateString() === date.toDateString()) ??
[];
const tvShows =
medias?.tvShows.filter(
(tv) => new Date(tv.airDateUtc).toDateString() === date.toDateString()
) ?? [];
const totalCount = medias ? books.length + movies.length + musics.length + tvShows.length : 0;
return {
books,
movies,
musics,
tvShows,
totalCount,
};
};
2022-12-18 21:21:23 +01:00
export default definition;