Add default theme management to branding settings and admin interface
All checks were successful
Build & Push Docker Image / build (push) Successful in 6m14s
All checks were successful
Build & Push Docker Image / build (push) Successful in 6m14s
This commit is contained in:
@@ -1,23 +1,29 @@
|
||||
import { createContext, useContext, useState, useEffect, useCallback } from 'react';
|
||||
import api from '../services/api';
|
||||
import { useTheme } from './ThemeContext';
|
||||
|
||||
const BrandingContext = createContext();
|
||||
|
||||
export function BrandingProvider({ children }) {
|
||||
const { applyBrandingDefault } = useTheme();
|
||||
const [branding, setBranding] = useState({
|
||||
appName: 'Redlight',
|
||||
hasLogo: false,
|
||||
logoUrl: null,
|
||||
defaultTheme: null,
|
||||
});
|
||||
|
||||
const fetchBranding = useCallback(async () => {
|
||||
try {
|
||||
const res = await api.get('/branding');
|
||||
setBranding(res.data);
|
||||
if (res.data.defaultTheme) {
|
||||
applyBrandingDefault(res.data.defaultTheme);
|
||||
}
|
||||
} catch {
|
||||
// keep defaults
|
||||
}
|
||||
}, []);
|
||||
}, [applyBrandingDefault]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchBranding();
|
||||
|
||||
@@ -4,20 +4,33 @@ const ThemeContext = createContext(null);
|
||||
|
||||
export function ThemeProvider({ children }) {
|
||||
const [theme, setThemeState] = useState(() => {
|
||||
return localStorage.getItem('theme') || 'dark';
|
||||
// Personal preference > last known branding default > hardcoded fallback
|
||||
return localStorage.getItem('theme') || localStorage.getItem('branding-default-theme') || 'dark';
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
document.documentElement.setAttribute('data-theme', theme);
|
||||
localStorage.setItem('theme', theme);
|
||||
}, [theme]);
|
||||
|
||||
// Called by user intentionally (Settings page etc.) — saves as personal preference
|
||||
const setTheme = useCallback((newTheme) => {
|
||||
localStorage.setItem('theme', newTheme);
|
||||
setThemeState(newTheme);
|
||||
}, []);
|
||||
|
||||
// Called by BrandingContext after loading the admin-configured default theme.
|
||||
// Only takes effect when the user has no personal preference stored.
|
||||
const applyBrandingDefault = useCallback((newDefault) => {
|
||||
if (!newDefault) return;
|
||||
localStorage.setItem('branding-default-theme', newDefault);
|
||||
if (!localStorage.getItem('theme')) {
|
||||
setThemeState(newDefault);
|
||||
document.documentElement.setAttribute('data-theme', newDefault);
|
||||
}
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<ThemeContext.Provider value={{ theme, setTheme }}>
|
||||
<ThemeContext.Provider value={{ theme, setTheme, applyBrandingDefault }}>
|
||||
{children}
|
||||
</ThemeContext.Provider>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user