225 lines
8.2 KiB
TypeScript
225 lines
8.2 KiB
TypeScript
import ical, { VEvent } from 'node-ical';
|
|
import CategoryChart from '@/components/CategoryChart';
|
|
|
|
interface PageProps {
|
|
searchParams: Promise<{
|
|
'days'?: string;
|
|
'ical-link'?: string | 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<string, number>;
|
|
|
|
export default async function EvaluationPage({ searchParams }: PageProps) {
|
|
const times: TimeMap = {};
|
|
let total = 0;
|
|
|
|
const resolvedSearchParams = await searchParams;
|
|
const daysToFetch = Number(resolvedSearchParams['days']) || 7;
|
|
|
|
const rawUrl = resolvedSearchParams['ical-link'];
|
|
|
|
if (!rawUrl) {
|
|
return (
|
|
<div style={{ padding: '1rem', color: '#dc2626', fontFamily: 'system-ui, sans-serif' }}>
|
|
Fehler: Kein iCal-Link übergeben. Bitte verwende den Query-Parameter ?ical-link=...
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const singleUrl = Array.isArray(rawUrl) ? rawUrl[0] : rawUrl;
|
|
const icalUrl = decodeURIComponent(singleUrl);
|
|
|
|
const response = await fetch(icalUrl, { next: { revalidate: 3600 } });
|
|
if (!response.ok) {
|
|
return (
|
|
<div style={{ padding: '1rem', color: '#dc2626', fontFamily: 'system-ui, sans-serif' }}>
|
|
Fehler beim Laden der Kalenderdaten. Status: {response.status}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
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 (
|
|
<main
|
|
style={{
|
|
width: '100%',
|
|
maxWidth: '800px',
|
|
margin: '0 auto',
|
|
padding: '1rem',
|
|
boxSizing: 'border-box',
|
|
fontFamily: 'system-ui, sans-serif',
|
|
}}
|
|
>
|
|
<section style={{ marginBottom: '2.5rem' }}>
|
|
<h2 style={{ fontSize: '1.25rem', fontWeight: 600, marginBottom: '1rem' }}>Gesamtdauer pro Kategorie</h2>
|
|
{Object.keys(times).length === 0 ? (
|
|
<p>Keine Zeiten erfasst.</p>
|
|
) : (
|
|
<>
|
|
<CategoryChart times={times} />
|
|
|
|
{/* Responsive Container für die Tabelle */}
|
|
<div style={{ width: '100%', overflowX: 'auto', marginTop: '1.5rem', WebkitOverflowScrolling: 'touch' }}>
|
|
<table style={{ width: '100%', minWidth: '480px', borderCollapse: 'collapse', textAlign: 'left' }}>
|
|
<thead>
|
|
<tr style={{ borderBottom: '2px solid #e5e7eb' }}>
|
|
<th style={{ padding: '0.5rem' }}>Kategorie</th>
|
|
<th style={{ padding: '0.5rem' }}>Dauer</th>
|
|
<th style={{ padding: '0.5rem' }}>Anteil</th>
|
|
<th style={{ padding: '0.5rem' }}>Insg. Anteil</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{Object.entries(times).map(([category, minutes]) => (
|
|
<tr key={category} style={{ borderBottom: '1px solid #e5e7eb' }}>
|
|
<td style={{ padding: '0.5rem' }}>{category}</td>
|
|
<td style={{ padding: '0.5rem', whiteSpace: 'nowrap' }}>
|
|
{Math.floor(minutes / 60)}:{Math.round(minutes % 60)}h (={Math.round(minutes)} min)
|
|
</td>
|
|
<td style={{ padding: '0.5rem' }}>
|
|
{total > 0 ? Math.round((minutes / total) * 1000) / 10 : 0}%
|
|
</td>
|
|
<td style={{ padding: '0.5rem' }}>
|
|
{total > 0 ? Math.round((minutes / (daysToFetch * 24 * 60)) * 1000) / 10 : 0}%
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
<tfoot>
|
|
<tr style={{ fontWeight: 'bold' }}>
|
|
<td style={{ padding: '0.5rem' }}>Total</td>
|
|
<td style={{ padding: '0.5rem', whiteSpace: 'nowrap' }}>
|
|
{Math.floor(total / 60)}:{Math.round(total % 60)}h (={Math.round(total)} min)
|
|
</td>
|
|
<td style={{ padding: '0.5rem' }}>100%</td>
|
|
<td style={{ padding: '0.5rem' }}>
|
|
{total > 0 ? Math.round((total / (daysToFetch * 24 * 60)) * 1000) / 10 : 0}%
|
|
</td>
|
|
</tr>
|
|
</tfoot>
|
|
</table>
|
|
</div>
|
|
</>
|
|
)}
|
|
</section>
|
|
|
|
<h2 style={{ fontSize: '1.25rem', fontWeight: 600, marginBottom: '1rem' }}>Termine ({events.length})</h2>
|
|
{events.length === 0 ? (
|
|
<p>Keine Termine gefunden.</p>
|
|
) : (
|
|
<ul style={{ display: 'flex', flexDirection: 'column', gap: '1rem', padding: 0, listStyle: 'none' }}>
|
|
{events.map((event, index) => {
|
|
const startDate = getIcalDate(event.start);
|
|
const endDate = getIcalDate(event.end);
|
|
|
|
return (
|
|
<li
|
|
key={event.uid || index}
|
|
style={{
|
|
border: '1px solid #ccc',
|
|
padding: '1rem',
|
|
borderRadius: '8px',
|
|
wordBreak: 'break-word', // Verhindert Überlaufen von sehr langem Text/URLs
|
|
}}
|
|
>
|
|
<h3 style={{ margin: '0 0 0.5rem 0', wordBreak: 'break-word' }}>{getIcalText(event.summary)}</h3>
|
|
<p style={{ margin: '0 0 0.5rem 0' }}>
|
|
<strong>Start:</strong> {startDate.toLocaleString('de-DE')} <br />
|
|
<strong>Ende:</strong> {endDate.toLocaleString('de-DE')}
|
|
</p>
|
|
{event.location && (
|
|
<p style={{ margin: '0 0 0.5rem 0' }}>
|
|
<strong>Ort:</strong> {getIcalText(event.location)}
|
|
</p>
|
|
)}
|
|
{event.description && (
|
|
<p style={{ margin: '0 0 0.5rem 0', whiteSpace: 'pre-line' }}>
|
|
<strong>Beschreibung:</strong> {getIcalText(event.description)}
|
|
</p>
|
|
)}
|
|
{event.categories && (
|
|
<div
|
|
style={{
|
|
display: 'flex',
|
|
flexWrap: 'wrap',
|
|
alignItems: 'center',
|
|
gap: '0.35rem',
|
|
marginTop: '0.5rem',
|
|
}}
|
|
>
|
|
<strong>Kategorien: </strong>
|
|
{getCategories(event.categories).map((cat, i) => (
|
|
<span
|
|
key={i}
|
|
style={{
|
|
display: 'inline-block',
|
|
background: '#white',
|
|
border: '1px solid #e5e7eb',
|
|
padding: '2px 8px',
|
|
borderRadius: '4px',
|
|
fontSize: '0.85rem',
|
|
}}
|
|
>
|
|
{cat}
|
|
</span>
|
|
))}
|
|
</div>
|
|
)}
|
|
</li>
|
|
);
|
|
})}
|
|
</ul>
|
|
)}
|
|
</main>
|
|
);
|
|
} |