Logo: show the logo set for dark mode in the dark theme #245

Merged
jcoffey-dev merged 1 commits from feat/410-dark-logo into main 2026-11-18 20:44:09 +00:00
3 changed files with 64 additions and 23 deletions

No files matched your search

+12 -6
View File
@@ -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<string | null>(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 ? (
<DefaultLogo />
) : (
<img
src={logo.url}
alt={t('logo.alt', 'Logo')}
className="h-7 w-auto max-w-[220px] object-contain"
onError={() => setBroken(true)}
onError={() => setBrokenUrl(logo.url)}
/>
);
case 'loading':
+29 -5
View File
@@ -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');
}
});
});
+23 -12
View File
@@ -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<LogoTheme, LogoState> = { light: LOADING, dark: LOADING };
const requested = new Set<LogoTheme>();
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 `<img>`. */
function loadsAsImage(src: string): Promise<boolean> {
return new Promise((resolve) => {
@@ -39,13 +50,13 @@ function loadsAsImage(src: string): Promise<boolean> {
});
}
/** 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();
});
}