Add theme handling when without user context

This commit is contained in:
MerlinScheurer
2025-05-23 18:54:29 +02:00
parent ef07dc91b4
commit 068cd2e407
4 changed files with 40 additions and 10 deletions

View File

@@ -8,6 +8,14 @@ export type ColourVariants =
| 'midnight.css' | 'midnight.css'
| 'custom.css'; | 'custom.css';
export const ColourConstant = {
Dark: 'dark.css',
Light: 'light.css',
Matrix: 'matrix.css',
Midnight: 'midnight.css',
Custom: 'custom.css',
};
export const FileSizeUnits = { export const FileSizeUnits = {
Binary: 'binary', Binary: 'binary',
Metric: 'metric', Metric: 'metric',

View File

@@ -1,14 +1,40 @@
import { ColourConstant, ColourVariants } from '../../api/actions/updateUserConfig';
import { useUserConfigStore } from '../../stores/UserConfigStore'; import { useUserConfigStore } from '../../stores/UserConfigStore';
import { ColourConstant } from './colourConstant';
import CustomStylesheet from './components/Custom'; import CustomStylesheet from './components/Custom';
import DarkStylesheet from './components/Dark'; import DarkStylesheet from './components/Dark';
import LightStylesheet from './components/Light'; import LightStylesheet from './components/Light';
import MatrixStylesheet from './components/Matrix'; import MatrixStylesheet from './components/Matrix';
import MidnightStylesheet from './components/Midnight'; import MidnightStylesheet from './components/Midnight';
function getThemeFromLocalStorage(stylesheet: string): ColourVariants {
// Check when localStorage when its possibly the default theme. ( e.g. login page )
if (stylesheet === ColourConstant.Dark) {
const fromLocalStorage = localStorage.getItem('stylesheet');
if (!fromLocalStorage) {
localStorage.setItem('stylesheet', stylesheet);
}
if (fromLocalStorage) {
stylesheet = fromLocalStorage;
}
} else {
const fromLocalStorage = localStorage.getItem('stylesheet');
// Re-sync when localStorage is not the same as in userConfig
if (stylesheet !== fromLocalStorage) {
localStorage.setItem('stylesheet', stylesheet);
}
}
return stylesheet as ColourVariants;
}
const Colours = () => { const Colours = () => {
const { userConfig } = useUserConfigStore(); const { userConfig } = useUserConfigStore();
const stylesheet = userConfig?.stylesheet; let stylesheet = userConfig?.stylesheet;
stylesheet = getThemeFromLocalStorage(stylesheet);
switch (stylesheet) { switch (stylesheet) {
case ColourConstant.Dark: case ColourConstant.Dark:

View File

@@ -1,7 +0,0 @@
export const ColourConstant = {
Dark: 'dark.css',
Light: 'light.css',
Matrix: 'matrix.css',
Midnight: 'midnight.css',
Custom: 'custom.css',
};

View File

@@ -1,4 +1,5 @@
import updateUserConfig, { import updateUserConfig, {
ColourConstant,
ColourVariants, ColourVariants,
FileSizeUnits, FileSizeUnits,
UserConfigType, UserConfigType,
@@ -10,7 +11,6 @@ import useIsAdmin from '../functions/useIsAdmin';
import { useUserConfigStore } from '../stores/UserConfigStore'; import { useUserConfigStore } from '../stores/UserConfigStore';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import ToggleConfig from '../components/ToggleConfig'; import ToggleConfig from '../components/ToggleConfig';
import { ColourConstant } from '../configuration/colours/colourConstant';
const SettingsUser = () => { const SettingsUser = () => {
const { userConfig, setUserConfig } = useUserConfigStore(); const { userConfig, setUserConfig } = useUserConfigStore();
@@ -38,6 +38,9 @@ const SettingsUser = () => {
const handleStyleSheetChange = async (selectedStyleSheet: ColourVariants) => { const handleStyleSheetChange = async (selectedStyleSheet: ColourVariants) => {
handleUserConfigUpdate({ stylesheet: selectedStyleSheet }); handleUserConfigUpdate({ stylesheet: selectedStyleSheet });
setStyleSheet(selectedStyleSheet); setStyleSheet(selectedStyleSheet);
// Store in local storage for pages like login, without a userConfig
localStorage.setItem('stylesheet', selectedStyleSheet);
}; };
const handlePageSizeChange = async () => { const handlePageSizeChange = async () => {