2import { useEffect } from 'react';
4export function useTheme(settings) {
6 applyThemeFromSettings(settings);
10export function applyThemeFromSettings(settings) {
11 if (!settings || !settings.display) return;
12 const theme = settings.display.theme || 'default';
13 const auto = settings.display.autoTheme || false;
15 function setTheme(name) {
16 if (!name || name === 'default') {
17 document.documentElement.removeAttribute('data-theme');
19 document.documentElement.setAttribute('data-theme', name);
24 const matcher = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)');
25 const apply = () => setTheme(matcher.matches ? 'dark' : 'light');
27 if (matcher && matcher.addEventListener) matcher.addEventListener('change', apply);
28 else if (matcher && matcher.addListener) matcher.addListener(apply);
34export async function initTheme() {
36 // Public pages should use the frontpage theme, not the dashboard theme
37 const p = window.location.pathname || '/';
40 p.startsWith('/login') ||
41 p.startsWith('/forgot-password') ||
42 p.startsWith('/reset-password') ||
43 p.startsWith('/verify-email') ||
44 p.startsWith('/docs') ||
52 const raw = localStorage.getItem('app_settings');
54 const cfg = JSON.parse(raw);
55 applyThemeFromSettings(cfg);
58 // try fetch settings if token exists
59 const token = localStorage.getItem('token');
61 const res = await fetch('/api/settings', { headers: { Authorization: `Bearer ${token}` } });
63 const cfg = await res.json();
64 localStorage.setItem('app_settings', JSON.stringify(cfg));
65 applyThemeFromSettings(cfg);
69 console.warn('theme init failed', err);