// ── Kalender Widget mit iCal-Unterstützung ──────────────────────────────────── import { useState, useEffect, useMemo } from 'react'; import { api, S } from './lib.js'; // iCal-Parser – unterstützt gefaltete Zeilen, TZID, VALUE=DATE function parseIcal(raw) { // 1. Gefaltete Zeilen (RFC 5545) zusammenführen const text = raw.replace(/\r\n[ \t]/g, '').replace(/\r/g, ''); // 2. Alle Properties als Map extrahieren function getProps(block) { // VALARM-Blöcke entfernen (haben eigene SUMMARY/DESCRIPTION die nicht zum Termin gehören) const stripped = block.replace(/BEGIN:VALARM[\s\S]*?END:VALARM/g, ''); const props = {}; for (const line of stripped.split('\n')) { const col = line.indexOf(':'); if (col < 0) continue; const namepart = line.slice(0, col).toUpperCase(); const val = line.slice(col + 1).trim(); // Basisname ohne Parameter const name = namepart.split(';')[0]; // TZID-Parameter extrahieren const tzidM = namepart.match(/TZID=([^;:]+)/); props[name] = { val, raw: namepart, tzid: tzidM?.[1] || null }; } return props; } // 3. Datum parsen – Ortszeit wenn TZID gesetzt, UTC wenn Z-Suffix function parseDate(prop, isEnd) { if (!prop) return null; const s = prop.val; if (!s) return null; const allDay = s.length === 8 || prop.raw.includes('VALUE=DATE'); if (allDay) { // DTEND bei all-day ist exklusiv → einen Tag abziehen const d = new Date(+s.slice(0,4), +s.slice(4,6)-1, +s.slice(6,8)); if (isEnd) d.setDate(d.getDate() - 1); return { date: d, allDay: true }; } const utc = s.endsWith('Z'); const y=+s.slice(0,4), mo=+s.slice(4,6)-1, d=+s.slice(6,8); const h=+s.slice(9,11)||0, mi=+s.slice(11,13)||0, sec=+s.slice(13,15)||0; const date = utc ? new Date(Date.UTC(y,mo,d,h,mi,sec)) : new Date(y,mo,d,h,mi,sec); // Ortszeit des Browsers (beste Annäherung) return { date, allDay: false }; } const events = []; const blocks = text.split('BEGIN:VEVENT'); for (let i = 1; i < blocks.length; i++) { const end = blocks[i].indexOf('END:VEVENT'); const block = end >= 0 ? blocks[i].slice(0, end) : blocks[i]; const p = getProps(block); const startObj = parseDate(p['DTSTART']); const endObj = parseDate(p['DTEND'], true) || startObj; if (!startObj || isNaN(startObj.date)) continue; events.push({ summary: (p['SUMMARY']?.val || '(kein Titel)').replace(/\\n/g,'\n').replace(/\\,/g,','), location: (p['LOCATION']?.val || '').replace(/\\n/g,'\n').replace(/\\,/g,','), dtstart: startObj.date, dtend: endObj.date, allDay: startObj.allDay, }); } return events; } function sameDay(a, b) { return a.getFullYear()===b.getFullYear() && a.getMonth()===b.getMonth() && a.getDate()===b.getDate(); } function inRange(event, day) { const d = new Date(day); d.setHours(0,0,0,0); const s = new Date(event.dtstart); s.setHours(0,0,0,0); const e = new Date(event.dtend); e.setHours(0,0,0,0); return d >= s && d <= e; } const WEEKDAYS = ['Mo','Di','Mi','Do','Fr','Sa','So']; const MONTHS = ['Januar','Februar','März','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember']; export default function CalendarWidget() { const [isOpen, setIsOpen] = useState(false); const [today] = useState(new Date()); const [cur, setCur] = useState({ y: today.getFullYear(), m: today.getMonth() }); const [events, setEvents] = useState([]); // Feeds immer frisch aus localStorage lesen const getFeeds = () => { try { return JSON.parse(localStorage.getItem('dd_cal_feeds') || '[]'); } catch { return []; } }; const [feedCount, setFeedCount] = useState(0); // trigger re-render const [loading, setLoading] = useState(false); const [selected, setSelected] = useState(new Date()); // default: heute const [errors, setErrors] = useState([]); // Load all feeds const loadFeeds = async (feedList) => { if (!feedList.length) { setEvents([]); setDebug(['Keine Feeds konfiguriert']); return; } setLoading(true); setErrors([]); const all = []; const errs = []; for (const f of feedList) { try { const res = await fetch(`/api/calendar/fetch?url=${encodeURIComponent(f.url)}`, { headers: { Authorization: `Bearer ${localStorage.getItem('sk_token')}` } }); if (res.ok) { const text = await res.text(); const evs = parseIcal(text).map(e => ({ ...e, color: f.color, feedName: f.name })); all.push(...evs); } else { const d = await res.json().catch(()=>({error:'Unbekannter Fehler'})); errs.push(`${f.name}: ${d.error}`); } } catch(e) { errs.push(`${f.name}: ${e.message}`); } } setEvents(all); setErrors(errs); setLoading(false); }; const refresh = () => { const f = getFeeds(); if (f.length > 0) loadFeeds(f); setFeedCount(f.length); }; useEffect(() => { if (isOpen) refresh(); }, [isOpen]); // Nächster oder aktueller Termin (für zugeklappte Ansicht) const nextEvent = useMemo(() => { const now = new Date(); const upcoming = events .filter(e => e.dtend >= now) .sort((a,b) => a.dtstart - b.dtstart); return upcoming[0] || null; }, [events]); // Calendar grid const days = useMemo(() => { const first = new Date(cur.y, cur.m, 1); const dow = (first.getDay() + 6) % 7; // 0=Mo const total = new Date(cur.y, cur.m+1, 0).getDate(); const grid = []; for (let i = 0; i < dow; i++) grid.push(null); for (let d = 1; d <= total; d++) grid.push(new Date(cur.y, cur.m, d)); return grid; }, [cur]); const eventsOnDay = (day) => day ? events.filter(e => inRange(e, day)) : []; const selectedEvents = selected ? events.filter(e => inRange(e, selected)) : []; const prevMonth = () => setCur(p => p.m === 0 ? { y:p.y-1, m:11 } : { y:p.y, m:p.m-1 }); const nextMonth = () => setCur(p => p.m === 11 ? { y:p.y+1, m:0 } : { y:p.y, m:p.m+1 }); return (