Files
stack/packages/organization/src/appearance.mjs
T

31 lines
1.6 KiB
JavaScript

// The organization file's `appearance` (stream S4, PLAN C5): { theme, mode }, which theme the
// Console shows for this organization and in which mode. Both are optional, and a file without
// `appearance` is valid: the Console then shows its built-in default theme in the system mode.
//
// `theme` is a module name (the plugin name pattern), checked for shape only. Whether a module of
// that name is installed is the Console's question at render time, not the file's: a file that
// names a theme which was since uninstalled stays valid and keeps its choice, and the Console
// shows the default with a warning until the module is back.
import { NAME } from "../../plugins/src/kinds.mjs";
import { refuse } from "./errors.mjs";
import { requireObject, rejectUnknownKeys } from "./util.mjs";
export const APPEARANCE_MODES = Object.freeze(["system", "light", "dim", "dark"]);
// The checked, frozen appearance, or undefined when the file has none.
export function checkAppearance(value, where) {
if (value === undefined) return undefined;
requireObject(value, where);
rejectUnknownKeys(value, ["theme", "mode"], where);
if (Object.hasOwn(value, "theme") && (typeof value.theme !== "string" || value.theme.length > 64 || !NAME.test(value.theme))) {
refuse(`${where}.theme must be an installed theme's name, matching ${NAME}`);
}
if (Object.hasOwn(value, "mode") && !APPEARANCE_MODES.includes(value.mode)) {
refuse(`${where}.mode must be one of ${APPEARANCE_MODES.join(", ")}`);
}
return Object.freeze({
...(Object.hasOwn(value, "theme") ? { theme: value.theme } : {}),
...(Object.hasOwn(value, "mode") ? { mode: value.mode } : {}),
});
}