From 11487cc18640b23d8a39ef2bd5b74d2bc6c5b014 Mon Sep 17 00:00:00 2001 From: Markus Tacker Date: Wed, 24 Aug 2022 14:27:17 +0200 Subject: [PATCH] fix: update iCal export for tracks --- src/useIcalExport.ts | 46 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/src/useIcalExport.ts b/src/useIcalExport.ts index f91d94fe..abcb1fc2 100644 --- a/src/useIcalExport.ts +++ b/src/useIcalExport.ts @@ -30,15 +30,49 @@ export const useIcalExport = (schedule: Schedule) => { eventTimezoneName: schedule.tz, }) const { error, value } = createEvents( - Object.entries(schedule.sessions).map( - ([timeWithTrack, name], i, sessions) => { + Object.entries(schedule.sessions) + .sort( + ([timeWithTrackA], [timeWithTrackB]) => + parseInt(timeWithTrackA.split('@')[0]) - + parseInt(timeWithTrackB.split('@')[0]), + ) + .map(([timeWithTrack, name], i, sessions) => { const { sessionName, url } = formatSessionName(name) const urlText = url === undefined ? undefined : url.toString() const [time, track] = timeWithTrack.split('@') const startTime = utcTime(parseInt(time, 10)) - const next = sessions[i + 1] + // Find next entry for end time + let next: [string, string] | undefined = undefined + let nextStartTime: Date | undefined = undefined + + if (track === undefined) { + // This session has no track. Find next entry in all tracks. This entry is probably a Lunch break that is valid for all tracks. + let j = i + while ( + j < sessions.length - 1 && + (nextStartTime?.getTime() ?? 0) <= startTime.getTime() + ) { + next = sessions[++j] + const [nextStartTimeString] = next[0].split('@') + nextStartTime = utcTime(parseInt(nextStartTimeString, 10)) + } + } else { + let nextTrack: string | undefined = undefined + // This session a track. Find next entry in the same track OR a without a track (e.g. a lunch break) + let j = i + while ( + j < sessions.length - 1 && + (nextStartTime?.getTime() ?? 0) <= startTime.getTime() && + (nextTrack !== track || nextTrack === undefined) + ) { + next = sessions[++j] + const [nextStartTimeString, nextTrackString] = next[0].split('@') + nextTrack = nextTrackString + nextStartTime = utcTime(parseInt(nextStartTimeString, 10)) + } + } const description = [ schedule.name, @@ -88,8 +122,7 @@ export const useIcalExport = (schedule: Schedule) => { location: track, } } - }, - ), + }), ) if (value !== undefined) { @@ -108,6 +141,7 @@ export const useIcalExport = (schedule: Schedule) => { }, 0) } - if (error !== undefined) console.error(`[iCalExport]`, error) + if (error !== undefined && error !== null) + console.error(`[iCalExport]`, error) } }