import { useEffect, useState } from "react"; /** How this installation looks different from the others (a dev copy next to production), set by ENVIRONMENT_NAME / ENVIRONMENT_COLOR on the server. */ export interface EnvironmentLook { /** Shown as a badge and in the browser tab; may be empty. */ name: string; /** A #rrggbb colour. */ color: string; } const BASE_TITLE = "Homelab Manager"; let pending: Promise | null = null; /** Asked once per page load; a failure just means a normal look. */ export function loadEnvironment(): Promise { if (!pending) { pending = fetch("/api/environment", { credentials: "same-origin" }) .then((res) => (res.ok ? res.json() : null)) .then((body: { environment?: EnvironmentLook | null } | null) => { const look = body?.environment; // The value goes into styles, so only a plain #rrggbb colour is accepted here too. if (look && typeof look.color === "string" && /^#[0-9a-f]{6}$/i.test(look.color)) { return { name: typeof look.name === "string" ? look.name : "", color: look.color }; } return null; }) .catch(() => null) .then((look) => { applyEnvironment(look); return look; }); } return pending; } function applyEnvironment(look: EnvironmentLook | null): void { const root = document.documentElement; if (!look) return; root.style.setProperty("--hlm-env-color", look.color); root.setAttribute("data-env", look.name || "custom"); document.title = look.name ? `[${look.name}] ${BASE_TITLE}` : BASE_TITLE; document.querySelector('meta[name="theme-color"]')?.setAttribute("content", look.color); tintFavicon(look.color); } /** The tab icon gets a dot in the environment's colour in its corner, so this tab stands out among several. */ function tintFavicon(color: string): void { const links = document.querySelectorAll('link[rel~="icon"]'); if (links.length === 0) return; const image = new Image(); image.onload = () => { try { const size = 64; const canvas = document.createElement("canvas"); canvas.width = canvas.height = size; const ctx = canvas.getContext("2d"); if (!ctx) return; ctx.drawImage(image, 0, 0, size, size); ctx.beginPath(); ctx.arc(size - 19, size - 19, 18, 0, Math.PI * 2); ctx.fillStyle = color; ctx.fill(); ctx.lineWidth = 5; ctx.strokeStyle = "#ffffff"; ctx.stroke(); const href = canvas.toDataURL("image/png"); links.forEach((link) => { link.href = href; link.removeAttribute("sizes"); }); } catch { // The plain icon stays; this is only a nicety. } }; image.src = "/icon-192.png"; } /** Whether white text on this colour would be hard to read. */ export function isLightColor(hex: string): boolean { const n = parseInt(hex.slice(1), 16); const [r, g, b] = [(n >> 16) & 255, (n >> 8) & 255, n & 255]; return (0.299 * r + 0.587 * g + 0.114 * b) / 255 > 0.6; } /** undefined while it is being asked, null for a normal installation. */ export function useEnvironment(): EnvironmentLook | null | undefined { const [look, setLook] = useState(undefined); useEffect(() => { let alive = true; loadEnvironment().then((l) => alive && setLook(l)); return () => { alive = false; }; }, []); return look; }