forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

modules-context.tsx96 lines, 3.2 KiBHistoryRawDownload
1import {
2 type Accessor,
3 type ParentProps,
4 createContext,
5 createMemo,
6 onCleanup,
7 onMount,
8 useContext,
9} from "solid-js";
10import { createResource } from "@/lib/create-resource";
11import { onModuleDisabled } from "@/api/client";
12import { getModules } from "@/api/modules";
13import { useAuth } from "@/stores/auth-context";
14
15type 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
33const ModulesContext = createContext<ModulesContextValue>();
34
35export 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
92export function useModules(): ModulesContextValue {
93 const context = useContext(ModulesContext);
94 if (!context) throw new Error("useModules must be used inside ModulesProvider");
95 return context;
96}