wrolpi/app/src/themes/names.ts
Roland 6b9bc4b072 fix: the map viewer was blank in night and amber
MapViewer handed protomaps-themes-base the WROLPi theme name as its
basemap "flavor".  That worked while the only themes were `light` and
`dark`, and broke the day night and amber arrived: protomaps looks the
flavor up in a record and dereferences the result without checking, so an
unknown name is not a fallback but

    TypeError: Cannot read properties of undefined (reading 'background')
        at nolabels_layers (base_layers.ts:18)

thrown before the first tile.  MapLibre never constructed, so /map
rendered nothing in two of the four themes.

`mapFlavor()` in themes/names.ts maps a theme to one of the two flavors we
can actually serve.  Only `light` and `dark`: the flavor is interpolated
into `/map-assets/sprites/<flavor>` too, and those are the two sprite sets
we ship, so protomaps' `black` and `grayscale` would load a style and then
find no icons.  Night and amber take `dark` and let the media filter tint
the canvas -- a filter shifts hue, so handing them `light` would give a
red or amber slab, which is the opposite of the point.

Two layers of test, because they cover different halves:

  - map-flavor.test.js checks every theme against the REAL protomaps
    (calling layers() and requiring it not to throw) and against the
    sprites directory on disk, rather than a list of valid names copied
    into the test, which is what would have passed here in the first
    place.  It fails when a fifth theme is added without a mapping.
  - map-themes.cy.js drives the actual page in all four themes and waits
    for the MapLibre canvas.  The unit test cannot see whether MapViewer
    USES the mapping, which is the half that was broken.  Verified against
    the planted regression: night and amber fail with no canvas, light and
    dark still pass.

Not a bug: the 404 on /blobs/map-overview.pmtiles in the same trace.  That
is a HEAD probe for an optional planet overview, gated on `resp.ok`, and
the blob is absent in a dev checkout.  The console line is unavoidable.

1093 jest / 61 suites, clean build.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-02 13:48:32 -06:00

158 lines
6.5 KiB
TypeScript

import {SavedThemeName, ThemeMediaFilter, ThemeName} from '../types/theme';
/*
* Theme names and the choices offered by the pickers.
*
* These live here rather than in components/Theme.tsx so the component library can
* use them without importing that module, which still pulls in Semantic UI.
*/
export const darkTheme = 'dark';
export const lightTheme = 'light';
export const nightTheme = 'night';
export const amberTheme = 'amber';
export const defaultTheme = lightTheme;
export const systemTheme = 'system';
export const themeSessionKey = 'color-scheme';
/** Every theme a user can apply, in the order the pickers offer them. */
export const themeNames: ThemeName[] = [lightTheme, darkTheme, nightTheme, amberTheme];
/** Themes built on a dark background. Semantic components are `inverted` in these. */
export const darkThemes: ThemeName[] = [darkTheme, nightTheme, amberTheme];
/** Themes a user must choose deliberately; `prefers-color-scheme` never picks them. */
export const explicitOnlyThemes: ThemeName[] = [nightTheme, amberTheme];
/*
* Themes built on a single hue, which is the constraint the semantic roles exist for.
*
* These have no second colour to spend, so a role cannot be a hue there -- it has to be
* a step on a brightness ramp, and `--orange` being byte-identical to `--text` in night
* is what that costs when it is ignored. Light and dark have hues and are free to use
* them, so severity does NOT read as brightness there and must not be asserted to.
*
* The same set as `explicitOnlyThemes` today, deliberately named separately: one is about
* how a theme is chosen, the other about what a theme has to work with.
*/
export const monochromeThemes: ThemeName[] = [nightTheme, amberTheme];
export const isMonochromeTheme = (theme: ThemeName): boolean =>
monochromeThemes.includes(theme);
export const isDarkTheme = (theme: ThemeName): boolean => darkThemes.includes(theme);
/*
* The basemap flavor for a theme.
*
* The map viewer draws with protomaps-themes-base, which takes a flavor of its own and is
* NOT a place to pass a WROLPi theme name. It looks the flavor up in a record and then
* dereferences the result without checking, so an unknown name is not a fallback but a
* `TypeError: Cannot read properties of undefined (reading 'background')` thrown before the
* first tile -- which is what /map did in night and amber once those themes existed.
*
* Only `light` and `dark` are available: the flavor is also interpolated into
* `/map-assets/sprites/<flavor>`, and those are the two sprite sets we ship. protomaps'
* `black` and `grayscale` would load a style and then fail to find any icons.
*
* Night and amber take `dark` and let the media filter tint the canvas from there. Handing
* them `light` would not work even with the filter on: a filter shifts hue, so a white
* basemap becomes a red one, which is the opposite of what a night-vision theme is for.
*/
export type MapFlavor = 'light' | 'dark';
export const mapFlavor = (theme: ThemeName): MapFlavor =>
isDarkTheme(theme) ? 'dark' : 'light';
export const isThemeName = (value: unknown): value is ThemeName =>
themeNames.includes(value as ThemeName);
/*
* Media filtering.
*
* Images, video, PDFs, embedded pages, and the map canvas cannot consume theme
* tokens, so a monochrome theme can pass them through an SVG color matrix
* instead (see MediaFilterDefs.tsx for the filters, tokens.css for the rule).
*
* A theme declares which filter it uses and whether it is on to begin with; the
* user can then turn it on or off for that theme. Night defaults to on — an
* unfiltered thumbnail undoes dark adaptation, which is the whole point of the
* mode — and amber defaults to off, since it is a look rather than a night-vision
* aid. Neither is forced: some users want their media tinted to match, some
* want to see the actual image.
*
* The choice is stored per theme, so turning it off in amber does not disarm it
* in night.
*/
export const themeMediaFilters: Partial<Record<ThemeName, ThemeMediaFilter>> = {
[nightTheme]: {
id: 'night-red',
defaultOn: true,
label: 'Filter media to red',
description: 'Videos, images, documents, and maps are remapped to red. Turning this off '
+ 'shows their real colors, which will spoil your night vision.',
},
[amberTheme]: {
id: 'amber-mono',
defaultOn: false,
label: 'Filter media to amber',
description: 'Videos, images, documents, and maps are remapped to amber, matching the '
+ 'interface. Off by default so media keeps its real colors.',
},
};
/** The filter a theme offers, or undefined when it has none to offer. */
export const themeMediaFilter = (theme: ThemeName): ThemeMediaFilter | undefined =>
themeMediaFilters[theme];
export const mediaFilterSessionKey = 'media-filter';
/**
* Resolve the filter id to stamp on `<html>` — '' when nothing should be filtered.
*
* `overrides` is the stored map of theme name to boolean; a theme missing from it
* has never been changed and follows its own default.
*/
export const resolveMediaFilter = (
theme: ThemeName,
overrides: Partial<Record<ThemeName, boolean>>,
): string => {
const filter = themeMediaFilters[theme];
if (!filter) return '';
const enabled = overrides[theme] ?? filter.defaultOn;
return enabled ? filter.id : '';
}
export interface ThemeChoice {
value: SavedThemeName;
text: string;
/** Semantic icon name; resolved to a Tabler component by the Icon shim. */
icon: string;
description: string;
}
/** Shared by the navigation bar picker and the Settings page, so the two cannot drift. */
export const themeChoices: ThemeChoice[] = [
{
value: systemTheme,
text: 'System',
icon: 'lightbulb outline',
description: "Follow this device's light or dark preference.",
},
{value: lightTheme, text: 'Light', icon: 'sun', description: 'A light background.'},
{value: darkTheme, text: 'Dark', icon: 'moon', description: 'A dark background.'},
{
value: nightTheme,
text: 'Night',
icon: 'eye',
description: 'Red only, to preserve your night vision in the dark. Videos, documents, ' +
'and maps are filtered to red as well, unless you turn that off.',
},
{
value: amberTheme,
text: 'Amber',
icon: 'terminal',
description: 'Monochrome amber, like a classic terminal. Media keeps its real colors '
+ 'unless you ask for it to be tinted too.',
},
];