@@ -30,15 +30,49 @@ export const useIcalExport = (schedule: Schedule) => {
3030 eventTimezoneName : schedule . tz ,
3131 } )
3232 const { error, value } = createEvents (
33- Object . entries ( schedule . sessions ) . map (
34- ( [ timeWithTrack , name ] , i , sessions ) => {
33+ Object . entries ( schedule . sessions )
34+ . sort (
35+ ( [ timeWithTrackA ] , [ timeWithTrackB ] ) =>
36+ parseInt ( timeWithTrackA . split ( '@' ) [ 0 ] ) -
37+ parseInt ( timeWithTrackB . split ( '@' ) [ 0 ] ) ,
38+ )
39+ . map ( ( [ timeWithTrack , name ] , i , sessions ) => {
3540 const { sessionName, url } = formatSessionName ( name )
3641 const urlText = url === undefined ? undefined : url . toString ( )
3742
3843 const [ time , track ] = timeWithTrack . split ( '@' )
3944 const startTime = utcTime ( parseInt ( time , 10 ) )
4045
41- const next = sessions [ i + 1 ]
46+ // Find next entry for end time
47+ let next : [ string , string ] | undefined = undefined
48+ let nextStartTime : Date | undefined = undefined
49+
50+ if ( track === undefined ) {
51+ // This session has no track. Find next entry in all tracks. This entry is probably a Lunch break that is valid for all tracks.
52+ let j = i
53+ while (
54+ j < sessions . length - 1 &&
55+ ( nextStartTime ?. getTime ( ) ?? 0 ) <= startTime . getTime ( )
56+ ) {
57+ next = sessions [ ++ j ]
58+ const [ nextStartTimeString ] = next [ 0 ] . split ( '@' )
59+ nextStartTime = utcTime ( parseInt ( nextStartTimeString , 10 ) )
60+ }
61+ } else {
62+ let nextTrack : string | undefined = undefined
63+ // This session a track. Find next entry in the same track OR a without a track (e.g. a lunch break)
64+ let j = i
65+ while (
66+ j < sessions . length - 1 &&
67+ ( nextStartTime ?. getTime ( ) ?? 0 ) <= startTime . getTime ( ) &&
68+ ( nextTrack !== track || nextTrack === undefined )
69+ ) {
70+ next = sessions [ ++ j ]
71+ const [ nextStartTimeString , nextTrackString ] = next [ 0 ] . split ( '@' )
72+ nextTrack = nextTrackString
73+ nextStartTime = utcTime ( parseInt ( nextStartTimeString , 10 ) )
74+ }
75+ }
4276
4377 const description = [
4478 schedule . name ,
@@ -88,8 +122,7 @@ export const useIcalExport = (schedule: Schedule) => {
88122 location : track ,
89123 }
90124 }
91- } ,
92- ) ,
125+ } ) ,
93126 )
94127
95128 if ( value !== undefined ) {
@@ -108,6 +141,7 @@ export const useIcalExport = (schedule: Schedule) => {
108141 } , 0 )
109142 }
110143
111- if ( error !== undefined ) console . error ( `[iCalExport]` , error )
144+ if ( error !== undefined && error !== null )
145+ console . error ( `[iCalExport]` , error )
112146 }
113147}
0 commit comments