🐛 hide calendar/notification badge for passed events
Browse files at this commite5e4774c6bf5051ae6bfc358c682c54f5ffffb36Parent: 11850f1- fix
- 🐛 hide calendar/notification badge for passed events
2 changed files +25 -18
src/components/layout/notification-center.tsxmodified+11 -6
| @@ -1,4 +1,4 @@ | |||
| 1 | -import { For, Show, Suspense, createMemo, createResource, createSignal } from "solid-js"; | 1 | +import { For, Show, Suspense, createMemo, createResource, createSignal, onCleanup } from "solid-js"; |
| 2 | import { useNavigate } from "@tanstack/solid-router"; | 2 | import { useNavigate } from "@tanstack/solid-router"; |
| 3 | import { getEvents } from "@/api/events"; | 3 | import { getEvents } from "@/api/events"; |
| 4 | import { getExams } from "@/api/exams"; | 4 | import { getExams } from "@/api/exams"; |
| @@ -66,8 +66,13 @@ export function NotificationCenter() { | |||
| 66 | } | 66 | } |
| 67 | }); | 67 | }); |
| 68 | 68 | ||
| 69 | - const now = new Date(); | 69 | + // Reactive clock so passed items drop off without a remount. |
| 70 | - const startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime(); | 70 | + const [nowMs, setNowMs] = createSignal(Date.now()); |
| 71 | + const clockTimer = setInterval(() => setNowMs(Date.now()), 60_000); | ||
| 72 | + onCleanup(() => clearInterval(clockTimer)); | ||
| 73 | + | ||
| 74 | + // An item is still notifiable until it has ended (ends_at, else starts_at). | ||
| 75 | + const notEnded = (starts: number, ends?: number | null) => (ends ?? starts) >= nowMs(); | ||
| 71 | 76 | ||
| 72 | // Combine notification items | 77 | // Combine notification items |
| 73 | const allNotifications = createMemo<NotificationItem[]>(() => { | 78 | const allNotifications = createMemo<NotificationItem[]>(() => { |
| @@ -91,7 +96,7 @@ export function NotificationCenter() { | |||
| 91 | for (const e of evts) { | 96 | for (const e of evts) { |
| 92 | if (!e.starts_at) continue; | 97 | if (!e.starts_at) continue; |
| 93 | const t = new Date(e.starts_at).getTime(); | 98 | const t = new Date(e.starts_at).getTime(); |
| 94 | - if (t >= startOfDay) { | 99 | + if (notEnded(t, e.ends_at ? new Date(e.ends_at).getTime() : null)) { |
| 95 | list.push({ | 100 | list.push({ |
| 96 | id: `evt_${e.id}`, | 101 | id: `evt_${e.id}`, |
| 97 | type: "event", | 102 | type: "event", |
| @@ -115,7 +120,7 @@ export function NotificationCenter() { | |||
| 115 | for (const ex of exms) { | 120 | for (const ex of exms) { |
| 116 | if (!ex.starts_at || ex.draft) continue; | 121 | if (!ex.starts_at || ex.draft) continue; |
| 117 | const t = new Date(ex.starts_at).getTime(); | 122 | const t = new Date(ex.starts_at).getTime(); |
| 118 | - if (t >= startOfDay) { | 123 | + if (notEnded(t, ex.ends_at ? new Date(ex.ends_at).getTime() : null)) { |
| 119 | list.push({ | 124 | list.push({ |
| 120 | id: `ex_${ex.id}`, | 125 | id: `ex_${ex.id}`, |
| 121 | type: "exam", | 126 | type: "exam", |
| @@ -138,7 +143,7 @@ export function NotificationCenter() { | |||
| 138 | const hws = homeworkRes()?.items ?? []; | 143 | const hws = homeworkRes()?.items ?? []; |
| 139 | for (const hw of hws) { | 144 | for (const hw of hws) { |
| 140 | if (!hw.due_at) continue; | 145 | if (!hw.due_at) continue; |
| 141 | - if (hw.due_at >= startOfDay) { | 146 | + if (hw.due_at >= nowMs()) { |
| 142 | list.push({ | 147 | list.push({ |
| 143 | id: `hw_${hw.id}`, | 148 | id: `hw_${hw.id}`, |
| 144 | type: "homework", | 149 | type: "homework", |
src/components/layout/right-nav.tsxmodified+14 -12
| @@ -1,4 +1,4 @@ | |||
| 1 | -import { For, Show, Suspense, createMemo, createResource, createSignal } from "solid-js"; | 1 | +import { For, Show, Suspense, createMemo, createResource, createSignal, onCleanup } from "solid-js"; |
| 2 | import { useLocation, useNavigate } from "@tanstack/solid-router"; | 2 | import { useLocation, useNavigate } from "@tanstack/solid-router"; |
| 3 | import { getEvents } from "@/api/events"; | 3 | import { getEvents } from "@/api/events"; |
| 4 | import { getExams } from "@/api/exams"; | 4 | import { getExams } from "@/api/exams"; |
| @@ -99,22 +99,24 @@ export function RightNav() { | |||
| 99 | return filteredMessages().slice(0, 5); | 99 | return filteredMessages().slice(0, 5); |
| 100 | }); | 100 | }); |
| 101 | 101 | ||
| 102 | - // Computed calendar data | 102 | + // Reactive clock so passed items clear without a remount. |
| 103 | - const now = new Date(); | 103 | + const [nowMs, setNowMs] = createSignal(Date.now()); |
| 104 | - const startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime(); | 104 | + const clockTimer = setInterval(() => setNowMs(Date.now()), 60_000); |
| 105 | + onCleanup(() => clearInterval(clockTimer)); | ||
| 106 | + | ||
| 107 | + // An item is still notifiable until it has ended (ends_at, else starts_at). | ||
| 108 | + const notEnded = (starts?: number | null, ends?: number | null) => { | ||
| 109 | + if (!starts) return false; | ||
| 110 | + const end = ends ?? starts; | ||
| 111 | + return end >= nowMs(); | ||
| 112 | + }; | ||
| 105 | 113 | ||
| 106 | const activeEvents = createMemo(() => { | 114 | const activeEvents = createMemo(() => { |
| 107 | - return eventsRes().filter((e: Event) => { | 115 | + return eventsRes().filter((e: Event) => notEnded(e.starts_at, e.ends_at)); |
| 108 | - if (!e.starts_at) return false; | ||
| 109 | - return new Date(e.starts_at).getTime() >= startOfDay; | ||
| 110 | - }); | ||
| 111 | }); | 116 | }); |
| 112 | 117 | ||
| 113 | const activeExams = createMemo(() => { | 118 | const activeExams = createMemo(() => { |
| 114 | - return examsRes().filter((e: Exam) => { | 119 | + return examsRes().filter((e: Exam) => !e.draft && notEnded(e.starts_at, e.ends_at)); |
| 115 | - if (!e.starts_at || e.draft) return false; | ||
| 116 | - return new Date(e.starts_at).getTime() >= startOfDay; | ||
| 117 | - }); | ||
| 118 | }); | 120 | }); |
| 119 | 121 | ||
| 120 | const hasTodayEvents = createMemo(() => activeEvents().length > 0); | 122 | const hasTodayEvents = createMemo(() => activeEvents().length > 0); |