mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-03 13:19:58 -05:00
ControllerPage was the final call site; Theme.tsx then had nothing left to wrap, so it drops from 861 lines to 338 -- the provider that resolves the theme, stamps `data-theme` and `data-media-filter`, persists both and hands Mantine our tokens. Everything below that was Semantic wrappers. The dark-mode compatibility props went with them. `i`, `s`, `t` and `inverted` existed so unmigrated components could borrow Semantic's dark treatment, and they carried hardcoded greys -- #1B1C1D, #dddddd, #eeeeee -- which is exactly what the token themes replaced. Three call sites in Common.js were still injecting the #eeeeee one into links. Then `semantic-ui-offline/semantic.min.css` came out of App.js. That took more with it than expected, and looking at the running app is the only reason it was caught: Every link lost its colour and gained the browser's underline and visited purple. The dashboard's load averages and section headings were underlined and purple. Links now take `--blue`, which remaps per theme, and underline only on hover -- many of them wrap cards, posters and icons. `box-sizing: border-box` disappeared, which silently changes the size of every padded element in the app. Semantic's reset had been providing it. The navigation bar overflowed a 1000px viewport with `nowrap` and cut "Admin" off the right edge -- the one link a user needs to reach the settings that fix a broken WROLPi. Semantic's menu wrapped; ours does now too. The gzipped CSS bundle is 47.5 kB, and one call site was still leaning on Semantic's grid classes -- now a flex column, since all three of its callers pass library components. The ratchet stays rather than being deleted. At zero it asserts four things: no file imports Semantic UI, none calls its toast library, none leans on its stylesheet, and no file reaches back through Theme.tsx. It can go when the packages are uninstalled. Theme.test.js is deleted, but not before porting the two behaviours it protected: that Button forwards a ref to the real DOM element (a class-component ref once crashed the Control page, because portals call `node.contains`), and that the progress percentage stays readable on every bar colour.
235 lines
8.5 KiB
TypeScript
235 lines
8.5 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'}
|
|
>
|
|
<Notifications position='bottom-right' limit={5}/>
|
|
{children}
|
|
</MantineProvider>
|
|
</ThemeContext.Provider>
|
|
}
|
|
/*
|
|
* The Semantic UI wrappers that used to live below this line are gone.
|
|
*
|
|
* They existed so call sites could get a themed Button, Segment, Modal and so on while
|
|
* the app still rendered Semantic components; every one of those call sites now imports
|
|
* from `src/components/ui` instead. The dark-mode compatibility props they consumed --
|
|
* `i`, `s`, `t`, `inverted` -- went with them: those carried hardcoded greys, which is
|
|
* precisely what the token themes replaced.
|
|
*
|
|
* What remains is the part that was never about Semantic: the provider that resolves the
|
|
* chosen theme, stamps `data-theme` and `data-media-filter` on <html>, persists both, and
|
|
* hands Mantine our tokens.
|
|
*/
|