Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
],
"dependencies": {
"@nbtca/docs": "^0.3.5",
"@nbtca/nbtcal": "^0.4.6",
"@nbtca/nbtcal": "^0.4.7",
"chalk": "^5.6.2",
"marked": "^15.0.12"
},
Expand Down
17 changes: 9 additions & 8 deletions src/app/views/schedule-grid-cursor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
createTimetableSchedule,
type Timetable,
type TimetableMeeting,
type TimetablePeriod,
type TimetableSchedule,
type Weekday,
import type {
Timetable,
TimetableMeeting,
TimetablePeriod,
TimetableSchedule,
Weekday,
} from '@nbtca/nbtcal/timetable';
import { termSchedule } from '../../features/school-days.js';

export interface GridCursor {
weekday: Weekday;
Expand Down Expand Up @@ -77,6 +77,7 @@ export function handleGridKey(
cursor: GridCursor,
tt: Timetable,
week: number,
weekOne?: string,
): GridKeyResult {
if (key === KEY_ARROW_LEFT) return { kind: 'moveCursor', cursor: moveCursorWeekday(cursor, -1) };
if (key === KEY_ARROW_RIGHT) return { kind: 'moveCursor', cursor: moveCursorWeekday(cursor, 1) };
Expand All @@ -85,7 +86,7 @@ export function handleGridKey(
if (key === KEY_ARROW_DOWN)
return { kind: 'moveCursor', cursor: moveCursorPeriod(cursor, tt.periods, 1) };
if (key === KEY_ENTER_CR || key === KEY_ENTER_LF) {
const meeting = createTimetableSchedule(tt).meetingAt(week, cursor.weekday, cursor.period);
const meeting = termSchedule(tt, weekOne).meetingAt(week, cursor.weekday, cursor.period);
return meeting ? { kind: 'openDetail', meeting } : { kind: 'emptyCell' };
}
return { kind: 'none' };
Expand Down
31 changes: 10 additions & 21 deletions src/app/views/schedule-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ import type { AcademicWindow, OnBreak } from '@nbtca/nbtcal';
import type { GridCursor } from './schedule-grid-cursor.js';
import { renderRecapCard, type RecapCard } from './term-recap-render.js';
import { courseProgress } from '../../features/course-progress.js';
import { peekSchoolCalendar } from '../../features/calendar.js';
import { schoolDays, type SchoolDays } from '../../features/school-days.js';
import { cachedSchoolDays, termSchedule } from '../../features/school-days.js';
import type { StudentExam } from '@nbtca/nbtcal/academic';
import type { TermRecap } from '../../features/term-recap.js';
import { getCapabilities } from '../../core/capabilities.js';
Expand All @@ -43,7 +42,7 @@ import {
visualWidth,
wrapAnsiWithIndent,
} from '../../core/text.js';
import { addLocalDays, isoDayDifference, parseLocalMonday } from '../../core/calendar-day.js';
import { isoDayDifference } from '../../core/calendar-day.js';
import { loadingLines } from '../../core/components/spinner.js';
import { loadingStateLines } from '../../core/components/empty-state.js';
import { offlineNotice } from '../chrome.js';
Expand Down Expand Up @@ -191,7 +190,7 @@ function hubPreGridLines(
const trans = t();
const tt = state.timetable;
if (!tt || !state.weekOne) return null;
const schedule = createTimetableSchedule(tt, { weekOneMonday: state.weekOne });
const schedule = termSchedule(tt, state.weekOne);
const week = schedule.weekAt(now);
const lines: string[] = [];
let next = null;
Expand Down Expand Up @@ -251,6 +250,7 @@ function fittingWeekGrid(

function renderDayFallback(
tt: Timetable,
weekOne: string | undefined,
week: number,
todayWd: Weekday,
now: Date,
Expand All @@ -261,7 +261,7 @@ function renderDayFallback(
return [
renderDaySwitcher(selectedWd, todayWd, cols),
...renderDayTimeline(
createTimetableSchedule(tt).meetingsOnDay(week, selectedWd),
termSchedule(tt, weekOne).meetingsOnDay(week, selectedWd),
tt.periods,
now,
{ weekday: selectedWd, isToday: selectedWd === todayWd },
Expand Down Expand Up @@ -313,7 +313,7 @@ function renderHubWeek(
}
}
return {
lines: [...fallbackLines, ...renderDayFallback(tt, week, todayWd, now, cols, cursor)],
lines: [...fallbackLines, ...renderDayFallback(tt, weekOne, week, todayWd, now, cols, cursor)],
grid: false,
};
}
Expand Down Expand Up @@ -345,7 +345,7 @@ function renderWeekBody(
if (dense && dense.length <= rows) return dense;
return [
...hintLines(t().timetable.weekTooSmall, cols),
...renderDayFallback(tt, week, todayWd, now, cols, cursor),
...renderDayFallback(tt, weekOne, week, todayWd, now, cols, cursor),
];
}

Expand Down Expand Up @@ -549,15 +549,6 @@ function renderPublicBody(
: lines;
}

const TERM_SPAN_DAYS = 26 * 7;

function termSchoolDays(weekOne: string): SchoolDays | undefined {
const calendar = peekSchoolCalendar();
if (!calendar) return undefined;
const start = parseLocalMonday(weekOne);
return schoolDays(calendar, start, addLocalDays(start, TERM_SPAN_DAYS));
}

/** The course's exam still to come, else its latest one; matched by course code, then by name. */
export function examFor(
exams: readonly StudentExam[] | undefined,
Expand Down Expand Up @@ -608,9 +599,7 @@ export function renderSchedule(
return renderHubBody(state, now, bodyRows, cols);
case 'week': {
if (!state.timetable || !state.weekOne) return [hint(trans.timetable.genericError)];
const schedule = createTimetableSchedule(state.timetable, {
weekOneMonday: state.weekOne,
});
const schedule = termSchedule(state.timetable, state.weekOne);
const currentWeek = Math.max(1, schedule.weekAt(now));
return renderWeekBody(
state.timetable,
Expand All @@ -629,7 +618,7 @@ export function renderSchedule(
return renderTermDensity(
state.timetable.meetings,
state.weekOne,
createTimetableSchedule(state.timetable, { weekOneMonday: state.weekOne }).weekAt(now),
termSchedule(state.timetable, state.weekOne).weekAt(now),
cols,
).split('\n');
case 'termPicker': {
Expand Down Expand Up @@ -669,7 +658,7 @@ export function renderSchedule(
state.weekOne,
state.detailMeeting,
now,
termSchoolDays(state.weekOne),
cachedSchoolDays(state.weekOne),
)
: undefined,
examFor(state.exams, state.detailMeeting, now),
Expand Down
37 changes: 22 additions & 15 deletions src/app/views/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { startConfirmation } from '../../core/motion.js';
import path from 'node:path';
import {
createNbtTimetableClient,
createTimetableSchedule,
timetableToIcs,
type AcademicTerm,
type NbtTimetableClient,
Expand Down Expand Up @@ -64,6 +63,7 @@ import {
peekSchoolCalendar,
} from '../../features/calendar.js';
import { createStudentRecordsClient } from '../../features/student-records-client.js';
import { cachedSchoolDays, termSchedule } from '../../features/school-days.js';
import {
currentAcademicWindow,
inferWeekOneMonday,
Expand Down Expand Up @@ -187,7 +187,7 @@ function returnToHub(): boolean {
const backKey = state.key;
const backWeekOne = state.weekOne;
if (tt && backKey && backWeekOne) {
const schedule = createTimetableSchedule(tt, { weekOneMonday: backWeekOne });
const schedule = termSchedule(tt, backWeekOne);
state = {
mode: 'hub',
key: backKey,
Expand Down Expand Up @@ -338,7 +338,7 @@ async function fetchAndShowHub(
await client.fetchTerm(term, ctx.signal === undefined ? {} : { signal: ctx.signal }),
);
if (!isLifecycleActive(ctx, generation)) return;
const schedule = createTimetableSchedule(timetable, { weekOneMonday: weekOne });
const schedule = termSchedule(timetable, weekOne);
saveTimetableCache(key, timetable);
saveCurrentPointer(key, weekOne);
state = {
Expand Down Expand Up @@ -466,15 +466,19 @@ async function openStudentRecords(
let examsRequested: string | undefined;

/** Fetches this term's exams once per term so the detail page can show a course's exam; failures stay silent. */
/** Fetches the school calendar so days off and make-up days apply; the timetable shows meanwhile. */
function refreshSchoolDays(ctx: AppContext): void {
const generation = lifecycleGeneration;
const before = peekSchoolCalendar();
void loadSchoolCalendarOrCache(ctx.signal)
.then(({ calendar }) => {
if (calendar !== before && isLifecycleActive(ctx, generation)) ctx.rerender();
})
.catch(() => undefined);
}

async function loadDetailExtras(ctx: AppContext): Promise<void> {
const generation = lifecycleGeneration;
if (!peekSchoolCalendar()) {
void loadSchoolCalendarOrCache(ctx.signal)
.then(() => {
if (isLifecycleActive(ctx, generation)) ctx.rerender();
})
.catch(() => undefined);
}
const term = state.term ?? state.timetable?.term;
if (!session || !term || state.exams || examsRequested === termKey(term)) return;
examsRequested = termKey(term);
Expand Down Expand Up @@ -536,7 +540,7 @@ export const scheduleView = {
const ptr = loadCurrentPointer();
const cached = readCachedTimetable(ptr ? loadTimetableCache(ptr.termKey) : null);
if (ptr && cached) {
const schedule = createTimetableSchedule(cached, { weekOneMonday: ptr.weekOneMonday });
const schedule = termSchedule(cached, ptr.weekOneMonday);
state = {
mode: 'hub',
key: ptr.termKey,
Expand All @@ -548,6 +552,7 @@ export const scheduleView = {
state = { mode: 'loading' };
}
ctx.rerender();
refreshSchoolDays(ctx);
await refreshFromNetwork(ctx, generation);
},

Expand Down Expand Up @@ -829,11 +834,11 @@ export const scheduleView = {
const hubWeekOne = state.weekOne;
if (!tt || !hubKey || !hubWeekOne) return;
{
const schedule = createTimetableSchedule(tt, { weekOneMonday: hubWeekOne });
const schedule = termSchedule(tt, hubWeekOne);
const now = new Date();
const cursor = state.gridCursor ?? todayGridCursor(schedule, tt.periods, now);
const week = Math.max(1, schedule.weekAt(now));
const nav = handleGridKey(key, cursor, tt, week);
const nav = handleGridKey(key, cursor, tt, week, hubWeekOne);
if (nav.kind === 'moveCursor') {
state = { ...state, gridCursor: nav.cursor };
return;
Expand Down Expand Up @@ -912,8 +917,10 @@ export const scheduleView = {
}
if (shortcut.key === 'e') {
try {
const schoolDays = cachedSchoolDays(hubWeekOne);
const ics = timetableToIcs(tt, {
weekOneMonday: hubWeekOne,
...(schoolDays ? { schoolDays } : {}),
calendarName: `NBT ${state.term?.academicYearLabel ?? ''}`,
});
const out = `timetable-${hubKey}.ics`;
Expand All @@ -936,7 +943,7 @@ export const scheduleView = {
returnToHub();
return;
}
const schedule = createTimetableSchedule(tt, { weekOneMonday: weekOne });
const schedule = termSchedule(tt, weekOne);
const now = new Date();
const cursor = state.gridCursor ?? todayGridCursor(schedule, tt.periods, now);
const currentWeek = Math.max(1, schedule.weekAt(now));
Expand All @@ -961,7 +968,7 @@ export const scheduleView = {
};
return;
}
const nav = handleGridKey(key, cursor, tt, week);
const nav = handleGridKey(key, cursor, tt, week, weekOne);
if (nav.kind === 'moveCursor') {
state = { ...state, gridCursor: nav.cursor };
return;
Expand Down
25 changes: 6 additions & 19 deletions src/features/course-progress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,19 @@ describe('courseProgress', () => {
});

it('skips days off and moves sessions onto make-up days', () => {
const days = {
off: new Map([['2026-09-24', 'Holiday']]),
swaps: new Map([['2026-10-03', { week: 3, weekday: 4 as const }]]),
};
const shifted = {
...timetable,
meetings: [meeting({ weeks: [1, 2, 3] }), meeting({ courseName: 'Other' })],
};
const progress = courseProgress(shifted, WEEK_ONE, thursday, new Date('2026-09-01'), {
...days,
swaps: new Map(),
});
expect(progress).toMatchObject({ total: 2 });
expect(progress?.cancelled.map((c) => [c.occurrence.date, c.reason])).toEqual([
['2026-09-24', 'Holiday'],
]);

const moved = courseProgress(shifted, WEEK_ONE, thursday, new Date('2026-09-01'), {
off: new Map([['2026-10-01', 'Holiday']]),
swaps: days.swaps,
daysOff: [{ date: '2026-09-24', reason: 'Holiday' }],
makeUpDays: [{ date: '2026-10-03', week: 3, weekday: 4, reason: 'Make-up' }],
});
expect(moved?.total).toBe(3);
expect(moved?.cancelled).toEqual([]);
expect(moved?.moved.map((m) => [m.occurrence.date, m.from])).toEqual([
expect(progress?.total).toBe(2);
expect(progress?.cancelled.map((o) => [o.date, o.reason])).toEqual([['2026-09-24', 'Holiday']]);
expect(progress?.sessions.map((o) => [o.date, o.rescheduledFrom])).toEqual([
['2026-09-17', undefined],
['2026-10-03', '2026-10-01'],
]);
expect(moved?.sessions.at(-1)?.start).toEqual(campusDateTime('2026-10-03', '08:00'));
});
});
Loading