fix(theme): stabilize tenant switch theme propagation and toggle race handling
Task: REVIEW-USER-THEME-001
This commit is contained in:
@@ -44,6 +44,9 @@ const switchTenant = async (link) => {
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
if (data.ok) {
|
||||
if (data.theme) {
|
||||
document.documentElement.dataset.theme = data.theme;
|
||||
}
|
||||
window.location.reload();
|
||||
} else {
|
||||
warnOnce('FETCH_FAILED', 'Tenant switch failed', { module: 'tenant-switcher', error: data.error });
|
||||
|
||||
@@ -42,6 +42,7 @@ const initThemeMenu = () => {
|
||||
return;
|
||||
}
|
||||
|
||||
let pending = false;
|
||||
const getCurrent = () => document.documentElement.dataset.theme || '';
|
||||
const setActive = (theme) => {
|
||||
options.forEach((option) => {
|
||||
@@ -61,10 +62,12 @@ const initThemeMenu = () => {
|
||||
options.forEach((option) => {
|
||||
option.addEventListener('click', async (event) => {
|
||||
event.preventDefault();
|
||||
if (pending) {return;}
|
||||
const next = option.dataset.themeValue || '';
|
||||
if (!next) {return;}
|
||||
const current = getCurrent();
|
||||
if (next === current) {return;}
|
||||
pending = true;
|
||||
setTheme(next);
|
||||
setIcon(icon, next);
|
||||
setActive(next);
|
||||
@@ -74,6 +77,7 @@ const initThemeMenu = () => {
|
||||
setIcon(icon, current);
|
||||
setActive(current);
|
||||
}
|
||||
pending = false;
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -82,12 +86,15 @@ const initThemeToggle = () => {
|
||||
const button = optionalEl('[data-theme-toggle]');
|
||||
if (!button) {return;}
|
||||
const icon = button.querySelector('i');
|
||||
let pending = false;
|
||||
const getCurrent = () => document.documentElement.dataset.theme || '';
|
||||
setIcon(icon, getCurrent());
|
||||
|
||||
button.addEventListener('click', async () => {
|
||||
if (pending) {return;}
|
||||
const current = getCurrent();
|
||||
const next = isDarkTheme(current) ? 'light' : 'dark';
|
||||
pending = true;
|
||||
setTheme(next);
|
||||
setIcon(icon, next);
|
||||
const ok = await updateTheme(button, next);
|
||||
@@ -95,6 +102,7 @@ const initThemeToggle = () => {
|
||||
setTheme(current);
|
||||
setIcon(icon, current);
|
||||
}
|
||||
pending = false;
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user