Files
slp-modular-cms/frontend/themes/resolveTheme.ts
SluijsensandClaude Sonnet 5.5 a9b9ad2931 Mail-logo als vooraf samengestelde witte badge (geen grijze plaat); succes-toast bij rolwijziging
- themes/asf/logo-mail.png (+ make-mail-logo.py), geleverd als /admin/brand-logo-mail.png; ASF Email:LogoUrl wijst ernaar
- Renderer: plaat weggehaald, licht logo 56px, dark-swap naar LogoDarkUrl blijft
- UsersPage: toast na geslaagde rolwijziging (nl+en, ook Owner-bevestiging)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-09 16:11:33 +01:00

58 lines
2.4 KiB
TypeScript

import fs from 'node:fs';
import path from 'node:path';
export interface ResolvedTheme {
/** Absolute path of the CSS file to import (the empty module when no theme is selected). */
css: string;
/** Absolute path of the theme favicon, or null when there is no theme or it ships none. */
favicon: string | null;
/**
* Brand logo files: one for light backgrounds (dark ink) and one for dark backgrounds (light
* ink), or null when the theme ships none. Both or neither: a logo that is invisible in one
* colour mode is worse than none.
*/
logo: { onLight: string; onDark: string; mail: string | null } | null;
}
const THEMES_DIR = import.meta.dirname;
/** Theme folders available next to this file (each holds a theme.css). */
export function availableThemes(dir: string = THEMES_DIR): string[] {
return fs
.readdirSync(dir, { withFileTypes: true })
.filter((e) => e.isDirectory() && fs.existsSync(path.join(dir, e.name, 'theme.css')))
.map((e) => e.name)
.sort();
}
/**
* Resolves VITE_THEME to files. Unset or blank means "no theme": an empty CSS module, so the admin
* looks exactly as it always did. An unknown name is a build error, never a silent fallback, so a
* typo in the pipeline cannot ship an unthemed admin unnoticed.
*/
export function resolveTheme(name: string | undefined, dir: string = THEMES_DIR): ResolvedTheme {
const trimmed = (name ?? '').trim();
if (trimmed === '') {
return { css: path.join(dir, 'none.css'), favicon: null, logo: null };
}
// The name becomes a path segment: only known theme folders are accepted.
const known = availableThemes(dir);
if (!known.includes(trimmed)) {
throw new Error(
`Unknown VITE_THEME "${trimmed}". Available themes: ${known.length > 0 ? known.join(', ') : '(none)'}.`,
);
}
const favicon = path.join(dir, trimmed, 'favicon.png');
const onLight = path.join(dir, trimmed, 'logo-on-light.png');
const onDark = path.join(dir, trimmed, 'logo-on-dark.png');
// Optional: the logo for e-mail headers, on a baked-in white badge (see themes/asf/make-mail-logo.py).
const mail = path.join(dir, trimmed, 'logo-mail.png');
return {
css: path.join(dir, trimmed, 'theme.css'),
favicon: fs.existsSync(favicon) ? favicon : null,
logo: fs.existsSync(onLight) && fs.existsSync(onDark) ? { onLight, onDark, mail: fs.existsSync(mail) ? mail : null } : null,
};
}