Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): let the theme button return to the system setting

Yudhi Armyndharis1e462f31bc47
diff --git a/dashboard/src/components/Layout.test.ts b/dashboard/src/components/Layout.test.ts
new file mode 100644
index 000000000..831657f15
--- /dev/null
+++ b/dashboard/src/components/Layout.test.ts
@@ -0,0 +1,50 @@
+// The sidebar theme button cycles light, dark and "follow the system". The choice is stored, so a button
+// that only flipped light and dark lost "System" for good after the first click.
+import '../test-helpers/register-hooks.ts';
+import { test, before, after } from 'node:test';
+import assert from 'node:assert/strict';
+import { createElement } from 'react';
+
+let rtl: typeof import('@testing-library/react');
+let Layout: (typeof import('./Layout.tsx'))['Layout'];
+let MemoryRouter: (typeof import('react-router-dom'))['MemoryRouter'];
+
+before(async () => {
+ const { installJsdomGlobals } = await import('../test-helpers/jsdom.ts');
+ await installJsdomGlobals();
+ (globalThis as Record<string, unknown>).__APP_VERSION__ = '0.0.0-test';
+ // jsdom has no matchMedia; the theme hook reads it for the system preference.
+ window.matchMedia = ((query: string) => ({
+ matches: false,
+ media: query,
+ addEventListener: () => undefined,
+ removeEventListener: () => undefined,
+ })) as unknown as typeof window.matchMedia;
+ globalThis.fetch = (() =>
+ Promise.resolve(new Response(JSON.stringify({ message: 'unstubbed' }), { status: 404 }))) as typeof fetch;
+ const { i18nReady } = await import('../i18n/index.ts');
+ await i18nReady;
+ rtl = await import('@testing-library/react');
+ ({ MemoryRouter } = await import('react-router-dom'));
+ ({ Layout } = await import('./Layout.tsx'));
+});
+
+after(() => rtl.cleanup());
+
+test('the theme button can return to following the system', () => {
+ localStorage.removeItem('openwa_theme');
+ const { container } = rtl.render(
+ createElement(MemoryRouter, null, createElement(Layout, { onLogout: () => undefined, userRole: 'admin' })),
+ );
+ const button = container.querySelector<HTMLButtonElement>('.appearance-menu .theme-toggle-btn')!;
+ const seen = [button.textContent];
+ for (let i = 0; i < 3; i++) {
+ const announced = button.getAttribute('aria-label');
+ rtl.fireEvent.click(button);
+ seen.push(button.textContent);
+ // The label names the state the click selects.
+ assert.equal(announced, `Switch to ${button.textContent}`);
+ }
+ assert.deepEqual(seen, ['System', 'Light', 'Dark', 'System']);
+ assert.equal(localStorage.getItem('openwa_theme'), 'system');
+});
diff --git a/dashboard/src/components/Layout.tsx b/dashboard/src/components/Layout.tsx
index 495e66cd6..772676df0 100644
--- a/dashboard/src/components/Layout.tsx
+++ b/dashboard/src/components/Layout.tsx
@@ -52,9 +52,13 @@ const themeIcons = { light: Sun, dark: Moon, system: Monitor };
export function Layout({ onLogout, userRole }: LayoutProps) {
const { t, i18n } = useTranslation();
- const { theme, setTheme, resolvedTheme } = useTheme();
+ const { theme, toggleTheme } = useTheme();
const ThemeIcon = themeIcons[theme];
const themeLabel = t(`theme.${theme}`);
+ // toggleTheme cycles light, dark, system; the button names the state a click selects.
+ const nextThemeLabel = t('theme.toggleTo', {
+ value: t(`theme.${theme === 'light' ? 'dark' : theme === 'dark' ? 'system' : 'light'}`),
+ });
const navItems = allNavItems.filter(item => !item.adminOnly || userRole === 'admin');
@@ -262,9 +266,9 @@ export function Layout({ onLogout, userRole }: LayoutProps) {
<div className="appearance-menu">
<button
className="theme-toggle-btn"
- onClick={() => setTheme(resolvedTheme === 'dark' ? 'light' : 'dark')}
- title={t('theme.toggleTo', { value: t(resolvedTheme === 'dark' ? 'theme.light' : 'theme.dark') })}
- aria-label={t('theme.toggleTo', { value: t(resolvedTheme === 'dark' ? 'theme.light' : 'theme.dark') })}
+ onClick={toggleTheme}
+ title={nextThemeLabel}
+ aria-label={nextThemeLabel}
>
<span className="appearance-button-cue" aria-hidden="true">
<ThemeIcon size={16} />