feat: add chart
This commit is contained in:
1 parent
2ed6c7fca9
commit
c67080a3ef
4 files changed
+160
-64
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import ical, { VEvent } from 'node-ical';
|
||||
import CategoryChart from '@/components/CategoryChart'; // <-- Importieren
|
||||
|
||||
interface PageProps {
|
||||
params: Promise<{
|
||||
@@ -15,7 +16,7 @@ function getIcalText(field: any): string {
|
||||
}
|
||||
|
||||
function getIcalDate(dateValue: any): Date {
|
||||
if (!dateValue) return new Date(0); // Fallback: 01.01.1970, falls kein Datum da ist
|
||||
if (!dateValue) return new Date(0);
|
||||
if (dateValue instanceof Date) return dateValue;
|
||||
return new Date(dateValue);
|
||||
}
|
||||
@@ -28,25 +29,20 @@ function getCategories(categoriesField: any): string[] {
|
||||
return [getIcalText(categoriesField)];
|
||||
}
|
||||
|
||||
type TimeMap = Record<string, number>
|
||||
type TimeMap = Record<string, number>;
|
||||
|
||||
export default async function EvaluationPage({ params }: PageProps) {
|
||||
let times: TimeMap = {};
|
||||
let total: number = 0;
|
||||
const times: TimeMap = {};
|
||||
let total = 0;
|
||||
|
||||
// get all params
|
||||
const resolvedParams = await params;
|
||||
const daysToFetch = Number(resolvedParams['days']) || 7;
|
||||
|
||||
// get amount of days to fetch
|
||||
const daysToFetch = Number(resolvedParams['days']) || 7; //fallback to 7 days
|
||||
|
||||
// get the URL from the own URL (/evaluation/<ics-link>)
|
||||
const pathSegments = resolvedParams['ical-link'];
|
||||
let rawUrl = pathSegments.join('/');
|
||||
rawUrl = decodeURIComponent(rawUrl);
|
||||
const icalUrl = rawUrl.replace(/^(https?):\/+/, '$1://');
|
||||
|
||||
// fetch the file
|
||||
const response = await fetch(icalUrl, { next: { revalidate: 3600 } });
|
||||
if (!response.ok) {
|
||||
return <div>Fehler beim Laden der Kalenderdaten. Status: {response.status}</div>;
|
||||
@@ -55,103 +51,109 @@ export default async function EvaluationPage({ params }: PageProps) {
|
||||
const icalData = await response.text();
|
||||
const parsedData = ical.sync.parseICS(icalData);
|
||||
|
||||
// calculate range of time
|
||||
const now = new Date();
|
||||
const cutoffDate = new Date();
|
||||
cutoffDate.setDate(now.getDate() - daysToFetch);
|
||||
|
||||
// filter and sort data (needed: "VEVENT"; time range)
|
||||
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()); // newest first
|
||||
|
||||
// read time per categorie
|
||||
events.map((event, index) => {
|
||||
.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 = (total || 0) + durationInMinutes
|
||||
})
|
||||
categories.forEach((cat) => {
|
||||
times[cat] = (times[cat] || 0) + durationInMinutes;
|
||||
});
|
||||
|
||||
total += durationInMinutes;
|
||||
});
|
||||
|
||||
return (
|
||||
<main>
|
||||
<section>
|
||||
<main style={{ maxWidth: '800px', margin: '0 auto', padding: '2rem', fontFamily: 'system-ui, sans-serif' }}>
|
||||
<section style={{ marginBottom: '2.5rem' }}>
|
||||
<h2>Gesamtdauer pro Kategorie</h2>
|
||||
{Object.keys(times).length === 0 ? (
|
||||
<p>Keine Zeiten erfasst.</p>
|
||||
) : (
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<td>Categorie</td>
|
||||
<td>Time</td>
|
||||
<td>Percentage</td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{Object.entries(times).map(([category, minutes]) => (
|
||||
<tr key={category}>
|
||||
<td>{category}</td>
|
||||
<td>{Math.floor(minutes / 60)} Std. {minutes % 60} Min. ({minutes} Min. gesamt)</td>
|
||||
<td>{Math.round(minutes / total *100 *10)/10}</td>
|
||||
<>
|
||||
<CategoryChart times={times} />
|
||||
|
||||
<table style={{ width: '100%', borderCollapse: 'collapse', textAlign: 'left', marginTop: '1.5rem' }}>
|
||||
<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>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
<tfoot>
|
||||
<tr>
|
||||
<td>Total</td>
|
||||
<td>{Math.floor(total / 60)} Std. {total % 60} Min. ({total} Min. gesamt)</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
</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' }}>
|
||||
{Math.floor(minutes / 60)} Std. {Math.round(minutes % 60)} Min. ({Math.round(minutes)} Min. gesamt)
|
||||
</td>
|
||||
<td style={{ padding: '0.5rem' }}>
|
||||
{total > 0 ? Math.round((minutes / total) * 1000) / 10 : 0}%
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
<tfoot>
|
||||
<tr style={{ fontWeight: 'bold' }}>
|
||||
<td style={{ padding: '0.5rem' }}>Total</td>
|
||||
<td style={{ padding: '0.5rem' }}>
|
||||
{Math.floor(total / 60)} Std. {Math.round(total % 60)} Min. ({Math.round(total)} Min. gesamt)
|
||||
</td>
|
||||
<td style={{ padding: '0.5rem' }}>100%</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
</>
|
||||
)}
|
||||
</section>
|
||||
|
||||
|
||||
<h2>Termine ({events.length})</h2>
|
||||
{events.length === 0 ? (
|
||||
<p>Keine Termine gefunden.</p>
|
||||
) : (
|
||||
<ul style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
|
||||
<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' }}>
|
||||
<h2>{getIcalText(event.summary)}</h2>
|
||||
<h3 style={{ margin: '0 0 0.5rem 0' }}>{getIcalText(event.summary)}</h3>
|
||||
<p>
|
||||
<strong>Start:</strong> {startDate.toLocaleString('de-DE')} <br />
|
||||
<strong>Ende:</strong> {endDate.toLocaleString('de-DE')}
|
||||
</p>
|
||||
{event.location && (
|
||||
<p><strong>Ort:</strong> {getIcalText(event.location)}</p>
|
||||
)}
|
||||
{event.description && (
|
||||
<p><strong>Beschreibung:</strong> {getIcalText(event.description)}</p>
|
||||
)}
|
||||
{event.location && <p><strong>Ort:</strong> {getIcalText(event.location)}</p>}
|
||||
{event.description && <p><strong>Beschreibung:</strong> {getIcalText(event.description)}</p>}
|
||||
{event.categories && (
|
||||
<div>
|
||||
<strong>Kategorien: </strong>
|
||||
{getCategories(event.categories).map((cat, i) => (
|
||||
<span
|
||||
key={i}
|
||||
style={{
|
||||
<span
|
||||
key={i}
|
||||
style={{
|
||||
display: 'inline-block',
|
||||
background: "white",
|
||||
color: "black",
|
||||
padding: '2px 8px',
|
||||
borderRadius: '4px',
|
||||
marginRight: '0.5rem'
|
||||
background: '#e5e7eb',
|
||||
color: '#111827',
|
||||
padding: '2px 8px',
|
||||
borderRadius: '4px',
|
||||
marginRight: '0.5rem',
|
||||
fontSize: '0.85rem',
|
||||
}}
|
||||
>
|
||||
{cat}
|
||||
|
||||
Reference in new issue
Block a user