forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

today-lessons.ts44 lines, 1.8 KiBHistoryRawDownload
1import type { CourseSession, Instance } from "@/api/client";
2
3export type RollCallState = "not-taken" | "partial" | "done" | "upcoming" | "in-progress";
4
5export type TodayLesson = {
6 session: CourseSession;
7 instance: Instance;
8 /** Roll-call rows saved for the session; null when the read failed. */
9 marked: number | null;
10 state: RollCallState | null;
11};
12
13/** Local-day bounds [start, end) around `now`, in unix milliseconds. */
14export function dayBounds(now: number): [number, number] {
15 const start = new Date(now);
16 start.setHours(0, 0, 0, 0);
17 const end = new Date(start);
18 end.setDate(end.getDate() + 1);
19 return [start.getTime(), end.getTime()];
20}
21
22/** Sessions that start today, earliest first. */
23export function sessionsToday<T extends { starts_at: number }>(sessions: T[], now: number): T[] {
24 const [start, end] = dayBounds(now);
25 return sessions.filter((session) => session.starts_at >= start && session.starts_at < end).sort((a, b) => a.starts_at - b.starts_at);
26}
27
28/**
29 * What the teacher still owes a lesson. A lesson that has not started yet is
30 * "upcoming" whatever its rows say; after the start, no rows is "not taken",
31 * fewer rows than enrolled students is "partial". Rows can include the
32 * teacher's own presence, so "done" is at least the roster size.
33 */
34export function rollCallState(startsAt: number, now: number, marked: number | null, enrolled: number): RollCallState | null {
35 if (startsAt > now) return "upcoming";
36 if (marked == null) return null;
37 if (marked === 0) return "not-taken";
38 return marked >= enrolled ? "done" : "partial";
39}
40
41/** A student's view of a lesson: only whether it is on right now. */
42export function lessonNow(startsAt: number, endsAt: number | null, now: number): "in-progress" | null {
43 return startsAt <= now && endsAt != null && now < endsAt ? "in-progress" : null;
44}