|
| 1 | +import { getTranslation, useTranslationTable } from "@/locales"; |
| 2 | +import styles from "@/styles/Timetable.module.scss"; |
| 3 | +import { ICBDActivity } from "@/types/aliases"; |
| 4 | +import { useRouter } from "next/router"; |
| 5 | + |
| 6 | +type Timeslot = { |
| 7 | + start_time: string; |
| 8 | + end_time: string; |
| 9 | + room: string; |
| 10 | +}; |
| 11 | + |
| 12 | +// Convert string representation "hh:mm" to minutes |
| 13 | +function timeToMinutes(time: string) { |
| 14 | + const [hours, minutes, seconds = 0] = time.split(":").map(Number); |
| 15 | + return hours * 60 + minutes + seconds / 60; |
| 16 | +} |
| 17 | + |
| 18 | +function Hours(props: { startTime: string; endTime: string }) { |
| 19 | + let startHour = timeToMinutes(props.startTime) / 60; |
| 20 | + const endHour = timeToMinutes(props.endTime) / 60; |
| 21 | + const numberOfHours = endHour - startHour; |
| 22 | + |
| 23 | + let hours: any[] = []; |
| 24 | + |
| 25 | + for (; startHour <= endHour; startHour++) { |
| 26 | + let style = |
| 27 | + startHour == endHour ? {} : { height: `${100 / numberOfHours}%` }; |
| 28 | + hours.push( |
| 29 | + <div key={startHour} style={style} className={styles.hourEntry}> |
| 30 | + <span className={styles.line} /> |
| 31 | + <p>{`${startHour}h`}</p> |
| 32 | + </div> |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + return <>{hours}</>; |
| 37 | +} |
| 38 | + |
| 39 | +function TimeTableEvent(props: { |
| 40 | + timeslot: Timeslot; |
| 41 | + activity: ICBDActivity; |
| 42 | + startTime: string; |
| 43 | + endTime: string; |
| 44 | +}) { |
| 45 | + // In minutes |
| 46 | + const startTime = timeToMinutes(props.startTime); |
| 47 | + const endTime = timeToMinutes(props.endTime); |
| 48 | + const timeslot = props.timeslot; |
| 49 | + const tStartTime = timeToMinutes(timeslot.start_time); |
| 50 | + const tEndTime = timeToMinutes(timeslot.end_time); |
| 51 | + |
| 52 | + const duration = endTime - startTime; |
| 53 | + |
| 54 | + const size = (tEndTime - tStartTime) / duration; |
| 55 | + |
| 56 | + const position = (tStartTime - startTime) / duration; |
| 57 | + |
| 58 | + const style = { height: `calc(${size * 100}% - 0.4rem)` }; |
| 59 | + |
| 60 | + const router = useRouter(); |
| 61 | + |
| 62 | + const translation = getTranslation(props.activity, router.locale); |
| 63 | + |
| 64 | + return ( |
| 65 | + <div |
| 66 | + className={styles.event} |
| 67 | + style={{ |
| 68 | + top: `${position * 100}%`, |
| 69 | + backgroundColor: props.activity.color || "orange", |
| 70 | + ...style, |
| 71 | + }} |
| 72 | + > |
| 73 | + <p>{translation.name || ""}</p> |
| 74 | + </div> |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +function TimetableEntry(props: { |
| 79 | + timeslots: [Timeslot, ICBDActivity][]; |
| 80 | + startTime: string; |
| 81 | + endTime: string; |
| 82 | +}) { |
| 83 | + let key = 0; |
| 84 | + |
| 85 | + return ( |
| 86 | + <div className={`${styles.entry} ${styles.vertical}`}> |
| 87 | + {props.timeslots.map((t) => { |
| 88 | + return ( |
| 89 | + <TimeTableEvent |
| 90 | + key={key++} |
| 91 | + startTime={props.startTime} |
| 92 | + endTime={props.endTime} |
| 93 | + timeslot={t[0]} |
| 94 | + activity={t[1]} |
| 95 | + /> |
| 96 | + ); |
| 97 | + })} |
| 98 | + </div> |
| 99 | + ); |
| 100 | +} |
| 101 | + |
| 102 | +export function Timetable(props: { |
| 103 | + activities: ICBDActivity[]; |
| 104 | + startTime: string; |
| 105 | + endTime: string; |
| 106 | +}) { |
| 107 | + let rooms: Record<string, [Timeslot, ICBDActivity][]> = {}; |
| 108 | + |
| 109 | + let tt = useTranslationTable(); |
| 110 | + |
| 111 | + props.activities.forEach((activity) => { |
| 112 | + const timeslots: Timeslot[] = JSON.parse( |
| 113 | + JSON.stringify(activity.timeslots) |
| 114 | + ); |
| 115 | + |
| 116 | + timeslots.forEach((timeslot) => { |
| 117 | + let groupKey = timeslot.room; |
| 118 | + if (!rooms[groupKey]) { |
| 119 | + rooms[groupKey] = []; |
| 120 | + } |
| 121 | + rooms[groupKey].push([timeslot, activity]); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + return ( |
| 126 | + <> |
| 127 | + <table className={styles.table}> |
| 128 | + <tbody> |
| 129 | + <tr> |
| 130 | + <th /> |
| 131 | + {Object.keys(rooms).map((room) => ( |
| 132 | + <th className={styles.header} key={"header|" + room}> |
| 133 | + <p>{room}</p> |
| 134 | + </th> |
| 135 | + ))} |
| 136 | + </tr> |
| 137 | + <tr> |
| 138 | + <td className={styles.hours}> |
| 139 | + <Hours startTime={props.startTime} endTime={props.endTime} /> |
| 140 | + </td> |
| 141 | + {Object.keys(rooms).map((room) => ( |
| 142 | + <td key={"entry|" + room}> |
| 143 | + <TimetableEntry |
| 144 | + timeslots={rooms[room]} |
| 145 | + startTime={props.startTime} |
| 146 | + endTime={props.endTime} |
| 147 | + /> |
| 148 | + </td> |
| 149 | + ))} |
| 150 | + </tr> |
| 151 | + </tbody> |
| 152 | + </table> |
| 153 | + <p>{`${tt["icbd-end-of-event"]} ${props.endTime}`}</p> |
| 154 | + </> |
| 155 | + ); |
| 156 | +} |
0 commit comments