From 139d6c2c59fd10ad935ab917e16dcfa6bf757280 Mon Sep 17 00:00:00 2001 From: Maaxxs <61059039+MaaxxsDev@users.noreply.github.com> Date: Fri, 11 Sep 2026 11:47:53 +0200 Subject: [PATCH] Add a toggleable page loading screen that waits for images New app_settings.page_loader_enabled (default on) plus a checkbox in Website-Einstellungen -> "Seiten-Ladeanzeige". PageImageLoader.jsx wraps the routed page content (PublicLayout.jsx's ): the content stays mounted (so its images actually start loading and any data-fetching effects still run) but is visually hidden behind a spinner until every currently in it has fired load/error. Works generically across all public pages with no per-page changes, since many pages (gallery/brand/model photos) only render their tags after an async API call resolves - a plain "scan once on mount" check would find zero images and dismiss the loader immediately, so it uses a MutationObserver to keep tracking newly-inserted images as data arrives, and only dismisses once nothing has changed or finished loading for a short settle window. A hard 8-second cap prevents the loader from sticking forever if an image never resolves. Overlay only covers the main content area (navbar/footer stay visible and usable throughout), and the whole thing only mounts when the setting is enabled - disabled, PublicLayout renders the page exactly as before. Verified the backend setting end-to-end (migration, default value, save/round-trip through SiteSettingsController). The loading behavior itself is client-side timing logic I can't exercise in a real browser here, so it's worth clicking through a few pages (especially image-heavy ones like the gallery) before this goes live. Co-Authored-By: Claude Sonnet 5 --- hifi-src/src/components/PageImageLoader.jsx | 119 ++++++++++++++++++ hifi-src/src/components/PublicLayout.jsx | 6 +- hifi-src/src/context/SiteSettingsContext.jsx | 1 + .../pages/admin/settings/WebsiteSettings.jsx | 20 +++ hifi/api/database/migration_page_loader.sql | 10 ++ hifi/api/database/schema.sql | 1 + .../src/Controllers/SettingsController.php | 2 +- .../Controllers/SiteSettingsController.php | 8 +- hifi/api/src/Support/Schema.php | 2 + .../{index-BbPnpRj7.js => index-CjqwSRnf.js} | 92 +++++++------- hifi/assets/index-DP9BSFkw.css | 1 + hifi/assets/index-Dcmvz3nM.css | 1 - hifi/index.html | 4 +- 13 files changed, 214 insertions(+), 53 deletions(-) create mode 100644 hifi-src/src/components/PageImageLoader.jsx create mode 100644 hifi/api/database/migration_page_loader.sql rename hifi/assets/{index-BbPnpRj7.js => index-CjqwSRnf.js} (74%) create mode 100644 hifi/assets/index-DP9BSFkw.css delete mode 100644 hifi/assets/index-Dcmvz3nM.css diff --git a/hifi-src/src/components/PageImageLoader.jsx b/hifi-src/src/components/PageImageLoader.jsx new file mode 100644 index 0000000..8bc5f1f --- /dev/null +++ b/hifi-src/src/components/PageImageLoader.jsx @@ -0,0 +1,119 @@ +import { useEffect, useRef, useState } from 'react'; +import { useLocation } from 'react-router-dom'; + +// Solange der aktuellen Seite noch nicht alle Bilder fertig geladen (oder fehlgeschlagen) sind, +// bleibt der Inhalt unsichtbar (aber trotzdem gemountet - Bilder laden im Hintergrund weiter) +// und stattdessen ein Lade-Symbol angezeigt, damit nichts nachtraeglich "einpoppt". Per +// Admin-Einstellung an-/abschaltbar (siehe app_settings.page_loader_enabled). +// +// Funktioniert seitenunabhaengig per MutationObserver statt pro Seite einzeln verdrahtet zu +// werden: viele Seiten laden ihre Bilder (Galerie-/Marken-/Modellfotos) erst nach einem +// asynchronen API-Request, das existiert also beim ersten Render noch gar nicht. Der +// Observer merkt sich jedes neu eingefuegte , wartet auf dessen load/error-Event und +// betrachtet die Seite erst als fertig, wenn eine kurze Weile lang weder ein Bild offen noch +// eine neue DOM-Aenderung passiert ist ("settled"). MAX_WAIT_MS ist ein Sicherheitsnetz, falls +// ein Bild haengen bleibt oder eine Seite ungewoehnlich lange braucht. +const MIN_DISPLAY_MS = 400; +const SETTLE_MS = 400; +const MAX_WAIT_MS = 8000; + +function Spinner() { + // Nur ueber dem Inhaltsbereich (nicht die ganze Seite) - Navbar/Footer bleiben sichtbar + // und bedienbar, waehrend der Seiteninhalt selbst noch laedt. Ein Mindest-Hoehe, damit auf + // einer noch leeren Seite (Inhalt per visibility:hidden ohne eigene Groesse) trotzdem Platz + // fuer das Symbol da ist. + return ( +
+
+
+ ); +} + +export default function PageImageLoader({ enabled, children }) { + const location = useLocation(); + const [loading, setLoading] = useState(enabled); + const containerRef = useRef(null); + + useEffect(() => { + if (!enabled) { + setLoading(false); + return undefined; + } + + setLoading(true); + const container = containerRef.current; + if (!container) { + setLoading(false); + return undefined; + } + + let finished = false; + let settleTimer = null; + const pending = new Set(); + const startedAt = Date.now(); + + const finish = () => { + if (finished) return; + finished = true; + if (settleTimer) clearTimeout(settleTimer); + clearTimeout(maxTimer); + observer.disconnect(); + const elapsed = Date.now() - startedAt; + const remaining = Math.max(0, MIN_DISPLAY_MS - elapsed); + setTimeout(() => setLoading(false), remaining); + }; + + const scheduleSettleCheck = () => { + if (settleTimer) clearTimeout(settleTimer); + settleTimer = setTimeout(() => { + if (pending.size === 0) finish(); + }, SETTLE_MS); + }; + + const trackImage = (img) => { + if (pending.has(img) || img.complete) return; + pending.add(img); + const onSettled = () => { + pending.delete(img); + img.removeEventListener('load', onSettled); + img.removeEventListener('error', onSettled); + scheduleSettleCheck(); + }; + img.addEventListener('load', onSettled); + img.addEventListener('error', onSettled); + }; + + const scanNode = (node) => { + if (node.nodeType !== 1) return; + if (node.tagName === 'IMG') trackImage(node); + node.querySelectorAll?.('img').forEach(trackImage); + }; + + container.querySelectorAll('img').forEach(trackImage); + + const observer = new MutationObserver((mutations) => { + mutations.forEach((m) => m.addedNodes.forEach(scanNode)); + scheduleSettleCheck(); + }); + observer.observe(container, { childList: true, subtree: true }); + + scheduleSettleCheck(); + const maxTimer = setTimeout(finish, MAX_WAIT_MS); + + return () => { + finished = true; + observer.disconnect(); + if (settleTimer) clearTimeout(settleTimer); + clearTimeout(maxTimer); + }; + }, [location.pathname, enabled]); + + return ( +
+
+ {children} +
+ {enabled && loading && } +
+ ); +} diff --git a/hifi-src/src/components/PublicLayout.jsx b/hifi-src/src/components/PublicLayout.jsx index 285bca1..8e9280c 100644 --- a/hifi-src/src/components/PublicLayout.jsx +++ b/hifi-src/src/components/PublicLayout.jsx @@ -12,6 +12,7 @@ import { SiteSettingsProvider } from '../context/SiteSettingsContext.jsx'; import { TextStylesProvider } from '../context/TextStylesContext.jsx'; import { CustomFontsProvider } from '../context/CustomFontsContext.jsx'; import CustomFontFaces from './CustomFontFaces.jsx'; +import PageImageLoader from './PageImageLoader.jsx'; import { LanguageProvider } from '../context/LanguageContext.jsx'; import { useAuth } from '../context/AuthContext.jsx'; import { api } from '../api/client.js'; @@ -54,6 +55,7 @@ const DEFAULT_SITE_SETTINGS = { gallery_card_text_x: 0, gallery_card_text_y: 100, site_font: null, + page_loader_enabled: 1, }; // Das Impressum (und die anderen rechtlichen Pflichtseiten) müssen laut § 5 DDG @@ -115,7 +117,9 @@ export default function PublicLayout() {
- + + +