refactor: Enhance logo loading logic and streamline theme setting in Preview component

This commit is contained in:
sHa
2025-06-12 11:18:39 +03:00
parent dc4abca75b
commit 1b51146c29

View File

@@ -13,29 +13,34 @@
let setTheme = () => {}; // Default no-op function let setTheme = () => {}; // Default no-op function
onMount(() => { onMount(() => {
if (typeof window !== 'undefined' && window.appData) { function tryFindLogo() {
logos = window.appData.logos || []; if (typeof window !== 'undefined' && window.appData && Array.isArray(window.appData.logos) && window.appData.logos.length > 0) {
theme = window.appData.theme || "system"; logos = window.appData.logos || [];
theme = window.appData.theme || "system";
// Check if setTheme exists in window.appData if (window.appData.setTheme && typeof window.appData.setTheme === 'function') {
if (window.appData.setTheme && typeof window.appData.setTheme === 'function') { setTheme = window.appData.setTheme;
console.log("PreviewPage: Found window.appData.setTheme function"); }
setTheme = window.appData.setTheme; const logoName = params.logoName.replace(/-/g, ' ');
} else { logo = logos.find(l =>
console.warn("PreviewPage: window.appData.setTheme not found or not a function"); l.name.toLowerCase().replace(/\s+/g, '-') === params.logoName.toLowerCase() ||
l.name.toLowerCase() === logoName.toLowerCase()
);
if (!logo) {
console.error("Logo not found:", params.logoName);
push('/', { replace: true });
}
return true;
} }
return false;
}
// Find the logo based on the URL parameter if (!tryFindLogo()) {
const logoName = params.logoName.replace(/-/g, ' '); // Poll until logos are loaded
logo = logos.find(l => const interval = setInterval(() => {
l.name.toLowerCase().replace(/\s+/g, '-') === params.logoName.toLowerCase() || if (tryFindLogo()) {
l.name.toLowerCase() === logoName.toLowerCase() clearInterval(interval);
); }
}, 100);
if (!logo) {
console.error("Logo not found:", params.logoName);
push('/', { replace: true });
}
} }
// Back button event listener // Back button event listener