Files
Homelab-manager/web/src/utils/environment.ts
T
bobbanandClaude Sonnet 5.5 617dc1ea2f Toasts, logo icons, shared table toolbar with search, and a density toggle
Feedback: toasts confirm common actions, and a page's error or success message
that appears out of view is also shown as a toast. Icons: the tab, app and
home-screen icons are the house from the logo; a dev instance gets a coloured dot
on its tab icon. Tables: Secrets, Admin Links, IP Addresses and Alerts share one
toolbar (search, filters, row count, actions, Export CSV); twelve more tables get
a multi-word search box; long tables keep their column headings in view on wide
screens. Density: Compact / Normal / Roomy in the user menu, remembered per
browser.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 23:39:19 +02:00

98 lines
3.4 KiB
TypeScript

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);
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<HTMLLinkElement>('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<EnvironmentLook | null | undefined>(undefined);
useEffect(() => {
let alive = true;
loadEnvironment().then((l) => alive && setLook(l));
return () => {
alive = false;
};
}, []);
return look;
}