import ical, { VEvent } from 'node-ical'; import CategoryChart from '@/components/CategoryChart'; interface PageProps { params: Promise<{ 'days': string; 'ical-link': string[]; }>; } function getIcalText(field: any): string { if (!field) return ''; if (typeof field === 'string') return field; if (typeof field === 'object' && 'val' in field) return field.val as string; return String(field); } function getIcalDate(dateValue: any): Date { if (!dateValue) return new Date(0); if (dateValue instanceof Date) return dateValue; return new Date(dateValue); } function getCategories(categoriesField: any): string[] { if (!categoriesField) return []; if (Array.isArray(categoriesField)) { return categoriesField.map((cat) => getIcalText(cat)); } return [getIcalText(categoriesField)]; } type TimeMap = Record; export default async function EvaluationPage({ params }: PageProps) { const times: TimeMap = {}; let total = 0; const resolvedParams = await params; const daysToFetch = Number(resolvedParams['days']) || 7; const pathSegments = resolvedParams['ical-link']; let rawUrl = pathSegments.join('/'); rawUrl = decodeURIComponent(rawUrl); const icalUrl = rawUrl.replace(/^(https?):\/+/, '$1://'); const response = await fetch(icalUrl, { next: { revalidate: 3600 } }); if (!response.ok) { return (
Fehler beim Laden der Kalenderdaten. Status: {response.status}
); } const icalData = await response.text(); const parsedData = ical.sync.parseICS(icalData); const now = new Date(); const cutoffDate = new Date(); cutoffDate.setDate(now.getDate() - daysToFetch); const events = Object.values(parsedData) .filter((item): item is VEvent => item !== undefined && item.type === 'VEVENT') .filter((event) => { const startDate = getIcalDate(event.start); return startDate >= cutoffDate && startDate <= now; }) .sort((a, b) => getIcalDate(b.start).getTime() - getIcalDate(a.start).getTime()); events.forEach((event) => { const startDate = getIcalDate(event.start); const endDate = getIcalDate(event.end); const durationInMs = endDate.getTime() - startDate.getTime(); const durationInMinutes = durationInMs / (1000 * 60); const categories = getCategories(event.categories); categories.forEach((cat) => { times[cat] = (times[cat] || 0) + durationInMinutes; }); total += durationInMinutes; }); return (

Gesamtdauer pro Kategorie

{Object.keys(times).length === 0 ? (

Keine Zeiten erfasst.

) : ( <> {/* Responsive Container für die Tabelle */}
{Object.entries(times).map(([category, minutes]) => ( ))}
Kategorie Dauer Anteil Insg. Anteil
{category} {Math.floor(minutes / 60)} Std. {Math.round(minutes % 60)} Min. ({Math.round(minutes)} Min. gesamt) {total > 0 ? Math.round((minutes / total) * 1000) / 10 : 0}% {total > 0 ? Math.round((minutes / (daysToFetch * 24 * 60)) * 1000) / 10 : 0}%
Total {Math.floor(total / 60)} Std. {Math.round(total % 60)} Min. ({Math.round(total)} Min. gesamt) 100% {total > 0 ? Math.round((total / (daysToFetch * 24 * 60)) * 1000) / 10 : 0}%
)}

Termine ({events.length})

{events.length === 0 ? (

Keine Termine gefunden.

) : ( )}
); }