diff --git a/src/components/layout/notification-center.tsx b/src/components/layout/notification-center.tsx index 49f181e..9cd8ff0 100644 --- a/src/components/layout/notification-center.tsx +++ b/src/components/layout/notification-center.tsx @@ -1,4 +1,4 @@ -import { For, Show, Suspense, createMemo, createResource, createSignal } from "solid-js"; +import { For, Show, Suspense, createMemo, createResource, createSignal, onCleanup } from "solid-js"; import { useNavigate } from "@tanstack/solid-router"; import { getEvents } from "@/api/events"; import { getExams } from "@/api/exams"; @@ -66,8 +66,13 @@ export function NotificationCenter() { } }); - const now = new Date(); - const startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime(); + // Reactive clock so passed items drop off without a remount. + const [nowMs, setNowMs] = createSignal(Date.now()); + const clockTimer = setInterval(() => setNowMs(Date.now()), 60_000); + onCleanup(() => clearInterval(clockTimer)); + + // An item is still notifiable until it has ended (ends_at, else starts_at). + const notEnded = (starts: number, ends?: number | null) => (ends ?? starts) >= nowMs(); // Combine notification items const allNotifications = createMemo(() => { @@ -91,7 +96,7 @@ export function NotificationCenter() { for (const e of evts) { if (!e.starts_at) continue; const t = new Date(e.starts_at).getTime(); - if (t >= startOfDay) { + if (notEnded(t, e.ends_at ? new Date(e.ends_at).getTime() : null)) { list.push({ id: `evt_${e.id}`, type: "event", @@ -115,7 +120,7 @@ export function NotificationCenter() { for (const ex of exms) { if (!ex.starts_at || ex.draft) continue; const t = new Date(ex.starts_at).getTime(); - if (t >= startOfDay) { + if (notEnded(t, ex.ends_at ? new Date(ex.ends_at).getTime() : null)) { list.push({ id: `ex_${ex.id}`, type: "exam", @@ -138,7 +143,7 @@ export function NotificationCenter() { const hws = homeworkRes()?.items ?? []; for (const hw of hws) { if (!hw.due_at) continue; - if (hw.due_at >= startOfDay) { + if (hw.due_at >= nowMs()) { list.push({ id: `hw_${hw.id}`, type: "homework", diff --git a/src/components/layout/right-nav.tsx b/src/components/layout/right-nav.tsx index 5caf5dd..cc4f8f5 100644 --- a/src/components/layout/right-nav.tsx +++ b/src/components/layout/right-nav.tsx @@ -1,4 +1,4 @@ -import { For, Show, Suspense, createMemo, createResource, createSignal } from "solid-js"; +import { For, Show, Suspense, createMemo, createResource, createSignal, onCleanup } from "solid-js"; import { useLocation, useNavigate } from "@tanstack/solid-router"; import { getEvents } from "@/api/events"; import { getExams } from "@/api/exams"; @@ -99,22 +99,24 @@ export function RightNav() { return filteredMessages().slice(0, 5); }); - // Computed calendar data - const now = new Date(); - const startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime(); + // Reactive clock so passed items clear without a remount. + const [nowMs, setNowMs] = createSignal(Date.now()); + const clockTimer = setInterval(() => setNowMs(Date.now()), 60_000); + onCleanup(() => clearInterval(clockTimer)); + + // An item is still notifiable until it has ended (ends_at, else starts_at). + const notEnded = (starts?: number | null, ends?: number | null) => { + if (!starts) return false; + const end = ends ?? starts; + return end >= nowMs(); + }; const activeEvents = createMemo(() => { - return eventsRes().filter((e: Event) => { - if (!e.starts_at) return false; - return new Date(e.starts_at).getTime() >= startOfDay; - }); + return eventsRes().filter((e: Event) => notEnded(e.starts_at, e.ends_at)); }); const activeExams = createMemo(() => { - return examsRes().filter((e: Exam) => { - if (!e.starts_at || e.draft) return false; - return new Date(e.starts_at).getTime() >= startOfDay; - }); + return examsRes().filter((e: Exam) => !e.draft && notEnded(e.starts_at, e.ends_at)); }); const hasTodayEvents = createMemo(() => activeEvents().length > 0);