Files
crosscode-platform/apps/accounts/frontend/src/lib/branding.ts
T

46 lines
1.9 KiB
TypeScript

import type { FooterChurch } from "@crosscode/ui";
/** The church's branding from the backend, for the server-rendered frame of every page. */
export type BrandingData = {
name: string;
shortName: string | null;
logoUrl: string | null;
colours: { primary: string; accent: string | null };
/** 1.7.0: the font pairing and corners (null: not picked). */
style?: { fontPairing: string | null; corners: string | null };
signupEnabled: boolean;
setupNeeded: boolean;
};
const api = (process.env.ACCOUNTS_API_URL || "http://127.0.0.1:5200").replace(/\/+$/, "");
export async function loadBranding(): Promise<BrandingData> {
try {
const res = await fetch(`${api}/api/v2/branding`, { cache: "no-store" });
if (res.ok) return (await res.json()) as BrandingData;
} catch {
// The backend is starting or down: plain defaults, the page still works.
}
return { name: "Sign in", shortName: null, logoUrl: null, colours: { primary: "#1f4e79", accent: null }, signupEnabled: true, setupNeeded: false };
}
/** The church profile (`crosscode.church/1`) for the shared footer (1.7.0), or null while it can't be had. */
export async function loadChurch(): Promise<FooterChurch | null> {
try {
const res = await fetch(`${api}/api/church/v1`, { next: { revalidate: 60 } });
if (res.ok) return (await res.json()) as FooterChurch;
} catch {
// As loadBranding: the page works without it.
}
return null;
}
/** "#1f4e79" → "31 78 121" (Tailwind's rgb(var(--brand) / alpha)), and a darker shade for hover. */
export function rgbVars(hex: string): { brand: string; dark: string } {
const m = /^#([0-9a-f]{6})$/i.exec(hex) ?? ["", "1f4e79"];
const n = parseInt(m[1]!, 16);
const [r, g, b] = [(n >> 16) & 255, (n >> 8) & 255, n & 255];
const d = (x: number) => Math.round(x * 0.8);
return { brand: `${r} ${g} ${b}`, dark: `${d(r)} ${d(g)} ${d(b)}` };
}