Deploy / validate-dispatch (push) Successful in 43s
Deploy / scheduled-rebuild-dev (push) Skipped
Deploy / scheduled-rebuild-master (push) Skipped
Deploy / rollback-dev (push) Skipped
Deploy / rollback-master (push) Skipped
Deploy / changes (push) Successful in 13s
Deploy / secret-scan (push) Successful in 9s
Deploy / app-quality (push) Successful in 11m21s
Deploy / security-deps (push) Successful in 14s
Deploy / context (push) Failing after 1s
Deploy / app-image (push) Skipped
Deploy / deploy-dev (push) Skipped
Deploy / deploy-master (push) Skipped
224 lines
6.5 KiB
TypeScript
224 lines
6.5 KiB
TypeScript
import type { CalendarEvent } from "../types";
|
|
import { formatEventDateRange, getEffectiveEventEnd } from "./eventFormat";
|
|
|
|
const HERO_SPECIAL_LIMIT = 5;
|
|
|
|
export interface CalendarEventGroup {
|
|
label: string;
|
|
events: CalendarEvent[];
|
|
}
|
|
|
|
export function filterUpcomingEvents(events: CalendarEvent[], now = new Date()): CalendarEvent[] {
|
|
const limit = new Date(now);
|
|
limit.setUTCHours(23, 59, 59, 999);
|
|
limit.setUTCMonth(limit.getUTCMonth() + 12);
|
|
|
|
return events
|
|
.filter((event) => {
|
|
const start = new Date(event.start);
|
|
return getEffectiveEventEnd(event) >= now && start <= limit;
|
|
})
|
|
.sort((a, b) => new Date(a.start).getTime() - new Date(b.start).getTime());
|
|
}
|
|
|
|
export function getSpecialEvents(events: CalendarEvent[], now = new Date()): CalendarEvent[] {
|
|
return events
|
|
.filter((event) => event.special && getEffectiveEventEnd(event) >= now)
|
|
.slice(0, HERO_SPECIAL_LIMIT);
|
|
}
|
|
|
|
export function getCalendarGroups(
|
|
events: CalendarEvent[],
|
|
now = new Date(),
|
|
): CalendarEventGroup[] {
|
|
const upcomingEvents = filterUpcomingEvents(events, now);
|
|
const groups = new Map<string, CalendarEventGroup>();
|
|
|
|
for (const event of upcomingEvents) {
|
|
const start = new Date(event.start);
|
|
const parts = new Intl.DateTimeFormat("en-CA", {
|
|
timeZone: event.timeZone,
|
|
year: "numeric",
|
|
month: "2-digit",
|
|
}).formatToParts(start);
|
|
const year = parts.find((part) => part.type === "year")?.value;
|
|
const month = parts.find((part) => part.type === "month")?.value;
|
|
const key = `${year}-${month}`;
|
|
|
|
if (!groups.has(key)) {
|
|
groups.set(key, {
|
|
label: new Intl.DateTimeFormat("de-DE", {
|
|
month: "long",
|
|
year: "numeric",
|
|
timeZone: event.timeZone,
|
|
}).format(start),
|
|
events: [],
|
|
});
|
|
}
|
|
|
|
groups.get(key)?.events.push(event);
|
|
}
|
|
|
|
return Array.from(groups.values());
|
|
}
|
|
|
|
function createMessageElement(
|
|
documentRef: Document,
|
|
className: string,
|
|
message: string,
|
|
): HTMLParagraphElement {
|
|
const element = documentRef.createElement("p");
|
|
element.className = className;
|
|
element.textContent = message;
|
|
return element;
|
|
}
|
|
|
|
export function createSpecialEventsFragment(
|
|
documentRef: Document,
|
|
events: CalendarEvent[],
|
|
): DocumentFragment {
|
|
const fragment = documentRef.createDocumentFragment();
|
|
const specialEvents = getSpecialEvents(events);
|
|
|
|
if (!specialEvents.length) {
|
|
fragment.append(
|
|
createMessageElement(
|
|
documentRef,
|
|
"muted",
|
|
"Aktuell sind keine besonderen Termine geplant.",
|
|
),
|
|
);
|
|
return fragment;
|
|
}
|
|
|
|
const wrapper = documentRef.createElement("div");
|
|
wrapper.className = "hero-special-events";
|
|
|
|
const list = documentRef.createElement("ul");
|
|
const dateFormatter = new Intl.DateTimeFormat("de-DE", { dateStyle: "long" });
|
|
|
|
for (const event of specialEvents) {
|
|
const item = documentRef.createElement("li");
|
|
const title = documentRef.createElement("span");
|
|
title.className = "group-name";
|
|
title.textContent = event.title;
|
|
|
|
const date = documentRef.createElement("span");
|
|
date.textContent = dateFormatter.format(new Date(event.start));
|
|
|
|
item.append(title, date);
|
|
list.append(item);
|
|
}
|
|
|
|
const note = documentRef.createElement("p");
|
|
note.className = "calendar-note";
|
|
note.append("Alle Termine mit weiteren Details findest Du im ");
|
|
|
|
const link = documentRef.createElement("a");
|
|
link.href = "/kalender";
|
|
link.textContent = "Kalender";
|
|
|
|
note.append(link, ".");
|
|
|
|
wrapper.append(list, note);
|
|
fragment.append(wrapper);
|
|
return fragment;
|
|
}
|
|
|
|
export function createCalendarFragment(
|
|
documentRef: Document,
|
|
events: CalendarEvent[],
|
|
): DocumentFragment {
|
|
const fragment = documentRef.createDocumentFragment();
|
|
const groups = getCalendarGroups(events);
|
|
|
|
if (!groups.length) {
|
|
fragment.append(
|
|
createMessageElement(
|
|
documentRef,
|
|
"muted",
|
|
"In den nächsten 12 Monaten sind noch keine Termine geplant.",
|
|
),
|
|
);
|
|
return fragment;
|
|
}
|
|
|
|
const wrapper = documentRef.createElement("div");
|
|
wrapper.className = "calendar-table";
|
|
|
|
for (const group of groups) {
|
|
const month = documentRef.createElement("section");
|
|
month.className = "calendar-table-month";
|
|
month.setAttribute("aria-label", group.label);
|
|
|
|
const heading = documentRef.createElement("h2");
|
|
heading.className = "calendar-month-heading";
|
|
heading.textContent = group.label;
|
|
|
|
const table = documentRef.createElement("div");
|
|
table.className = "calendar-table-grid";
|
|
table.setAttribute("role", "table");
|
|
|
|
const headerRow = documentRef.createElement("div");
|
|
headerRow.className = "calendar-table-row calendar-table-row-head";
|
|
headerRow.setAttribute("role", "row");
|
|
|
|
const titleHeader = documentRef.createElement("span");
|
|
titleHeader.className = "calendar-table-title";
|
|
titleHeader.setAttribute("role", "columnheader");
|
|
titleHeader.textContent = "Termin";
|
|
|
|
const dateHeader = documentRef.createElement("span");
|
|
dateHeader.className = "calendar-table-date";
|
|
dateHeader.setAttribute("role", "columnheader");
|
|
dateHeader.textContent = "Datum & Uhrzeit";
|
|
|
|
headerRow.append(titleHeader, dateHeader);
|
|
table.append(headerRow);
|
|
|
|
for (const event of group.events) {
|
|
const row = documentRef.createElement("div");
|
|
row.className = "calendar-table-row";
|
|
row.setAttribute("role", "row");
|
|
|
|
const titleCell = documentRef.createElement("span");
|
|
titleCell.className = "calendar-table-title";
|
|
titleCell.setAttribute("role", "cell");
|
|
|
|
const titleWrapper = documentRef.createElement("span");
|
|
titleWrapper.className = "calendar-event-title";
|
|
|
|
if (event.special) {
|
|
const highlight = documentRef.createElement("span");
|
|
highlight.className = "calendar-highlight";
|
|
highlight.textContent = "★";
|
|
titleWrapper.append(highlight, " ");
|
|
}
|
|
|
|
titleWrapper.append(event.title);
|
|
titleCell.append(titleWrapper);
|
|
|
|
if (event.location) {
|
|
const location = documentRef.createElement("span");
|
|
location.className = "calendar-event-location";
|
|
location.textContent = `, ${event.location}`;
|
|
titleCell.append(location);
|
|
}
|
|
|
|
const dateCell = documentRef.createElement("span");
|
|
dateCell.className = "calendar-table-date";
|
|
dateCell.setAttribute("role", "cell");
|
|
dateCell.textContent = formatEventDateRange(event);
|
|
|
|
row.append(titleCell, dateCell);
|
|
table.append(row);
|
|
}
|
|
|
|
month.append(heading, table);
|
|
wrapper.append(month);
|
|
}
|
|
|
|
fragment.append(wrapper);
|
|
return fragment;
|
|
}
|