import { useId } from "react";
import {
Badge,
Box,
Button,
darken,
Group,
Image,
lighten,
ScrollArea,
Stack,
Text,
useMantineColorScheme,
} from "@mantine/core";
import { IconClock, IconPin } from "@tabler/icons-react";
import dayjs from "dayjs";
import { isNullOrWhitespace } from "@homarr/common";
import type { CalendarEvent } from "@homarr/integrations/types";
import { useCurrentIntlLocale, useI18n } from "@homarr/translation/client";
import { groupEventsByDate } from "./calendar-events";
import { getSafeApplicationUrl, SAFE_NEW_TAB_REL } from "../common/application-url";
import { formatLocalizedTime } from "../common/locale";
import classes from "./calendar-event-list.module.css";
export type CalendarEventWithSource = CalendarEvent & {
source?: { integrationId: string; integrationName: string };
};
interface CalendarEventListProps {
events: CalendarEventWithSource[];
advanced?: boolean;
fillHeight?: boolean;
groupByDate?: boolean;
locale?: string;
}
export const CalendarEventList = ({
events,
advanced = false,
fillHeight = false,
groupByDate = false,
locale,
}: CalendarEventListProps) => {
const headingIdPrefix = useId();
const currentLocale = useCurrentIntlLocale();
const effectiveLocale = locale ?? currentLocale;
const dateFormatter = new Intl.DateTimeFormat(effectiveLocale, {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
});
const groups = groupByDate ? [...groupEventsByDate(events).entries()] : null;
return (
{groups ? (
groups.map(([date, groupedEvents]) => {
const headingId = `${headingIdPrefix}-${date}`;
return (
{dateFormatter.format(groupedEvents[0]?.startDate)}
);
})
) : (
)}
);
};
const CalendarEventRows = ({
events,
locale,
advanced,
}: Pick & { locale: string }) => {
const { colorScheme } = useMantineColorScheme();
const t = useI18n("widget.calendar");
return (
<>
{events.map((event, eventIndex) => (
{event.image !== null && (
{event.image.badge !== undefined && (
{event.image.badge.content}
)}
)}
{event.subTitle !== null && (
{event.subTitle}
)}
{event.title}
{advanced && event.source && (
{event.source.integrationName}
)}
{event.metadata?.type === "radarr" && (
{t(`option.releaseType.options.${event.metadata.releaseType}`)}
)}
{isAllDay(event) ? (
{t("duration.allDay")}
) : (
<>
{formatLocalizedTime(event.startDate, locale)}
{event.endDate !== null && (
<>
-{" "}
{formatLocalizedTime(event.endDate, locale)}
>
)}
>
)}
{event.location !== null && (
{event.location}
)}
{!isNullOrWhitespace(event.description) && (
{event.description}
)}
{event.links.length > 0 && (
{event.links
.filter((link) => link.href)
.map((link) => {
const href = getSafeApplicationUrl(link.href.toString());
return (
: undefined}
>
{link.name}
);
})}
)}
))}
>
);
};
const isAllDay = (event: Pick) => {
if (!event.endDate) return false;
const start = dayjs(event.startDate);
const end = dayjs(event.endDate);
return start.startOf("day").isSame(start) && end.endOf("day").isSame(end);
};