Archived
Site settings -> Campuses tab; campus picker in the event editor; campus filter and badges on the events list, public events page (remembered, starts on home campus) and reports/dashboards; /campus/<slug> pages and sitemap; home campus on the profile; "Everyone at a campus" in email and WhatsApp broadcasts. Help, FEATURES, SETTINGS, API docs, protocol doc (campusCount), handover and roadmap updated; campuses seeded for e2e. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { apiFetch } from "@/lib/api";
|
|
|
|
export type Campus = {
|
|
id: string;
|
|
name: string;
|
|
slug: string;
|
|
address: string | null;
|
|
colour: string | null;
|
|
logoUrl: string | null;
|
|
defaultLocation: string | null;
|
|
contactName: string | null;
|
|
contactEmail: string | null;
|
|
contactPhone: string | null;
|
|
order: number;
|
|
};
|
|
|
|
/** What an event carries about its campus. */
|
|
export type EventCampus = { id: string; name: string; slug: string; colour: string | null };
|
|
|
|
type CampusState = { enabled: boolean; campuses: Campus[] };
|
|
|
|
// One request per page load, shared by every component that asks.
|
|
let cache: Promise<CampusState> | null = null;
|
|
|
|
function load(): Promise<CampusState> {
|
|
if (!cache) {
|
|
cache = apiFetch<CampusState>("/api/campuses").catch(() => {
|
|
cache = null; // try again next time
|
|
return { enabled: false, campuses: [] };
|
|
});
|
|
}
|
|
return cache;
|
|
}
|
|
|
|
/** Forget the cached list (after Site settings → Campuses changes it). */
|
|
export function reloadCampuses() {
|
|
cache = null;
|
|
}
|
|
|
|
/**
|
|
* Whether campuses are on (Site settings → Campuses) and the active campuses. While off,
|
|
* `enabled` is false and `campuses` is empty, so callers hide every campus field.
|
|
*/
|
|
export function useCampuses() {
|
|
const [state, setState] = useState<CampusState & { loading: boolean }>({ enabled: false, campuses: [], loading: true });
|
|
useEffect(() => {
|
|
let alive = true;
|
|
load().then(s => { if (alive) setState({ ...s, loading: false }); });
|
|
return () => { alive = false; };
|
|
}, []);
|
|
return state;
|
|
}
|