Let an installation look different via ENVIRONMENT_NAME and ENVIRONMENT_COLOR

Setting ENVIRONMENT_NAME (and optionally ENVIRONMENT_COLOR, a name or hex
value) gives the sidebar that colour, adds a name badge next to the title and
on the sign-in page, and prefixes the browser tab title, so a dev copy can't
be taken for production. Unset, nothing changes. The colour is validated to a
plain #rrggbb before it reaches the page's styles. A public
GET /api/environment lets the sign-in page use it too.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-13 20:59:25 +02:00
1 parent 44cc9d1cf2
commit 7e196be56d
11 files changed
+253 -3

No files matched your search

+64
View File
@@ -0,0 +1,64 @@
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<EnvironmentLook | null> | null = null;
/** Asked once per page load; a failure just means a normal look. */
export function loadEnvironment(): Promise<EnvironmentLook | null> {
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);
}
/** 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<EnvironmentLook | null | undefined>(undefined);
useEffect(() => {
let alive = true;
loadEnvironment().then((l) => alive && setLook(l));
return () => {
alive = false;
};
}, []);
return look;
}