mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-20 15:06:19 -05:00
The library has been uninstalled for some time, but the codebase still read as though it were there: identifiers named after it, rules written against its class names, and about 180 comments explaining a rule in terms of a thing that is no longer installed. Renamed, keeping the vocabulary the app actually uses: semanticUIColorMap -> navColorHexMap, semanticColorNames -> paletteColorNames, semanticNames -> iconNameAliases, semanticSizes -> sizeAliases, and SemanticUIColors -> NavColors in wrolpi/schema.py, which is what validates nav_color on the way in. ColorToSemanticHexColor had no callers at all and is gone. The baseline ratchet has done its job -- the list reached zero, so the JSON is deleted and the guard keeps only the checks with teeth: no package, no import, no `className="ui "`, no toast library. Its Theme.tsx check went with the wrappers it was watching for. It also asserts its own walk found files, so a clean result cannot mean it scanned nothing. Deleted from App.css: every rule requiring `.ui`, `.inverted`, `.segment` or `.modal`, none of which any component has set since the stylesheet stopped loading, plus the form skeletons, ui-alerts and progress-text rules that nothing renders. Two of them were doing harm rather than nothing: the drag tints had a dark-theme variant under `.inverted` that stopped applying, leaving 15% of a fixed blue over a dark row, and now mix a token instead. Two selectors naming markup that no longer exists were live bugs. The file browser's filter had `.ui.input.file-browser-filter`, so it rendered at the browser default width on a bar laid out to give it 34em -- now qualified by its container, because `.wrolpi-action-input` sets `width: 100%` at the same specificity and is loaded later. And SearchBox takes named props rather than a rest spread, so the `id` DestinationForm hands it was dropped in silence and the label it renders pointed at nothing; the download forms' Destination field has had no associated label since the migration. "Semantic roles" survives in four places, where it means what it says. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
237 lines
8.6 KiB
TypeScript
237 lines
8.6 KiB
TypeScript
import React, {useEffect, useState} from 'react';
|
|
import {ThemeContext} from "../contexts/contexts";
|
|
import {MantineProvider} from "@mantine/core";
|
|
import {Notifications} from "@mantine/notifications";
|
|
import {cssVariablesResolver, mantineTheme} from "../themes/mantine";
|
|
import {
|
|
amberTheme,
|
|
darkTheme,
|
|
defaultTheme,
|
|
isDarkTheme,
|
|
isThemeName,
|
|
lightTheme,
|
|
mediaFilterSessionKey,
|
|
nightTheme,
|
|
resolveMediaFilter,
|
|
systemTheme,
|
|
themeChoices,
|
|
themeMediaFilter,
|
|
themeNames,
|
|
themeSessionKey,
|
|
} from "../themes/names";
|
|
import _ from "lodash";
|
|
import {ThemeContextValue, ThemeName, SavedThemeName} from "../types/theme";
|
|
|
|
// Theme names and picker choices live in themes/names, and are re-exported here because
|
|
// most call sites already import them from Theme.
|
|
export {
|
|
amberTheme,
|
|
darkTheme,
|
|
defaultTheme,
|
|
isDarkTheme,
|
|
lightTheme,
|
|
nightTheme,
|
|
systemTheme,
|
|
themeChoices,
|
|
themeMediaFilter,
|
|
themeNames,
|
|
themeSessionKey,
|
|
};
|
|
|
|
/** Resolve the saved preference to the theme that should be applied right now. */
|
|
export const resolveTheme = (saved: SavedThemeName): ThemeName => {
|
|
if (isThemeName(saved)) {
|
|
return saved;
|
|
}
|
|
// `system`, null, or a value written by an older version.
|
|
const prefersDark = typeof window.matchMedia === 'function'
|
|
&& window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
return prefersDark ? darkTheme : lightTheme;
|
|
}
|
|
|
|
const readSavedTheme = (): SavedThemeName => {
|
|
try {
|
|
const value = localStorage.getItem(themeSessionKey);
|
|
if (isThemeName(value) || value === systemTheme) {
|
|
return value;
|
|
}
|
|
} catch (e) {
|
|
// localStorage can throw when cookies/storage are blocked.
|
|
console.error('Unable to read the saved theme', e);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* The user's per-theme media filter choices: `{night: false}` means "night, but
|
|
* do not filter media". A theme absent from the map follows its own default.
|
|
*/
|
|
const readMediaFilters = (): Partial<Record<ThemeName, boolean>> => {
|
|
try {
|
|
const raw = localStorage.getItem(mediaFilterSessionKey);
|
|
if (raw) {
|
|
const parsed = JSON.parse(raw);
|
|
// Only keep known themes with boolean values; the rest is someone else's data.
|
|
if (parsed && typeof parsed === 'object') {
|
|
return Object.fromEntries(Object.entries(parsed)
|
|
.filter(([key, value]) => isThemeName(key) && typeof value === 'boolean'));
|
|
}
|
|
}
|
|
} catch (e) {
|
|
// Blocked storage, or a value an older version wrote.
|
|
console.error('Unable to read the media filter settings', e);
|
|
}
|
|
return {};
|
|
}
|
|
|
|
interface ThemeProviderProps {
|
|
children: React.ReactNode;
|
|
}
|
|
|
|
export function ThemeProvider({children, ...props}: ThemeProviderProps) {
|
|
if (!_.isEmpty(props)) {
|
|
console.log(props);
|
|
console.error('ThemeWrapper does not support props!');
|
|
}
|
|
|
|
// savedTheme is the user's preference: a theme name, `system`, or null (never chosen).
|
|
const [savedTheme, setSavedTheme] = useState<SavedThemeName>(readSavedTheme);
|
|
// theme is what is currently applied.
|
|
const [theme, setThemeName] = useState<ThemeName>(() => resolveTheme(readSavedTheme()));
|
|
|
|
// Which themes the user has turned media filtering on or off for.
|
|
const [mediaFilters, setMediaFilters] = useState<Partial<Record<ThemeName, boolean>>>(
|
|
readMediaFilters);
|
|
|
|
// `data-theme` on <html> is what the token CSS keys off of. index.html stamps it before
|
|
// first paint from the same localStorage value, so the first render already matches.
|
|
useEffect(() => {
|
|
document.documentElement.setAttribute('data-theme', theme);
|
|
}, [theme]);
|
|
|
|
// `data-media-filter` names the filter to apply, or is absent when nothing should be
|
|
// filtered. index.html stamps this too: a flash of unfiltered thumbnails before the
|
|
// bundle arrives would cost the user the dark adaptation night mode exists to protect.
|
|
useEffect(() => {
|
|
const filter = resolveMediaFilter(theme, mediaFilters);
|
|
if (filter) {
|
|
document.documentElement.setAttribute('data-media-filter', filter);
|
|
} else {
|
|
document.documentElement.removeAttribute('data-media-filter');
|
|
}
|
|
}, [theme, mediaFilters]);
|
|
|
|
// Follow the OS preference only while the user has not chosen a specific theme.
|
|
useEffect(() => {
|
|
if (isThemeName(savedTheme) || typeof window.matchMedia !== 'function') {
|
|
return;
|
|
}
|
|
const query = window.matchMedia('(prefers-color-scheme: dark)');
|
|
const onChange = () => setThemeName(resolveTheme(savedTheme));
|
|
query.addEventListener('change', onChange);
|
|
return () => query.removeEventListener('change', onChange);
|
|
}, [savedTheme]);
|
|
|
|
const saveTheme = (value: SavedThemeName) => {
|
|
setSavedTheme(value);
|
|
try {
|
|
if (value === null) {
|
|
localStorage.removeItem(themeSessionKey);
|
|
} else {
|
|
localStorage.setItem(themeSessionKey, value);
|
|
}
|
|
} catch (e) {
|
|
console.error('Unable to save the theme', e);
|
|
}
|
|
}
|
|
|
|
/** Apply and persist a theme. Pass `system` to follow the OS preference. */
|
|
const setTheme = (value: SavedThemeName) => {
|
|
if (value !== systemTheme && value !== null && !isThemeName(value)) {
|
|
console.error(`Unknown theme! ${value}`);
|
|
return;
|
|
}
|
|
saveTheme(value);
|
|
setThemeName(resolveTheme(value));
|
|
}
|
|
|
|
/**
|
|
* Turn the current theme's media filter on or off. Stored per theme, so a user who
|
|
* wants amber's tint but not night's — or the reverse — gets both.
|
|
*/
|
|
const setMediaFilterEnabled = (enabled: boolean) => {
|
|
const next = {...mediaFilters, [theme]: enabled};
|
|
setMediaFilters(next);
|
|
try {
|
|
localStorage.setItem(mediaFilterSessionKey, JSON.stringify(next));
|
|
} catch (e) {
|
|
console.error('Unable to save the media filter setting', e);
|
|
}
|
|
}
|
|
|
|
// Retained for callers written against the old two-theme API.
|
|
const setDarkTheme = (save = false) => {
|
|
setThemeName(darkTheme);
|
|
if (save) saveTheme(darkTheme);
|
|
}
|
|
|
|
const setLightTheme = (save = false) => {
|
|
setThemeName(lightTheme);
|
|
if (save) saveTheme(lightTheme);
|
|
}
|
|
|
|
const cycleSavedTheme = (e?: React.MouseEvent) => {
|
|
// Cycle: System -> Light -> Dark -> Night -> Amber -> System
|
|
if (e) {
|
|
e.preventDefault();
|
|
}
|
|
const order: SavedThemeName[] = [systemTheme, ...themeNames];
|
|
const current = order.indexOf(isThemeName(savedTheme) ? savedTheme : systemTheme);
|
|
setTheme(order[(current + 1) % order.length]);
|
|
}
|
|
|
|
const dark = isDarkTheme(theme);
|
|
|
|
const themeValue: ThemeContextValue = {
|
|
theme,
|
|
savedTheme,
|
|
isDark: dark,
|
|
// The filter this theme offers, if any, and whether it is currently applied.
|
|
mediaFilter: themeMediaFilter(theme),
|
|
mediaFilterEnabled: !!resolveMediaFilter(theme, mediaFilters),
|
|
setMediaFilterEnabled,
|
|
setTheme,
|
|
setDarkTheme,
|
|
setLightTheme,
|
|
cycleSavedTheme,
|
|
};
|
|
|
|
// Mantine's own scheme decides the surfaces its internals use (overlays, scrollbars,
|
|
// focus rings). Night and amber are neither of its two schemes, so they ride on dark
|
|
// and take their actual colors from our tokens. This is the theme `base` flag.
|
|
return <ThemeContext.Provider value={themeValue}>
|
|
<MantineProvider
|
|
theme={mantineTheme}
|
|
cssVariablesResolver={cssVariablesResolver}
|
|
forceColorScheme={dark ? 'dark' : 'light'}
|
|
>
|
|
{/*
|
|
* Top right. Bottom right put toasts over the sticky footer on the file browser
|
|
* and over the player controls on a video, which are the two places a background
|
|
* task is most likely to report while the user is doing something else.
|
|
*/}
|
|
<Notifications position='top-right' limit={5}/>
|
|
{children}
|
|
</MantineProvider>
|
|
</ThemeContext.Provider>
|
|
}
|
|
/*
|
|
* This module is the theme provider and nothing else: it resolves the chosen theme, stamps
|
|
* `data-theme` and `data-media-filter` on <html>, persists both, and hands Mantine our
|
|
* tokens.
|
|
*
|
|
* It used to re-export a set of themed component wrappers as well, along with the
|
|
* dark-mode compatibility props they consumed (`i`, `s`, `t`, `inverted`). Those carried
|
|
* hardcoded greys, which is precisely what the token themes replaced; components come from
|
|
* `src/components/ui` instead.
|
|
*/
|