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 | onCleanup, |
| 7 | onMount, |
| 8 | useContext, |
| 9 | } from "solid-js"; |
| 10 | import { createResource } from "@/lib/create-resource"; |
| 11 | import { onModuleDisabled } from "@/api/client"; |
| 12 | import { getModules } from "@/api/modules"; |
| 13 | import { useAuth } from "@/stores/auth-context"; |
| 14 | |
| 15 | type ModulesContextValue = { |
| 16 | /** |
| 17 | * The school's switched-on modules, sorted by name. Null while logged out, |
| 18 | * still loading, or failed — every consumer treats null as "unknown" and |
| 19 | * shows everything (fail open), so a modules outage never blanks the shell. |
| 20 | */ |
| 21 | enabled: Accessor<string[] | null>; |
| 22 | /** |
| 23 | * True until the first lookup for this account settles. Readers that would |
| 24 | * otherwise fire at a possibly-off module on first paint wait for it; after |
| 25 | * that a failed lookup still fails open. |
| 26 | */ |
| 27 | loading: Accessor<boolean>; |
| 28 | /** Fail-open membership test over `enabled`. */ |
| 29 | isEnabled: (module: string) => boolean; |
| 30 | refresh: () => void; |
| 31 | }; |
| 32 | |
| 33 | const ModulesContext = createContext<ModulesContextValue>(); |
| 34 | |
| 35 | export function ModulesProvider(props: ParentProps) { |
| 36 | const auth = useAuth(); |
| 37 | // Keyed on the account id: switching account refetches. While logged out |
| 38 | // the source is null and the resource keeps its seeded null — the shell |
| 39 | // shows no nav without a role anyway, so nothing stale can render. |
| 40 | const userId = createMemo(() => auth.user()?.id ?? null); |
| 41 | const [modules, { refetch }] = createResource( |
| 42 | userId, |
| 43 | async (id) => { |
| 44 | if (id === null) return null; |
| 45 | try { |
| 46 | return (await getModules()).enabled; |
| 47 | } catch { |
| 48 | return null; |
| 49 | } |
| 50 | }, |
| 51 | { initialValue: null }, |
| 52 | ); |
| 53 | |
| 54 | // The enabled set changes under a live session when a builder flips a |
| 55 | // module. Two cues bring it up to date without a reload: a route answering |
| 56 | // `403 {module}` (switched off), and the tab coming back into view after a |
| 57 | // while (switched on, or off where nothing was asked of it yet). |
| 58 | const REVISIT_AFTER_MS = 60_000; |
| 59 | let lastFetch = Date.now(); |
| 60 | const refreshNow = () => { |
| 61 | if (userId() === null || modules.loading) return; |
| 62 | lastFetch = Date.now(); |
| 63 | void refetch(); |
| 64 | }; |
| 65 | onMount(() => { |
| 66 | const stop = onModuleDisabled((module) => { |
| 67 | if (modules.latest?.includes(module) !== false) refreshNow(); |
| 68 | }); |
| 69 | const onVisible = () => { |
| 70 | if (document.visibilityState === "visible" && Date.now() - lastFetch > REVISIT_AFTER_MS) refreshNow(); |
| 71 | }; |
| 72 | document.addEventListener("visibilitychange", onVisible); |
| 73 | onCleanup(() => { |
| 74 | stop(); |
| 75 | document.removeEventListener("visibilitychange", onVisible); |
| 76 | }); |
| 77 | }); |
| 78 | |
| 79 | const value: ModulesContextValue = { |
| 80 | enabled: () => modules(), |
| 81 | loading: () => userId() !== null && modules.loading && modules.latest === null, |
| 82 | isEnabled: (module: string) => { |
| 83 | const enabled = modules(); |
| 84 | return enabled === null || enabled.includes(module); |
| 85 | }, |
| 86 | refresh: () => void refetch(), |
| 87 | }; |
| 88 | |
| 89 | return <ModulesContext.Provider value={value}>{props.children}</ModulesContext.Provider>; |
| 90 | } |
| 91 | |
| 92 | export function useModules(): ModulesContextValue { |
| 93 | const context = useContext(ModulesContext); |
| 94 | if (!context) throw new Error("useModules must be used inside ModulesProvider"); |
| 95 | return context; |
| 96 | } |