hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { |
| 2 | type Accessor, |
| 3 | type ParentProps, |
| 4 | createContext, |
| 5 | createMemo, |
| 6 | createSignal, |
| 7 | useContext, |
| 8 | } from "solid-js"; |
| 9 | import { createResource } from "@/lib/create-resource"; |
| 10 | import { getEvents } from "@/api/events"; |
| 11 | import { getExams } from "@/api/exams"; |
| 12 | import { getHomework } from "@/api/homework"; |
| 13 | import { getAppointments } from "@/api/appointments"; |
| 14 | import { getMessages } from "@/api/messages"; |
| 15 | import type { Appointment, Event, Exam, Homework, Message, Page } from "@/api/client"; |
| 16 | import { createLivePoll } from "@/lib/create-live-poll"; |
| 17 | import { useAuth } from "@/stores/auth-context"; |
| 18 | import { useModules } from "@/stores/modules-context"; |
| 19 | |
| 20 | // One shared poller for the always-mounted shell (NotificationCenter header + |
| 21 | // header message/notification controls). Both used to `createResource` messages/events/exams |
| 22 | // independently — ~8 GETs/min. This fetches the widest form of each source |
| 23 | // ONCE; both consumers derive their own views locally. |
| 24 | // |
| 25 | // Every accessor returns `resource.latest`, not `resource()`: these feed |
| 26 | // always-rendered badges/counts OUTSIDE any <Suspense> (beside <Outlet>). A |
| 27 | // bare read re-suspends on every 60s refetch and blanks the whole page for the |
| 28 | // fetch duration (AGENTS.md #6). `.latest` + `initialValue` never suspends. |
| 29 | |
| 30 | type ShellFeedContextValue = { |
| 31 | messages: Accessor<Page<Message>>; |
| 32 | /** Newest unread inbox rows; `.total` is the *exact* server-side unread count. */ |
| 33 | unreadMessages: Accessor<Page<Message>>; |
| 34 | events: Accessor<Page<Event>>; |
| 35 | exams: Accessor<Page<Exam>>; |
| 36 | appointments: Accessor<Page<Appointment>>; |
| 37 | homework: Accessor<Page<Homework>>; |
| 38 | nowMs: Accessor<number>; |
| 39 | refetchMessages: () => void; |
| 40 | refetchEvents: () => void; |
| 41 | refetchExams: () => void; |
| 42 | refetchAppointments: () => void; |
| 43 | refetchHomework: () => void; |
| 44 | refreshAll: () => void; |
| 45 | }; |
| 46 | |
| 47 | const ShellFeedContext = createContext<ShellFeedContextValue>(); |
| 48 | |
| 49 | // 50: the feed drives "upcoming" badges and the notification rows — it needs |
| 50 | // the next handful, not the archive, and every windowed source orders by its |
| 51 | // own schedule now. 50 soonest-first rows cover a busy month per source; the |
| 52 | // consumers render only not-yet-passed rows, so the dropped rows were never |
| 53 | // rendered. |
| 54 | // ponytail: a school with >50 simultaneously-open items per source loses the |
| 55 | // tail — upgrade path is narrowing the window further, not a bigger cap. |
| 56 | const FEED_LIMIT = 50; |
| 57 | |
| 58 | const upcoming = () => ({ ends_after: Date.now(), limit: FEED_LIMIT }); |
| 59 | |
| 60 | const emptyPage = <T,>(): Page<T> => ({ items: [], total: 0, limit: null, offset: 0 }); |
| 61 | |
| 62 | type UserFeed = Omit<ShellFeedContextValue, "nowMs">; |
| 63 | |
| 64 | // Everything user-scoped lives here so it can be created per account id and |
| 65 | // disposed with it (see ShellFeedProvider): a logged-out or switched account |
| 66 | // gets brand-new resources seeded with empty pages, so no previous user's rows |
| 67 | // can survive into the next session. Solid keeps a resource's last value when |
| 68 | // its source goes falsy, so gating alone would leak user A's inbox into user |
| 69 | // B's shell until the refetch resolved. |
| 70 | function createUserFeed(loggedIn: boolean, modulesLoading: () => boolean, isEnabled: (module: string) => boolean): UserFeed { |
| 71 | // Only fetch for a logged-in user: source is false while logged out, so the |
| 72 | // resource keeps its seeded empty value and never hits the network. |
| 73 | // It also waits for the school's module list, so a first paint never asks a |
| 74 | // switched-off module (and gets 403) before the list says it is off. |
| 75 | const source = () => loggedIn && !modulesLoading(); |
| 76 | |
| 77 | const [messagesRes, { refetch: refetchInbox }] = createResource( |
| 78 | source, |
| 79 | async () => { |
| 80 | try { |
| 81 | // A switched-off module would only answer 403 on every poll. |
| 82 | if (!isEnabled("messages")) return emptyPage<Message>(); |
| 83 | return await getMessages("inbox", { limit: 100 }); |
| 84 | } catch { |
| 85 | return emptyPage<Message>(); |
| 86 | } |
| 87 | }, |
| 88 | { initialValue: emptyPage<Message>() } |
| 89 | ); |
| 90 | |
| 91 | // The badge cannot be derived from the inbox page above: that page is |
| 92 | // ORDER BY id DESC capped at 100, so an old unread message falls off it. |
| 93 | // `read=false` makes `total` the exact server-side unread count, and the 10 |
| 94 | // items are the newest unread ones the popover lists. |
| 95 | const [unreadRes, { refetch: refetchUnread }] = createResource( |
| 96 | source, |
| 97 | async () => { |
| 98 | try { |
| 99 | if (!isEnabled("messages")) return emptyPage<Message>(); |
| 100 | return await getMessages("inbox", { read: false, limit: 10 }); |
| 101 | } catch { |
| 102 | return emptyPage<Message>(); |
| 103 | } |
| 104 | }, |
| 105 | { initialValue: emptyPage<Message>() } |
| 106 | ); |
| 107 | |
| 108 | const [eventsRes, { refetch: refetchEvents }] = createResource( |
| 109 | source, |
| 110 | async () => { |
| 111 | try { |
| 112 | if (!isEnabled("events")) return emptyPage<Event>(); |
| 113 | return await getEvents(upcoming()); |
| 114 | } catch { |
| 115 | return emptyPage<Event>(); |
| 116 | } |
| 117 | }, |
| 118 | { initialValue: emptyPage<Event>() } |
| 119 | ); |
| 120 | |
| 121 | const [examsRes, { refetch: refetchExams }] = createResource( |
| 122 | source, |
| 123 | async () => { |
| 124 | try { |
| 125 | if (!isEnabled("exams")) return emptyPage<Exam>(); |
| 126 | return await getExams(upcoming()); |
| 127 | } catch { |
| 128 | return emptyPage<Exam>(); |
| 129 | } |
| 130 | }, |
| 131 | { initialValue: emptyPage<Exam>() } |
| 132 | ); |
| 133 | |
| 134 | // Same feed semantics as events/exams: only what is still ahead. The |
| 135 | // notification feed renders homework whose due date has not passed, and both |
| 136 | // polls used to carry a blind newest-100 cap only because no window existed. |
| 137 | const [appointmentsRes, { refetch: refetchAppointments }] = createResource( |
| 138 | source, |
| 139 | async () => { |
| 140 | try { |
| 141 | if (!isEnabled("appointments")) return emptyPage<Appointment>(); |
| 142 | return await getAppointments({ starts_after: Date.now(), limit: FEED_LIMIT }); |
| 143 | } catch { |
| 144 | return emptyPage<Appointment>(); |
| 145 | } |
| 146 | }, |
| 147 | { initialValue: emptyPage<Appointment>() } |
| 148 | ); |
| 149 | |
| 150 | const [homeworkRes, { refetch: refetchHomework }] = createResource( |
| 151 | source, |
| 152 | async () => { |
| 153 | try { |
| 154 | if (!isEnabled("homework")) return emptyPage<Homework>(); |
| 155 | return await getHomework({ due_after: Date.now(), limit: FEED_LIMIT }); |
| 156 | } catch { |
| 157 | return emptyPage<Homework>(); |
| 158 | } |
| 159 | }, |
| 160 | { initialValue: emptyPage<Homework>() } |
| 161 | ); |
| 162 | |
| 163 | // One call refreshes both message views — marking a message read changes the |
| 164 | // preview list and the unread count together. |
| 165 | const refetchMessages = () => { |
| 166 | void refetchInbox(); |
| 167 | void refetchUnread(); |
| 168 | }; |
| 169 | |
| 170 | const refreshAll = () => { |
| 171 | refetchMessages(); |
| 172 | void refetchEvents(); |
| 173 | void refetchExams(); |
| 174 | void refetchAppointments(); |
| 175 | void refetchHomework(); |
| 176 | }; |
| 177 | |
| 178 | return { |
| 179 | messages: () => messagesRes.latest, |
| 180 | unreadMessages: () => unreadRes.latest, |
| 181 | events: () => eventsRes.latest, |
| 182 | exams: () => examsRes.latest, |
| 183 | appointments: () => appointmentsRes.latest, |
| 184 | homework: () => homeworkRes.latest, |
| 185 | refetchMessages, |
| 186 | refetchEvents: () => void refetchEvents(), |
| 187 | refetchExams: () => void refetchExams(), |
| 188 | refetchAppointments: () => void refetchAppointments(), |
| 189 | refetchHomework: () => void refetchHomework(), |
| 190 | refreshAll, |
| 191 | }; |
| 192 | } |
| 193 | |
| 194 | export function ShellFeedProvider(props: ParentProps) { |
| 195 | const auth = useAuth(); |
| 196 | // Keyed on the account id: logging out (null) or switching account disposes |
| 197 | // the whole feed and builds a fresh one. Children never remount, so the |
| 198 | // router tree below is untouched. |
| 199 | const userId = createMemo(() => auth.user()?.id ?? null); |
| 200 | const modules = useModules(); |
| 201 | const feed = createMemo(() => createUserFeed(userId() !== null, modules.loading, modules.isEnabled)); |
| 202 | |
| 203 | // Reactive clock so passed items drop off without a remount. Visibility-aware: |
| 204 | // no ticks/GETs while the tab is hidden, refetch on tab-back so nothing is |
| 205 | // stale on a parked tab. This is the single shell poller — it outlives the |
| 206 | // per-user feeds and always drives the current one. |
| 207 | const [nowMs, setNowMs] = createSignal(Date.now()); |
| 208 | createLivePoll(() => { |
| 209 | if (!auth.user()) return; |
| 210 | setNowMs(Date.now()); |
| 211 | feed().refreshAll(); |
| 212 | }, 60_000); |
| 213 | |
| 214 | const value: ShellFeedContextValue = { |
| 215 | messages: () => feed().messages(), |
| 216 | unreadMessages: () => feed().unreadMessages(), |
| 217 | events: () => feed().events(), |
| 218 | exams: () => feed().exams(), |
| 219 | appointments: () => feed().appointments(), |
| 220 | homework: () => feed().homework(), |
| 221 | nowMs, |
| 222 | refetchMessages: () => feed().refetchMessages(), |
| 223 | refetchEvents: () => feed().refetchEvents(), |
| 224 | refetchExams: () => feed().refetchExams(), |
| 225 | refetchAppointments: () => feed().refetchAppointments(), |
| 226 | refetchHomework: () => feed().refetchHomework(), |
| 227 | refreshAll: () => feed().refreshAll(), |
| 228 | }; |
| 229 | |
| 230 | return <ShellFeedContext.Provider value={value}>{props.children}</ShellFeedContext.Provider>; |
| 231 | } |
| 232 | |
| 233 | export function useShellFeed(): ShellFeedContextValue { |
| 234 | const ctx = useContext(ShellFeedContext); |
| 235 | if (!ctx) throw new Error("useShellFeed must be used within ShellFeedProvider"); |
| 236 | return ctx; |
| 237 | } |