From 8eb0ba2f21ae1f3c17d45dcb005ba6e2b1b4fc7d Mon Sep 17 00:00:00 2001 From: John Coffey Date: Wed, 18 Nov 2026 13:36:14 -0700 Subject: [PATCH] Logo: show the logo set for dark mode in the dark theme MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The console showed the server's logo from /logo in both themes, so a logo drawn for a light background was the only choice on a dark one. The server now takes `?theme=dark` and answers with the logo an admin set for dark mode (logoUrlDark, or a domain's or tenant's logoDark), falling back to the ordinary one (inbuxa-server#410, branding BT-27). The logo cache probes each theme's /logo once per page load, on first use: the light theme as before, the dark theme with ?theme=dark. The header logo follows the theme switch, and a logo that broke in one theme no longer hides the other theme's. A server that predates the parameter ignores it and answers with its ordinary logo, so nothing changes against it. The dark logo fields themselves come from the served schema (Settings › System › Branding, and each domain's and tenant's form), so the forms need no change here. Test: the cache asks /logo?theme=dark in the dark theme and /logo in the light one, each once, and follows the page's dark class when no theme is given. --- src/components/common/Logo.tsx | 18 +++++++++++------ src/lib/logoCache.test.ts | 34 ++++++++++++++++++++++++++++----- src/lib/logoCache.ts | 35 ++++++++++++++++++++++------------ 3 files changed, 64 insertions(+), 23 deletions(-) diff --git a/src/components/common/Logo.tsx b/src/components/common/Logo.tsx index a862b46..bc4d25f 100644 --- a/src/components/common/Logo.tsx +++ b/src/components/common/Logo.tsx @@ -9,6 +9,7 @@ import { useTranslation } from 'react-i18next'; import inbuxaMark from '@/assets/inbuxa-mark.png'; import { getLogoState, loadLogoOnce, subscribeToLogo } from '@/lib/logoCache'; +import { useUIStore } from '@/stores/uiStore'; export function DefaultLogo() { const { t } = useTranslation(); @@ -58,25 +59,30 @@ export function DefaultLogo() { /** * The server's own logo if an admin set one, else inbuxa's. Until the server - * answers, an empty space the logo's size holds the layout still. + * answers, an empty space the logo's size holds the layout still. The dark + * theme shows the logo set for dark mode, where there is one (BT-27). */ export default function Logo() { const { t } = useTranslation(); - const logo = useSyncExternalStore(subscribeToLogo, getLogoState, getLogoState); - const [broken, setBroken] = useState(false); - useEffect(loadLogoOnce, []); + const theme = useUIStore((state) => state.theme); + const read = () => getLogoState(theme); + const logo = useSyncExternalStore(subscribeToLogo, read, read); + // The URL that failed: a broken image in one theme says nothing about the + // other theme's logo + const [brokenUrl, setBrokenUrl] = useState(null); + useEffect(() => loadLogoOnce(theme), [theme]); switch (logo.status) { case 'custom': // A logo that loaded once can still fail later (its host goes away // after the cache runs out); the inbuxa logo beats a broken image. - return broken ? ( + return brokenUrl === logo.url ? ( ) : ( {t('logo.alt', setBroken(true)} + onError={() => setBrokenUrl(logo.url)} /> ); case 'loading': diff --git a/src/lib/logoCache.test.ts b/src/lib/logoCache.test.ts index 7de527c..37c791d 100644 --- a/src/lib/logoCache.test.ts +++ b/src/lib/logoCache.test.ts @@ -26,17 +26,17 @@ function fakeImage(loads: boolean) { } /** A fresh copy of the module, since it probes once per page load. */ -async function load(loads: boolean) { +async function load(loads: boolean, theme: 'light' | 'dark' = 'light') { vi.resetModules(); const image = fakeImage(loads); const mod = await import('./logoCache'); const changed = vi.fn(); mod.subscribeToLogo(changed); - expect(mod.getLogoState()).toEqual({ status: 'loading' }); - mod.loadLogoOnce(); - mod.loadLogoOnce(); + expect(mod.getLogoState(theme)).toEqual({ status: 'loading' }); + mod.loadLogoOnce(theme); + mod.loadLogoOnce(theme); await vi.waitFor(() => expect(changed).toHaveBeenCalled()); - return { state: mod.getLogoState(), ...image }; + return { state: mod.getLogoState(theme), mod, ...image }; } describe('logo cache', () => { @@ -59,4 +59,28 @@ describe('logo cache', () => { it('falls back to the inbuxa logo when /logo is not an image', async () => { expect((await load(false)).state).toEqual({ status: 'default' }); }); + + // inbuxa-server#410: the dark theme asks for the logo set for dark mode + it('asks for the dark-mode logo in the dark theme, apart from the light one', async () => { + const { state, sources, mod } = await load(true, 'dark'); + expect(state).toEqual({ status: 'custom', url: 'https://mail.example/logo?theme=dark' }); + expect(sources).toEqual(['https://mail.example/logo?theme=dark']); + expect(mod.getLogoState('light')).toEqual({ status: 'loading' }); + mod.loadLogoOnce('light'); + await vi.waitFor(() => expect(mod.getLogoState('light')).toEqual({ status: 'custom', url: 'https://mail.example/logo' })); + expect(sources).toEqual(['https://mail.example/logo?theme=dark', 'https://mail.example/logo']); + }); + + it('follows the page theme when none is given', async () => { + vi.resetModules(); + const { sources } = fakeImage(true); + document.documentElement.classList.add('dark'); + try { + const mod = await import('./logoCache'); + mod.loadLogoOnce(); + expect(sources).toEqual(['https://mail.example/logo?theme=dark']); + } finally { + document.documentElement.classList.remove('dark'); + } + }); }); diff --git a/src/lib/logoCache.ts b/src/lib/logoCache.ts index 99d514b..5998e23 100644 --- a/src/lib/logoCache.ts +++ b/src/lib/logoCache.ts @@ -5,9 +5,14 @@ */ /** - * The server's own logo, if an admin set one: probed once per page load by - * loading `/logo` as an image. Anything that isn't an image (no logo, an - * error page, no answer) means the inbuxa logo. + * The server's own logo, if an admin set one: probed once per page load and + * theme by loading `/logo` as an image. Anything that isn't an image (no + * logo, an error page, no answer) means the inbuxa logo. + * + * The dark theme asks for `/logo?theme=dark`, the logo an admin set for dark + * mode, which the server falls back from to the ordinary one (branding + * BT-27, inbuxa-server#410). A server that predates it ignores the + * parameter and answers with the ordinary logo. * * An image, not a fetch: a logo set as an https URL makes `/logo` redirect to * that URL, and a fetch only follows a redirect to another site when that @@ -16,19 +21,25 @@ */ import { getApiBaseUrl } from '@/services/api'; +export type LogoTheme = 'light' | 'dark'; export type LogoState = { status: 'loading' } | { status: 'custom'; url: string } | { status: 'default' }; -let logo: LogoState = { status: 'loading' }; -let requested = false; +const LOADING: LogoState = { status: 'loading' }; +const logos: Record = { light: LOADING, dark: LOADING }; +const requested = new Set(); const watchers = new Set<() => void>(); -export const getLogoState = (): LogoState => logo; +export const getLogoState = (theme: LogoTheme): LogoState => logos[theme]; export function subscribeToLogo(watch: () => void): () => void { watchers.add(watch); return () => void watchers.delete(watch); } +/** The theme the page shows now, from the `dark` class the console sets. */ +export const currentLogoTheme = (): LogoTheme => + document.documentElement.classList.contains('dark') ? 'dark' : 'light'; + /** Whether `src` loads as an image; the browser caches it for the ``. */ function loadsAsImage(src: string): Promise { return new Promise((resolve) => { @@ -39,13 +50,13 @@ function loadsAsImage(src: string): Promise { }); } -/** Starts the probe; later calls do nothing. */ -export function loadLogoOnce(): void { - if (requested) return; - requested = true; - const url = `${getApiBaseUrl()}/logo`; +/** Starts the probe for a theme; later calls for it do nothing. */ +export function loadLogoOnce(theme: LogoTheme = currentLogoTheme()): void { + if (requested.has(theme)) return; + requested.add(theme); + const url = `${getApiBaseUrl()}/logo${theme === 'dark' ? '?theme=dark' : ''}`; void loadsAsImage(url).then((loaded) => { - logo = loaded ? { status: 'custom', url } : { status: 'default' }; + logos[theme] = loaded ? { status: 'custom', url } : { status: 'default' }; for (const watch of watchers) watch(); }); } -- 2.54.0